-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdaggen_commons.c
502 lines (423 loc) · 13.3 KB
/
daggen_commons.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/******************************************************************************
* Copyright (c) 2007-2013. F. Suter, S. Hunold.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL 2.1) which comes with this package.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include "daggen_commons.h"
/*********************************/
/** Command line option parsing **/
/*********************************/
/*
* Parse the options and set default values
*
* returns -1 for usage
* returns -2 for no usage
* returns 0 when Ok
*/
int parseOptions(int argc, char *const *argv) {
int ret_val = 0;
int c;
int oflag = 0;
int nflag = 0;
int fat_flag = 0;
int density_flag = 0;
int ccr_flag = 0;
int mindata_flag = 0;
int maxdata_flag = 0;
int minalpha_flag = 0;
int maxalpha_flag = 0;
int regular_flag = 0;
int jump_flag = 0;
int dot_flag = 0;
global.output_file=stdout;
global.jump=1;
global.mindata=2048;
global.maxdata=11264;
global.minalpha=0.0;
global.maxalpha=0.2;
global.ccr=0;
global.density=0.5;
global.fat=0.5;
global.regular=0.9;
global.n=100;
global.dot_output=0;
while (ret_val == 0) {
static struct option long_options[] = {
{ "fat", 1, 0, 0 }, /* double */
{ "density", 1, 0, 0 }, /* double */
{ "ccr", 1, 0, 0 }, /* int */
{ "mindata", 1, 0, 0 }, /* int */
{ "maxdata", 1, 0, 0 }, /* int */
{ "minalpha", 1, 0, 0 }, /* double */
{ "maxalpha", 1, 0, 0 }, /* double */
{ "regular", 1, 0, 0 }, /* double */
{ "jump", 1, 0, 0 }, /* int */
{ "dot", 0, 0, 0},
{ 0, 0, 0, 0 } };
/* getopt_long stores the option index here. */
int option_index = 0;
/*
parameters
h help
o string output
n int nodes
*/
c = getopt_long(argc, argv, "ho:n:", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c) {
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
// we do nothing right now with the long opts
const char *optname = long_options[option_index].name;
if (!strcmp(optname, "fat")) {
double fat=atof(optarg);
fat_flag = 1;
if ((fat < 0.0) || (fat > 1.0)) {
fprintf(stderr,"Error: invalid fat value: %f\n",fat);
ret_val = -1;
}
global.fat=fat;
} else if (!strcmp(optname, "density")) {
double density=atof(optarg);
density_flag = 1;
if ((density < 0.0) || (density > 1.0)) {
fprintf(stderr,"Error: invalid density value: %f\n",density);
ret_val = -1;
}
global.density=density;
} else if (!strcmp(optname, "ccr")) {
int ccr=atoi(optarg);
ccr_flag = 1;
if (ccr < 0) {
fprintf(stderr,"Error: invalid ccr value: %d\n",ccr);
ret_val = -1;
}
global.ccr=ccr;
} else if (!strcmp(optname, "mindata")) {
int mindata=atoi(optarg);
mindata_flag = 1;
if (mindata <= 0) {
fprintf(stderr,"Error: invalid mindata value: %d\n",mindata);
ret_val = -1;
}
global.mindata=mindata;
} else if (!strcmp(optname, "maxdata")) {
int maxdata=atoi(optarg);
maxdata_flag = 1;
if ((maxdata <= 0) || (maxdata < global.mindata)) {
fprintf(stderr,"Error: invalid maxdata value: %d\n",maxdata);
ret_val = -1;
}
global.maxdata=maxdata;
} else if (!strcmp(optname, "minalpha")) {
double minalpha=atof(optarg);
minalpha_flag = 1;
if (minalpha < 0.0) {
fprintf(stderr,"Error: invalid minalpha value: %f\n",minalpha);
ret_val = -1;
}
global.minalpha=minalpha;
} else if (!strcmp(optname, "maxalpha")) {
double maxalpha=atof(optarg);
maxalpha_flag = 1;
if ((maxalpha < 0.0) || (maxalpha < global.minalpha)) {
fprintf(stderr,"Error: invalid maxalpha value: %f\n",maxalpha);
ret_val = -1;
}
global.maxalpha=maxalpha;
} else if (!strcmp(optname, "regular")) {
double regular=atof(optarg);
regular_flag = 1;
if ((regular < 0.0) || (regular > 1.0)) {
fprintf(stderr,"Error: invalid regular value: %f\n",regular);
ret_val = -1;
}
global.regular=regular;
} else if (!strcmp(optname, "jump")) {
int jump = atoi(optarg);
jump_flag = 1;
if (jump < 0) {
fprintf(stderr,"Error: invalud jump value: %d\n",jump);
ret_val = -1;
}
global.jump=jump;
} else if (!strcmp(optname, "dot")) {
dot_flag = 1;
global.dot_output=1;
} else {
fprintf(stderr, "unknown switch: %s\n", optname);
}
break;
case 'n': {
int n=atoi(optarg);
nflag = 1;
if (n <= 0) {
fprintf(stderr,"Error: invalid n value: %d\n", n);
ret_val = -1;
}
global.n=n;
}
break;
case 'o':
oflag = 1;
const char *filename = optarg;
if ((global.output_file=fopen(filename, "w")) == NULL) {
fprintf(stderr,"Error: Cannot open file '%s' for output\n", filename);
ret_val = -1;
}
break;
case 'h':
ret_val = -1; // show usage
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort();
break;
}
}
if( ret_val == 0 ) {
if (!nflag) {
fprintf(stderr,"Warning: using default n value (%d)\n", global.n);
}
if (!oflag) {
fprintf(stderr,"Warning: Sending output to stdout\n");
}
if (!jump_flag) {
fprintf(stderr,"Warning: using default jump value (%d)\n", global.jump);
}
if (!fat_flag) {
fprintf(stderr,"Warning: using default fat value (%g)\n", global.fat);
}
if (!density_flag) {
fprintf(stderr,"Warning: using default density value (%g)\n",
global.density);
}
if (!ccr_flag) {
fprintf(stderr,"Warning: using default ccr value (%d)\n", global.ccr);
}
if (!mindata_flag) {
fprintf(stderr,"Warning: using default mindata value (%g)\n",
global.mindata);
}
if (!maxdata_flag) {
fprintf(stderr,"Warning: using default maxdata value (%g)\n",
global.maxdata);
}
if (!minalpha_flag) {
fprintf(stderr,"Warning: using default minalpha value (%g)\n",
global.minalpha);
}
if (!maxalpha_flag) {
fprintf(stderr,"Warning: using default maxalpha value (%g)\n",
global.maxalpha);
}
if (!regular_flag) {
fprintf(stderr,"Warning: using default regular value (%g)\n",
global.regular);
}
}
return ret_val;
}
/*
* printUsage()
*/
void printUsage(void)
{
fprintf(stderr,"daggen [options] [-o <output file>]\n"
"\t -n <number of tasks>\n"
"\t --mindata <minimum data size>\n"
"\t --maxdata <maximum data size>\n"
"\t --minalpha <minimum Amdahl's law parameter value>\n"
"\t --maxalpha <maximum Amdahl's law parameter value>\n");
fprintf(stderr,
"\t --fat <dag shape>\n"
"\t fat=1.0: fat (maximum parallelism)\n"
"\t fat=0.0: thin (minimum parallelism)\n"
"\t --density <density>\n"
"\t density=0.0: minimum number of dependencies\n"
"\t density=1.0: full graph\n"
"\t --regular <regularity for num tasks per level>\n"
"\t regular= 1.0: perfectly regular\n"
"\t regular= 0.0: irregular\n"
"\t --ccr <communication(MBytes) to computation(sec) ratio>\n"
"\t --jump <number of levels spanned by communications>\n"
"\t jump=1: perfectly synchronized levels\n"
"\t --dot: output generated DAG in the DOT format\n"
"\n");
return;
}
/****************/
/** DAG output **/
/****************/
void outputDAG(DAG dag) {
int i, j, k;
/* starting at 1 for the root node */
int node_count=1;
/* count and tag the nodes */
for (i=0; i<dag->nb_levels; i++) {
for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
dag->levels[i][j]->tag = node_count++;
}
for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
for (k=0; k<dag->levels[i][j]->nb_children; k++) {
dag->levels[i][j]->transfer_tags[k] = node_count++;
}
}
}
/* accounting for the END node */
fprintf(OUTPUT,"NODE_COUNT %d\n",node_count+1);
/* Create the root node */
fprintf(OUTPUT,"NODE 0 ");
for (i=0; i<dag->nb_tasks_per_level[0]-1; i++) {
fprintf(OUTPUT,"%d,",dag->levels[0][i]->tag);
}
if (dag->nb_tasks_per_level[0])
fprintf(OUTPUT,"%d ROOT 0.0 0.0\n",dag->levels[0][i]->tag);
else
fprintf(OUTPUT,"%d ROOT 0.0 0.0\n",node_count);
/* Creating the regular nodes until next to last level */
for (i=0; i<dag->nb_levels-1; i++) {
for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
/* do the COMPUTATION */
fprintf(OUTPUT,"NODE %d ",dag->levels[i][j]->tag);
for (k=0; k<dag->levels[i][j]->nb_children-1; k++) {
fprintf(OUTPUT,"%d,",dag->levels[i][j]->transfer_tags[k]);
}
if (dag->levels[i][j]->nb_children) {
fprintf(OUTPUT,"%d COMPUTATION %.0f %.2f\n",
dag->levels[i][j]->transfer_tags[k],
dag->levels[i][j]->cost,
dag->levels[i][j]->alpha);
} else {
fprintf(OUTPUT,"%d COMPUTATION %.0f %.2f\n",
node_count,
dag->levels[i][j]->cost,
dag->levels[i][j]->alpha);
}
/* do the TRANSFER */
for (k=0; k<dag->levels[i][j]->nb_children; k++) {
fprintf(OUTPUT,"NODE %d ",dag->levels[i][j]->transfer_tags[k]);
fprintf(OUTPUT,"%d TRANSFER %.0f 0.0\n",
dag->levels[i][j]->children[k]->tag,
dag->levels[i][j]->comm_costs[k]);
}
}
}
/* Do the last level */
for (j=0; j<dag->nb_tasks_per_level[dag->nb_levels-1]; j++) {
fprintf(OUTPUT,"NODE %d %d COMPUTATION %.0f %.2f\n",
dag->levels[dag->nb_levels-1][j]->tag,
node_count,
dag->levels[dag->nb_levels-1][j]->cost,
dag->levels[dag->nb_levels-1][j]->alpha);
}
/* Do the end node */
fprintf(OUTPUT,"NODE %d - END 0.0 0.0\n",node_count);
}
void outputDOT(DAG dag) {
int i, j, k;
/* starting at 1 for the root node */
int node_count=1;
/* count and tag the nodes */
for (i=0; i<dag->nb_levels; i++) {
for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
dag->levels[i][j]->tag = node_count++;
}
// for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
// for (k=0; k<dag->levels[i][j]->nb_children; k++) {
// dag->levels[i][j]->transfer_tags[k] = node_count++;
// }
// }
}
/* accounting for the END node */
fprintf(OUTPUT,"digraph G {\n");
/* Create the root node */
// fprintf(OUTPUT,"NODE 0 ");
// for (i=0; i<dag->nb_tasks_per_level[0]-1; i++) {
// fprintf(OUTPUT,"%d,",dag->levels[0][i]->tag);
// }
// if (dag->nb_tasks_per_level[0])
// fprintf(OUTPUT,"%d ROOT 0.0 0.0\n",dag->levels[0][i]->tag);
// else
// fprintf(OUTPUT,"%d ROOT 0.0 0.0\n",node_count);
//
/* Creating the regular nodes until next to last level */
for (i=0; i<dag->nb_levels; i++) {
for (j=0; j<dag->nb_tasks_per_level[i]; j++) {
/* do the COMPUTATION */
fprintf(OUTPUT," %d [size=\"%.0f\", alpha=\"%.2f\"]\n",
dag->levels[i][j]->tag,
dag->levels[i][j]->cost,
dag->levels[i][j]->alpha);
// for (k=0; k<dag->levels[i][j]->nb_children-1; k++) {
// fprintf(OUTPUT,"%d,",dag->levels[i][j]->transfer_tags[k]);
// }
// if (dag->levels[i][j]->nb_children) {
// fprintf(OUTPUT,"%d COMPUTATION %.0f %.2f\n",
// dag->levels[i][j]->transfer_tags[k],
// dag->levels[i][j]->cost,
// dag->levels[i][j]->alpha);
// } else {
// fprintf(OUTPUT,"%d COMPUTATION %.0f %.2f\n",
// node_count,
// dag->levels[i][j]->cost,
// dag->levels[i][j]->alpha);
// }
/* do the TRANSFER */
for (k=0; k<dag->levels[i][j]->nb_children; k++) {
fprintf(OUTPUT," %d -> %d [size =\"%.f\"]\n",
dag->levels[i][j]->tag,
dag->levels[i][j]->children[k]->tag,
dag->levels[i][j]->comm_costs[k]);
}
}
}
// /* Do the last level */
// for (j=0; j<dag->nb_tasks_per_level[dag->nb_levels-1]; j++) {
// fprintf(OUTPUT,"NODE %d %d COMPUTATION %.0f %.2f\n",
// dag->levels[dag->nb_levels-1][j]->tag,
// node_count,
// dag->levels[dag->nb_levels-1][j]->cost,
// dag->levels[dag->nb_levels-1][j]->alpha);
// }
fprintf(OUTPUT,"}\n");
}
/***********************/
/** Random generators **/
/***********************/
/*
* getIntRandomNumberAround()
*
* returns a STRICTLY POSITIVIE int around x, by perc percents.
*/
int getIntRandomNumberAround(int x, double perc) {
double r;
int new_int;
r = -perc + (2*perc*rand()/(RAND_MAX+1.0));
new_int = MAX(1,(int)((double)x * (1.0 + r/100.00)));
return new_int;
}
/*
* getRandomNumberBetween()
*
*/
double getRandomNumberBetween(double x, double y) {
double r;
r = x + (y-x)*rand()/(RAND_MAX+1.0);
return r;
}