-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenario1.c
More file actions
436 lines (359 loc) · 14.2 KB
/
Copy pathscenario1.c
File metadata and controls
436 lines (359 loc) · 14.2 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include<string.h> // for memcmp
#include<assert.h>
#include<stdlib.h> // for free
#include<algorithms.h>
#include<gen_utils.h> // for reset_array_sizet
#include<gen_data.h> // get_vector, gen_vector, gen_block_tensor
#include<file_utils.h> // for save_to_file
#include<time_meas.h>
#include<bench_utils.h>
// This macro creates a proper filename for the results folder
#define FILENAME(x); snprintf(filename, BUFSIZE, "%s/%.0f_dimmin_%d_dimmax_%d_nmin_%d_nmax_%d_modemin_%d_modemax_%d_blockn_%d.csv", RESULTS_FOLDER, timespec_to_microseconds(time), dim_min, dim_max, n_min, n_max, mode_min, mode_max, block_n)
#define TEST(x); assert( memcmp(model_result->lin.data, x->lin.data, x->lin.size*sizeof(DTYPE))== equality )
int scenario1(int argc, char ** argv) {
int dim_min, dim_max, n_min, n_max;
int mode_min, mode_max;
int block_n;
// we must provide default arguments
dim_min = 3;
dim_max = 3;
n_min = 3;
n_max = 128;
mode_min = 0;
mode_max = -1;
//int block_n_min = 1;
//int block_n_max = n_max;
// could be problematic...
block_n = 8;
// if an odd number:
// -> the last element is the specific value for block_n
// block_n = argv-1 (last element)
if ((argc % 2) != 0) {
//printf("block_n=%s\n", *(argv+argc--));
// CONVERT string representation to integer
sscanf (*(argv+argc--), "%d", &block_n);
// we did -- to decrease used argument count (to say we used this el)
}
switch (argc) {
case 6:
// mode
sscanf (*(argv+argc--), "%d", &mode_max);
sscanf (*(argv+argc--), "%d", &mode_min);
case 4:
// dim, n
sscanf (*(argv+argc--), "%d", &n_max);
sscanf (*(argv+argc--), "%d", &n_min);
sscanf (*(argv+argc--), "%d", &dim_max);
sscanf (*(argv+argc--), "%d", &dim_min);
}
if (mode_max == -1) {
// default stayed, so we must finish the default max
mode_max = dim_max-1;
}
printf("int dim_min=%d\n", dim_min);
printf("int dim_max=%d\n", dim_max);
printf("int n_min=%d\n", n_min);
printf("int n_max=%d\n", n_max);
printf("int mode_min=%d\n", mode_min);
printf("int mode_max=%d\n", mode_max);
printf("int block_n=%d\n", block_n);
char filename[BUFSIZE];
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
FILENAME("results");
printf("filename=%s\n", filename);
FILE * file = fopen(filename, "a");
if (file == NULL) {
perror("Error opening file.\n");
}
//write_header(file);
// improvement: could include the numbered versions for completeness
// +1 FROM TESTS: we include the model algorithm here (tvm_tesor_major)
const int algos = 1;
TVM unfold_unfold_algorithms[1] = {
tvm_output_major_BLAS_row
};
const int blocked_algos = 2;
TVM block_block_algorithms[2] = {
tvm_block_major_input_aligned_output_aligned_BLAS_POWERS,
tvm_morton_block_major_input_aligned_output_aligned_BLAS_POWERS
//tvm_output_major_BLAS_row,
//tvm_output_major_BLAS_row
//tvm_block_major_input_aligned_output_aligned_BLAS_POWERS,
};
//double time = 0;
//TVM block_unfold_algorithm = tvm_block_major_input_aligned;
//TVM block_block_algorithm = tvm_block_major_input_aligned_output_aligned;
//TVM morton_block_morton_block_algorithm = tvm_morton_block_major_input_aligned_output_aligned;
//TVM morton_block_morton_block_algorithm_POWERS = tvm_morton_block_major_input_aligned_output_aligned_POWERS;
//TVM morton_block_unfold_algorithm = tvm_morton_block_major_input_aligned;
// parameters' loops ordered according to their dependency
for (size_t dim=(size_t) dim_min; dim<=(size_t) dim_max; ++dim) {
printf("dim=%zu:\n", dim);
size_t block_layout[dim];
size_t tensor_layout[dim];
size_t temp_mode_max;
if (dim-1 < (size_t) mode_max) {
temp_mode_max = dim-1;
} else {
temp_mode_max = mode_max;
}
for (size_t mode=(size_t) mode_min; mode<=temp_mode_max; ++mode) {
printf(" mode=%zu:\n", mode);
for (size_t n=(size_t) n_min; n<=(size_t) n_max; n*=2) {
printf(" n=%zu:\n", n);
// BEFORE: temp_block_n was meant to reduce the block size so its not more than n
size_t temp_block_n;
if ((size_t) block_n > n) {
temp_block_n = n;
} else {
temp_block_n = block_n;
}
size_t block_size = 1;
for (size_t d=0; d<dim; ++d) {
block_size *= block_n;
}
// put n as each element of tensor_layout
reset_array_sizet(tensor_layout, dim, n);
// or, for a change:
//randomize_array_int(tensor_layout, dim, n);
//printf(" n = ");
//print_to_console_sizet(tensor_layout, dim);
//for (size_t block_n=1; block_n<=(n+1/2); ++block_n) {
//for (size_t block_n=block_n_min; block_n<=block_n_max && block_n<=n; block_n*=2) {
printf(" block_n=%d:\n", block_n);
// put block_n as each element of block_layout
reset_array_sizet(block_layout, dim, temp_block_n);
// allocate tensor,vector,result on the stack
// allocate tensor,vector,result on the stack
struct tensor_storage *tensor = gen_block_tensor(dim, tensor_layout, block_layout);
// allocate tensor,vector,result on the stack
struct lin_storage *vector = gen_vector(tensor->layout[mode]);
// allocate tensor,vector,result on the stack
struct tensor_storage *result = get_block_result_tensor(tensor, mode);
// allocate tensor,vector,result on the stack
//const long FLOPS = tensor->lin.size*2;
//printf("Number of FLOPS: %ld\n", FLOPS);
// run all algorithms in a loop
for (int algo=0; algo<algos; ++algo) {
reset_array(result->lin.data, result->lin.size, 0);
measure(
unfold_unfold_algorithms[algo], tensor, vector, &result->lin, mode,
file, n, temp_block_n, block_size);
}
fflush(file);
// run all blocked algorithms in a loop
for (int algo=0; algo<blocked_algos; ++algo) {
reset_array(result->lin.data, result->lin.size, 0);
measure(
block_block_algorithms[algo], tensor, vector, &result->lin, mode,
file, n, temp_block_n, block_size);
}
fflush(file);
///////////////
free_tensor_storage(result);
free_tensor_storage(tensor);
free_lin_storage(vector);
#if 0
struct timespec start;
struct timespec stop;
reset_array(result->lin.data, result->lin.size, 0);
double average_time = 0;
for (int i=0; i<TIMES; ++i) {
sleep(1);
clock_gettime(CLOCK_MONOTONIC, &start);
tvm_tensor_major_m(tensor, vector, &result->lin, mode, 1);
clock_gettime(CLOCK_MONOTONIC, &stop);
average_time += timespec_to_microseconds(timespec_diff(start, stop));
printf("%lf \n", timespec_to_microseconds(timespec_diff(start,stop)));
}
average_time /= TIMES;
printf("averaged normalized total_time: %f microsec\n", average_time);
write_perf_result(file, time, dim, mode, n, temp_block_n, 99);
#endif
#if 0
int out_algo = algos;
// NOW, let's add the LAPACK to the testing
struct tensor_storage_double *lapack_tensor = get_unfold(tensor, mode);
struct lin_storage_double *lapack_vector = gen_vector_double(tensor->layout[mode]);
struct tensor_storage_double *lapack_result = get_double_tensor(tensor, mode);
time = measure(tvm_lapack_dgemv_input_aligned, lapack_tensor, lapack_vector, &lapack_result->lin, mode);
printf("performance: FLOPS/time(ns) = %d/%lf = %lf\n",
FLOPS,
time*1000,
(FLOPS/(time*1000)));
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// sanity check that we are calculating something (!)
// should be the same as the output major input aligned version
//print_to_console_double(lapack_result->lin.data, lapack_result->lin.size);
free_tensor_storage_double(lapack_tensor);
free_tensor_storage_double(lapack_result);
free_lin_storage_double(lapack_vector);
#endif
#if 0
/////////////// block storage algorithms
// SETUP
int out_algo = algos;
//struct tensor_storage *blocked_tensor = get_block_tensor(tensor, 0, 0); // block - normal
// we don't need tensor anymore!
// free_tensor_storage(tensor);
// CASE 2: block -> block algorithm
// run on blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(block_block_algorithm, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// DESTROY
// free_tensor_storage(blocked_tensor);
// SETUP
++out_algo;
// struct tensor_storage *morton_blocked_tensor = get_block_tensor(tensor, 0, 1); // block - morton
// CASE 2: morton_block -> morton_block
// run on morton blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(morton_block_morton_block_algorithm, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// POWERS algorithm
++out_algo;
// run on morton blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(morton_block_morton_block_algorithm_POWERS, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// DESTROY
// free_tensor_storage(morton_blocked_tensor);
#endif
}
}
}
#if 0
// parameters' loops ordered according to their dependency
for (int dim=dim_min; dim<=dim_max; ++dim) {
printf("dim=%d:\n", dim);
int block_layout[dim];
int tensor_layout[dim];
int temp_mode_max;
if (dim-1 < mode_max) {
printf("set to mode_dim-1\n");
temp_mode_max = dim-1;
} else {
temp_mode_max = mode_max;
}
for (int mode=mode_min; mode<=temp_mode_max; ++mode) {
printf(" mode=%d:\n", mode);
for (int n=n_min; n<=n_max; n*=2) {
printf(" n=%d:\n", n);
// put n as each element of tensor_layout
reset_array_sizet(tensor_layout, dim, n);
int temp_block_n;
if (block_n > n) {
temp_block_n = n;
} else {
temp_block_n = block_n;
}
printf(" block_n=%d:\n", temp_block_n);
// put block_n as each element of block_layout
reset_array_sizet(block_layout, dim, temp_block_n);
// allocate tensor,vector,result on the stack
struct tensor_storage *tensor = gen_block_tensor(dim, tensor_layout, block_layout);
// allocate tensor,vector,result on the stack
struct lin_storage *vector = gen_vector(tensor->layout[mode]);
// allocate tensor,vector,result on the stack
struct tensor_storage *result = get_block_result_tensor(tensor, mode);
// allocate tensor,vector,result on the stack
//const long FLOPS = tensor->lin.size*2;
//printf("Number of FLOPS: %ld\n", FLOPS);
// run all algorithms in a loop
for (int algo=0; algo<algos; ++algo) {
reset_array(result->lin.data, result->lin.size, 0);
measure(
unfold_unfold_algorithms[algo], tensor, vector, &result->lin, mode,
file, n, temp_block_n);
}
fflush(file);
// run all blocked algorithms in a loop
for (int algo=0; algo<blocked_algos; ++algo) {
reset_array(result->lin.data, result->lin.size, 0);
measure(
block_block_algorithms[algo], tensor, vector, &result->lin, mode,
file, n, temp_block_n);
}
fflush(file);
#if 0
struct timespec start;
struct timespec stop;
reset_array(result->lin.data, result->lin.size, 0);
double average_time = 0;
for (int i=0; i<TIMES; ++i) {
sleep(1);
clock_gettime(CLOCK_MONOTONIC, &start);
tvm_tensor_major_m(tensor, vector, &result->lin, mode, 1);
clock_gettime(CLOCK_MONOTONIC, &stop);
average_time += timespec_to_microseconds(timespec_diff(start, stop));
printf("%lf \n", timespec_to_microseconds(timespec_diff(start,stop)));
}
average_time /= TIMES;
printf("averaged normalized total_time: %f microsec\n", average_time);
write_perf_result(file, time, dim, mode, n, temp_block_n, 99);
#endif
#if 0
int out_algo = algos;
// NOW, let's add the LAPACK to the testing
struct tensor_storage_double *lapack_tensor = get_unfold(tensor, mode);
struct lin_storage_double *lapack_vector = gen_vector_double(tensor->layout[mode]);
struct tensor_storage_double *lapack_result = get_double_tensor(tensor, mode);
time = measure(tvm_lapack_dgemv_input_aligned, lapack_tensor, lapack_vector, &lapack_result->lin, mode);
printf("performance: FLOPS/time(ns) = %d/%lf = %lf\n",
FLOPS,
time*1000,
(FLOPS/(time*1000)));
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// sanity check that we are calculating something (!)
// should be the same as the output major input aligned version
//print_to_console_double(lapack_result->lin.data, lapack_result->lin.size);
free_tensor_storage_double(lapack_tensor);
free_tensor_storage_double(lapack_result);
free_lin_storage_double(lapack_vector);
#endif
#if 0
/////////////// block storage algorithms
// SETUP
int out_algo = algos;
//struct tensor_storage *blocked_tensor = get_block_tensor(tensor, 0, 0); // block - normal
// we don't need tensor anymore!
// free_tensor_storage(tensor);
// CASE 2: block -> block algorithm
// run on blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(block_block_algorithm, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// DESTROY
// free_tensor_storage(blocked_tensor);
// SETUP
++out_algo;
// struct tensor_storage *morton_blocked_tensor = get_block_tensor(tensor, 0, 1); // block - morton
// CASE 2: morton_block -> morton_block
// run on morton blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(morton_block_morton_block_algorithm, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// POWERS algorithm
++out_algo;
// run on morton blocked tensor
reset_array(result->lin.data, result->lin.size, 0);
time = measure(morton_block_morton_block_algorithm_POWERS, tensor, vector, &result->lin, mode);
write_perf_result(file, time, dim, mode, n, temp_block_n, out_algo);
// DESTROY
// free_tensor_storage(morton_blocked_tensor);
#endif
///////////////
free_tensor_storage(tensor);
free_tensor_storage(result);
free_lin_storage(vector);
}
}
}
#endif
if (file != NULL) {
fclose(file);
}
return 0;
}