-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbenchmark.c
141 lines (125 loc) · 4.29 KB
/
benchmark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "utils.h"
#include <assert.h>
#include <immintrin.h>
#include <stdio.h>
#ifdef OPENBLAS
#include <cblas.h>
#else
#include "matmul.h"
#endif
#define MEMALIGN 64
#define max(x, y) ((x) > (y) ? (x) : (y))
int main(int argc, char* argv[]) {
srand(time(NULL));
int MINSIZE = 200;
int STEPSIZE = 200;
int NPTS = 40;
int WNITER = 5;
int NITER_START = 1001;
int NITER_END = 5;
if (argc > 6) {
MINSIZE = atoi(argv[1]);
STEPSIZE = atoi(argv[2]);
NPTS = atoi(argv[3]);
WNITER = atoi(argv[4]);
NITER_START = atoi(argv[5]);
NITER_END = atoi(argv[6]);
}
assert(NPTS > 0 && MINSIZE > 0 && STEPSIZE > 0 && NPTS > 0 && (NITER_START >= NITER_END));
// Warm-up
int wmatsize = MINSIZE + (int)(NPTS / 2) * STEPSIZE;
printf("================\n");
printf("Warm-up: m=n=k=%i\n", wmatsize);
float* A = (float*)_mm_malloc(wmatsize * wmatsize * sizeof(float), MEMALIGN);
float* B = (float*)_mm_malloc(wmatsize * wmatsize * sizeof(float), MEMALIGN);
float* C = (float*)_mm_malloc(wmatsize * wmatsize * sizeof(float), MEMALIGN);
int m = wmatsize;
int n = wmatsize;
int k = wmatsize;
for (int j = 0; j < WNITER; j++) {
fflush(stdout);
printf("\r%i / %i", j + 1, WNITER);
#ifdef OPENBLAS
cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, m, n, k, 1.0, A, m, B, k, 0.0, C, m);
#else
matmul(A, B, C, m, n, k);
#endif
}
printf("\n");
_mm_free(A);
_mm_free(B);
_mm_free(C);
// Benchmark
printf("==========================\n");
#ifdef OPENBLAS
printf("Benchmarking OpenBLAS\n");
#else
printf("Benchmarking matmul.c\n");
#endif
printf("==========================\n");
int* gflops_median_all = (int*)malloc(NPTS * sizeof(int));
int* gflops_max_all = (int*)malloc(NPTS * sizeof(int));
int* matsizes = (int*)malloc(NPTS * sizeof(int));
for (int i = 0; i < NPTS; i++) {
matsizes[i] = MINSIZE + i * STEPSIZE;
}
for (int i = 0; i < NPTS; i++) {
int matsize = matsizes[i];
int m = matsize;
int n = matsize;
int k = matsize;
float* A = (float*)_mm_malloc(matsize * matsize * sizeof(float), MEMALIGN);
float* B = (float*)_mm_malloc(matsize * matsize * sizeof(float), MEMALIGN);
float* C = (float*)_mm_malloc(matsize * matsize * sizeof(float), MEMALIGN);
init_rand(A, matsize * matsize);
init_rand(B, matsize * matsize);
int n_iter =
max(1, calculate_niter(matsize, NITER_START, NITER_END, MINSIZE, matsizes[NPTS - 1]));
float* runtimes = (float*)malloc(n_iter * sizeof(float));
double FLOP = 2 * (double)matsize * matsize * matsize;
for (int j = 0; j < n_iter; j++) {
uint64_t start = timer();
#ifdef OPENBLAS
cblas_sgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, m, n, k, 1, A, m, B, k, 0, C, m);
#else
matmul(A, B, C, m, n, k);
#endif
uint64_t end = timer();
runtimes[j] = (end - start) * 1e-9;
}
qsort(runtimes, n_iter, sizeof(float), compare_floats);
float runtime_median = n_iter % 2 == 0 ?
(runtimes[n_iter / 2] + runtimes[n_iter / 2 - 1]) / 2 :
runtimes[n_iter / 2];
int gflops_median = (int)(FLOP / (double)runtime_median / 1e9);
int gflops_max = (int)(FLOP / (double)runtimes[0] / 1e9);
gflops_max_all[i] = gflops_max;
gflops_median_all[i] = gflops_median;
printf("m=n=k=%i | PEAK/MEDIAN GFLOPS = %i/%i\n", matsize, gflops_max, gflops_median);
_mm_free(A);
_mm_free(B);
_mm_free(C);
free(runtimes);
}
printf("\n================\n");
FILE* fptr;
#ifdef OPENBLAS
const char* filename = "benchmark_openblas.txt";
#else
const char* filename = "benchmark_matmul.txt";
#endif
fptr = fopen(filename, "w");
if (fptr == NULL) {
printf("Error opening the file %s\n", filename);
return -1;
}
for (int i = 0; i < NPTS; i++) {
fprintf(fptr, "%i %i %i\n", matsizes[i], gflops_max_all[i], gflops_median_all[i]);
}
fclose(fptr);
printf("Saved in %s\n", filename);
free(gflops_max_all);
free(gflops_median_all);
free(matsizes);
return 0;
}