-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatmul-bench-main.c
267 lines (225 loc) · 6.47 KB
/
matmul-bench-main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "matmul-bench.h"
enum run_type {
RUN_ALL,
RUN_SIZE1,
DISPLAY_TEST_INFO,
};
static void
usage(void)
{
puts("matmul-bench [-n size] [-I] [-t test1,test2,...] [-i iter] [-m <min size>] [-s <size step>] [-T <time limit>] [-o out.csv] [-O theradnum] ");
puts(" -I : display test info");
puts(" -n : matrix size (default : auto)");
puts(" -t : set test list (default : all)");
puts(" -i : num iter (default 3)");
puts(" -b : i thread block size (default 1)");
puts(" -m : min size (default 64)");
puts(" -s : size step (default 64)");
puts(" -T : time limit [float sec] (default 0.5)");
puts(" -o : out csv");
puts(" -O : thread num");
}
static void
disp_flops(const struct MatmulBenchTest *test,
double sec,
unsigned int iter,
unsigned long mat_size,
void *p)
{
double n = mat_size;
printf("(%5d-%2d):%-20s: sec=%8.5f, %9.5f[GFLOPS], %8.5f[GB/s]\n",
(int)mat_size,
(int)iter,
test->name,
sec,
n*n*n*2/(sec*1000.0*1000.0*1000.0),
(n*n*3.0*sizeof(float))/(sec*1000.0*1000.0*1000.0));
}
int
main(int argc, char **argv)
{
int ai;
enum run_type run_type = RUN_ALL;
unsigned long size1 = 512;
const char *test_list = NULL;
const char *out_csv = NULL;
int iter = 3;
unsigned long size_min = 64;
unsigned long size_step = 64;
double timeout_sec = 0.5;
int num_thread = 0;
unsigned int i_block_size = 1;
for (ai=1; ai<argc; ai++) {
if (argv[ai][0] == '-') {
switch (argv[ai][1]) {
case 'I':
run_type = DISPLAY_TEST_INFO;
break;
case 'i':
if (ai == argc-1) {
usage();
exit(1);
}
iter = atoi(argv[ai+1]);
ai++;
break;
case 'T':
if (ai == argc-1) {
usage();
exit(1);
}
timeout_sec = atof(argv[ai+1]);
ai++;
break;
case 'n':
run_type = RUN_SIZE1;
if (ai == argc-1) {
usage();
exit(1);
}
size1 = atoi(argv[ai+1]);
ai++;
break;
case 't':
if (ai == argc-1) {
usage();
exit(1);
}
test_list = argv[ai+1];
ai++;
break;
case 's':
if (ai == argc-1) {
usage();
exit(1);
}
size_step = atoi(argv[ai+1]);
ai++;
break;
case 'm':
if (ai == argc-1) {
usage();
exit(1);
}
size_min = atoi(argv[ai+1]);
ai++;
break;
case 'h':
usage();
exit(0);
case 'o':
if (ai == argc-1) {
usage();
exit(1);
}
out_csv = argv[ai+1];
ai++;
break;
case 'O':
if (ai == argc-1) {
usage();
exit(1);
}
num_thread = atoi(argv[ai+1]);
ai++;
break;
case 'b':
if (ai == argc-1) {
usage();
exit(1);
}
i_block_size = atoi(argv[ai+1]);
ai++;
break;
default:
usage();
exit(1);
}
}
}
if (argc != ai) {
usage();
exit(0);
}
struct MatmulBench *b = matmul_bench_init(num_thread);
if (run_type == DISPLAY_TEST_INFO) {
int i;
printf("CPU = %s, %f[MHz]\n",
b->arch_name, b->cpu_freq / (1000.0*1000.0));
printf("<cpu feature =");
if (b->feature_bits & MATMULBENCH_FEATURE_SSE) {
printf(" sse");
}
if (b->feature_bits & MATMULBENCH_FEATURE_AVX) {
printf(" avx");
}
if (b->feature_bits & MATMULBENCH_FEATURE_FMA) {
printf(" fma");
}
if (b->feature_bits & MATMULBENCH_FEATURE_NEON) {
printf(" neon");
}
if (b->feature_bits & MATMULBENCH_FEATURE_VFPV4) {
printf(" vfpv4");
}
if (b->feature_bits & MATMULBENCH_FEATURE_GCCVEC) {
printf(" gccvec");
}
printf(">\n");
for (i=0; i<b->num_test; i++) {
printf("%15s: size_step=%d\n", b->test_set[i].name, b->test_set[i].size_step);
}
matmul_bench_fini(b);
exit(0);
}
struct MatmulBenchConfig *config = matmul_bench_config_init(b);
if (run_type == RUN_SIZE1) {
config->mat_size = size1;
}
config->iter = iter;
config->size_min = size_min;
config->size_step = size_step;
config->max_time_sec = timeout_sec;
config->i_block_size = i_block_size;
if (test_list) {
int i;
for (i=0; i<b->num_test; i++) {
config->enable[i] = 0;
}
char name[512];
size_t cur = 0;
size_t len = strlen(test_list);
while (cur < len) {
for (i=0; i<511; i++, cur++) {
if (test_list[cur] == ',' || test_list[cur] == '\0') {
cur++;
name[i] = '\0';
break;
}
name[i] = test_list[cur];
}
int r = matmul_bench_config_enable_test(b, config, name);
if (r < 0) {
fprintf(stderr, "test '%s' is not defined.\n", name);
exit(1);
}
}
}
struct MatmulBenchResult *result = matmul_bench_run(b, config, disp_flops, NULL);
if (out_csv) {
FILE *fp = fopen(out_csv, "wb");
if (fp == NULL) {
perror(out_csv);
} else {
matmul_bench_export_csv(fp, b, config, result);
}
}
matmul_bench_result_fini(b, result);
matmul_bench_config_fini(b, config);
matmul_bench_fini(b);
return 0;
}