forked from MohsenKoohi/LaganLighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.c
1236 lines (1057 loc) · 34.9 KB
/
cc.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __CC_C
#define __CC_C
// This file contains implementation of the Connected Components algorithms
/*
CC with pull direction in all iterations. We use this for validation of results.
flags:
bit 0: print stats
bit 1: do not reset papi
exec_info: if not NULL, will have
[0]: exec time
[1-7]: papi events
*/
unsigned int* cc_pull(struct par_env* pe, struct ll_400_graph* g, unsigned int flags, unsigned long* exec_info, unsigned int* ccs_p)
{
// Initial checks
assert(pe != NULL && g != NULL);
unsigned long t0 = - get_nano_time();
printf("\n\033[3;31mcc_pull\033[0;37m\n");
// Reset papi
if(!(flags & 2U))
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
papi_reset(pe->papi_args[tid]);
}
// Allocate memory
unsigned int* cc = numa_alloc_interleaved(sizeof(unsigned int) * g->vertices_count);
assert(cc != NULL);
#pragma omp parallel for
for(unsigned int v = 0; v < g->vertices_count; v++)
cc[v] = v;
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
// Edge partitioning
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning(g, partitions, partitions_count);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Pull iterations
unsigned int cc_changed = 0;
unsigned int cc_iter = 0;
do
{
cc_changed = 0;
unsigned long mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned long thread_cc_changed = 0;
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int component = cc[v];
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v + 1]; e++)
{
unsigned int neighbour = g->edges_list[e];
if(cc[neighbour] < component)
component = cc[neighbour];
}
if(component < cc[v])
{
cc[v] = component;
thread_cc_changed++;
}
}
}
if(thread_cc_changed)
__sync_fetch_and_add(&cc_changed, thread_cc_changed, __ATOMIC_SEQ_CST);
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
if(flags & 1U)
{
char temp[255];
if(cc_changed < 100)
sprintf(temp, "Iter: %'3u, |F|: %5u, time:", cc_iter, cc_changed);
else
sprintf(temp, "Iter: %'3u, |F|: %'4.1f%, time:", cc_iter, 100.0*cc_changed/g->vertices_count);
PTIP(temp);
}
cc_iter++;
}while(cc_changed);
// Saving events
if(!(flags & 2U))
{
#pragma omp parallel
{
assert(0 == thread_papi_read(pe));
}
if(flags & 1U)
print_hw_events(pe, 1);
if(exec_info)
copy_reset_hw_events(pe, &exec_info[1], 1);
}
// Number of components
if(ccs_p)
{
unsigned int ccs = 0;
#pragma omp parallel for reduction(+:ccs)
for(unsigned int v = 0; v < g->vertices_count; v++)
if(cc[v] == v)
ccs++;
printf("|CCs|: \t\t%'u\n",ccs);
*ccs_p = ccs;
}
// Finalizing
t0 += get_nano_time();
printf("Total exec. time: \t\t %'.1f (ms)\n\n",t0/1e6);
if(exec_info)
exec_info[0] = t0;
// Releasing memory
free(partitions);
partitions = NULL;
free(ttimes);
ttimes = NULL;
return cc;
}
/*
Thrifty Label Propagation Connected Components
https://blogs.qub.ac.uk/DIPSA/Thrifty-Label-Propagation-Fast-Connected-Components-for-Skewed-Degree-Graphs/
@INPROCEEDINGS{10.1109/Cluster48925.2021.00042,
author={Koohi Esfahani, Mohsen and Kilpatrick, Peter and Vandierendonck, Hans},
booktitle={2021 IEEE International Conference on Cluster Computing (CLUSTER)},
title={Thrifty Label Propagation: Fast Connected Components for Skewed-Degree Graphs},
year={2021},
volume={},
number={},
pages={226-237},
publisher={IEEE Computer Society},
doi={10.1109/Cluster48925.2021.00042}
}
flags:
bit 0: print stats
bit 1: do not reset papi
exec_info: if not NULL, will have
[0]: exec time
[1-7]: papi events
[8]: push max-degree
*/
unsigned int* cc_thrifty_400(struct par_env* pe, struct ll_400_graph* g, unsigned int flags, unsigned long* exec_info, unsigned int* ccs_p)
{
// Initial checks
assert(pe != NULL && g != NULL);
unsigned long t0 = - get_nano_time();
printf("\n\033[3;31mcc_thrifty\033[0;37m\n");
// Reset papi
if(!(flags & 2U))
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
papi_reset(pe->papi_args[tid]);
}
// Allocate memory
unsigned int* cc = numa_alloc_interleaved(sizeof(unsigned int) * g->vertices_count);
assert(cc != NULL);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
// Edge partitioning
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning(g, partitions, partitions_count);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Zero Planting: Assigning the zero label to the vertex with max degree
unsigned long mt = - get_nano_time();
unsigned int max_degree_id = 0;
{
unsigned int max_vals[2] = {0,0};
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_max_vals[2] = {0,0};
#pragma omp for nowait
for(unsigned int v = 0; v < g->vertices_count; v++)
{
cc[v] = v + 1;
unsigned int degree = g->offsets_list[v+1] - g->offsets_list[v];
if(degree > thread_max_vals[0])
{
thread_max_vals[0] = degree;
thread_max_vals[1] = v;
}
}
// Update max_vals
while(1)
{
unsigned long prev_val = *(unsigned long*)max_vals;
if((unsigned int)prev_val >= thread_max_vals[0])
break;
__sync_val_compare_and_swap((unsigned long*)max_vals, prev_val, *(unsigned long*)thread_max_vals);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
PTIP("Zero Planting:");
printf("Max. degree: \t %'u \t\t (ID: %'u)\n", max_vals[0], max_vals[1]);
// Plant the zero label
cc[max_vals[1]] = 0;
max_degree_id = max_vals[1];
}
// Initial Push: Propagate the zero label to the neighbours of the max-degree vertex
mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
#pragma omp for nowait
for(unsigned long e = g->offsets_list[max_degree_id]; e < g->offsets_list[max_degree_id + 1]; e++)
cc[g->edges_list[e]] = 0;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
PTIP("Initial Push:");
// Pull iterations with Zero Convergence:
// If a vertex has reached zero label, its label cannot be reduced => do not process it.
double frontier_density;
unsigned int next_vertices;
unsigned int cc_iter = 0;
do
{
unsigned long next_edges = 0;
next_vertices = 0;
unsigned long mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_next_vertices = 0;
unsigned long thread_next_edges = 0;
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int component = cc[v];
// Zero Convergence
if(!component)
continue;
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v + 1]; e++)
{
unsigned int neighbour = g->edges_list[e];
if(cc[neighbour] < component)
{
component = cc[neighbour];
// Zero Convergence
if(!component)
break;
}
}
if(component < cc[v])
{
cc[v] = component;
thread_next_vertices++;
thread_next_edges += g->offsets_list[v+1] - g->offsets_list[v];
}
}
}
__sync_fetch_and_add(&next_vertices, thread_next_vertices, __ATOMIC_SEQ_CST);
__sync_fetch_and_add(&next_edges, thread_next_edges, __ATOMIC_SEQ_CST);
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
frontier_density = 1.0 * (next_vertices + next_edges) / g->edges_count;
if(flags & 1U)
{
char temp[255];
if(next_vertices < 100)
sprintf(temp, "Pull %'3u, |F|: %5u, Dns: %'5.2f, time:", cc_iter, next_vertices, frontier_density);
else
sprintf(temp, "Pull %'3u, |F|: %'4.1f%, Dns: %'5.2f, time:", cc_iter, 100.0 * next_vertices/g->vertices_count, frontier_density);
PTIP(temp);
}
cc_iter++;
}while(frontier_density >= 0.01);
// Allocating memory for the shared worklists
/*
We implement worklists as a shared SPF (Sequentially Partially Filled) array between threads.
Since push iterations are sparse, we dedicate a chunk (with a size of cacheline, i.e., 16 uints) to each thread and after filling it we allocate another chunk. This way, we do not need to allocate per thread worklist.
The another point is about tiling. The initial implementation of thrifty used the
edge-tiling [Galois, DOI:10.1145/2517349.2522739] in the push iterations to allow
concurrent processing of blocks of edges of vertices with large degrees. However, in the
push direction, we do not expect to see very high degree vertices and we do not use edge-tiling in this implementation.
Although, it is possible to add edge-tiling using the current data structure of worklist. To that target, we can perform edge-tiling before submitting vertices to the worklist: we can check degree of vertex and if it can be divided, we write multiple entries in the worklist, one for each tile.
In that case, We will need 3 `unsigned long`s per each tile: (vertex_id, start_neighbour_offset, end_neighbour_offset).
The `df` and `next_df` are used as byte array frontiers to identify if a vertex has been
previously stored in the worklist. We do not use atomics for accessing `df` as it is correct to process a
vertex multiple times in a CC iteration.
*/
unsigned int waspr = 16; // worklist_allocation_size_per_request
unsigned int worklist_size = max(2 * next_vertices + waspr * pe->threads_count, 1024U * 1024);
unsigned int* worklist = numa_alloc_interleaved(sizeof(unsigned int) * worklist_size);
unsigned int worklist_length = waspr * pe->threads_count; // initial allocation per thread
unsigned int* next_worklist = numa_alloc_interleaved(sizeof(unsigned int) * worklist_size);
unsigned int next_worklist_length = waspr * pe->threads_count; // initial allocation per thread
unsigned char* df = numa_alloc_interleaved(sizeof(unsigned char) * g->vertices_count);
unsigned char* next_df = numa_alloc_interleaved(sizeof(unsigned char) * g->vertices_count);
assert(worklist != NULL && next_worklist != NULL && df != NULL && next_df != NULL);
// Pull-Frontier: One more pull iteration to store active vertices into worklist
mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
unsigned int thread_worklist_index = tid * waspr;
unsigned int thread_worklist_end = (tid + 1) * waspr;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int component = cc[v];
// Zero Convergence
if(!component)
continue;
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v + 1]; e++)
{
unsigned int neighbour = g->edges_list[e];
if(cc[neighbour] < component)
{
component = cc[neighbour];
// Zero Convergence
if(!component)
break;
}
}
// if new label has been found
if(component < cc[v])
{
cc[v] = component;
// set the frontier
if(df[v])
continue;
// add to worklist
df[v] = 1;
worklist[thread_worklist_index++] = v;
if(thread_worklist_index == thread_worklist_end)
{
// grab a new chunk
do
{
thread_worklist_index = worklist_length;
thread_worklist_end = thread_worklist_index + waspr;
}while(__sync_val_compare_and_swap(&worklist_length, thread_worklist_index, thread_worklist_end) != thread_worklist_index);
assert(worklist_length <= worklist_size);
}
}
}
}
// fill unused indecis with -1U to prevent from being processed in the next iteration
while(thread_worklist_index < thread_worklist_end)
worklist[thread_worklist_index++] = -1U;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
if(flags & 1U)
{
char temp[255];
sprintf(temp, "Pull-Frontier, |F|: %'u, time:", worklist_length);
PTIP(temp);
}
cc_iter++;
// Push iterations
unsigned int push_max_degree = 0;
do
{
mt = - get_nano_time();
next_vertices = 0;
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_next_worklist_index = tid * waspr;
unsigned int thread_next_worklist_end = (tid + 1) * waspr;
unsigned int thread_next_vertices = 0;
#pragma omp for nowait reduction(max:push_max_degree)
for(unsigned int index = 0; index < worklist_length; index++)
{
if(worklist[index] == -1U)
continue;
unsigned int v = worklist[index];
if(df[v] == 0)
continue;
df[v] = 0;
unsigned int degree = g->offsets_list[v+1] - g->offsets_list[v];
if(degree > push_max_degree)
push_max_degree = degree;
for(unsigned long e = g->offsets_list[v]; e < g->offsets_list[v+1]; e++)
{
unsigned int neighbour = g->edges_list[e];
unsigned int changed = 0;
while(1)
{
unsigned int cc_neighbour = cc[neighbour];
unsigned int cc_v = cc[v];
if(cc_neighbour <= cc_v)
break;
unsigned int prev_val = __sync_val_compare_and_swap(&cc[neighbour], cc_neighbour, cc_v);
if(prev_val == cc_neighbour)
{
changed = 1;
break;
}
}
if(!changed)
continue;
if(next_df[neighbour])
continue;
next_df[neighbour] = 1;
thread_next_vertices++;
next_worklist[thread_next_worklist_index++] = neighbour;
if(thread_next_worklist_index == thread_next_worklist_end)
{
// grab a new chunk
do
{
thread_next_worklist_index = next_worklist_length;
thread_next_worklist_end = thread_next_worklist_index + waspr;
}while(__sync_val_compare_and_swap(&next_worklist_length, thread_next_worklist_index, thread_next_worklist_end) != thread_next_worklist_index);
assert(next_worklist_length <= worklist_size);
}
}
}
__sync_fetch_and_add(&next_vertices, thread_next_vertices, __ATOMIC_SEQ_CST);
// fill unused indecis with -1U to prevent from being processed in the next iteration
while(thread_next_worklist_index < thread_next_worklist_end)
next_worklist[thread_next_worklist_index++] = -1U;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
{
char temp[255];
sprintf(temp, "Push, |F|: %5u, time:", next_vertices);
PTIP(temp);
}
cc_iter++;
// swapping
{
unsigned int* temp = worklist;
worklist = next_worklist;
next_worklist = temp;
worklist_length = next_worklist_length;
next_worklist_length = waspr * pe->threads_count;
unsigned char* temp2 = df;
df = next_df;
next_df = temp2;
}
}while(next_vertices);
if(flags & 1U)
printf("Max-degree in push iterations: \t\t%'u\n", push_max_degree);
if(exec_info)
exec_info[8] = push_max_degree;
// Saving events
if(!(flags & 2U))
{
#pragma omp parallel
{
assert(0 == thread_papi_read(pe));
}
if(flags & 1U)
print_hw_events(pe, 1);
if(exec_info)
copy_reset_hw_events(pe, &exec_info[1], 1);
}
// Counting number of components
if(ccs_p)
{
unsigned int ccs = 1;
#pragma omp parallel for reduction(+:ccs)
for(unsigned int v = 0; v < g->vertices_count; v++)
if(cc[v] == v + 1)
ccs++;
printf("|CCs|: \t\t%'u\n",ccs);
*ccs_p = ccs;
}
// Finalizing
t0 += get_nano_time();
printf("Total exec. time: \t\t %'.1f (ms)\n\n",t0/1e6);
if(exec_info)
exec_info[0] = t0;
// Releasing memory
free(partitions);
partitions = NULL;
free(ttimes);
ttimes = NULL;
numa_free(worklist, sizeof(unsigned int) * worklist_size);
worklist = NULL;
numa_free(next_worklist, sizeof(unsigned int) * worklist_size);
next_worklist = NULL;
numa_free(df, sizeof(unsigned char) * g->vertices_count);
df = NULL;
numa_free(next_df, sizeof(unsigned char) * g->vertices_count);
next_df = NULL;
return cc;
}
/*
It is the thrifty for weighted graphs
*/
unsigned int* cc_thrifty_404(struct par_env* pe, struct ll_404_graph* g, unsigned int flags, unsigned long* exec_info, unsigned int* ccs_p)
{
// Initial checks
assert(pe != NULL && g != NULL);
unsigned long t0 = - get_nano_time();
printf("\n\033[3;31mcc_thrifty_w4\033[0;37m\n");
// Reset papi
if(!(flags & 2U))
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
papi_reset(pe->papi_args[tid]);
}
// Allocate memory
unsigned int* cc = numa_alloc_interleaved(sizeof(unsigned int) * g->vertices_count);
assert(cc != NULL);
unsigned long* ttimes = calloc(sizeof(unsigned long), pe->threads_count);
assert(ttimes != NULL);
// Edge partitioning
unsigned int thread_partitions = 64;
unsigned int partitions_count = pe->threads_count * thread_partitions;
unsigned int* partitions = calloc(sizeof(unsigned int), partitions_count+1);
assert(partitions != NULL);
parallel_edge_partitioning((struct ll_400_graph*)g, partitions, partitions_count);
struct dynamic_partitioning* dp = dynamic_partitioning_initialize(pe, partitions_count);
// Zero Planting: Assigning the zero label to the vertex with max degree
unsigned long mt = - get_nano_time();
unsigned int max_degree_id = 0;
{
unsigned int max_vals[2] = {0,0};
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_max_vals[2] = {0,0};
#pragma omp for nowait
for(unsigned int v = 0; v < g->vertices_count; v++)
{
cc[v] = v + 1;
unsigned int degree = g->offsets_list[v+1] - g->offsets_list[v];
if(degree > thread_max_vals[0])
{
thread_max_vals[0] = degree;
thread_max_vals[1] = v;
}
}
// Update max_vals
while(1)
{
unsigned long prev_val = *(unsigned long*)max_vals;
if((unsigned int)prev_val >= thread_max_vals[0])
break;
__sync_val_compare_and_swap((unsigned long*)max_vals, prev_val, *(unsigned long*)thread_max_vals);
}
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
PTIP("Zero Planting:");
printf("Max. degree: \t %'u \t\t (ID: %'u)\n", max_vals[0], max_vals[1]);
// Plant the zero label
cc[max_vals[1]] = 0;
max_degree_id = max_vals[1];
}
// Initial Push: Propagate the zero label to the neighbours of the max-degree vertex
mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
#pragma omp for nowait
for(unsigned long e = 2 * g->offsets_list[max_degree_id]; e < 2 * g->offsets_list[max_degree_id + 1]; e+=2)
cc[g->edges_list[e]] = 0;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
PTIP("Initial Push:");
// Pull iterations with Zero Convergence:
// If a vertex has reached zero label, its label cannot be reduced => do not process it.
double frontier_density;
unsigned int next_vertices;
unsigned int cc_iter = 0;
do
{
unsigned long next_edges = 0;
next_vertices = 0;
unsigned long mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_next_vertices = 0;
unsigned long thread_next_edges = 0;
unsigned int partition = -1U;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int component = cc[v];
// Zero Convergence
if(!component)
continue;
for(unsigned long e = 2 * g->offsets_list[v]; e < 2 * g->offsets_list[v + 1]; e += 2)
{
unsigned int neighbour = g->edges_list[e];
if(cc[neighbour] < component)
{
component = cc[neighbour];
// Zero Convergence
if(!component)
break;
}
}
if(component < cc[v])
{
cc[v] = component;
thread_next_vertices++;
thread_next_edges += g->offsets_list[v+1] - g->offsets_list[v];
}
}
}
__sync_fetch_and_add(&next_vertices, thread_next_vertices, __ATOMIC_SEQ_CST);
__sync_fetch_and_add(&next_edges, thread_next_edges, __ATOMIC_SEQ_CST);
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
frontier_density = 1.0 * (next_vertices + next_edges) / g->edges_count;
if(flags & 1U)
{
char temp[255];
if(next_vertices < 100)
sprintf(temp, "Pull %'3u, |F|: %5u, Dns: %'5.2f, time:", cc_iter, next_vertices, frontier_density);
else
sprintf(temp, "Pull %'3u, |F|: %'4.1f%, Dns: %'5.2f, time:", cc_iter, 100.0 * next_vertices/g->vertices_count, frontier_density);
PTIP(temp);
}
cc_iter++;
}while(frontier_density >= 0.01);
// Allocating memory for the shared worklists
/*
We implement worklists as a shared SPF (Sequentially Partially Filled) array between threads.
Since push iterations are sparse, we dedicate a chunk of `waspr` size to each thread and
after filling it we allocate another chunk. This way, we do not need to alocate per vertex worklist.
The another point is about tiling. The initial implementation of thrifty had edge-tiling to allow
concurrent processing of blocks of edges of vertices with large degrees. However, in the
push direction, we do not expect to see very high degree vertices.
Although, it is possible to add edge-tiling using the current data structure of worklist. To that target, we can perform edge-tiling before submitting vertices to the worklist: we can check degree of vertex and if it can be divided, we write multiple entries in the worklist, one for each tile.
In that case, We will need 3 `unsigned long`s per each tile: (vertex_id, start_neighbour_offset, end_neighbour_offset).
The `df` and `next_df` are used as byte array frontiers to identify if a vertex has been
previously stored in the worklist. We do not use atomics for accessing df as it is correct to process a
vertex multiple times in a CC iteration.
*/
unsigned int waspr = 16; // worklist_allocation_size_per_request
unsigned int worklist_size = max(2 * next_vertices + waspr * pe->threads_count, 1024U * 1024);
unsigned int* worklist = numa_alloc_interleaved(sizeof(unsigned int) * worklist_size);
unsigned int worklist_length = waspr * pe->threads_count; // initial allocation per thread
unsigned int* next_worklist = numa_alloc_interleaved(sizeof(unsigned int) * worklist_size);
unsigned int next_worklist_length = waspr * pe->threads_count; // initial allocation per thread
unsigned char* df = numa_alloc_interleaved(sizeof(unsigned char) * g->vertices_count);
unsigned char* next_df = numa_alloc_interleaved(sizeof(unsigned char) * g->vertices_count);
assert(worklist != NULL && next_worklist != NULL && df != NULL && next_df != NULL);
// Pull-Frontier: One more pull iteration to store active vertices into worklist
mt = - get_nano_time();
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int partition = -1U;
unsigned int thread_worklist_index = tid * waspr;
unsigned int thread_worklist_end = (tid + 1) * waspr;
while(1)
{
partition = dynamic_partitioning_get_next_partition(dp, tid, partition);
if(partition == -1U)
break;
for(unsigned int v = partitions[partition]; v < partitions[partition + 1]; v++)
{
unsigned int component = cc[v];
// Zero Convergence
if(!component)
continue;
for(unsigned long e = 2 * g->offsets_list[v]; e < 2 * g->offsets_list[v + 1]; e += 2)
{
unsigned int neighbour = g->edges_list[e];
if(cc[neighbour] < component)
{
component = cc[neighbour];
// Zero Convergence
if(!component)
break;
}
}
// if new label has been found
if(component < cc[v])
{
cc[v] = component;
// set the frontier
if(df[v])
continue;
// add to worklist
df[v] = 1;
worklist[thread_worklist_index++] = v;
if(thread_worklist_index == thread_worklist_end)
{
// grab a new chunk
do
{
thread_worklist_index = worklist_length;
thread_worklist_end = thread_worklist_index + waspr;
}while(__sync_val_compare_and_swap(&worklist_length, thread_worklist_index, thread_worklist_end) != thread_worklist_index);
assert(worklist_length <= worklist_size);
}
}
}
}
// fill unused indecis with -1U to prevent from being processed in the next iteration
while(thread_worklist_index < thread_worklist_end)
worklist[thread_worklist_index++] = -1U;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
dynamic_partitioning_reset(dp);
if(flags & 1U)
{
char temp[255];
sprintf(temp, "Pull-Frontier, |F|: %'u, time:", worklist_length);
PTIP(temp);
}
cc_iter++;
// Push iterations
unsigned int push_max_degree = 0;
do
{
mt = - get_nano_time();
next_vertices = 0;
#pragma omp parallel
{
unsigned tid = omp_get_thread_num();
ttimes[tid] = - get_nano_time();
unsigned int thread_next_worklist_index = tid * waspr;
unsigned int thread_next_worklist_end = (tid + 1) * waspr;
unsigned int thread_next_vertices = 0;
#pragma omp for nowait reduction(max:push_max_degree)
for(unsigned int index = 0; index < worklist_length; index++)
{
if(worklist[index] == -1U)
continue;
unsigned int v = worklist[index];
if(df[v] == 0)
continue;
df[v] = 0;
unsigned int degree = g->offsets_list[v+1] - g->offsets_list[v];
if(degree > push_max_degree)
push_max_degree = degree;
for(unsigned long e = 2 * g->offsets_list[v]; e < 2 * g->offsets_list[v+1]; e += 2)
{
unsigned int neighbour = g->edges_list[e];
unsigned int changed = 0;
while(1)
{
unsigned int cc_neighbour = cc[neighbour];
unsigned int cc_v = cc[v];
if(cc_neighbour <= cc_v)
break;
unsigned int prev_val = __sync_val_compare_and_swap(&cc[neighbour], cc_neighbour, cc_v);
if(prev_val == cc_neighbour)
{
changed = 1;
break;
}
}
if(!changed)
continue;
if(next_df[neighbour])
continue;
next_df[neighbour] = 1;
thread_next_vertices++;
next_worklist[thread_next_worklist_index++] = neighbour;
if(thread_next_worklist_index == thread_next_worklist_end)
{
// grab a new chunk
do
{
thread_next_worklist_index = next_worklist_length;
thread_next_worklist_end = thread_next_worklist_index + waspr;
}while(__sync_val_compare_and_swap(&next_worklist_length, thread_next_worklist_index, thread_next_worklist_end) != thread_next_worklist_index);
assert(next_worklist_length <= worklist_size);
}
}
}
__sync_fetch_and_add(&next_vertices, thread_next_vertices, __ATOMIC_SEQ_CST);
// fill unused indecis with -1U to prevent from being processed in the next iteration
while(thread_next_worklist_index < thread_next_worklist_end)
next_worklist[thread_next_worklist_index++] = -1U;
ttimes[tid] += get_nano_time();
}
mt += get_nano_time();
if(flags & 1U)
{
char temp[255];
sprintf(temp, "Push, |F|: %5u, time:", next_vertices);
PTIP(temp);
}
cc_iter++;
// swapping
{
unsigned int* temp = worklist;
worklist = next_worklist;
next_worklist = temp;
worklist_length = next_worklist_length;
next_worklist_length = waspr * pe->threads_count;
unsigned char* temp2 = df;
df = next_df;
next_df = temp2;
}
}while(next_vertices);
if(flags & 1U)
printf("Max-degree in push iterations: \t\t%'u\n", push_max_degree);
if(exec_info)
exec_info[8] = push_max_degree;
// Saving events
if(!(flags & 2U))
{
#pragma omp parallel
{
assert(0 == thread_papi_read(pe));
}
if(flags & 1U)
print_hw_events(pe, 1);
if(exec_info)
copy_reset_hw_events(pe, &exec_info[1], 1);
}