-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_op.c
More file actions
388 lines (291 loc) · 8.46 KB
/
timer_op.c
File metadata and controls
388 lines (291 loc) · 8.46 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
/*
Timer harness for running a "function under test" for num_runs number of
runs.
- richard.m.veras@ou.edu
*/
#include <limits.h>
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include "timer.h"
// Function under test
extern void COMPUTE_NAME_REF( int m0, int k0,
float *input_distributed,
float *weights_distributed,
float *output_distributed );
extern void COMPUTE_NAME_TST( int m0, int k0,
float *input_distributed,
float *weights_distributed,
float *output_distributed );
extern void DISTRIBUTED_ALLOCATE_NAME_REF( int m0, int k0,
float **input_distributed,
float **weights_distributed,
float **output_distributed );
extern void DISTRIBUTED_ALLOCATE_NAME_TST( int m0, int k0,
float **input_distributed,
float **weights_distributed,
float **output_distributed );
extern void DISTRIBUTE_DATA_NAME_REF( int m0, int k0,
float *input_sequential,
float *weights_sequential,
float *input_distributed,
float *weights_distributed );
extern void DISTRIBUTE_DATA_NAME_TST( int m0, int k0,
float *input_sequential,
float *weights_sequential,
float *input_distributed,
float *weights_distributed );
extern void COLLECT_DATA_NAME_REF( int m0, int k0,
float *output_distributed,
float *output_sequential );
extern void COLLECT_DATA_NAME_TST( int m0, int k0,
float *output_distributed,
float *output_sequential );
extern void DISTRIBUTED_FREE_NAME_REF( int m0, int k0,
float *input_distributed,
float *weights_distributed,
float *output_distributed );
extern void DISTRIBUTED_FREE_NAME_TST( int m0, int k0,
float *input_distributed,
float *weights_distributed,
float *output_distributed );
extern void FUN_NAME_TST( int m, int n,
float *src,
int rs_s, int cs_s,
float *dst,
int rs_d, int cs_d);
void fill_buffer_with_random( int num_elems, float *buff )
{
for(int i = 0; i < num_elems; ++i)
buff[i] = ((float)(rand()-((RAND_MAX)/2)))/((float)RAND_MAX);
}
void fill_buffer_with_value( int num_elems, float val, float *buff )
{
for(int i = 0; i < num_elems; ++i)
buff[i] = val;
}
long pick_min_in_list(int num_trials, long *results)
{
long current_min = LONG_MAX;
for( int i = 0; i < num_trials; ++i )
if( results[i] < current_min )
current_min = results[i];
return current_min;
}
void flush_cache()
{
int size = 1024*1024*8;
int *buff = (int *)malloc(sizeof(int)*size);
int i, result = 0;
volatile int sink;
for (i = 0; i < size; i ++)
result += buff[i];
sink = result; /* So the compiler doesn't optimize away the loop */
free(buff);
}
void time_function_under_test(int num_trials,
int num_runs_per_trial,
long *results, // results from each trial
int m0, int k0,
float *input_distributed,
float *weights_distributed,
float *output_distributed
)
{
// Initialize the start and stop variables.
TIMER_INIT_COUNTERS(stop, start);
// Click the timer a few times so the subsequent measurements are more accurate
MPI_Barrier(MPI_COMM_WORLD);
TIMER_WARMUP(stop,start);
// flush the cache
flush_cache();
MPI_Barrier(MPI_COMM_WORLD);
for(int trial = 0; trial < num_trials; ++trial )
{
/*
Time code.
*/
// start timer
TIMER_GET_CLOCK(start);
////////////////////////
// Benchmark the code //
////////////////////////
for(int runs = 0; runs < num_runs_per_trial; ++runs )
{
COMPUTE_NAME_TST( m0, k0,
input_distributed,
weights_distributed,
output_distributed );
}
////////////////////////
// End Benchmark //
////////////////////////
// stop timer
TIMER_GET_CLOCK(stop);
// subtract the start time from the stop time
TIMER_GET_DIFF(start,stop,results[trial])
}
}
int scale_p_on_pos_ret_v_on_neg(int p, int v)
{
if (v < 1)
return -1*v;
else
return v*p;
}
int main( int argc, char *argv[] )
{
int rid;
int num_ranks;
int tag = 0;
MPI_Status status;
int root_rid = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rid);
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
// What we will output to
FILE *result_file;
int num_trials = 30;
int num_runs_per_trial = 1;
// Problem parameters
int min_size;
int max_size;
int step_size;
int in_m0;
int in_k0;
// Get command line arguments
if(argc == 1 )
{
min_size = 16;
max_size = 256;
step_size = 16;
// defaults
in_m0=1;
in_k0=-3;
// default to printing to stdout
result_file = stdout;
}
else if(argc == 5 + 1 || argc == 6 + 1 )
{
min_size = atoi(argv[1]);
max_size = atoi(argv[2]);
step_size = atoi(argv[3]);
in_m0=atoi(argv[4]);
in_k0=atoi(argv[5]);
// default to printing to stdout
result_file = stdout;
if(argc == 6 + 1)
{
// we don't want every node opening the same file
// to write to.
if(rid == 0 )
{
result_file = fopen(argv[6],"w");
}
else
{
result_file = NULL;
}
}
}
else
{
printf("usage: %s min max step m0 k0 [filename]\n",
argv[0]);
exit(1);
}
// Print out the first line of the output in csv format
if( rid == 0 )
{
/*root node */
fprintf(result_file, "num_ranks,m0,k0,result\n");
}
else
{/* all other nodes*/ }
for( int p = min_size;
p < max_size;
p += step_size )
{
// input sizes
int m0=scale_p_on_pos_ret_v_on_neg(p,in_m0);
int k0=scale_p_on_pos_ret_v_on_neg(p,in_k0);
// How big of a buffer do we need
int input_sequential_sz =m0;
int output_sequential_sz =m0;
int weights_sequential_sz=k0;
float *input_sequential_tst = (float *)malloc(sizeof(float)*input_sequential_sz);
float *output_sequential_tst = (float *)malloc(sizeof(float)*output_sequential_sz);
float *weights_sequential_tst = (float *)malloc(sizeof(float)*weights_sequential_sz);
if( rid == 0)
{ /* root node */
// fill src_ref with random values
fill_buffer_with_random( input_sequential_sz, input_sequential_tst );
fill_buffer_with_random( weights_sequential_sz, weights_sequential_tst );
fill_buffer_with_value( output_sequential_sz, -1, output_sequential_tst );
}
else
{/* all other nodes. */}
// run the test
float *input_distributed_tst;
float *weights_distributed_tst;
float *output_distributed_tst;
// Allocate distributed buffers for the reference
DISTRIBUTED_ALLOCATE_NAME_TST( m0, k0,
&input_distributed_tst,
&weights_distributed_tst,
&output_distributed_tst );
// Distribute the sequential buffers
DISTRIBUTE_DATA_NAME_TST( m0, k0,
input_sequential_tst,
weights_sequential_tst,
input_distributed_tst,
weights_distributed_tst );
// Perform the computation
long *results = (long *)malloc(sizeof(long)*num_trials);
time_function_under_test(num_trials,
num_runs_per_trial,
results, // results from each trial
m0, k0,
input_distributed_tst,
weights_distributed_tst,
output_distributed_tst
);
long min_res = pick_min_in_list(num_trials, results);
float nanoseconds = ((float)min_res)/(num_runs_per_trial);
// Number of floating point operations
long num_flops = sizeof(float)*m0*k0*2;
// This gives us throughput as GFLOP/s
float throughput = num_flops / nanoseconds;
free(results);
// Collect the distributed data and write it to a sequential buffer
COLLECT_DATA_NAME_TST( m0, k0,
output_distributed_tst,
output_sequential_tst );
// Finally free the buffers
DISTRIBUTED_FREE_NAME_TST( m0, k0,
input_distributed_tst,
weights_distributed_tst,
output_distributed_tst );
if( rid == 0)
{
/* root node */
fprintf(result_file, "%i,%i,%i,%2.2f\n",
num_ranks,
m0,k0, throughput);
}
else
{/* all other nodes */}
// Free the sequential buffers
free(input_sequential_tst);
free(output_sequential_tst);
free(weights_sequential_tst);
}
if( rid == 0)
{
/* root node */
fclose(result_file);
}
else
{/* all other nodes */}
MPI_Finalize();
}