-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmean_trimming.cl
More file actions
973 lines (679 loc) · 34.7 KB
/
mean_trimming.cl
File metadata and controls
973 lines (679 loc) · 34.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
R"(
/*
N trimming rounds can be performed with the following:
Trimming round 1: clear number of edges per bucket one, step one, clear number of edges per bucket two, step two
Trimming round 2: clear number of edges per bucket one, step three
Trimming round 3: clear number of edges per bucket two, step four
Trimming round 4: clear number of edges per bucket one, step five
Trimming round 5: clear number of edges per bucket two, step five
...
Trimming round n - 1: clear number of edges per bucket one, step five
Trimming round n: clear number of edges per bucket two, step five, clear first remaining edge, step six
Get result from remaining edges
This trimming algorithm can be made faster by storing the edge's index and both its nodes in step one instead of just storing the edge's index. This would require 3x more memory though, but then steps two, three, and four could be skipped.
*/
// Constants
// Bits in a byte
#define BITS_IN_A_BYTE 8
// Number of edges
#define NUMBER_OF_EDGES ((ulong)1 << EDGE_BITS)
// Node mask
#define NODE_MASK (NUMBER_OF_EDGES - 1)
// SipRound rotation
#define SIP_ROUND_ROTATION 21
// Bitmap mask
#define BITMAP_MASK (NUMBER_OF_BITMAP_BYTES * BITS_IN_A_BYTE - 1)
// Function prototypes
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_ONE_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_ONE_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Check if local buckets size is one
#if LOCAL_BUCKETS_SIZE == 1
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys);
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint *restrict bucketsSecondPart);
#endif
// Otherwise check if local buckets size is two
#elif LOCAL_BUCKETS_SIZE == 2
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint2 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys);
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint2 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint2 *restrict bucketsSecondPart);
#endif
// Otherwise
#else
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint4 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys);
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint4 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint4 *restrict bucketsSecondPart);
#endif
#endif
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_TWO_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_TWO_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step two
__kernel void trimEdgesStepTwo(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys);
// Otherwise
#else
// Trim edges step two
__kernel void trimEdgesStepTwo(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys, __global const uint *restrict sourceBucketsSecondPart);
#endif
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_THREE_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_THREE_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Trim edges step three
__kernel void trimEdgesStepThree(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint2 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys);
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_FOUR_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_FOUR_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Trim edges step four
__kernel void trimEdgesStepFour(__global const uint2 *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint4 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys);
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_FIVE_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_FIVE_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Trim edges step five
__kernel void trimEdgesStepFive(__global const uint4 *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint4 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket);
// Check if work items per work group exists
#ifdef TRIM_EDGES_STEP_SIX_WORK_ITEMS_PER_WORK_GROUP
// Required work group size
__attribute__((reqd_work_group_size(TRIM_EDGES_STEP_SIX_WORK_ITEMS_PER_WORK_GROUP, 1, 1)))
#endif
// Check if trimming rounds is one
#if TRIMMING_ROUNDS == 1
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges, const ulong4 sipHashKeys);
// Otherwise check if trimming rounds is two
#elif TRIMMING_ROUNDS == 2
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint2 *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges, const ulong4 sipHashKeys);
// Otherwise
#else
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint4 *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges);
#endif
// SipHash-2-4
static inline uint sipHash24(ulong4 keys, const ulong nonce);
// SipRound
static inline void sipRound(ulong4 *keys);
// Set bit in bitmap
static inline void setBitInBitmap(__local uint *bitmap, const uint index);
// Is bit set in bitmap
static inline bool isBitSetInBitmap(__local const uint *bitmap, const uint index);
// Supporting function implementation
// Check if local buckets size is one
#if LOCAL_BUCKETS_SIZE == 1
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys) {
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint *restrict bucketsSecondPart) {
#endif
// Otherwise check if local buckets size is two
#elif LOCAL_BUCKETS_SIZE == 2
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint2 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys) {
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint2 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint2 *restrict bucketsSecondPart) {
#endif
// Otherwise
#else
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint4 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys) {
// Otherwise
#else
// Trim edges step one
__kernel void trimEdgesStepOne(__global uint4 *restrict buckets, __global uint *restrict numberOfEdgesPerBucket, const ulong4 sipHashKeys, __global uint4 *restrict bucketsSecondPart) {
#endif
#endif
// Check if local buckets size is one
#if LOCAL_BUCKETS_SIZE == 1
// Get global ID
const uint globalId = get_global_id(0);
// Get work item's edge indices
const uint indices = globalId * NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM;
// Go through all of this work item's edges
for(short i = 0; i < NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM; ++i) {
// Get edge's index
const uint edgeIndex = indices + i;
// Get edge's node's bucket index
const uint bucketIndex = sipHash24(sipHashKeys, (ulong)edgeIndex * 2) >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_inc(&numberOfEdgesPerBucket[bucketIndex]), (uint)(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET - 1));
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get bucket's next indices
__global uint *bucketNextIndices = &buckets[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex];
// Otherwise
#else
// Get bucket's next indices
__global uint *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex] : &bucketsSecondPart[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (bucketIndex - INITIAL_BUCKETS_NUMBER_OF_BUCKETS) + nextEdgeIndex];
#endif
// Set bucket's next edge to the edge
*bucketNextIndices = edgeIndex;
}
// Otherwise
#else
// Declare local buckets
__local uint localBuckets[NUMBER_OF_BUCKETS][LOCAL_BUCKETS_SIZE - 1];
// Declare number of edges per local bucket
__local uint numberOfEdgesPerLocalBucket[(short)((NUMBER_OF_BUCKETS + sizeof(uint) - 1) / sizeof(uint))];
// Get global ID
const uint globalId = get_global_id(0);
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Go through all groups of local buckets as a work group
for(short i = localId; i < (short)((NUMBER_OF_BUCKETS + sizeof(uint) - 1) / sizeof(uint)); i += localSize) {
// Set group of local bucket's number of edges to zero
numberOfEdgesPerLocalBucket[i] = 0;
}
// Get work item's edge indices
const uint indices = globalId * NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM;
// Go through all of this work item's edges
for(short i = 0; i < NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM; ++i) {
// Get edge's index
const uint edgeIndex = indices + i;
// Get edge's node's bucket index
const uint bucketIndex = sipHash24(sipHashKeys, (ulong)edgeIndex * 2) >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Increment local bucket's number of edges
uchar numberOfEdges = atomic_add(&numberOfEdgesPerLocalBucket[bucketIndex / (char)sizeof(uint)], 1 << ((bucketIndex % (char)sizeof(uint)) * BITS_IN_A_BYTE)) >> ((bucketIndex % (char)sizeof(uint)) * BITS_IN_A_BYTE);
// Check if local bucket isn't full with this edge
if(numberOfEdges < LOCAL_BUCKETS_SIZE - 1) {
// Append edge index to local bucket
localBuckets[bucketIndex][numberOfEdges] = edgeIndex;
}
// Loop until edges have been added to a local bucket
for(char j = 0; j < 8 / LOCAL_BUCKETS_SIZE; ++j) {
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Check if local bucket is full with this edge
if(numberOfEdges == LOCAL_BUCKETS_SIZE - 1) {
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_add(&numberOfEdgesPerBucket[bucketIndex], LOCAL_BUCKETS_SIZE), (uint)(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET - LOCAL_BUCKETS_SIZE));
// Check if local buckets size is two
#if LOCAL_BUCKETS_SIZE == 2
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get bucket's next indices
__global uint2 *bucketNextIndices = &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 2];
// Otherwise
#else
// Get bucket's next indices
__global uint2 *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 2] : &bucketsSecondPart[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (bucketIndex - INITIAL_BUCKETS_NUMBER_OF_BUCKETS) + nextEdgeIndex) / 2];
#endif
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint2)(localBuckets[bucketIndex][0], edgeIndex);
// Otherwise
#else
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get bucket's next indices
__global uint4 *bucketNextIndices = &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 4];
// Otherwise
#else
// Get bucket's next indices
__global uint4 *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 4] : &bucketsSecondPart[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (bucketIndex - INITIAL_BUCKETS_NUMBER_OF_BUCKETS) + nextEdgeIndex) / 4];
#endif
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint4)(localBuckets[bucketIndex][0], localBuckets[bucketIndex][1], localBuckets[bucketIndex][2], edgeIndex);
#endif
// Update local bucket's number of edges
atomic_sub(&numberOfEdgesPerLocalBucket[bucketIndex / (char)sizeof(uint)], LOCAL_BUCKETS_SIZE << ((bucketIndex % (char)sizeof(uint)) * BITS_IN_A_BYTE));
}
// Update number of edges
numberOfEdges -= LOCAL_BUCKETS_SIZE;
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Check if local bucket isn't full with this edge
if(numberOfEdges < LOCAL_BUCKETS_SIZE - 1) {
// Append edge index to local bucket
localBuckets[bucketIndex][numberOfEdges] = edgeIndex;
}
}
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all local buckets as a work group
for(int i = localId; i < NUMBER_OF_BUCKETS; i += localSize) {
// Get local bucket's number of edges
const uchar numberOfEdges = numberOfEdgesPerLocalBucket[i / (char)sizeof(uint)] >> ((i % (char)sizeof(uint)) * BITS_IN_A_BYTE);
// Check if local bucket isn't empty
if(numberOfEdges) {
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_add(&numberOfEdgesPerBucket[i], LOCAL_BUCKETS_SIZE), (uint)(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET - LOCAL_BUCKETS_SIZE));
// Check if local buckets size is two
#if LOCAL_BUCKETS_SIZE == 2
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get bucket's next indices
__global uint2 *bucketNextIndices = &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * i + nextEdgeIndex) / 2];
// Otherwise
#else
// Get bucket's next indices
__global uint2 *bucketNextIndices = (i < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * i + nextEdgeIndex) / 2] : &bucketsSecondPart[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (i - INITIAL_BUCKETS_NUMBER_OF_BUCKETS) + nextEdgeIndex) / 2];
#endif
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint2)(localBuckets[i][0]);
// Otherwise
#else
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get bucket's next indices
__global uint4 *bucketNextIndices = &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * i + nextEdgeIndex) / 4];
// Otherwise
#else
// Get bucket's next indices
__global uint4 *bucketNextIndices = (i < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * i + nextEdgeIndex) / 4] : &bucketsSecondPart[((ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (i - INITIAL_BUCKETS_NUMBER_OF_BUCKETS) + nextEdgeIndex) / 4];
#endif
// Check number of edges
switch(numberOfEdges) {
// One
case 1:
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint4)(localBuckets[i][0]);
// Break
break;
// Two
case 2:
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint4)((uint3)(localBuckets[i][0]), localBuckets[i][1]);
// Break
break;
// Default
default:
// Set bucket's next edges to the local bucket's edges
*bucketNextIndices = (uint4)((uint2)(localBuckets[i][0]), localBuckets[i][1], localBuckets[i][2]);
}
#endif
}
}
#endif
}
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step two
__kernel void trimEdgesStepTwo(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys) {
// Otherwise
#else
// Trim edges step two
__kernel void trimEdgesStepTwo(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys, __global const uint *restrict sourceBucketsSecondPart) {
#endif
// Declare bitmap
__local uint bitmap[(short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Get group ID
const uint groupId = get_group_id(0);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < (short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to zero
bitmap[i] = 0;
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerSourceBucket[groupId], (uint)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET);
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Get work group's bucket's indices
__global const uint *indices = &sourceBuckets[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Otherwise
#else
// Get work group's bucket's indices
__global const uint *indices = (groupId < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &sourceBuckets[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId] : &sourceBucketsSecondPart[(ulong)INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (groupId - INITIAL_BUCKETS_NUMBER_OF_BUCKETS)];
#endif
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index
const uint edgeIndex = indices[i];
// Enable edge's node in the bitmap
setBitInBitmap(bitmap, sipHash24(sipHashKeys, (ulong)edgeIndex * 2) & BITMAP_MASK);
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index
const uint edgeIndex = indices[i];
// Check if local buckets size isn't one
#if LOCAL_BUCKETS_SIZE != 1
// Check if edge isn't a duplicate
if(edgeIndex != indices[min(i - 1, numberOfEdges - 1)]) {
#endif
// Get edge's node
const uint node = sipHash24(sipHashKeys, (ulong)edgeIndex * 2);
// Check if edge's node has a pair in the bitmap
if(isBitSetInBitmap(bitmap, (node & BITMAP_MASK) ^ 1)) {
// Get edge's other node
const uint otherNode = sipHash24(sipHashKeys, ((ulong)edgeIndex * 2) | 1);
// Get edge's other node's bucket index
const uint bucketIndex = otherNode >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_inc(&numberOfEdgesPerDestinationBucket[bucketIndex]), (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET - 1));
// Get destination bucket's next indices
__global uint *bucketNextIndices = &destinationBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex];
// Set destination bucket's next edge to the edge
*bucketNextIndices = edgeIndex;
}
// Check if local buckets size isn't one
#if LOCAL_BUCKETS_SIZE != 1
}
#endif
}
}
// Trim edges step three
__kernel void trimEdgesStepThree(__global const uint *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint2 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys) {
// Declare bitmap
__local uint bitmap[(short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Get group ID
const uint groupId = get_group_id(0);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < (short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to zero
bitmap[i] = 0;
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerSourceBucket[groupId], (uint)AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET);
// Get work group's bucket's indices
__global const uint *indices = &sourceBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index
const uint edgeIndex = indices[i];
// Enable edge's node in the bitmap
setBitInBitmap(bitmap, sipHash24(sipHashKeys, ((ulong)edgeIndex * 2) | 1) & BITMAP_MASK);
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index
const uint edgeIndex = indices[i];
// Get edge's node
const uint node = sipHash24(sipHashKeys, ((ulong)edgeIndex * 2) | 1);
// Check if edge's node has a pair in the bitmap
if(isBitSetInBitmap(bitmap, (node & BITMAP_MASK) ^ 1)) {
// Get edge's other node
const uint otherNode = sipHash24(sipHashKeys, (ulong)edgeIndex * 2);
// Get edge's other node's bucket index
const uint bucketIndex = otherNode >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_inc(&numberOfEdgesPerDestinationBucket[bucketIndex]), (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2 - 1));
// Get destination bucket's next indices
__global uint2 *bucketNextIndices = &destinationBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2 * bucketIndex + nextEdgeIndex];
// Set destination bucket's next edge to the edge and its other node
*bucketNextIndices = (uint2)(edgeIndex, otherNode);
}
}
}
// Trim edges step four
__kernel void trimEdgesStepFour(__global const uint2 *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint4 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket, const ulong4 sipHashKeys) {
// Declare bitmap
__local uint bitmap[(short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Get group ID
const uint groupId = get_group_id(0);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < (short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to zero
bitmap[i] = 0;
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerSourceBucket[groupId], (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2));
// Get work group's bucket's indices
__global const uint2 *indices = &sourceBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2 * groupId];
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Enable edge's node in the bitmap
setBitInBitmap(bitmap, indices[i].y & BITMAP_MASK);
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index and node
const uint2 edgeIndexAndNode = indices[i];
// Check if edge's node has a pair in the bitmap
if(isBitSetInBitmap(bitmap, (edgeIndexAndNode.y & BITMAP_MASK) ^ 1)) {
// Get edge's other node
const uint otherNode = sipHash24(sipHashKeys, ((ulong)edgeIndexAndNode.x * 2) | 1);
// Get edge's other node's bucket index
const uint bucketIndex = otherNode >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_inc(&numberOfEdgesPerDestinationBucket[bucketIndex]), (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 - 1));
// Get destination bucket's next indices
__global uint4 *bucketNextIndices = &destinationBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 * bucketIndex + nextEdgeIndex];
// Set destination bucket's next edge to the edge and its nodes
*bucketNextIndices = (uint4)(edgeIndexAndNode.x, otherNode, edgeIndexAndNode.y, otherNode & BITMAP_MASK);
}
}
}
// Trim edges step five
__kernel void trimEdgesStepFive(__global const uint4 *restrict sourceBuckets, __global const uint *restrict numberOfEdgesPerSourceBucket, __global uint4 *restrict destinationBuckets, __global uint *restrict numberOfEdgesPerDestinationBucket) {
// Declare bitmap
__local uint bitmap[(short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Get group ID
const uint groupId = get_group_id(0);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < (short)(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to zero
bitmap[i] = 0;
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerSourceBucket[groupId], (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4));
// Get work group's bucket's indices
__global const uint4 *indices = &sourceBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 * groupId];
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Enable edge's node in the bitmap
setBitInBitmap(bitmap, indices[i].w);
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get edge's index and nodes
const uint4 edgeIndexAndNodes = indices[i];
// Check if edge's node has a pair in the bitmap
if(isBitSetInBitmap(bitmap, edgeIndexAndNodes.w ^ 1)) {
// Get edge's other node's bucket index
const uint bucketIndex = edgeIndexAndNodes.z >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_inc(&numberOfEdgesPerDestinationBucket[bucketIndex]), (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 - 1));
// Get destination bucket's next indices
__global uint4 *bucketNextIndices = &destinationBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 * bucketIndex + nextEdgeIndex];
// Set destination bucket's next edge to the edge and its nodes
*bucketNextIndices = (uint4)(edgeIndexAndNodes.xzy, edgeIndexAndNodes.z & BITMAP_MASK);
}
}
}
// Check if trimming rounds is one
#if TRIMMING_ROUNDS == 1
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges, const ulong4 sipHashKeys) {
// Otherwise check if trimming rounds is two
#elif TRIMMING_ROUNDS == 2
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint2 *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges, const ulong4 sipHashKeys) {
// Otherwise
#else
// Trim edges step six
__kernel void trimEdgesStepSix(__global const uint4 *restrict buckets, __global const uint *restrict numberOfEdgesPerBucket, __global uint *restrict remainingEdges) {
#endif
// Declare index
__local uint index;
// Get local ID
const ushort localId = get_local_id(0);
// Get local size
const ushort localSize = get_local_size(0);
// Get group ID
const uint groupId = get_group_id(0);
// Check if trimming rounds is one
#if TRIMMING_ROUNDS == 1
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], (uint)AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET);
// Get work group's bucket's indices
__global const uint *indices = &buckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Otherwise check if trimming rounds is two
#elif TRIMMING_ROUNDS == 2
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2));
// Get work group's bucket's indices
__global const uint2 *indices = &buckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 2 * groupId];
// Otherwise
#else
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], (uint)(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4));
// Get work group's bucket's indices
__global const uint4 *indices = &buckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_BUCKET / 4 * groupId];
#endif
// Check if this work item is the first in the work group
if(localId == 0) {
// Add number of edges to the number of remaining edges
index = atomic_add(remainingEdges, numberOfEdges);
}
// Synchronize work group
barrier(CLK_LOCAL_MEM_FENCE);
// Go through all edges in this work group's bucket as a work group
for(uint i = localId; i < numberOfEdges; i += localSize) {
// Get next remaining edge
__global uint *nextRemainingEdge = &remainingEdges[min(index + i, (uint)(MAX_NUMBER_OF_EDGES_AFTER_TRIMMING - 1)) * EDGE_NUMBER_OF_COMPONENTS + 1];
// Check if trimming rounds is one
#if TRIMMING_ROUNDS == 1
// Get edge's index
const uint edgeIndex = indices[i];
// Get edge's node
const uint node = sipHash24(sipHashKeys, (ulong)edgeIndex * 2);
// Get edge's other node
const uint otherNode = sipHash24(sipHashKeys, ((ulong)edgeIndex * 2) | 1);
// Set next remaining edge to the edge and its nodes
nextRemainingEdge[0] = edgeIndex;
nextRemainingEdge[1] = node;
nextRemainingEdge[2] = otherNode;
// Otherwise check if trimming rounds is two
#elif TRIMMING_ROUNDS == 2
// Get edge's other node
const uint otherNode = sipHash24(sipHashKeys, ((ulong)indices[i].x * 2) | 1);
// Set next remaining edge to the edge and its nodes
nextRemainingEdge[0] = indices[i].x;
nextRemainingEdge[1] = indices[i].y;
nextRemainingEdge[2] = otherNode;
// Otherwise
#else
// Check if trimming rounds is even
#if TRIMMING_ROUNDS % 2 == 0
// Set next remaining edge to the edge and its nodes
nextRemainingEdge[0] = indices[i].x;
nextRemainingEdge[1] = indices[i].y;
nextRemainingEdge[2] = indices[i].z;
// Otherwise
#else
// Set next remaining edge to the edge and its nodes
nextRemainingEdge[0] = indices[i].x;
nextRemainingEdge[1] = indices[i].z;
nextRemainingEdge[2] = indices[i].y;
#endif
#endif
}
}
// SipHash-2-4
uint sipHash24(ulong4 keys, const ulong nonce) {
// Perform hash on keys
keys.w ^= nonce;
sipRound(&keys);
sipRound(&keys);
keys.even ^= (ulong2)(nonce, 255);
sipRound(&keys);
sipRound(&keys);
sipRound(&keys);
sipRound(&keys);
keys.lo ^= keys.hi;
// Check if edge bits is 32
#if EDGE_BITS == 32
// Return node from keys
return keys.x ^ keys.y;
// Otherwise
#else
// Return node from keys
return (keys.x ^ keys.y) & NODE_MASK;
#endif
}
// SipRound
void sipRound(ulong4 *keys) {
// Perform SipRound on keys
keys->even += keys->odd;
keys->odd = rotate(keys->odd, (ulong2)(13, 16));
keys->odd ^= keys->even;
keys->x = rotate(keys->x, (ulong)32);
keys->even += keys->wy;
keys->odd = rotate(keys->odd, (ulong2)(17, SIP_ROUND_ROTATION));
keys->odd ^= keys->zx;
keys->z = rotate(keys->z, (ulong)32);
}
// Set bit in bitmap
void setBitInBitmap(__local uint *bitmap, const uint index) {
// Set bit in bitmap
atomic_or(&bitmap[index / (char)(sizeof(uint) * BITS_IN_A_BYTE)], 1 << (index % (char)(sizeof(uint) * BITS_IN_A_BYTE)));
}
// Is bit set in bitmap
bool isBitSetInBitmap(__local const uint *bitmap, const uint index) {
// Return if bit is set in bitmap
return bitmap[index / (char)(sizeof(uint) * BITS_IN_A_BYTE)] & (1 << (index % (char)(sizeof(uint) * BITS_IN_A_BYTE)));
}
)"