-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_op.c
More file actions
368 lines (279 loc) · 8.65 KB
/
verify_op.c
File metadata and controls
368 lines (279 loc) · 8.65 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
#include <mpi.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR_THRESHOLD 1e-4
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 );
void fill_buffer_with_random( int num_elems, float *buff )
{
long long range = RAND_MAX;
//long long range = 1000;
for(int i = 0; i < num_elems; ++i)
{
buff[i] = ((float)(rand()-((range)/2)))/((float)range);
}
}
void fill_buffer_with_value( int num_elems, float val, float *buff )
{
for(int i = 0; i < num_elems; ++i)
buff[i] = val;
}
float max_pair_wise_diff(int m, int n, int rs, int cs, float *a, float *b)
{
float max_diff = 0.0;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
{
float sum = fabs(a[i*rs+j*cs]+b[i*rs+j*cs]);
float diff = fabs(a[i*rs+j*cs]-b[i*rs+j*cs]);
float res = 0.0f;
if(sum == 0.0f)
res = diff;
else
res = 2*diff/sum;
if( res > max_diff )
max_diff = res;
}
return max_diff;
}
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;
// 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_ref = (float *)malloc(sizeof(float)*input_sequential_sz);
float *output_sequential_ref = (float *)malloc(sizeof(float)*output_sequential_sz);
float *weights_sequential_ref = (float *)malloc(sizeof(float)*weights_sequential_sz);
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);
// We only want to allocate the buffers on every node, but
// we don't want to fill them with random data on every node
// just from the root node.
if( rid == 0)
{ /* root node */
// fill src_ref with random values
fill_buffer_with_random( input_sequential_sz, input_sequential_ref );
fill_buffer_with_random( weights_sequential_sz, weights_sequential_ref );
fill_buffer_with_value( output_sequential_sz, -1, output_sequential_ref );
// copy src_ref to src_tst
memcpy(input_sequential_tst,input_sequential_ref,input_sequential_sz*sizeof(float));
memcpy(weights_sequential_tst,weights_sequential_ref,weights_sequential_sz*sizeof(float));
memcpy(output_sequential_tst,output_sequential_ref,output_sequential_sz*sizeof(float));
}
else
{/* all other nodes. */}
/*
Run the reference
*/
float *input_distributed_ref;
float *weights_distributed_ref;
float *output_distributed_ref;
// Allocate distributed buffers for the reference
DISTRIBUTED_ALLOCATE_NAME_REF( m0, k0,
&input_distributed_ref,
&weights_distributed_ref,
&output_distributed_ref );
// Distribute the sequential buffers
DISTRIBUTE_DATA_NAME_REF( m0, k0,
input_sequential_ref,
weights_sequential_ref,
input_distributed_ref,
weights_distributed_ref );
// Perform the computation
COMPUTE_NAME_REF( m0, k0,
input_distributed_ref,
weights_distributed_ref,
output_distributed_ref );
// Collect the distributed data and write it to a sequential buffer
COLLECT_DATA_NAME_REF( m0, k0,
output_distributed_ref,
output_sequential_ref );
// Finally free the buffers
DISTRIBUTED_FREE_NAME_REF( m0, k0,
input_distributed_ref,
weights_distributed_ref,
output_distributed_ref );
// 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
COMPUTE_NAME_TST( m0, k0,
input_distributed_tst,
weights_distributed_tst,
output_distributed_tst );
// 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 );
// We only need to verify the results sequentially
if( rid == 0)
{
/* root node */
float res = max_pair_wise_diff(m0,1,1,1, output_sequential_ref, output_sequential_tst);
fprintf(result_file, "%i,%i,%i,",
num_ranks,
m0,k0);
// if our error is greater than some threshold
if( res > ERROR_THRESHOLD )
fprintf(result_file, "FAIL\n");
else
fprintf(result_file, "PASS\n");
}
else
{/* all other nodes */}
// Free the sequential buffers
free(input_sequential_ref);
free(output_sequential_ref);
free(weights_sequential_ref);
free(input_sequential_tst);
free(output_sequential_tst);
free(weights_sequential_tst);
}
// Only needs to be done by root node
if(rid == 0)
{
/* root node */
fclose(result_file);
}
else
{/* all other nodes */}
MPI_Finalize();
}