-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboruvka_el.cpp
More file actions
1125 lines (1030 loc) · 39.7 KB
/
boruvka_el.cpp
File metadata and controls
1125 lines (1030 loc) · 39.7 KB
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
#include "gen.h"
#include <cmath>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <tuple>
#include <map>
#include <vector>
#include <numeric>
#include "stat.h"
#ifdef __clang__
#include "omp.h"
#else
#include <omp.h>
#endif
using namespace std;
typedef pair<weight_t, eid_t> pwe;
vid_t *comp;
weight_t taskResult;
vid_t *vertexIds;
eid_t *startEdgesIds;
struct Result {
weight_t weight;
vid_t destComp;
#ifdef USE_RESULT_VERTEX
vid_t from;
#else
eid_t edgeId;
#endif
bool operator<(const Result& o) const {
return weight < o.weight;
}
};
Result **localResult, *bestResult;
double times[kMaxIterations][kMaxThreads][40];
bool haveOuterComps[kMaxThreads];
#ifdef USE_SKIP_LOOPS
bool useSkipLoops = true;
#endif /* USE_SKIP_LOOPS */
eid_t loopsViewEdges, loopsSkipEdges; // USE_SKIP_LOOPS
#ifdef USE_SKIP_LAST_ITER
vid_t aliveComponents;
#endif /* USE_SKIP_LAST_ITER */
#ifdef USE_COMPRESS
int64_t *generatedComps;
vid_t lastUsedVid;
vid_t prevUsedVid;
#endif /* USE_COMPRESS */
#ifdef USE_ANSWER_IN_VECTOR
namespace Answer {
eid_t **data;
vid_t **pos;
void init() {
data = new eid_t*[threadsCount];
pos = new vid_t*[threadsCount];
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
data[threadId] = static_cast<eid_t*>(malloc(sizeof(eid_t) * vertexCount));
pos[threadId] = static_cast<vid_t*>(malloc(sizeof(vid_t) * 16));
pos[threadId][0] = 0;
const int pageSize = 4 * 1024;
const int elementsPerPage = pageSize / sizeof(eid_t);
for (vid_t i = vertexCount / threadsCount - 1; i >= 0; i -= elementsPerPage)
data[threadId][i] = 0;
}
}
void reset() {
for (int i = 0; i < threadsCount; ++i) pos[i][0] = 0;
}
}
#endif /* USE_ANSWER_IN_VECTOR */
#ifdef USE_REDUCTION_MESSAGES
#include "vector.h"
namespace Reduction {
vector<Result> **messages;
//Vector<Result, 1000, 2, 10> **messages;
vid_t *resultIndex[kMaxThreads];
vid_t vertexesPerThread;
void init() {
vertexesPerThread = vertexCount / threadsCount;
//assert(vertexesPerThread * threadsCount == vertexCount);
// TODO fix initializtion on threads
for (int i = 0; i < threadsCount; ++i)
resultIndex[i] = static_cast<vid_t*>(malloc(sizeof(vid_t) * vertexCount * 2));
messages = new vector<Result>*[threadsCount];
//messages = new Vector<Result, 1000, 2, 10>*[threadsCount];
for (int i = 0; i < threadsCount; ++i) {
messages[i] = new vector<Result>[threadsCount];
}
}
void reset() {
for (int i = 0; i < threadsCount; ++i)
for (int j = 0; j < vertexCount * 2; ++j)
resultIndex[i][j] = -1;
for (int i = 0; i < threadsCount; ++i)
for (int j = 0; j < threadsCount; ++j)
messages[i][j].clear();
}
}
#endif /* USE_REDUCTION_MESSAGES */
vid_t compBound[kMaxThreads][16];
pvv *vertexBound;
//Stat<vid_t> actulVers;
//Stat<eid_t> less010, more010;
Stat<vid_t> diedComps;
Stat<vid_t> catchComps;
Stat<vid_t> potentComps;
vid_t maxWindowSize;
bool useMagicGlobal = true;
template<int definedIter, int useMagic>
bool doAll() {
Eo(iterationNumber);
int updated = 0; // reduce stage
int diedComponents = 0; // merge stage
weight_t tmpTaskResult = 0.0; // merge stage
const bool usePrefetchStartEdge = (iterationNumber < 3 || double(vertexCount) / generatedComps[threadsCount - 1] > pow(10.0, iterationNumber));
#pragma omp parallel reduction(+:diedComponents) reduction(+:loopsViewEdges) reduction(+:loopsSkipEdges) reduction(+:tmpTaskResult)
{
const int threadId = omp_get_thread_num();
stickThisThreadToCore(threadId);
//
// find min
//
rdtsc.start(threadId);
auto result = (definedIter == 0 ? bestResult : localResult[threadId]);
#ifdef USE_COMPRESS
const weight_t MAX_DROPPED_WEIGHT = MAX_WEIGHT + 0.3 * iterationNumber;
#else
const weight_t MAX_DROPPED_WEIGHT = MAX_WEIGHT + 0.1;
#endif /* USE_COMPRESS */
#ifdef USE_REDUCTION_MESSAGES
for (int i = 0; i < threadsCount; ++i) {
Reduction::messages[threadId][i].clear();
Reduction::messages[threadId][i].reserve(vertexCount * 2 / threadsCount / threadsCount);
}
const int approxCompsPerThread = (lastUsedVid - prevUsedVid) / threadsCount;
#endif /* USE_REDUCTION_MESSAGES */
#ifdef USE_SKIP_LOOPS
if (iterationNumber >= USE_SKIP_LOOPS + 1 && useSkipLoops) {
for (vid_t v = vertexIds[threadId]; v < vertexIds[threadId + 1] - 1; ++v) {
const eid_t edgesStart = startEdgesIds[v];
const eid_t edgesEnd = edgesIds[v + 1];
if (edgesStart >= edgesEnd) continue;
const vid_t cv = comp[v];
eid_t newEdgesStart = edgesStart;
for (; newEdgesStart < edgesEnd; ++newEdgesStart) {
const vid_t u = edges[newEdgesStart].dest;
const vid_t cu = comp[u];
if (cu == cv) continue;
break;
}
if (iterationNumber == USE_SKIP_LOOPS + 1) {
loopsViewEdges += newEdgesStart - edgesStart + 1;
loopsSkipEdges += newEdgesStart - edgesStart;
}
if (newEdgesStart != edgesStart) startEdgesIds[v] = newEdgesStart;
}
}
times[iterationNumber][threadId][4] = rdtsc.end(threadId);
#endif /* USE_SKIP_LOOPS */
rdtsc.start(threadId);
bool outerComps = false;
vid_t minv = vertexCount * 2, maxv = 0;
if (definedIter == 0) {
#if 0
const int64_t vkf = 98; // TODO fix on SSCA2
const vid_t vfrom = vertexIds[threadId] * vkf / 100;
const vid_t vto = (threadId == threadsCount - 1 ? vertexIds[threadsCount] : vertexIds[threadId + 1] * vkf / 100);
#else
const vid_t vfrom = vertexIds[threadId];
const vid_t vto = vertexIds[threadId + 1];
#endif
//for (vid_t v = vertexIds[threadId]; v < vertexIds[threadId + 1]; ++v) {
for (vid_t v = vfrom; v < vto; ++v) {
if (usePrefetchStartEdge) {
__builtin_prefetch(edges + startEdgesIds[v + PREFETCH_START_EDGE]);
}
const eid_t edgesStart = startEdgesIds[v];
const eid_t edgesEnd = edgesIds[v + 1];
if (edgesStart >= edgesEnd) {
result[v].weight = MAX_WEIGHT + 0.1;
comp[v] = v;
} else {
const vid_t u = edges[edgesStart].dest;
result[v].weight = MAX_DROPPED_WEIGHT;
comp[v] = u;
assert(comp[v] != v);
startEdgesIds[v] = edgesStart + 1; // TODO fix start edge on initialization
}
}
} else {
const vid_t vBegin = vertexIds[threadId], vEnd = vertexIds[threadId + 1];
vid_t currCompBegin = vBegin, currCompEnd = vBegin + 1;
vid_t currCompValue = comp[vBegin];
if (useMagic) {
while (currCompBegin > 0 && currCompBegin > currCompEnd - MAGIC_BOUND && comp[currCompBegin - 1] == currCompValue) --currCompBegin;
}
for (vid_t v = vBegin; v < vEnd; ++v) {
if (usePrefetchStartEdge) {
__builtin_prefetch(edges + startEdgesIds[v + PREFETCH_START_EDGE]);
if (!useMagic) __builtin_prefetch(comp + edges[startEdgesIds[v + PREFETCH_START_EDGE / 2]].dest); // TODO
}
if (useMagic) {
if (comp[v] != currCompValue) {
currCompValue = comp[v];
currCompBegin = v;
currCompEnd = v + 1;
}
while (currCompEnd < currCompBegin + MAGIC_BOUND && currCompEnd < vertexCount && comp[currCompEnd] == currCompValue)
++currCompEnd;
}
const eid_t edgesStart = startEdgesIds[v];
const eid_t edgesEnd = edgesIds[v + 1];
if (edgesStart >= edgesEnd) continue;
const vid_t cv = comp[v];
minv = std::min(minv, cv);
maxv = std::max(maxv, cv);
//assert(prevUsedVid <= v && v < lastUsedVid);
eid_t newEdgesStart = edgesStart;
#ifdef USE_FAST_REDUCTION
if (cv < vertexIds[threadId] || cv >= vertexIds[threadId + 1]) outerComps = true;
#endif /* USE_FAST_REDUCTION */
if (useMagic) {
if (currCompBegin <= vertexBound[v].first && vertexBound[v].second < currCompEnd) {
//catchComps.add(threadId, 1);
startEdgesIds[v] = edgesEnd;
continue;
}
}
if (useMagic) {
for (; newEdgesStart < edgesEnd; ++newEdgesStart) {
const vid_t u = edges[newEdgesStart].dest;
if (currCompBegin <= u && u < currCompEnd) continue;
const vid_t cu = comp[u];
if (cu != cv) break;
}
} else {
#pragma unroll(2)
for (; newEdgesStart < edgesEnd; ++newEdgesStart) {
const vid_t u = edges[newEdgesStart].dest;
const vid_t cu = comp[u];
if (cu != cv) break;
}
}
if (newEdgesStart < edgesEnd) {
const weight_t weight = edges[newEdgesStart].weight;
#ifdef USE_REDUCTION_MESSAGES
const vid_t u = edges[newEdgesStart].dest;
const vid_t cu = comp[u];
//const int threadTo = cv / Reduction::vertexesPerThread; // TODO!!!
int threadTo = (approxCompsPerThread ? (cv - prevUsedVid) / approxCompsPerThread : 0);
while (true) {
const vid_t usedInterval = lastUsedVid - prevUsedVid;
vid_t usedFrom = usedInterval * threadTo / threadsCount;
vid_t usedTo = usedInterval * (threadTo + 1) / threadsCount;
if (prevUsedVid + usedFrom > cv) --threadTo;
else if (prevUsedVid + usedTo <= cv) ++threadTo;
else break;
}
vid_t resultId = Reduction::resultIndex[threadId][cv];
if (resultId == -1) {
Reduction::resultIndex[threadId][cv] = Reduction::messages[threadId][threadTo].size();
Reduction::messages[threadId][threadTo].push_back(Result{edges[newEdgesStart].weight, cu, v});
} else if (weight < Reduction::messages[threadId][threadTo][resultId].weight) {
Reduction::messages[threadId][threadTo][resultId] = Result{weight, cu, v};
}
#else /* USE_REDUCTION_MESSAGES */
if (weight < result[cv].weight) {
const vid_t u = edges[newEdgesStart].dest;
const vid_t cu = comp[u];
#ifdef USE_RESULT_VERTEX
result[cv] = Result{edges[newEdgesStart].weight, cu, v};
#else
result[cv] = Result{edges[newEdgesStart].weight, cu, newEdgesStart};
#endif /* USE_RESULT_VERTEX */
}
#endif /* USE_REDUCTION_MESSAGES */
}
if (newEdgesStart != edgesStart) startEdgesIds[v] = newEdgesStart;
}
haveOuterComps[threadId] = outerComps;
compBound[threadId][0] = minv;
compBound[threadId][1] = maxv;
} /* definedIter */
times[iterationNumber][threadId][0] = rdtsc.end(threadId);
#pragma omp barrier
//
// reduce min
//
rdtsc.start(threadId);
#ifdef USE_FAST_REDUCTION
bool doFastReduction = true;
for (int i = 0; i < threadsCount; ++i) if (haveOuterComps[i]) {
doFastReduction = false;
break;
}
#else
const bool doFastReduction = false;
#endif
if (definedIter == 0) {
if (!threadId) updated = 1;
} else if (doFastReduction) {
int localUpdated = 0;
const vid_t vto = vertexIds[threadId + 1];
for (vid_t v = vertexIds[threadId]; v < vto; ++v) {
if (comp[v] == v && localResult[threadId][v].weight <= MAX_WEIGHT) {
bestResult[v] = localResult[threadId][v];
//if (localResult[threadId][v].weight <= MAX_WEIGHT) {
comp[v] = localResult[threadId][v].destComp;
//isCoolEdge[localResult[threadId][v].edgeId] = true;
localUpdated = 1;
localResult[threadId][v].weight = MAX_WEIGHT + 0.1;
//}
} else {
//assert(localResult[threadId][v].weight > MAX_WEIGHT);
bestResult[v].weight = MAX_WEIGHT + 0.1;
}
}
if (localUpdated)
updated = localUpdated;
} else {
#if defined(USE_REDUCTION_SIMPLE)
#ifdef USE_COMPRESS
int localUpdated = 0;
const vid_t reduceInterval = lastUsedVid - prevUsedVid;
const vid_t reduceFrom = prevUsedVid + int64_t(reduceInterval) * (threadId + 0) / threadsCount;
const vid_t reduceTo = prevUsedVid + int64_t(reduceInterval) * (threadId + 1) / threadsCount;
for (vid_t i = reduceFrom; i < reduceTo; ++i) {
thread_vector_t haveResult = 0;
for (int j = 0; j < threadsCount; ++j)
if (localResult[j][i].weight <= MAX_WEIGHT)
haveResult |= bit<thread_vector_t>(j);
int bestThread = -1;
for (int j = 0; j < threadsCount; ++j) if (haveResult & bit<thread_vector_t>(j)) {
if (bestThread == -1 || localResult[j][i].weight < localResult[bestThread][i].weight)
bestThread = j;
}
if (bestThread == -1) {
bestResult[i].weight = MAX_WEIGHT + 0.1;
} else {
bestResult[i] = localResult[bestThread][i];
comp[i] = localResult[bestThread][i].destComp;
//isCoolEdge[localResult[bestThread][i].edgeId] = true;
localUpdated = 1;
}
//#pragma ivdep
for (int j = 0; j < threadsCount; ++j) if (haveResult & bit<thread_vector_t>(j)) {
localResult[j][i].weight = MAX_WEIGHT + 0.1;
}
}
if (localUpdated) updated = 1;
#else
#pragma omp for reduction(+:updated) nowait
for (vid_t i = 0; i < vertexCount; ++i) {
if (comp[i] == i) {
thread_vector_t haveResult = 0;
for (int j = 0; j < threadsCount; ++j)
if (localResult[j][i].weight <= MAX_WEIGHT)
haveResult |= bit<thread_vector_t>(j);
int bestThread = -1;
for (int j = 0; j < threadsCount; ++j) if (haveResult & bit<thread_vector_t>(j)) {
if (bestThread == -1 || localResult[j][i].weight < localResult[bestThread][i].weight)
bestThread = j;
}
if (bestThread == -1) {
bestResult[i].weight = MAX_WEIGHT + 0.1;
} else {
bestResult[i] = localResult[bestThread][i];
comp[i] = localResult[bestThread][i].destComp;
//isCoolEdge[localResult[bestThread][i].edgeId] = true;
updated = 1;
}
for (int j = 0; j < threadsCount; ++j) if (haveResult & bit<thread_vector_t>(j)) {
localResult[j][i].weight = MAX_WEIGHT + 0.1;
}
} else {
//bestResult[i].weight = MAX_WEIGHT + 0.1;
}
}
#endif /* USE_COMPRESS */
#elif defined(USE_REDUCTION_TREE)
int localUpdated = 0;
const int threadsPerSocket = threadsCount / 2;
const int socketId = threadId / threadsPerSocket;
const int threadsFrom = threadsPerSocket * socketId;
const int threadsTo = threadsPerSocket * (socketId + 1);
const vid_t reduceInterval = lastUsedVid - prevUsedVid;
for (int reductionIter = 0; reductionIter < 2; ++reductionIter) {
const int redThreadId = (reductionIter == 0 ? threadId : (threadId + threadsPerSocket) % threadsCount);
const vid_t reduceFrom = prevUsedVid + int64_t(reduceInterval) * (redThreadId + 0) / threadsCount;
const vid_t reduceTo = prevUsedVid + int64_t(reduceInterval) * (redThreadId + 1) / threadsCount;
for (int t = threadsFrom; t < threadsTo; ++t) if (t != threadId) {
const vid_t tReduceFrom = std::max(reduceFrom, compBound[t][0]);
const vid_t tReduceTo = std::min(reduceTo, compBound[t][1] + 1);
for (vid_t i = tReduceFrom; i < tReduceTo; ++i) {
if (localResult[t][i].weight < localResult[threadId][i].weight) {
localResult[threadId][i] = localResult[t][i];
}
localResult[t][i].weight = MAX_WEIGHT + 0.1;
}
}
}
#pragma omp barrier
{
const vid_t reduceFrom = prevUsedVid + int64_t(reduceInterval) * (threadId + 0) / threadsCount;
const vid_t reduceTo = prevUsedVid + int64_t(reduceInterval) * (threadId + 1) / threadsCount;
const int opponentThread = (threadId + threadsPerSocket) % threadsCount;
for (vid_t v = reduceFrom; v < reduceTo; ++v) {
if (localResult[threadId][v].weight < localResult[opponentThread][v].weight)
bestResult[v] = localResult[threadId][v];
else
bestResult[v] = localResult[opponentThread][v];
localResult[threadId][v].weight = localResult[opponentThread][v].weight = MAX_WEIGHT + 0.1;
if (bestResult[v].weight <= MAX_WEIGHT) {
comp[v] = bestResult[v].destComp;
localUpdated = 1;
}
}
}
if (localUpdated) updated = 1;
#elif defined(USE_REDUCTION_MESSAGES)
#pragma omp for
for (int i = prevUsedVid; i < lastUsedVid; ++i) bestResult[i].weight = MAX_WEIGHT + 0.1; // TODO optimize
//auto *result = localResult[threadId];
// copy local thread result
for (const auto& res : Reduction::messages[threadId][threadId]) {
const vid_t cv = comp[res.from];
comp[cv] = res.destComp;
bestResult[cv] = res;
}
// recieve messages from other threads
for (int from = 0; from < threadsCount; ++from) if (from != threadId) {
for (const auto& res : Reduction::messages[from][threadId]) {
const vid_t cv = comp[res.from];
if (res.weight < bestResult[cv].weight) {
comp[cv] = res.destComp;
bestResult[cv] = res;
updated = 1;
}
}
}
#else /* REDUCTION_TYPE */
#error reduction type should be set
#endif /* REDUCTION_TYPE */
}
times[iterationNumber][threadId][1] = rdtsc.end(threadId);
#pragma omp barrier
//
// merge components
//
eid_t *mergeData = Answer::data[threadId];
vid_t mergePos = Answer::pos[threadId][0];
rdtsc.start(threadId);
vid_t localGeneratedComps = 0;
if (!threadId) Eo(lastUsedVid - prevUsedVid);
#ifdef USE_REDUCTION_MESSAGES
const vid_t usedVidFrom = prevUsedVid + (lastUsedVid - prevUsedVid) * (threadId + 0) / threadsCount;
const vid_t usedVidTo = prevUsedVid + (lastUsedVid - prevUsedVid) * (threadId + 1) / threadsCount;
for (vid_t i = usedVidFrom; i < usedVidTo; ++i) {
#else
#ifdef ON_HOME
#pragma omp for nowait schedule(static)
#else
#pragma omp for nowait
#endif /* ON_HOME */
#ifdef USE_COMPRESS
for (vid_t i = prevUsedVid; i < lastUsedVid; ++i) {
#else
for (vid_t i = 0; i < vertexCount; ++i) {
#endif /* USE_COMPRESS */
#endif
if (definedIter == 0) {
const vid_t oc = comp[i];
if (oc == i) {
//bestResult[i].weight = MAX_WEIGHT + 0.1;
} else {
if (comp[oc] == i && i < oc) {
comp[i] = i;
#ifdef USE_COMPRESS
++localGeneratedComps;
#endif /* USE_COMPRESS */
} else {
#ifdef USE_RESULT_VERTEX
const eid_t edgeId = startEdgesIds[i] - 1;
//isCoolEdge[edgeId] = true;
mergeData[mergePos++] = edgeId;
#else
isCoolEdge[best.edgeId] = true;
#endif /* USE_RESULT_VERTEX */
#ifdef ON_HOME
tmpTaskResult += edges[edgeId].weight;
#endif /* ON_HOME */
}
}
} else {
Result& best = bestResult[i];
if (best.weight > MAX_WEIGHT) continue;
vid_t oc = best.destComp;
if (comp[oc] == i && i < oc) {
comp[i] = i;
#ifdef USE_COMPRESS
++localGeneratedComps;
#endif /* USE_COMPRESS */
} else {
#ifdef ON_HOME
tmpTaskResult += best.weight;
#endif /* ON_HOME */
#ifdef USE_RESULT_VERTEX
const eid_t edgeId = startEdgesIds[best.from];
//isCoolEdge[edgeId] = true;
mergeData[mergePos++] = edgeId;
#else
isCoolEdge[best.edgeId] = true;
#endif /* USE_RESULT_VERTEX */
}
bestResult[i].weight = MAX_DROPPED_WEIGHT;
}
}
#ifdef USE_SKIP_LAST_ITER
diedComponents = localGeneratedComps;
#endif /* USE_SKIP_LAST_ITER */
Answer::pos[threadId][0] = mergePos;
times[iterationNumber][threadId][2] = rdtsc.end(threadId);
#ifdef USE_COMPRESS
generatedComps[threadId] = localGeneratedComps;
#pragma omp barrier
#pragma omp single
{
for (int i = 1; i < threadsCount; ++i)
generatedComps[i] += generatedComps[i - 1];
}
#pragma omp barrier
//
// compress component data
//
{
vid_t currentVid = lastUsedVid + (threadId ? generatedComps[threadId - 1] : 0);
#ifdef USE_REDUCTION_MESSAGES
const vid_t usedVidFrom = prevUsedVid + (lastUsedVid - prevUsedVid) * (threadId + 0) / threadsCount;
const vid_t usedVidTo = prevUsedVid + (lastUsedVid - prevUsedVid) * (threadId + 1) / threadsCount;
for (vid_t i = usedVidFrom; i < usedVidTo; ++i) if (comp[i] == i && bestResult[i].weight == MAX_DROPPED_WEIGHT) {
comp[i] = currentVid++;
}
#else /* USE_REDUCTION_MESSAGES */
#pragma omp for nowait
for (vid_t i = prevUsedVid; i < lastUsedVid; ++i) if (comp[i] == i && bestResult[i].weight == MAX_DROPPED_WEIGHT)
comp[i] = currentVid++;
#endif /* USE_REDUCTION_MESSAGES */
assert(currentVid == lastUsedVid + generatedComps[threadId]);
#endif /* USE_COMPRESS */
}
}
#ifdef USE_COMPRESS
prevUsedVid = lastUsedVid;
lastUsedVid += generatedComps[threadsCount - 1];
#endif /* USE_COMPRESS */
#ifdef USE_SKIP_LAST_ITER
aliveComponents = diedComponents;
if (aliveComponents == 1) {
updated = 0;
}
#endif /* USE_SKIP_LAST_ITER */
//
// pointer jumping
//
#ifdef USE_COMPRESS
int changed = 100500;
if (definedIter == 0) {
do {
changed = 0;
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
rdtsc.start(threadId);
#pragma omp for reduction(+:changed) nowait
for (vid_t i = 0; i < prevUsedVid; ++i) {
const vid_t myComp = comp[i];
__builtin_prefetch(comp + comp[i + PREFETCH_PJ_COMP]);
if (myComp == i) continue;
const vid_t parentComp = comp[myComp];
if (myComp != parentComp) {
comp[i] = parentComp;
changed = 1;
}
}
times[iterationNumber][threadId][3] += rdtsc.end(threadId);
}
} while (changed);
} else {
while (changed) {
changed = 0;
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
rdtsc.start(threadId);
#pragma omp for reduction(+:changed) nowait
for (vid_t i = vertexCount; i < prevUsedVid; ++i) {
const vid_t myComp = comp[i];
if (myComp == i) continue;
const vid_t parentComp = comp[myComp];
if (myComp != parentComp) {
comp[i] = parentComp;
changed = 1;
}
}
times[iterationNumber][threadId][3] += rdtsc.end(threadId);
}
}
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
rdtsc.start(threadId);
#pragma omp for nowait
for (vid_t i = 0; i < vertexCount; ++i) {
const vid_t myComp = comp[i];
if (myComp == i) continue;
const vid_t parentComp = comp[myComp];
if (myComp != parentComp) {
comp[i] = parentComp;
}
}
times[iterationNumber][threadId][3] += rdtsc.end(threadId);
}
}
#else /* NOT USE_COMPRESS */
int changed = 100500;
while (changed) {
changed = 0;
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
rdtsc.start(threadId);
stickThisThreadToCore(threadId);
#pragma omp for reduction(+:changed) nowait
for (vid_t i = 0; i < vertexCount; ++i) {
const vid_t myComp = comp[i];
if (myComp == i) continue;
const vid_t parentComp = comp[myComp];
if (myComp != parentComp) {
comp[i] = parentComp;
changed = 1;
}
}
times[iterationNumber][threadId][3] += rdtsc.end(threadId);
}
}
#endif /* USE_COMPRESS */
#ifdef USE_SKIP_LOOPS
if (loopsViewEdges) {
double skipRatio = double(loopsSkipEdges) / loopsViewEdges;
E(loopsViewEdges);
E(loopsSkipEdges);
Eo(skipRatio);
loopsViewEdges = loopsSkipEdges = 0;
if (skipRatio < 0.7) useSkipLoops = false;
}
#endif
taskResult += tmpTaskResult;
++iterationNumber;
return updated;
}
void doReset() {
iterationNumber = 0;
taskResult = 0;
useMagicGlobal = maxWindowSize < vertexCount / 2;
#ifdef USE_ANSWER_IN_VECTOR
Answer::reset();
#endif /* USE_ANSWER_IN_VECTOR */
#ifdef USE_REDUCTION_MESSAGES
Reduction::reset();
#endif /* USE_REDUCTION_MESSAGES */
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
stickThisThreadToCore(threadId);
#if 0
#pragma omp for nowait
for (eid_t e = 0; e < edgesCount; ++e)
isCoolEdge[e] = false;
#elif 0
eid_t startOffset = int64_t(edgesCount) * (threadId + 0) / threadsCount;
eid_t endOffset = int64_t(edgesCount) * (threadId + 1) / threadsCount;
memset(isCoolEdge + startOffset, 0, endOffset - startOffset);
#endif
#ifdef USE_COMPRESS
/*
#pragma omp for nowait
for (vid_t i = 0; i < vertexCount; ++i) {
comp[i] = i;
//bestResult[i].weight = 0;
}
*/
#pragma omp for nowait
for (vid_t i = vertexCount; i < lastUsedVid; ++i) { // TODO fix len
comp[i] = i;
bestResult[i].weight = 0; // TODO remove?
}
#else
#pragma omp for nowait
for (vid_t i = 0; i < vertexCount; ++i) {
comp[i] = i;
//bestResult[i].weight = 0;
}
#endif /* USE_COMPRESS */
// TODO fix
#ifdef USE_COMPRESS
#pragma omp for nowait
for (vid_t i = vertexCount; i < lastUsedVid; ++i) // TODO decrase to one?
localResult[threadId][i] = Result{MAX_WEIGHT + 0.1, 0, 0};
#else
#pragma omp for nowait
for (vid_t i = vertexCount; i < vertexCount; ++i)
localResult[threadId][i] = Result{MAX_WEIGHT + 0.1, 0, 0};
#endif /* USE_COMPRESS */
#if 0 // TODO copy and skip loops
for (vid_t i = vertexIds[threadId]; i < vertexIds[threadId + 1]; ++i) {
const eid_t startEdge = edgesIds[i];
const eid_t endEdge = edgesIds[i + 1];
startEdgesIds[i] = edgesIds[i];
while (startEdgesIds[i] < endEdge && edges[startEdgesIds[i]].dest == i) ++startEdgesIds[i];
}
#else
memcpy(startEdgesIds + vertexIds[threadId], edgesIds + vertexIds[threadId], sizeof(eid_t) * (vertexIds[threadId + 1] - vertexIds[threadId]));
#endif
}
#ifdef USE_COMPRESS
lastUsedVid = vertexCount;
prevUsedVid = 0;
#endif /* USE_COMPRESS */
#ifdef USE_SKIP_LOOPS
useSkipLoops = true;
#endif
}
void doPrepare() {
#pragma omp parallel for
for (vid_t v = 0; v < vertexCount; ++v)
sort(edges + edgesIds[v], edges + edgesIds[v + 1], [&](const Edge& a, const Edge& b) -> bool {
vid_t va = a.dest;
eid_t da = edgesIds[va + 1] - edgesIds[va];
vid_t vb = b.dest;
eid_t db = edgesIds[vb + 1] - edgesIds[vb];
//return a.weight < b.weight;
return da < db;
});
doReorder();
#ifdef USE_SKIP_LAST_ITER
aliveComponents = vertexCount;
#endif /* USE_SKIP_LAST_ITER */
#ifdef USE_ANSWER_IN_VECTOR
Answer::init();
#endif /* USE_ANSWER_IN_VECTOR */
#ifdef USE_REDUCTION_MESSAGES
Reduction::init();
#endif /* USE_REDUCTION_MESSAGES */
vertexIds = new vid_t[threadsCount + 1]; // TODO threads & align
vertexIds[0] = 0;
vertexBound = static_cast<pvv*>(malloc(sizeof(pvv) * vertexCount));
isCoolEdge = static_cast<bool*>(malloc(edgesCount));// new bool[edgesCount](); // TODO NUMA
#pragma omp parallel
{
stickThisThreadToCore(omp_get_thread_num());
#pragma omp for
for (eid_t i = 0; i < edgesCount; ++i) isCoolEdge[i] = false;
}
//memset(isCoolEdge, 0, edgesCount);
#ifdef USE_COMPRESS
// bestResult = new Result[vertexCount * 2]; ALLOC
bestResult = static_cast<Result*>(malloc(sizeof(Result) * vertexCount * 2));
// comp = new vid_t[vertexCount * 2]; ALLOC
comp = static_cast<vid_t*>(malloc(sizeof(vid_t) * vertexCount * 2));
#pragma omp parallel
{
stickThisThreadToCore(omp_get_thread_num());
#pragma omp for
for (vid_t i = 0; i < vertexCount; ++i) {
comp[i] = i;
//bestResult[i].weight = 0;
}
}
#pragma omp parallel for
for (vid_t i = vertexCount; i < vertexCount * 2; ++i) {
comp[i] = i;
//bestResult[i].weight = 0;
}
#else
bestResult = new Result[vertexCount];
comp = new vid_t[vertexCount];
for (vid_t i = 0; i < vertexCount; ++i)
comp[i] = i;
#endif /* USE_COMPRESS */
// startEdgesIds = new eid_t[vertexCount]; ALLOC
startEdgesIds = static_cast<eid_t*>(malloc(sizeof(eid_t) * (vertexCount + PREFETCH_START_EDGE)));
for (vid_t i = vertexCount; i < vertexCount + PREFETCH_START_EDGE; ++i) { // TODO allocate on last thread
startEdgesIds[i] = edgesCount-1;
}
#ifdef USE_COMPRESS
//generatedComps = new vid_t[threadsCount];
generatedComps = static_cast<int64_t*>(malloc(sizeof(int64_t) * threadsCount));
lastUsedVid = vertexCount;
prevUsedVid = 0;
#endif /* USE_COMPRESS */
#pragma omp parallel
{
const int threadId = omp_get_thread_num();
stickThisThreadToCore(threadId);
const int curThreadsCount = omp_get_num_threads();
if (!threadId) {
threadsCount = curThreadsCount;
localResult = new Result*[threadsCount];
}
#pragma omp barrier
#if 0
const int64_t paramA = 10;
const int64_t paramB = 1;
const int64_t sumAll = paramA * vertexCount + paramB * edgesCount;
const int64_t expectedSum = sumAll * (threadId + 1) / threadsCount;
//eid_t degreeEnd = int64_t(edgesCount) * (threadId + 1) / threadsCount;
//eid_t degreeSum = 0;
int64_t currentSum = 0;
for (vid_t i = 0; i < vertexCount; ++i) {
const eid_t diff = edgesIds[i + 1] - edgesIds[i];
/*
degreeSum += diff;
if (degreeSum >= degreeEnd) {
vertexIds[threadId + 1] = i + 1;
break;
}
*/
currentSum += paramA + paramB * diff;
if (currentSum >= expectedSum) {
vertexIds[threadId + 1] = i + 1;
break;
}
}
#pragma omp critical
{
E(threadId); Eo(vertexIds[threadId + 1]);
}
#elif 1
vertexIds[threadId + 1] = int64_t(vertexCount) * (threadId + 1) / threadsCount;
#else
const eid_t degreeEnd = int64_t(edgesCount) * (threadId + 1) / threadsCount;
eid_t degreeSum = 0;
vertexIds[threadId + 1] = -1;
for (vid_t i = 0; i < vertexCount; ++i) {
eid_t diff = edgesIds[i + 1] - edgesIds[i];
degreeSum += diff;
if (degreeSum >= degreeEnd && componentEnd[i]) {
vertexIds[threadId + 1] = i + 1;
break;
}
}
assert(vertexIds[threadId + 1] > 0);
#endif /* vertexes distribution */
#pragma omp barrier
#ifdef USE_COMPRESS
localResult[threadId] = new Result[vertexCount * 2];
for (vid_t i = 0; i < vertexCount * 2; ++i)
localResult[threadId][i] = Result{MAX_WEIGHT + 0.1, 0, 0};
#else
localResult[threadId] = new Result[vertexCount];
for (vid_t i = 0; i < vertexCount; ++i)
localResult[threadId][i] = Result{MAX_WEIGHT + 0.1, 0, 0};
#endif /* USE_COMPRESS */
for (vid_t i = vertexIds[threadId]; i < vertexIds[threadId + 1]; ++i) {
const eid_t startEdge = edgesIds[i];
const eid_t endEdge = edgesIds[i + 1];
vid_t minv = vertexCount, maxv = 0;
for (eid_t e = startEdge; e < endEdge; ++e) {
minv = std::min(minv, edges[e].dest);
maxv = std::max(maxv, edges[e].dest);
}
vertexBound[i] = pvv(minv, maxv);
maxWindowSize = std::max(maxWindowSize, maxv - minv);
sort(edges + startEdge, edges + endEdge, EdgeWeightCmp());
startEdgesIds[i] = edgesIds[i];
while (startEdgesIds[i] < edgesIds[i + 1] && edges[startEdgesIds[i]].dest == i) ++startEdgesIds[i];
}
}
}
void warmup() {
const int64_t iterationCount = 1e9;
#pragma omp parallel
{
volatile int64_t fpre = 0, fcur = 1;
stickThisThreadToCore(omp_get_thread_num());
for (int64_t i = iterationCount; i > 0; --i) {
int64_t fnext = fpre + fcur;
fpre = fcur;
fcur = fnext;
}
}
}
#ifndef ON_DISLAB
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input\n", argv[0]);
return 1;
}
readAll(argv[1]);
warmup();
fprintf(stderr, "Done\n");
int64_t prepareTime = -currentNanoTime();
doPrepare();
prepareTime += currentNanoTime();
int64_t calcTime;
for (int i = 0; i < 1; ++i) {
memset(times, 0, sizeof(times));
calcTime = -currentNanoTime();
doReset();
if (doAll<0, 0>()) {