-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_reader.c
475 lines (434 loc) · 11 KB
/
script_reader.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
// #include <sys/resource.h>
#include <unistd.h>
#include <assert.h>
#include <stdint.h>
#ifdef __MTA__
#include <sys/mta_task.h>
#include <machine/runtime.h>
#else
#include "compat/xmt-ops.h"
#endif
#include "globals.h"
#include "defs.h"
#include "xmt-luc.h"
#include "luc_file_io/luc_file_io.h"
#include "stinger-atomics.h"
int64_t isint(char * buffer)
{
while(*buffer != 0)
{
if(*buffer >= '0' && *buffer <= '9')
buffer++;
else return 0;
}
return 1;
}
void bfs(graph * G, int64_t v)
{
int64_t NV = G->numVertices;
int64_t * Q = xmalloc(sizeof(int64_t)*NV);
int64_t * dist = xmalloc(sizeof(int64_t)*NV);
Q[0] = v;
int64_t Qnext = 1;
int64_t nQ = 1;
int64_t * marks = G->marks;
int64_t * start = G->startVertex;
int64_t * eV = G->endVertex;
int64_t d_phase;
int64_t Qstart = 0;
int64_t Qend = 1;
int64_t j, k;
for(j=0; j<NV; j++)
marks[j] = 0;
marks[v] = 1;
PushOnStack: /* Push nodes onto Q */
d_phase = nQ;
MTA("mta assert no dependence")
MTA("mta block dynamic schedule")
MTA("mta use 100 streams")
for (j = Qstart; j < Qend; j++) {
int64_t v = Q[j];
int64_t myStart = start[v];
int64_t myEnd = start[v+1];
for (k = myStart; k < myEnd; k++) {
int64_t d, w;
w = eV[k];
d = dist[w];
if (d < 0) {
if (stinger_int_fetch_add(marks + w, 1) == 0) {
dist[w] = d_phase; Q[stinger_int64_fetch_add(&Qnext, 1)] = w;
}
}
}
}
Qstart = Qend;
Qend = Qnext;
if(Qstart != Qend)
goto PushOnStack;
}
void scriptCheck ( char * scriptname, int64_t run)
{
FILE * sfile = fopen(scriptname, "r");
char buffer[1000];
char secondary[1000];
int64_t opened = 0;
int64_t saved = 0;
int64_t writeable = 0;
int64_t kernel_id = 0;
graph * G = NULL;
graph * S = NULL;
double * result = NULL;
int64_t NV, NE;
int64_t extract = 0;
int64_t max, maxV;
while(fscanf(sfile, "%s", buffer) != EOF)
{
if(!opened && strcmp(buffer, "read") != 0)
{
printf("SCRIPT ERROR: first command must be 'read'.\n");
exit(1);
}
if(strcmp(buffer, "modularity") != 0 &&
strcmp(buffer, "conductance") != 0 && extract == 1)
{
printf("Generating subgraph...");
graph * G2 = genSubGraph(G, NV, 1);
printf("done\n");
if(G != S)
free_graph(G);
G = G2;
extract = 0;
}
if(strcmp(buffer, "read") == 0)
{
fscanf(sfile, "%s", secondary);
printf("trying to read file as %s...\n", secondary);
if(G != S)
free_graph(G);
printf("done free graph\n");
if(strcmp(secondary, "dimacs")==0)
{
fscanf(sfile, "%s", secondary);
G = parse_DIMACS_graph(secondary);
}
else if(strcmp(secondary, "binary")==0)
{
printf("reading binary graph sizeof(graph) = %ld\n", sizeof(graph));
fscanf(sfile, "%s", secondary);
G = (graph *) xmalloc(sizeof(graph));
printf("init graphio...\n");
graphio_b(G, secondary, 4, NULL, NULL);
printf("done\n");
}
else if(strcmp(secondary, "el")==0)
{
printf("reading binary edge list graph sizeof(graph) = %ld\n", sizeof(graph));
fscanf(sfile, "%s", secondary);
G = (graph *) xmalloc(sizeof(graph));
printf("init graphio...\n");
graphio_el(G, secondary);
printf("done\n");
}
else
{
printf("SYNTAX ERROR: '%s' is not a valid graph type to read.\n", secondary);
exit(1);
}
opened = 1;
NV = G->numVertices;
NE = G->numEdges;
printf("NV: %ld, NE: %ld\n", NV, NE);
result = xmalloc(sizeof(double)*NV);
graphCheck(G);
printf("Estimating graph diameter using 128 sources...\n");
fflush(stdout);
double time = timer();
SCALE = NV >> 4;
stats_tic ("estdiameter");
int64_t est_diameter = calculateGraphDiameter(G, 128);
stats_toc ();
SCALE = est_diameter << 2;
getUserParameters(SCALE, 8);
time = timer() - time;
printf("Estimated graph diameter: %ld\n", est_diameter);
printf("Setting SCALE appropriately.\n");
printf("Time taken is %9.6lf sec.\n", time);
fflush(stdout);
}
else if(strcmp(buffer, "=>") == 0)
{
if(!writeable)
{
printf("SCRIPT ERROR: '=>' was not preceded by operation that produces writeable output.\n");
exit(1);
}
fscanf(sfile, "%s", secondary);
if(writeable == 2)
{
fwrite_doubles(result, G->numVertices, secondary);
}
else
{
size_t filesize = 3+G->numVertices+2*G->numEdges;
uint32_t * output = xmalloc(sizeof(uint32_t)*filesize);
output[0] = (uint32_t) G->numEdges;
output[1] = (uint32_t) G->numVertices;
for(int64_t i=0; i<G->numVertices; i++)
{
output[3+i] = (uint32_t) G->edgeStart[i];
}
for(int64_t i=0; i<G->numEdges; i++)
{
output[3+G->numVertices+i] = (uint32_t) G->endVertex[i];
output[3+G->numVertices+numEdges+i] = (uint32_t) G->intWeight[i];
}
// luc_fwrite(output, filesize, sizeof(uint32_t), 0, secondary);
xmt_luc_snapout(secondary, output, filesize*sizeof(uint32_t));
free(output);
}
}
writeable = 0;
if(strcmp(buffer, "read") == 0);
else if(strcmp(buffer, "=>") == 0);
else if(strcmp(buffer, "restore") == 0)
{
if(!saved)
{
printf("SCRIPT ERROR: 'restore' command before any graph 'save'.\n");
exit(1);
}
if(G != S)
free_graph(G);
G = S;
writeable = 1;
}
else if(strcmp(buffer, "save") == 0)
{
free_graph(S);
S = G;
saved = 1;
writeable = 1;
}
else if(strcmp(buffer, "extract") == 0)
{
extract = 1;
writeable = 1;
fscanf(sfile, "%s", secondary);
if(strcmp(secondary, "bfs")== 0)
{
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'bfs' extraction requires numeric argument\n");
exit(1);
}
bfs(G, atoi(secondary));
}
else if(strcmp(secondary, "kcore") == 0)
{
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'kcore' extraction requires numeric argument\n");
exit(1);
}
kcore(G, atoi(secondary));
int64_t i;
for(i=0; i<NV; i++)
{
G->marks[i] = (G->marks[i] > 0);
}
}
else if(strcmp(secondary, "component") == 0)
{
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'component' extraction requires numeric argument\n");
exit(1);
}
connectedComponents(G);
int64_t rank = atoi(secondary);
int64_t i;
int64_t * D = G->marks;
int64_t * T = xmalloc (NV * sizeof(int64_t));
for (i = 0; i < NV; i++) {
T[i] = 0;
}
MTA("mta assert no dependence")
for (i = 0; i < NV; i++) {
stinger_int64_fetch_add(&T[D[i]], 1);
}
int64_t argmax;
for (i = 0; i<rank; i++)
{
int64_t max = 0;
int64_t j;
for( j=0; j<NV; j++)
{
if(T[j] > max)
{
max = T[j];
argmax = j;
}
}
T[argmax] = 0;
}
printf("extracted component number %ld\n\n", argmax);
for (i = 0; i<NV; i++)
if(D[i] == argmax)
D[i] = 1;
else
D[i] = 0;
}
}
else if (strcmp(buffer, "kcentrality")==0)
{
writeable = 2;
kernel_id = 1;
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'kcentrality' kernel requires (2) numeric arguments\n");
exit(1);
}
int64_t k = atoi(secondary);
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'kcore' extraction requires (2) numeric arguments\n");
exit(1);
}
int64_t v0 = atoi(secondary);
if(result != NULL)
{
free(result);
result = xmalloc(sizeof(double)*NV);
}
printf("\nkcentrality(%ld) beginning execution...\n", k);
fflush (stdout);
stats_tic ("kcentrality");
double timeBC = kcentrality(G, result, v0, k);
stats_toc ();
printf("\tkcentrality() completed execution.\n");
printf("Maximum kBC Vertices\n");
printf("--------------------\n");
int64_t j;
double maxBC;
int64_t maxI;
for (j = 0; j < 10; j++) {
maxI = 0;
maxBC = result[0];
int64_t i;
for (i = 1; i < NV; i++) if (result[i] > maxBC) {maxBC = result[i]; maxI = i;}
printf("#%2ld: %8ld - %20.15e\n", j+1, maxI, maxBC);
result[maxI] = -1.0;
}
printf("Time taken is %9.6lf sec.\n\n", timeBC);
}
else if (strcmp(buffer, "modularity")==0)
{
kernel_id = 2;
printf("\nCalculating the modularity score of the graph...\n");
fflush (stdout);
double time = timer();
stats_tic ("modularity");
computeModularityValue(G, G->marks, NV);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "degree")==0)
{
kernel_id = 3;
printf("\nCalculating out-degree distributions...\n");
fflush (stdout);
double time = timer();
stats_tic ("degreedist");
calculateDegreeDistributions(G);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "conductance")==0)
{
kernel_id = 4;
printf("Calculating conductance of the largest connected component...\n");
fflush (stdout);
double time = timer();
stats_tic ("conductance");
computeConductanceValue(G, G->marks);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "components")==0)
{
kernel_id = 5;
double time = timer();
int64_t numComp = connectedComponents(G);
time = timer() - time;
printf("There are %ld components in the graph.\n", numComp);
calculateComponentDistributions(G, numComp, &max, &maxV);
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "clustering")==0)
{
writeable = 2;
kernel_id = 6;
if(result != NULL)
free(result);
printf("\nCalculating local clustering coefficients...\n");
fflush (stdout);
double time = timer();
stats_tic ("clusteringcoeff");
result = calculateClusteringLocal(G);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "transitivity")==0)
{
writeable = 2;
kernel_id = 7;
if(result != NULL)
free(result);
printf("\nCalculating the global transitivity coefficient...\n");
fflush (stdout);
double time = timer();
stats_tic ("transitivity");
result = calculateTransitivityLocal(G);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
}
else if (strcmp(buffer, "diameter")==0)
{
kernel_id = 8;
fscanf(sfile, "%s", secondary);
if(!isint(secondary))
{
printf("SYNTAX ERROR: 'diameter' kernel requires numeric argument\n");
exit(1);
}
int64_t Vs = atoi(secondary);
printf("\nCalculating graph diameter...\n");
fflush (stdout);
double time = timer();
stats_tic ("diameter");
calculateGraphDiameter(G, Vs);
stats_toc ();
time = timer() - time;
printf("Time taken is %9.6lf sec.\n\n", time);
} else if (strcmp (buffer, "printstats") == 0) {
print_stats ();
} else {
printf("SYNTAX ERROR: %s is not a recognized command.\n", buffer);
}
}
}