forked from Bader-Research/triangle-counting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.c
896 lines (731 loc) · 21.4 KB
/
bfs.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
#include "types.h"
#include "bfs.h"
#ifdef PARALLEL
#include "omp.h"
#endif
void bfs(const GRAPH_TYPE *graph, const UINT_t startVertex, UINT_t* level) {
bool *visited = (bool *)calloc(graph->numVertices, sizeof(bool));
assert_malloc(visited);
Queue *queue = createQueue(graph->numVertices);
visited[startVertex] = true;
enqueue(queue, startVertex);
level[startVertex] = 0;
while (!isEmpty(queue)) {
UINT_t v = dequeue(queue);
for (UINT_t i = graph->rowPtr[v]; i < graph->rowPtr[v + 1]; i++) {
UINT_t w = graph->colInd[i];
if (!visited[w]) {
visited[w] = true;
enqueue(queue, w);
level[w] = level[v] + 1;
}
}
}
free(visited);
free_queue(queue);
}
void bfs_visited(const GRAPH_TYPE *graph, const UINT_t startVertex, UINT_t* level, bool *visited) {
Queue *queue = createQueue(graph->numVertices);
visited[startVertex] = true;
enqueue(queue, startVertex);
level[startVertex] = 0;
while (!isEmpty(queue)) {
UINT_t v = dequeue(queue);
for (UINT_t i = graph->rowPtr[v]; i < graph->rowPtr[v + 1]; i++) {
UINT_t w = graph->colInd[i];
if (!visited[w]) {
visited[w] = true;
enqueue(queue, w);
level[w] = level[v] + 1;
}
}
}
free_queue(queue);
}
void bfs_visited_P(const GRAPH_TYPE *graph, const UINT_t startVertex, UINT_t* level, bool *visited) {
omp_lock_t qlock;
Queue *queue = createQueue(graph->numVertices);
visited[startVertex] = true;
enqueue(queue, startVertex);
level[startVertex] = 0;
omp_init_lock(&qlock);
#pragma omp parallel
{
UINT_t v;
while (!isEmpty(queue)) {
omp_set_lock(&qlock);
if (!isEmpty(queue)) {
v = dequeue(queue);
omp_unset_lock(&qlock);
}
else {
omp_unset_lock(&qlock);
continue;
}
omp_set_lock(&qlock);
for (UINT_t i = graph->rowPtr[v]; i < graph->rowPtr[v + 1]; i++) {
UINT_t w = graph->colInd[i];
if (!visited[w]) {
visited[w] = true;
enqueue(queue, w);
level[w] = level[v] + 1;
}
}
omp_unset_lock(&qlock);
}
}
free_queue(queue);
omp_destroy_lock(&qlock);
}
void bfs_visited_P_DEBUG(const GRAPH_TYPE *graph, const UINT_t startVertex, UINT_t* level, bool *visited) {
omp_lock_t qlock;
UINT_t qsize;
UINT_t totalenq;
UINT_t totaldeq;
printf("BFS_Visited_P (%d):\n",startVertex);
int *vstate = (int *)calloc(graph->numVertices, sizeof(int));
assert_malloc(vstate);
Queue *queue = createQueue(graph->numVertices);
visited[startVertex] = true;
enqueue(queue, startVertex);
if (vstate[startVertex] != 0) printf("T%2d: ERROR: ENQ %3d\n",omp_get_thread_num(), startVertex);
vstate[startVertex] = 1;
qsize = 1;
totalenq = 1;
level[startVertex] = 0;
omp_init_lock(&qlock);
#pragma omp parallel
{
UINT_t v;
while ( (!isEmpty(queue)) || (qsize>0)) {
omp_set_lock(&qlock);
if (!isEmpty(queue)) {
v = dequeue(queue);
printf("T%2d: DEQ %d\n",omp_get_thread_num(), v);
if (vstate[v] != 1) printf("T%2d: ERROR: DEQ %3d\n",omp_get_thread_num(), v);
vstate[v] = 2;
qsize--;
totaldeq++;
omp_unset_lock(&qlock);
}
else {
omp_unset_lock(&qlock);
continue;
}
omp_set_lock(&qlock);
for (UINT_t i = graph->rowPtr[v]; i < graph->rowPtr[v + 1]; i++) {
UINT_t w = graph->colInd[i];
if (!visited[w]) {
/* omp_set_lock(&qlock); */
if (!visited[w]) {
visited[w] = true;
printf("T%2d: ENQ %d\n",omp_get_thread_num(), w);
if (vstate[w] != 0) printf("T%2d: ERROR: ENQ %3d\n",omp_get_thread_num(), w);
vstate[w] = 1;
enqueue(queue, w);
qsize++;
totalenq++;
level[w] = level[v] + 1;
printf("T%2d: Level[%3d] <-- %3d treeedge: (%3d, %3d) \n",omp_get_thread_num(), w, level[w],v, w);
}
/* omp_unset_lock(&qlock); */
}
}
omp_unset_lock(&qlock);
}
fflush(stdout);
#pragma omp barrier
}
printf("q empty: %d qsize: %d total enq: %d total deq: %d n: %d\n",isEmpty(queue)?1:0, qsize, totalenq, totaldeq, graph->numVertices);
/* CHECK */
UINT_t *checkLevel = (UINT_t *)malloc(graph->numVertices * sizeof(UINT_t));
assert_malloc(checkLevel);
bool *checkVisited = (bool *)malloc(graph->numVertices * sizeof(bool));
assert_malloc(checkVisited);
for (int i=0 ; i<graph->numVertices; i++) checkLevel[i] = 0;
for (int i=0 ; i<graph->numVertices; i++) checkVisited[i] = false;
bfs_visited(graph, startVertex, checkLevel, checkVisited);
for (int i=0 ; i<graph->numVertices; i++) {
if (checkVisited[i]) {
if (level[i] != checkLevel[i])
printf("ERROR: Level[%3d]: %3d (not %3d)\n",i, level[i], checkLevel[i]);
}
}
free(checkVisited);
free(checkLevel);
/********/
free(vstate);
free_queue(queue);
omp_destroy_lock(&qlock);
}
void bfs_mark_horizontal_edges(const GRAPH_TYPE *graph, const UINT_t startVertex, UINT_t* restrict level, Queue* queue, bool* visited, bool* horiz) {
const UINT_t *restrict Ap = graph->rowPtr;
const UINT_t *restrict Ai = graph->colInd;
visited[startVertex] = true;
enqueue(queue, startVertex);
level[startVertex] = 1;
while (!isEmpty(queue)) {
UINT_t v = dequeue(queue);
for (UINT_t i = Ap[v]; i < Ap[v + 1]; i++) {
UINT_t w = Ai[i];
if (!visited[w]) {
horiz[i] = false;
visited[w] = true;
enqueue(queue, w);
level[w] = level[v] + 1;
}
else {
horiz[i] = (level[w] == 0) || (level[w] == level[v]);
}
}
}
}
// Beamer
// function top-down-step(frontier, next, parents)
// for v ∈ frontier do
// for n ∈ neighbors[v] do
// if parents[n] = -1 then
// parents[n] ← v
// next ← next ∪ {n}
// end if
// end for
// end for
void top_down_step(UINT_t* frontier, UINT_t* next, bool* visited, const GRAPH_TYPE* graph, UINT_t frontier_size, UINT_t *level) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t next_size = 0; // track number of elements in the next array
for (UINT_t i = 0; i < frontier_size; i++) {
UINT_t v = frontier[i];
for (UINT_t j = Ap[v]; j < Ap[v + 1]; j++) {
UINT_t w = Ai[j];
if (!visited[w]) {
visited[w] = true;
level[w] = level[v] + 1;
next[next_size++] = w;
}
}
}
}
//
// function bottom-up-step(frontier, next, parents)
// for v ∈ vertices do
// if parents[v] = -1 then
// for n ∈ neighbors[v] do
// if n ∈ frontier then
// parents[v] ← n
// next ← next ∪ {v}
// break
// end if
// end for
// end if
// end for
void bottom_up_step(UINT_t* frontier, UINT_t* next, bool *visited, const GRAPH_TYPE* graph, UINT_t frontier_size, UINT_t *level) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t next_size = 0;
for (UINT_t i = 0; i < frontier_size; i++) {
UINT_t v = frontier[i];
if (!visited[v]) {
for (UINT_t j = Ap[v]; j < Ap[v + 1]; j++) {
UINT_t w = Ai[j];
if (w == frontier[j]) {
visited[v] = true;
level[v] = level[w] + 1;
next[next_size++] = v;
break;
}
}
}
}
}
#define ALPHA 14.0
#define BETA 24.0
//function breadth-first-search(graph, source)
// frontier ← {source}
// next ← {}
// parents ← [-1,-1,...-1]
// while frontier ̸= {} do
// top-down-step(frontier, next, parents)
// frontier ← next
// next ← {}
// end while
// return tree
// frontier - vertices that considered for exploration
void bfs_hybrid_visited(const GRAPH_TYPE* graph, const UINT_t startVertex, UINT_t* level, bool* visited) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t* frontier = (UINT_t*)malloc(n * sizeof(UINT_t));
assert_malloc(frontier);
UINT_t* next = (UINT_t*)malloc(n * sizeof(UINT_t));
assert_malloc(next);
UINT_t frontierSize = 0, nextSize = 0;
// Set the initial frontier to vertex startVertex
frontier[frontierSize++] = startVertex;
level[startVertex] = 0;
while (frontierSize > 0) {
UINT_t numEdgesFrontier = 0; // Number of edges in the frontier
for (UINT_t i = 0; i < frontierSize; i++) {
UINT_t v = frontier[i];
numEdgesFrontier += Ap[v + 1] - Ap[v];
}
UINT_t numEdgesUnexplored = 0; // Number of edges to check from unexplored vertices
for (UINT_t v = 0; v < n; v++) {
if (!visited[v]) {
numEdgesUnexplored += Ap[v + 1] - Ap[v];
}
}
if (numEdgesFrontier > numEdgesUnexplored / ALPHA) {
// Use bottom-up approach
bottom_up_step(frontier, next, visited, graph, frontierSize, level);
#if 0
printf("USING: bottom_up_step\n");
#endif
} else {
// Use top-down approach
top_down_step(frontier, next, visited, graph, frontierSize, level);
#if 0
printf("USING: top_down_step\n");
#endif
}
// Swap frontier and next arrays for the next iteration
UINT_t* temp = frontier;
frontier = next;
next = temp;
frontierSize = nextSize;
nextSize = 0; // Reset nextSize for the next iteration
if (frontierSize <= n / BETA) {
top_down_step(frontier, next, visited, graph, frontierSize, level);
#if 0
printf("USING: top_down_step\n");
#endif
}
}
free(frontier);
free(next);
}
#ifdef PARALLEL
void top_down_step_P(UINT_t* frontier, UINT_t* next, bool* visited, const GRAPH_TYPE* graph, UINT_t frontier_size, UINT_t *level) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t next_size = 0; // track number of elements in the next array
#pragma omp for schedule(dynamic)
for (UINT_t i = 0; i < frontier_size; i++) {
UINT_t v = frontier[i];
for (UINT_t j = Ap[v]; j < Ap[v + 1]; j++) {
UINT_t w = Ai[j];
if (!visited[w]) {
visited[w] = true;
level[w] = level[v] + 1;
next[next_size++] = w;
}
}
}
}
void bottom_up_step_P(UINT_t* frontier, UINT_t* next, bool *visited, const GRAPH_TYPE* graph, UINT_t frontier_size, UINT_t *level) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t next_size = 0;
#pragma omp for schedule(dynamic)
for (UINT_t i = 0; i < frontier_size; i++) {
UINT_t v = frontier[i];
if (!visited[v]) {
for (UINT_t j = Ap[v]; j < Ap[v + 1]; j++) {
UINT_t w = Ai[j];
if (w == frontier[j]) {
visited[v] = true;
level[v] = level[w] + 1;
next[next_size++] = v;
break;
}
}
}
}
}
void bfs_hybrid_visited_P(const GRAPH_TYPE* graph, const UINT_t startVertex, UINT_t* level, bool* visited) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t* frontier = (UINT_t*)malloc(n * sizeof(UINT_t));
assert_malloc(frontier);
UINT_t* next = (UINT_t*)malloc(n * sizeof(UINT_t));
assert_malloc(next);
UINT_t frontierSize = 0, nextSize = 0;
// Set the initial frontier to vertex startVertex
frontier[frontierSize++] = startVertex;
level[startVertex] = 0;
UINT_t numEdgesFrontier; // Number of edges in the frontier
UINT_t numEdgesUnexplored; // Number of edges to check from unexplored vertices
#pragma omp parallel
{
while (frontierSize > 0) {
numEdgesFrontier = 0; // Number of edges in the frontier
numEdgesUnexplored = 0; // Number of edges to check from unexplored vertices
#pragma omp for reduction (+:numEdgesFrontier)
for (UINT_t i = 0; i < frontierSize; i++) {
UINT_t v = frontier[i];
numEdgesFrontier += Ap[v + 1] - Ap[v];
}
#pragma omp for reduction (+:numEdgesUnexplored)
for (UINT_t v = 0; v < n; v++) {
if (!visited[v]) {
numEdgesUnexplored += Ap[v + 1] - Ap[v];
}
}
if (numEdgesFrontier > numEdgesUnexplored / ALPHA) {
bottom_up_step_P(frontier, next, visited, graph, frontierSize, level);
} else {
top_down_step_P(frontier, next, visited, graph, frontierSize, level);
}
// Swap frontier and next arrays for the next iteration
#pragma omp single
{
UINT_t* temp = frontier;
frontier = next;
next = temp;
frontierSize = nextSize;
nextSize = 0; // Reset nextSize for the next iteration
}
if (frontierSize <= n / BETA) {
top_down_step_P(frontier, next, visited, graph, frontierSize, level);
}
}
}
free(frontier);
free(next);
}
void bfs_chatgpt_P(const GRAPH_TYPE* graph, const UINT_t startVertex, UINT_t* level, bool* visited) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
visited[startVertex] = true;
UINT_t *current_level_vertices = (UINT_t *)malloc(n * sizeof(UINT_t));
assert_malloc(current_level_vertices);
UINT_t current_level = 0;
UINT_t current_level_size = 1;
UINT_t curStart = 0;
UINT_t curEnd = 1;
#pragma omp parallel
{
#pragma omp single
{
current_level_vertices[0] = startVertex;
}
while (curEnd > curStart) {
#pragma omp for
for (UINT_t i = curStart; i < curEnd; i++) {
UINT_t v = current_level_vertices[i];
UINT_t s = Ap[v];
UINT_t e = Ap[v + 1];
for (UINT_t j = s; j < e; j++) {
UINT_t w = Ai[j];
if (!visited[w]) {
#pragma omp critical
{
visited[w] = true;
}
#pragma omp critical
{
level[w] = current_level + 1;
}
#pragma omp critical
{
if (current_level_size >= n)
printf("ERROR: current_level_size: %d n: %d\n",current_level_size,n);
current_level_vertices[current_level_size++] = w;
}
}
}
}
#pragma omp single
{
current_level++;
curStart = curEnd;
curEnd = current_level_size;
}
}
}
free(current_level_vertices);
}
void bfs_locks_P(const GRAPH_TYPE* graph, const UINT_t startVertex, UINT_t* level, bool* visited) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
visited[startVertex] = true;
UINT_t *current_level_vertices = (UINT_t *)malloc(n * sizeof(UINT_t));
assert_malloc(current_level_vertices);
UINT_t current_level = 0;
UINT_t current_level_size = 1;
UINT_t curStart = 0;
UINT_t curEnd = 1;
omp_lock_t *vlocks = (omp_lock_t *)malloc(n * sizeof(omp_lock_t));
assert_malloc(vlocks);
for (int i=0 ; i<n ; i++)
omp_init_lock(&vlocks[i]);
#pragma omp parallel
{
#pragma omp single
{
current_level_vertices[0] = startVertex;
}
while (curEnd > curStart) {
#pragma omp for
for (UINT_t i = curStart; i < curEnd; i++) {
UINT_t v = current_level_vertices[i];
UINT_t s = Ap[v];
UINT_t e = Ap[v + 1];
for (UINT_t j = s; j < e; j++) {
UINT_t w = Ai[j];
omp_set_lock(&vlocks[w]);
if (!visited[w]) {
visited[w] = true;
level[w] = current_level + 1;
#pragma omp critical
{
if (current_level_size >= n)
printf("ERROR: current_level_size: %d n: %d\n",current_level_size,n);
current_level_vertices[current_level_size++] = w;
}
}
omp_unset_lock(&vlocks[w]);
}
}
#pragma omp single
{
current_level++;
curStart = curEnd;
curEnd = current_level_size;
}
}
}
for (int i=0 ; i<n ; i++)
omp_destroy_lock(&vlocks[i]);
free(vlocks);
free(current_level_vertices);
}
#if 1
/* BEAMER GAP BENCHMARK */
typedef struct {
UINT_t *shared;
UINT_t shared_in;
UINT_t shared_out_start;
UINT_t shared_out_end;
} SlidingQueue;
SlidingQueue *SQ_init(UINT_t shared_size);
void SQ_destroy(SlidingQueue *queue);
void SQ_push_back(SlidingQueue *queue, UINT_t to_add);
bool SQ_empty(const SlidingQueue *queue);
void SQ_reset(SlidingQueue *queue);
void SQ_slide_window(SlidingQueue *queue);
UINT_t SQ_begin(const SlidingQueue *queue);
UINT_t SQ_end(const SlidingQueue *queue);
UINT_t SQ_size(const SlidingQueue *queue);
SlidingQueue *SQ_init(UINT_t shared_size) {
SlidingQueue *queue = (SlidingQueue *)malloc(sizeof(SlidingQueue));
assert_malloc(queue);
queue->shared = (UINT_t*)malloc(shared_size*sizeof(UINT_t));
assert_malloc(queue->shared);
SQ_reset(queue);
return queue;
}
void SQ_destroy(SlidingQueue *queue) {
free(queue->shared);
free(queue);
return;
}
void SQ_push_back(SlidingQueue *queue, UINT_t to_add) {
queue->shared[queue->shared_in++] = to_add;
return;
}
bool SQ_empty(const SlidingQueue *queue) {
return queue->shared_out_start == queue->shared_out_end;
}
void SQ_reset(SlidingQueue *queue) {
queue->shared_out_start = 0;
queue->shared_out_end = 0;
queue->shared_in = 0;
return;
}
void SQ_slide_window(SlidingQueue *queue) {
queue->shared_out_start = queue->shared_out_end;
queue->shared_out_end = queue->shared_in;
return;
}
UINT_t SQ_begin(const SlidingQueue *queue) {
return queue->shared[queue->shared_out_start];
}
UINT_t SQ_end(const SlidingQueue *queue) {
return queue->shared[queue->shared_out_end];
}
UINT_t SQ_size(const SlidingQueue *queue) {
return SQ_end(queue) - SQ_begin(queue);
}
UINT_t BUStep(const GRAPH_TYPE *graph, INT_t *parent, bool *visited, bool *front, bool *next) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t awake_count = 0;
for (UINT_t i=0 ; i<n ; i++)
next[i] = false;
#pragma omp parallel for reduction(+ : awake_count) schedule(dynamic, 1024)
for (UINT_t u=0; u < n; u++) {
if (parent[u] < 0) {
UINT_t s = Ap[u];
UINT_t e = Ap[u+1];
for (UINT_t j=s ; j<e ; j++) {
UINT_t v = Ai[j];
if (front[v]) {
parent[u] = v;
visited[u] = true;
awake_count++;
next[u] = true;
break;
}
}
}
}
return awake_count;
}
UINT_t TDStep(const GRAPH_TYPE *graph, INT_t *parent, bool *visited, SlidingQueue *queue) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t scout_count = 0;
#pragma omp parallel
{
/* QueueBuffer<UINT_t> lqueue(queue); */
queue = SQ_init(n);
#pragma omp for reduction(+ : scout_count) nowait
for (UINT_t i = queue->shared_out_start; i < queue->shared_out_end; i++) {
UINT_t u = queue->shared[i];
UINT_t s = Ap[u];
UINT_t e = Ap[u+1];
for (UINT_t j=s ; j<e ; j++) {
UINT_t v = Ai[j];
UINT_t curr_val = parent[v];
if (curr_val < 0) {
#if 1
//#pragma omp atomic compare capture
#pragma omp critical
{
if (parent[v] == curr_val) {
parent[v] = u;
visited[v] = true;
SQ_push_back(queue, v);
scout_count += -curr_val;
}
}
#else
if (compare_and_swap(parent[v], curr_val, u)) {
SQ_push_back(queue, v);
scout_count += -curr_val;
}
#endif
}
}
}
SQ_destroy(queue);
}
return scout_count;
}
void QueueToBitmap(const SlidingQueue *queue, bool *bm) {
#pragma omp parallel for
for (UINT_t i = queue->shared_out_start; i < queue->shared_out_end; i++) {
UINT_t u = queue->shared[i];
bm[u] = true;
}
}
void BitmapToQueue(const GRAPH_TYPE *graph, const bool *bm, SlidingQueue *queue) {
const UINT_t n = graph->numVertices;
#pragma omp parallel
{
#pragma omp for nowait
for (UINT_t i=0; i < n; i++)
if (bm[i])
SQ_push_back(queue, i);
SQ_destroy(queue);
}
SQ_slide_window(queue);
}
#define BEAMERGAP_ALPHA 15
#define BEAMERGAP_BETA 18
void bfs_beamerGAP_P(const GRAPH_TYPE* graph, const UINT_t startVertex, UINT_t* level, bool* visited) {
const UINT_t n = graph->numVertices;
const UINT_t m = graph->numEdges;
UINT_t* Ap = graph->rowPtr;
UINT_t* Ai = graph->colInd;
UINT_t alpha = BEAMERGAP_ALPHA;
UINT_t beta = BEAMERGAP_BETA;
UINT_t source = startVertex;
SlidingQueue *queue = SQ_init(n);
SQ_push_back(queue, source);
SQ_slide_window(queue);
INT_t *parent = (INT_t *)malloc(n * sizeof(INT_t));
assert_malloc(parent);
printf("here 1 startVertex: %d\n",startVertex);
#pragma omp parallel for
for (UINT_t i=0 ; i<n ; i++) {
UINT_t d = Ap[i+1] = Ap[i];
parent[i] = (d != 0 ? -d : -1);
}
parent[startVertex] = startVertex;
printf("here 2\n");
bool *curr = (bool *)malloc(n * sizeof(bool));
assert_malloc(curr);
for (UINT_t i=0 ; i<n ; i++)
curr[i] = false;
bool *front = (bool *)malloc(n * sizeof(bool));
assert_malloc(front);
for (UINT_t i=0 ; i<n ; i++)
front[i] = false;
UINT_t edges_to_check = m;
UINT_t scout_count = Ap[source+1] - Ap[source];
while (!SQ_empty(queue)) {
printf("here 3\n");
if (scout_count > edges_to_check / alpha) {
printf("here 3a\n");
UINT_t awake_count, old_awake_count;
QueueToBitmap(queue, front);
awake_count = SQ_size(queue);
SQ_slide_window(queue);
do {
old_awake_count = awake_count;
awake_count = BUStep(graph, parent, visited, front, curr);
bool *temp = front;
front = curr;
curr = temp;
} while ((awake_count >= old_awake_count) ||
(awake_count > n / beta));
BitmapToQueue(graph, front, queue);
scout_count = 1;
} else {
printf("here 3b\n");
edges_to_check -= scout_count;
scout_count = TDStep(graph, parent, visited, queue);
SQ_slide_window(queue);
}
}
printf("here x\n");
SQ_destroy(queue);
free(parent);
free(front);
free(curr);
printf("here y\n");
return;
}
#endif
#endif