forked from Bader-Research/triangle-counting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
627 lines (489 loc) · 13.8 KB
/
graph.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include "types.h"
#include "graph.h"
#ifdef PARALLEL
#include "omp.h"
#endif
void copy_graph(const GRAPH_TYPE *srcGraph, GRAPH_TYPE *dstGraph) {
dstGraph->numVertices = srcGraph->numVertices;
dstGraph->numEdges = srcGraph->numEdges;
memcpy(dstGraph->rowPtr, srcGraph->rowPtr, (srcGraph->numVertices + 1) * sizeof(UINT_t));
memcpy(dstGraph->colInd, srcGraph->colInd, srcGraph->numEdges * sizeof(UINT_t));
}
void allocate_graph(GRAPH_TYPE* graph) {
graph->rowPtr = (UINT_t*)calloc((graph->numVertices + 1), sizeof(UINT_t));
assert_malloc(graph->rowPtr);
graph->colInd = (UINT_t*)calloc(graph->numEdges, sizeof(UINT_t));
assert_malloc(graph->colInd);
}
void free_graph(GRAPH_TYPE* graph) {
free(graph->rowPtr);
free(graph->colInd);
free(graph);
}
void allocate_graph_RMAT(const int scale, const int edgeFactor, GRAPH_TYPE* graph) {
graph->numVertices = 1 << scale;
graph->numEdges = 2 * graph->numVertices * edgeFactor; /* Factor of 2 is to store undirected edges (a, b) and (b, a) */
allocate_graph(graph);
}
static int compareInt_t(const void *a, const void *b) {
UINT_t arg1 = *(const UINT_t *)a;
UINT_t arg2 = *(const UINT_t *)b;
if (arg1 < arg2) return -1;
if (arg1 > arg2) return 1;
return 0;
}
void convert_edges_to_graph(const edge_t* edges, GRAPH_TYPE* graph) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
for (UINT_t i = 0 ; i < n + 1 ; i++)
Ap[i] = 0;
// Count the number of edges incident to each vertex
for (UINT_t i = 0; i < m; i++) {
UINT_t vertex = edges[i].src;
Ap[vertex + 1]++;
}
// Compute the prefix sum of the rowPtr array
for (UINT_t i = 1; i <= n; i++) {
Ap[i] += Ap[i - 1];
}
// Populate the col_idx array with the destination vertices
UINT_t *current_row = (UINT_t *)calloc(n, sizeof(UINT_t));
assert_malloc(current_row);
for (UINT_t i = 0; i < m; i++) {
UINT_t src_vertex = edges[i].src;
UINT_t dst_vertex = edges[i].dst;
UINT_t index = Ap[src_vertex] + current_row[src_vertex];
Ai[index] = dst_vertex;
current_row[src_vertex]++;
}
// Sort the column indices within each row
for (UINT_t i = 0; i < graph->numVertices; i++) {
UINT_t s = Ap[i];
UINT_t e = Ap[i + 1];
UINT_t size = e - s;
UINT_t *row_indices = &Ai[s];
qsort(row_indices, size, sizeof(UINT_t), compareInt_t);
}
free(current_row);
}
void create_graph_RMAT(GRAPH_TYPE* graph, const UINT_t scale) {
register int good;
register UINT_t src, dst;
edge_t* edges = (edge_t*)calloc(graph->numEdges, sizeof(edge_t));
assert_malloc(edges);
UINT_t e_start = 0;
for (UINT_t e = e_start ; e < graph->numEdges ; e+=2) {
good = 0;
while (!good) {
src = 0;
dst = 0;
for (UINT_t level = 0; level < scale; level++) {
double randNum = (double)rand() / RAND_MAX;
double a = 0.57, b = 0.19, c = 0.19; /* d = 1 - a - b - c */
if (randNum < a)
continue;
else if (randNum < a + b)
dst |= 1 << level;
else if (randNum < a + b + c)
src |= 1 << level;
else {
src |= 1 << level;
dst |= 1 << level;
}
}
good = 1;
/* Only keep unique edges */
for (UINT_t i = 0; i<e ; i++)
if ((edges[i].src == src) && (edges[i].dst == dst)) good = 0;
/* Do not keep self-loops */
if (src == dst) good = 0;
}
edges[e ].src = src;
edges[e ].dst = dst;
edges[e+1].src = dst;
edges[e+1].dst = src;
#if DEBUG
fprintf(stdout,"Edge[%5d]: (%5d, %5d)\n",e, edges[e].src, edges[e].dst);
#endif
}
convert_edges_to_graph(edges,graph);
free(edges);
}
void print_graph(const GRAPH_TYPE* graph, FILE *outfile) {
const UINT_t* Ap = graph->rowPtr;
const UINT_t* Ai = graph->colInd;
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
fprintf(outfile,"Number of Vertices: %u\n", n);
fprintf(outfile,"Number of Edges: %u\n", m);
fprintf(outfile,"RowPtr: ");
for (UINT_t i = 0; i <= n; i++)
fprintf(outfile,"%u ", Ap[i]);
fprintf(outfile,"\n");
fprintf(outfile,"ColInd: ");
for (UINT_t i = 0; i < m; i++)
fprintf(outfile,"%u ", Ai[i]);
fprintf(outfile,"\n");
}
bool check_edge(const GRAPH_TYPE *graph, const UINT_t v, const UINT_t w) {
const UINT_t* restrict Ap = graph->rowPtr;
const UINT_t* restrict Ai = graph->colInd;
UINT_t s = Ap[v];
UINT_t e = Ap[v+1];
for (UINT_t i = s; i < e; i++)
if (Ai[i] == w)
return true;
return false;
}
typedef struct {
UINT_t degree;
UINT_t index;
} vertexDegree_t;
static int compareVertexDegree_t(const void *a, const void *b) {
vertexDegree_t v1 = *(const vertexDegree_t *)a;
vertexDegree_t v2 = *(const vertexDegree_t *)b;
if (v1.degree > v2.degree) return -1;
if (v1.degree < v2.degree) return 1;
if (v1.index < v2.index) return -1;
if (v1.index > v2.index) return 1;
return 0;
}
static int compareVertexDegreeLowestFirst_t(const void *a, const void *b) {
vertexDegree_t v1 = *(const vertexDegree_t *)a;
vertexDegree_t v2 = *(const vertexDegree_t *)b;
if (v1.degree < v2.degree) return -1;
if (v1.degree > v2.degree) return 1;
if (v1.index < v2.index) return -1;
if (v1.index > v2.index) return 1;
return 0;
}
GRAPH_TYPE *reorder_graph_by_degree(const GRAPH_TYPE *graph, enum reorderDegree_t reorderDegree) {
register UINT_t s;
register UINT_t b, e;
const UINT_t* restrict Ap = graph->rowPtr;
const UINT_t* restrict Ai = graph->colInd;
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
bool* Hash = (bool *)calloc(m, sizeof(bool));
assert_malloc(Hash);
UINT_t* Size = (UINT_t *)calloc(n, sizeof(UINT_t));
assert_malloc(Size);
UINT_t* A = (UINT_t *)calloc(m, sizeof(UINT_t));
assert_malloc(A);
vertexDegree_t* Perm = (vertexDegree_t *)malloc(n * sizeof(vertexDegree_t));
assert_malloc(Perm);
for (UINT_t i=0; i<n ; i++) {
Perm[i].degree = Ap[i+1] - Ap[i];
Perm[i].index = i;
}
qsort(Perm, n, sizeof(vertexDegree_t), ((reorderDegree == REORDER_HIGHEST_DEGREE_FIRST)? compareVertexDegree_t: compareVertexDegreeLowestFirst_t));
GRAPH_TYPE *graph2;
graph2 = (GRAPH_TYPE *)malloc(sizeof(GRAPH_TYPE));
assert_malloc(graph2);
graph2->numVertices = n;
graph2->numEdges = m;
allocate_graph(graph2);
UINT_t* restrict Ap2 = graph2->rowPtr;
UINT_t* restrict Ai2 = graph2->colInd;
Ap2[0] = 0;
for (UINT_t i=1 ; i<=n ; i++)
Ap2[i] = Ap2[i-1] + Perm[i-1].degree;
UINT_t *reverse = (UINT_t *)malloc(n*sizeof(UINT_t));
assert_malloc(reverse);
for (UINT_t i=0 ; i<n ; i++)
reverse[Perm[i].index] = i;
for (s = 0; s < n ; s++) {
UINT_t ps = Perm[s].index;
b = Ap[ps];
e = Ap[ps+1];
UINT_t d = 0;
for (UINT_t i=b ; i<e ; i++) {
Ai2[Ap2[s] + d] = reverse[Ai[i]];
d++;
}
}
free(reverse);
free(Perm);
free(A);
free(Size);
free(Hash);
return graph2;
}
UINT_t intersectSizeMergePath(const GRAPH_TYPE* graph, const UINT_t v, const UINT_t w) {
register UINT_t vb, ve, wb, we;
register UINT_t ptr_v, ptr_w;
UINT_t count = 0;
const UINT_t* restrict Ap = graph->rowPtr;
const UINT_t* restrict Ai = graph->colInd;
vb = Ap[v ];
ve = Ap[v+1];
wb = Ap[w ];
we = Ap[w+1];
ptr_v = vb;
ptr_w = wb;
while ((ptr_v < ve) && (ptr_w < we)) {
if (Ai[ptr_v] == Ai[ptr_w]) {
count++;
ptr_v++;
ptr_w++;
}
else
if (Ai[ptr_v] < Ai[ptr_w])
ptr_v++;
else
ptr_w++;
}
return count;
}
static INT_t binarySearch(const UINT_t* list, const UINT_t start, const UINT_t end, const UINT_t target) {
register INT_t s=start, e=end, mid;
while (s < e) {
mid = s + (e - s) / 2;
if (list[mid] == target)
return mid;
if (list[mid] < target)
s = mid + 1;
else
e = mid;
}
return -1;
}
UINT_t intersectSizeBinarySearch(const GRAPH_TYPE* graph, const UINT_t v, const UINT_t w) {
register UINT_t vb, ve, wb, we;
UINT_t count=0;
const UINT_t* restrict Ap = graph->rowPtr;
const UINT_t* restrict Ai = graph->colInd;
const UINT_t n = graph->numVertices;
if ((v<0) || (v >= n) || (w<0) || (w >= n)) {
fprintf(stderr,"vertices out of range in intersectSize()\n");
exit(-1);
}
vb = Ap[v ];
ve = Ap[v+1];
wb = Ap[w ];
we = Ap[w+1];
UINT_t size_v = ve-vb;
UINT_t size_w = we-wb;
if (size_v <= size_w) {
for (UINT_t i=vb ; i<ve ; i++)
if (binarySearch((UINT_t *)Ai, wb, we, Ai[i])>=0) count++;
} else {
for (UINT_t i=wb ; i<we ; i++)
if (binarySearch((UINT_t *)Ai, vb, ve, Ai[i])>=0) count++;
}
return count;
}
static UINT_t binarySearch_partition(const UINT_t* list, const UINT_t start, const UINT_t end, const UINT_t target) {
register INT_t s=start, e=end, mid;
while (s < e) {
mid = s + (e - s) / 2;
#if 0
if (mid >=e) {
printf("ERROR, binarypart: mid >= e\n");
exit(-1);
}
#endif
if (list[mid] == target)
return mid;
if (list[mid] < target)
s = mid + 1;
else
e = mid;
}
return s;
}
UINT_t searchLists_with_partitioning(const UINT_t* list1, const INT_t s1, const INT_t e1, const UINT_t* list2, const INT_t s2, const INT_t e2) {
INT_t mid1, loc2;
UINT_t count = 0;
if ((s1>e1)||(s2>e2))
return 0;
mid1 = s1 + (e1 - s1)/2;
#if 0
if (mid1 > e1) {
printf("ERROR: searchpart: mid1 > e1\n");
exit(-1);
}
#endif
/* need a binary search that returns the item or the next higher position */
loc2 = binarySearch_partition(list2, s2, e2, list1[mid1]);
#if 0
if (loc2 > e2) {
printf("ERROR: searchpart loc2 (%d) > e2 (%d)\n",loc2,e2);
exit(-1);
}
#endif
INT_t s11 = s1;
INT_t e11 = mid1 - 1;
INT_t s21 = s2;
INT_t e21 = loc2;
INT_t s12 = mid1 + 1;
INT_t e12 = e1;
INT_t s22 = loc2;
INT_t e22 = e2;
if (list1[mid1] == list2[loc2]) {
count++;
e21--;
s22++;
}
count += searchLists_with_partitioning(list1, s11, e11, list2, s21, e21);
count += searchLists_with_partitioning(list1, s12, e12, list2, s22, e22);
return count;
}
UINT_t intersectSizeHash(const GRAPH_TYPE *graph, bool *Hash, const UINT_t v, const UINT_t w) {
register UINT_t vb, ve, wb, we;
register UINT_t s1, e1, s2, e2;
UINT_t count = 0;
const UINT_t* restrict Ap = graph->rowPtr;
const UINT_t* restrict Ai = graph->colInd;
vb = Ap[v ];
ve = Ap[v+1];
wb = Ap[w ];
we = Ap[w+1];
if ((ve-vb) < (we-wb)) {
s1 = vb;
e1 = ve;
s2 = wb;
e2 = we;
} else {
s1 = wb;
e1 = we;
s2 = vb;
e2 = ve;
}
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[Ai[i]] = true;
for (UINT_t i= s2; i<e2 ; i++)
if (Hash[Ai[i]]) count++;
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[Ai[i]] = false;
return count;
}
UINT_t intersectSizeMergePath_forward(const GRAPH_TYPE* graph, const UINT_t v, const UINT_t w, const UINT_t* A, const UINT_t* Size) {
register UINT_t vb, ve, wb, we;
register UINT_t ptr_v, ptr_w;
UINT_t count = 0;
const UINT_t* restrict Ap = graph->rowPtr;
vb = Ap[v ];
ve = vb + Size[v];
wb = Ap[w ];
we = wb + Size[w];
ptr_v = vb;
ptr_w = wb;
while ((ptr_v < ve) && (ptr_w < we)) {
if (A[ptr_v] == A[ptr_w]) {
count++;
ptr_v++;
ptr_w++;
}
else
if (A[ptr_v] < A[ptr_w])
ptr_v++;
else
ptr_w++;
}
return count;
}
UINT_t intersectSizeHash_forward(const GRAPH_TYPE *graph, bool *Hash, const UINT_t v, const UINT_t w, const UINT_t* A, const UINT_t* Size) {
register UINT_t vb, ve, wb, we;
register UINT_t s1, e1, s2, e2;
UINT_t count = 0;
const UINT_t* restrict Ap = graph->rowPtr;
vb = Ap[v ];
ve = vb + Size[v];
wb = Ap[w ];
we = wb + Size[w];
if (Size[v] < Size[w]) {
s1 = vb;
e1 = ve;
s2 = wb;
e2 = we;
} else {
s1 = wb;
e1 = we;
s2 = vb;
e2 = ve;
}
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = true;
for (UINT_t i= s2; i<e2 ; i++)
if (Hash[A[i]]) count++;
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = false;
return count;
}
#ifdef PARALLEL
UINT_t intersectSizeHash_forward_P(const GRAPH_TYPE *graph, bool *Hash, const UINT_t v, const UINT_t w, const UINT_t* A, const UINT_t* Size) {
register UINT_t vb, ve, wb, we;
register UINT_t s1, e1, s2, e2;
UINT_t count = 0;
int numThreads;
UINT_t *mycount;
const UINT_t* restrict Ap = graph->rowPtr;
vb = Ap[v ];
ve = vb + Size[v];
wb = Ap[w ];
we = wb + Size[w];
if (Size[v] < Size[w]) {
s1 = vb;
e1 = ve;
s2 = wb;
e2 = we;
} else {
s1 = wb;
e1 = we;
s2 = vb;
e2 = ve;
}
#pragma omp parallel
{
int myID = omp_get_thread_num();
if (myID==0) {
numThreads = omp_get_num_threads();
mycount = (UINT_t *)calloc(numThreads, sizeof(UINT_t));
assert_malloc(mycount);
}
#pragma omp barrier
#pragma omp for
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = true;
#pragma omp for
for (UINT_t i= s2; i<e2 ; i++)
if (Hash[A[i]]) mycount[myID]++;
#pragma omp for
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = false;
#pragma omp for reduction(+:count)
for (int i = 0; i < numThreads ; i++)
count += mycount[i];
}
free(mycount);
return count;
}
#endif
UINT_t intersectSizeHashSkip_forward(const GRAPH_TYPE *graph, bool *Hash, const UINT_t v, const UINT_t w, const UINT_t* A, const UINT_t* Size) {
register UINT_t s1, e1, s2, e2;
UINT_t count = 0;
const UINT_t* restrict Ap = graph->rowPtr;
if (Size[v] < Size[w]) {
if (Size[v] == 0) return 0;
s1 = Ap[v ];
e1 = s1 + Size[v];
s2 = Ap[w ];
e2 = s2 + Size[w];
} else {
if (Size[w] == 0) return 0;
s1 = Ap[w ];
e1 = s1 + Size[w];
s2 = Ap[v ];
e2 = s2 + Size[v];
}
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = true;
for (UINT_t i=s2 ; i<e2 ; i++)
if (Hash[A[i]]) count++;
for (UINT_t i=s1 ; i<e1 ; i++)
Hash[A[i]] = false;
return count;
}