-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslean_trimming.metal
More file actions
3129 lines (2179 loc) · 159 KB
/
slean_trimming.metal
File metadata and controls
3129 lines (2179 loc) · 159 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
MTLSTR(R"(
/*
N trimming rounds can be performed with the following:
Trimming round 1: clear number of edges per bucket one, step one, step two, clear number of edges per bucket one, step one, step three, clear number of edges per bucket one, step one, step three, ..., clear number of edges per bucket one, step one, step three, clear number of edges per bucket one, step one, clear number of edges per bucket two, step four, step five, clear number of edges per bucket one, step one, clear number of edges per bucket two, step six, step five, clear number of edges per bucket one, step one, clear number of edges per bucket two, step six, step five, ..., clear number of edges per bucket one, step one, clear number of edges per bucket two, step six, step five
Trimming round 2: clear number of edges per bucket one, step seven, step eight, clear number of edges per bucket one, step seven, step nine, clear number of edges per bucket one, step seven, step nine, ..., clear number of edges per bucket one, step seven, step nine, clear number of edges per bucket one, step seven, clear number of edges per bucket two, step ten, step eleven, clear number of edges per bucket one, step seven, clear number of edges per bucket two, step twelve, step eleven, clear number of edges per bucket one, step seven, clear number of edges per bucket two, step twelve, step eleven, ..., clear number of edges per bucket one, step seven, clear number of edges per bucket two, step twelve, step eleven
Trimming round 3: clear number of edges per bucket one, step thirteen, step fourteen, clear number of edges per bucket one, step thirteen, step fifteen, clear number of edges per bucket one, step thirteen, step fifteen, ..., clear number of edges per bucket one, step thirteen, step fifteen, clear number of edges per bucket one, step thirteen, clear number of edges per bucket two, step sixteen, step seventeen, clear number of edges per bucket one, step thirteen, clear number of edges per bucket two, step eighteen, step seventeen, clear number of edges per bucket one, step thirteen, clear number of edges per bucket two, step eighteen, step seventeen, ..., clear number of edges per bucket one, step thirteen, clear number of edges per bucket two, step eighteen, step seventeen
Trimming round 4: clear number of edges per bucket one, step nineteen, step twenty, clear number of edges per bucket one, step nineteen, step twenty-one, clear number of edges per bucket one, step nineteen, step twenty-one, ..., clear number of edges per bucket one, step nineteen, step twenty-one, clear number of edges per bucket one, step nineteen, clear number of edges per bucket two, step twenty-two, step seventeen, clear number of edges per bucket one, step nineteen, clear number of edges per bucket two, step twenty-three, step seventeen, clear number of edges per bucket one, step nineteen, clear number of edges per bucket two, step twenty-three, step seventeen, ..., clear number of edges per bucket one, step nineteen, clear number of edges per bucket two, step twenty-three, step seventeen
Repeat trimming round 4 if 2 trimming parts or until at most 25% of edges remain
Trimming round 5: clear number of edges per bucket one, step twenty-four, step twenty, clear number of edges per bucket one, step twenty-four, step twenty-one, clear number of edges per bucket one, step twenty-four, step twenty-one, ..., clear number of edges per bucket one, step twenty-four, step twenty-one, clear number of edges per bucket one, step twenty-four, clear number of edges per bucket two, step twenty-five, step twenty-six, clear number of edges per bucket one, step twenty-four, clear number of edges per bucket two, step twenty-seven, step twenty-six, clear number of edges per bucket one, step twenty-four, clear number of edges per bucket two, step twenty-seven, step twenty-six, ..., clear number of edges per bucket one, step twenty-four, clear number of edges per bucket two, step twenty-seven, step twenty-six
Repeat trimming round 5 if 4 trimming parts or until at most 12.5% of edges remain
Trimming round 6: clear number of edges per bucket one, step twenty-eight, step twenty, clear number of edges per bucket one, step twenty-eight, step twenty-one, clear number of edges per bucket one, step twenty-eight, step twenty-one, ..., clear number of edges per bucket one, step twenty-eight, step twenty-one, clear number of edges per bucket one, step twenty-eight, clear number of edges per bucket two, step twenty-nine, step thirty, clear number of edges per bucket one, step twenty-eight, clear number of edges per bucket two, step thirty-one, step thirty, clear number of edges per bucket one, step twenty-eight, clear number of edges per bucket two, step thirty-one, step thirty, ..., clear number of edges per bucket one, step twenty-eight, clear number of edges per bucket two, step thirty-one, step thirty
Repeat trimming round 6 if 8 trimming parts or until at most 6.25% of edges remain
Trimming round 7: clear number of edges per bucket one, step thirty-two, clear number of edges per bucket two, step thirty-three, step thirty-four
...
Trimming round n - 1: clear number of edges per bucket one, step thirty-two, clear number of edges per bucket two, step thirty-three, step thirty-four
Trimming round n: clear number of edges per bucket one, step thirty-two, clear number of edges per bucket two, step thirty-three, step thirty-four
Get result from edges bitmap
This trimming algorithm can be made faster by switching to mean trimming once the number of trimming parts has been reduced to one.
*/
// Header files
#include <metal_stdlib>
using namespace metal;
// Constants
// Bits in a byte
#define BITS_IN_A_BYTE 8
// Number of edges
#define NUMBER_OF_EDGES (static_cast<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)
// Remaining edges bitmap mask
#define REMAINING_EDGES_BITMAP_MASK (NUMBER_OF_REMAINING_EDGES_BITMAP_BYTES * BITS_IN_A_BYTE - 1)
// Function prototypes
// 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(device uint *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, device uint *__restrict bucketsSecondPart, const uint globalId);
#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(device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId, const ushort localId, const ushort localSize);
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, device uint2 *__restrict bucketsSecondPart, const uint globalId, const ushort localId, const ushort localSize);
#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(device uint4 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId, const ushort localId, const ushort localSize);
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint4 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, device uint4 *__restrict bucketsSecondPart, const uint globalId, const ushort localId, const ushort localSize);
#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(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step two
[[kernel]] void trimEdgesStepTwo(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, device const uint *__restrict bucketsSecondPart, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step three
[[kernel]] void trimEdgesStepThree(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step three
[[kernel]] void trimEdgesStepThree(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, device const uint *__restrict bucketsSecondPart, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step four
[[kernel]] void trimEdgesStepFour(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step four
[[kernel]] void trimEdgesStepFour(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, device const uint *__restrict sourceBucketsSecondPart, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Trim edges step five
[[kernel]] void trimEdgesStepFive(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step six
[[kernel]] void trimEdgesStepSix(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step six
[[kernel]] void trimEdgesStepSix(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, device const uint *__restrict sourceBucketsSecondPart, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check if local buckets size is one
#if LOCAL_BUCKETS_SIZE == 1
// Trim edges step seven
[[kernel]] void trimEdgesStepSeven(device const ulong *__restrict edgesBitmap, device uint *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
// Otherwise check if local buckets size is two
#elif LOCAL_BUCKETS_SIZE == 2
// Trim edges step seven
[[kernel]] void trimEdgesStepSeven(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId, const ushort localId, const ushort localSize);
// Otherwise
#else
// Trim edges step seven
[[kernel]] void trimEdgesStepSeven(device const ulong *__restrict edgesBitmap, device uint4 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId, const ushort localId, const ushort localSize);
#endif
// Trim edges step eight
[[kernel]] void trimEdgesStepEight(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step nine
[[kernel]] void trimEdgesStepNine(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step ten
[[kernel]] void trimEdgesStepTen(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step eleven
[[kernel]] void trimEdgesStepEleven(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step twelve
[[kernel]] void trimEdgesStepTwelve(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step thirteen
[[kernel]] void trimEdgesStepThirteen(device const ulong *__restrict edgesBitmap, device uint *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, const uint globalId);
// Otherwise
#else
// Trim edges step thirteen
[[kernel]] void trimEdgesStepThirteen(device const ulong *__restrict edgesBitmap, device uint *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
#endif
// Trim edges step fourteen
[[kernel]] void trimEdgesStepFourteen(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step fifteen
[[kernel]] void trimEdgesStepFifteen(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step sixteen
[[kernel]] void trimEdgesStepSixteen(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step sixteen
[[kernel]] void trimEdgesStepSixteen(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step seventeen
[[kernel]] void trimEdgesStepSeventeen(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step seventeen
[[kernel]] void trimEdgesStepSeventeen(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step eighteen
[[kernel]] void trimEdgesStepEighteen(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step eighteen
[[kernel]] void trimEdgesStepEighteen(device const uint *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step nineteen
[[kernel]] void trimEdgesStepNineteen(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, const uint globalId);
// Otherwise
#else
// Trim edges step nineteen
[[kernel]] void trimEdgesStepNineteen(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
#endif
// Trim edges step twenty
[[kernel]] void trimEdgesStepTwenty(device const uint2 *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step twenty-one
[[kernel]] void trimEdgesStepTwentyOne(device const uint2 *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step twenty-two
[[kernel]] void trimEdgesStepTwentyTwo(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-two
[[kernel]] void trimEdgesStepTwentyTwo(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is two
#if SLEAN_TRIMMING_PARTS == 2
// Trim edges step twenty-three
[[kernel]] void trimEdgesStepTwentyThree(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-three
[[kernel]] void trimEdgesStepTwentyThree(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is four
#if SLEAN_TRIMMING_PARTS == 4
// Trim edges step twenty-four
[[kernel]] void trimEdgesStepTwentyFour(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, const uint globalId);
// Otherwise
#else
// Trim edges step twenty-four
[[kernel]] void trimEdgesStepTwentyFour(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
#endif
// Check is slean trimming parts is four
#if SLEAN_TRIMMING_PARTS == 4
// Trim edges step twenty-five
[[kernel]] void trimEdgesStepTwentyFive(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-five
[[kernel]] void trimEdgesStepTwentyFive(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is four
#if SLEAN_TRIMMING_PARTS == 4
// Trim edges step twenty-six
[[kernel]] void trimEdgesStepTwentySix(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-six
[[kernel]] void trimEdgesStepTwentySix(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is four
#if SLEAN_TRIMMING_PARTS == 4
// Trim edges step twenty-seven
[[kernel]] void trimEdgesStepTwentySeven(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-seven
[[kernel]] void trimEdgesStepTwentySeven(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is eight
#if SLEAN_TRIMMING_PARTS == 8
// Trim edges step twenty-eight
[[kernel]] void trimEdgesStepTwentyEight(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, const uint globalId);
// Otherwise
#else
// Trim edges step twenty-eight
[[kernel]] void trimEdgesStepTwentyEight(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, constant const uchar &__restrict part, const uint globalId);
#endif
// Check is slean trimming parts is eight
#if SLEAN_TRIMMING_PARTS == 8
// Trim edges step twenty-nine
[[kernel]] void trimEdgesStepTwentyNine(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step twenty-nine
[[kernel]] void trimEdgesStepTwentyNine(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, device uint *__restrict nodesBitmap, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is eight
#if SLEAN_TRIMMING_PARTS == 8
// Trim edges step thirty
[[kernel]] void trimEdgesStepThirty(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step thirty
[[kernel]] void trimEdgesStepThirty(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Check is slean trimming parts is eight
#if SLEAN_TRIMMING_PARTS == 8
// Trim edges step thirty-one
[[kernel]] void trimEdgesStepThirtyOne(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Otherwise
#else
// Trim edges step thirty-one
[[kernel]] void trimEdgesStepThirtyOne(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device const uint *__restrict nodesBitmap, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, constant const uchar &__restrict part, const ushort localId, const ushort localSize, const uint groupId);
#endif
// Trim edges step thirty-two
[[kernel]] void trimEdgesStepThirtyTwo(device const ulong *__restrict edgesBitmap, device uint2 *__restrict buckets, device atomic_uint *__restrict numberOfEdgesPerBucket, constant const uchar &__restrict nodesInSecondPartition, constant const ulong4 &__restrict sipHashKeys, const uint globalId);
// Trim edges step thirty-three
[[kernel]] void trimEdgesStepThirtyThree(device const uint2 *__restrict sourceBuckets, device const uint *__restrict numberOfEdgesPerSourceBucket, device uint *__restrict destinationBuckets, device atomic_uint *__restrict numberOfEdgesPerDestinationBucket, const ushort localId, const ushort localSize, const uint groupId);
// Trim edges step thirty-four
[[kernel]] void trimEdgesStepThirtyFour(device const uint *__restrict buckets, device const uint *__restrict numberOfEdgesPerBucket, device uint *__restrict edgesBitmap, const ushort localId, const ushort localSize, const uint groupId);
// Check if number of buckets is one
#if NUMBER_OF_BUCKETS == 1
// Clear number of edges per source bucket
[[kernel]] void clearNumberOfEdgesPerSourceBucket(device uint *numberOfEdgesPerSourceBucket, const uint globalId);
// Otherwise
#else
// Clear number of edges per source bucket
[[kernel]] void clearNumberOfEdgesPerSourceBucket(device ulong *numberOfEdgesPerSourceBucket, const uint globalId);
#endif
// Check if number of remaining edges buckets is one
#if NUMBER_OF_REMAINING_EDGES_BUCKETS == 1
// Clear number of edges per destination bucket one
[[kernel]] void clearNumberOfEdgesPerDestinationBucketOne(device uint *numberOfEdgesPerDestinationBucket, const uint globalId);
// Otherwise
#else
// Clear number of edges per destination bucket one
[[kernel]] void clearNumberOfEdgesPerDestinationBucketOne(device ulong *numberOfEdgesPerDestinationBucket, const uint globalId);
#endif
// Clear number of edges per destination bucket two
[[kernel]] void clearNumberOfEdgesPerDestinationBucketTwo(device ulong *numberOfEdgesPerDestinationBucket, const uint globalId);
// Clear number of edges per destination bucket four
[[kernel]] void clearNumberOfEdgesPerDestinationBucketFour(device ulong *numberOfEdgesPerDestinationBucket, const uint globalId);
// Clear number of edges per destination bucket eight
[[kernel]] void clearNumberOfEdgesPerDestinationBucketEight(device ulong *numberOfEdgesPerDestinationBucket, const uint globalId);
// Clear number of edges per destination bucket sixteen
[[kernel]] void clearNumberOfEdgesPerDestinationBucketSixteen(device ulong *numberOfEdgesPerDestinationBucket, const uint globalId);
// SipHash-2-4
static inline uint sipHash24(ulong4 keys, const ulong nonce);
// SipRound
static inline void sipRound(thread ulong4 &keys);
// Set bit in bitmap
static inline void setBitInBitmap(threadgroup atomic_uint *bitmap, const uint index);
// Clear bit in bitmap
static inline void clearBitInBitmap(threadgroup atomic_uint *bitmap, const uint index);
// Is bit set in bitmap
static inline bool isBitSetInBitmap(threadgroup const atomic_uint *bitmap, const uint index);
// Is bit set in bitmap
static inline bool isBitSetInBitmap(threadgroup 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(device uint *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], const uint globalId [[thread_position_in_grid]]) {
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], device uint *__restrict bucketsSecondPart [[buffer(4)]], const uint globalId [[thread_position_in_grid]]) {
#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(device uint2 *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], const uint globalId [[thread_position_in_grid]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]]) {
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint2 *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], device uint2 *__restrict bucketsSecondPart [[buffer(4)]], const uint globalId [[thread_position_in_grid]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]]) {
#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(device uint4 *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], const uint globalId [[thread_position_in_grid]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]]) {
// Otherwise
#else
// Trim edges step one
[[kernel]] void trimEdgesStepOne(device uint4 *__restrict buckets [[buffer(0)]], device atomic_uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], constant const uchar &__restrict part [[buffer(3)]], device uint4 *__restrict bucketsSecondPart [[buffer(4)]], const uint globalId [[thread_position_in_grid]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]]) {
#endif
#endif
// Check if local buckets size is one
#if LOCAL_BUCKETS_SIZE == 1
// Get work item's edge indices in the part
const uint indices = globalId * NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM + part * static_cast<uint>(NUMBER_OF_EDGES / SLEAN_TRIMMING_PARTS);
// 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, static_cast<ulong>(edgeIndex) * 2) >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_fetch_add_explicit(&numberOfEdgesPerBucket[bucketIndex], 1, memory_order_relaxed), static_cast<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
device uint *bucketNextIndices = &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex];
// Otherwise
#else
// Get bucket's next indices
device uint *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex] : &bucketsSecondPart[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
threadgroup uint localBuckets[NUMBER_OF_BUCKETS][LOCAL_BUCKETS_SIZE - 1];
// Declare number of edges per local bucket
threadgroup atomic_uint numberOfEdgesPerLocalBucket[static_cast<short>((NUMBER_OF_BUCKETS + sizeof(uint) - 1) / sizeof(uint))];
// Go through all groups of local buckets as a work group
for(short i = localId; i < static_cast<short>((NUMBER_OF_BUCKETS + sizeof(uint) - 1) / sizeof(uint)); i += localSize) {
// Set group of local bucket's number of edges to zero
atomic_store_explicit(&numberOfEdgesPerLocalBucket[i], 0, memory_order_relaxed);
}
// Get work item's edge indices in the part
const uint indices = globalId * NUMBER_OF_EDGES_PER_STEP_ONE_WORK_ITEM + part * static_cast<uint>(NUMBER_OF_EDGES / SLEAN_TRIMMING_PARTS);
// 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, static_cast<ulong>(edgeIndex) * 2) >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_BUCKET_SORTING;
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// Increment local bucket's number of edges
uchar numberOfEdges = atomic_fetch_add_explicit(&numberOfEdgesPerLocalBucket[bucketIndex / static_cast<char>(sizeof(uint))], 1 << ((bucketIndex % static_cast<char>(sizeof(uint))) * BITS_IN_A_BYTE), memory_order_relaxed) >> ((bucketIndex % static_cast<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
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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_fetch_add_explicit(&numberOfEdgesPerBucket[bucketIndex], LOCAL_BUCKETS_SIZE, memory_order_relaxed), static_cast<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
device uint2 *bucketNextIndices = &buckets[(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 2];
// Otherwise
#else
// Get bucket's next indices
device uint2 *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 2] : &bucketsSecondPart[(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
device uint4 *bucketNextIndices = &buckets[(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 4];
// Otherwise
#else
// Get bucket's next indices
device uint4 *bucketNextIndices = (bucketIndex < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * bucketIndex + nextEdgeIndex) / 4] : &bucketsSecondPart[(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_fetch_sub_explicit(&numberOfEdgesPerLocalBucket[bucketIndex / static_cast<char>(sizeof(uint))], LOCAL_BUCKETS_SIZE << ((bucketIndex % static_cast<char>(sizeof(uint))) * BITS_IN_A_BYTE), memory_order_relaxed);
}
// Update number of edges
numberOfEdges -= LOCAL_BUCKETS_SIZE;
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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 = atomic_load_explicit(&numberOfEdgesPerLocalBucket[i / static_cast<char>(sizeof(uint))], memory_order_relaxed) >> ((i % static_cast<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_fetch_add_explicit(&numberOfEdgesPerBucket[i], LOCAL_BUCKETS_SIZE, memory_order_relaxed), static_cast<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
device uint2 *bucketNextIndices = &buckets[(static_cast<uint>(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET) * i + nextEdgeIndex) / 2];
// Otherwise
#else
// Get bucket's next indices
device uint2 *bucketNextIndices = (i < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[(static_cast<uint>(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET) * i + nextEdgeIndex) / 2] : &bucketsSecondPart[(static_cast<uint>(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
device uint4 *bucketNextIndices = &buckets[(static_cast<uint>(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET) * i + nextEdgeIndex) / 4];
// Otherwise
#else
// Get bucket's next indices
device uint4 *bucketNextIndices = (i < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[(static_cast<uint>(INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET) * i + nextEdgeIndex) / 4] : &bucketsSecondPart[(static_cast<uint>(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(device const uint *__restrict buckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
// Otherwise
#else
// Trim edges step two
[[kernel]] void trimEdgesStepTwo(device const uint *__restrict buckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], device const uint *__restrict bucketsSecondPart [[buffer(4)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
#endif
// Declare bitmap
threadgroup atomic_uint bitmap[static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to zero
atomic_store_explicit(&bitmap[i], 0, memory_order_relaxed);
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], static_cast<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
device const uint *indices = &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Otherwise
#else
// Get work group's bucket's indices
device const uint *indices = (groupId < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId] : &bucketsSecondPart[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (groupId - INITIAL_BUCKETS_NUMBER_OF_BUCKETS)];
#endif
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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, static_cast<ulong>(edgeIndex) * 2) & BITMAP_MASK);
}
// Get work group's nodes bitmap's index
device uint *nodesBitmapIndex = &nodesBitmap[groupId * static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in nodes bitmap to the bitmap's
nodesBitmapIndex[i] = atomic_load_explicit(&bitmap[i], memory_order_relaxed);
}
}
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step three
[[kernel]] void trimEdgesStepThree(device const uint *__restrict buckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
// Otherwise
#else
// Trim edges step three
[[kernel]] void trimEdgesStepThree(device const uint *__restrict buckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], device const uint *__restrict bucketsSecondPart [[buffer(4)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
#endif
// Declare bitmap
threadgroup atomic_uint bitmap[static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get work group's nodes bitmap's index
device uint *nodesBitmapIndex = &nodesBitmap[groupId * static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to the nodes bitmap's
atomic_store_explicit(&bitmap[i], nodesBitmapIndex[i], memory_order_relaxed);
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], static_cast<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
device const uint *indices = &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Otherwise
#else
// Get work group's bucket's indices
device const uint *indices = (groupId < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &buckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId] : &bucketsSecondPart[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (groupId - INITIAL_BUCKETS_NUMBER_OF_BUCKETS)];
#endif
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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, static_cast<ulong>(edgeIndex) * 2) & BITMAP_MASK);
}
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in nodes bitmap to the bitmap's
nodesBitmapIndex[i] = atomic_load_explicit(&bitmap[i], memory_order_relaxed);
}
}
// Check if initial buckets isn't disjointed
#if INITIAL_BUCKETS_NUMBER_OF_BUCKETS == NUMBER_OF_BUCKETS
// Trim edges step four
[[kernel]] void trimEdgesStepFour(device const uint *__restrict sourceBuckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerSourceBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], device uint *__restrict destinationBuckets [[buffer(6)]], device atomic_uint *__restrict numberOfEdgesPerDestinationBucket [[buffer(7)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
// Otherwise
#else
// Trim edges step four
[[kernel]] void trimEdgesStepFour(device const uint *__restrict sourceBuckets [[buffer(0)]], device const uint *__restrict numberOfEdgesPerSourceBucket [[buffer(1)]], device uint *__restrict nodesBitmap [[buffer(5)]], device uint *__restrict destinationBuckets [[buffer(6)]], device atomic_uint *__restrict numberOfEdgesPerDestinationBucket [[buffer(7)]], constant const ulong4 &__restrict sipHashKeys [[buffer(2)]], device const uint *__restrict sourceBucketsSecondPart [[buffer(4)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
#endif
// Declare bitmap
threadgroup atomic_uint bitmap[static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Get work group's nodes bitmap's index
device uint *nodesBitmapIndex = &nodesBitmap[groupId * static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint))];
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to the nodes bitmap's
atomic_store_explicit(&bitmap[i], nodesBitmapIndex[i], memory_order_relaxed);
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerSourceBucket[groupId], static_cast<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
device const uint *indices = &sourceBuckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId];
// Otherwise
#else
// Get work group's bucket's indices
device const uint *indices = (groupId < INITIAL_BUCKETS_NUMBER_OF_BUCKETS) ? &sourceBuckets[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * groupId] : &sourceBucketsSecondPart[INITIAL_MAX_NUMBER_OF_EDGES_PER_BUCKET * (groupId - INITIAL_BUCKETS_NUMBER_OF_BUCKETS)];
#endif
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// 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, static_cast<ulong>(edgeIndex) * 2) & BITMAP_MASK);
}
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in nodes bitmap to the bitmap's
nodesBitmapIndex[i] = atomic_load_explicit(&bitmap[i], memory_order_relaxed);
}
// 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
// Check if edge's node doesn't have a pair in the bitmap
if(!isBitSetInBitmap(bitmap, (sipHash24(sipHashKeys, static_cast<ulong>(edgeIndex) * 2) & BITMAP_MASK) ^ 1)) {
// Get edge's index's bucket index
const uint bucketIndex = ((edgeIndex - static_cast<uint>(NUMBER_OF_EDGES / SLEAN_TRIMMING_PARTS) * (SLEAN_TRIMMING_PARTS - 1)) * SLEAN_TRIMMING_PARTS) >> NUMBER_OF_LEAST_SIGNIFICANT_BITS_IGNORED_DURING_REMAINING_EDGES_BUCKET_SORTING;
// Get bucket's next edge index
const uint nextEdgeIndex = min(atomic_fetch_add_explicit(&numberOfEdgesPerDestinationBucket[bucketIndex], 1, memory_order_relaxed), static_cast<uint>(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_REMAINING_EDGES_BUCKET - 1));
// Get destination bucket's next indices
device uint *bucketNextIndices = &destinationBuckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_REMAINING_EDGES_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 five
[[kernel]] void trimEdgesStepFive(device const uint *__restrict buckets [[buffer(6)]], device const uint *__restrict numberOfEdgesPerBucket [[buffer(7)]], device uint *__restrict edgesBitmap [[buffer(8)]], constant const uchar &__restrict part [[buffer(3)]], const ushort localId [[thread_position_in_threadgroup]], const ushort localSize [[threads_per_threadgroup]], const uint groupId [[threadgroup_position_in_grid]]) {
// Declare bitmap
threadgroup atomic_uint bitmap[static_cast<short>(NUMBER_OF_REMAINING_EDGES_BITMAP_BYTES / sizeof(uint))];
// Go through all groups of bytes in the bitmap as a work group
for(short i = localId; i < static_cast<short>(NUMBER_OF_REMAINING_EDGES_BITMAP_BYTES / sizeof(uint)); i += localSize) {
// Set group of bytes in the bitmap to max
atomic_store_explicit(&bitmap[i], UINT_MAX, memory_order_relaxed);
}
// Get number of edges in this work group's bucket
const uint numberOfEdges = min(numberOfEdgesPerBucket[groupId], static_cast<uint>(AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_REMAINING_EDGES_BUCKET));
// Get work group's bucket's indices
device const uint *indices = &buckets[AFTER_TRIMMING_ROUND_MAX_NUMBER_OF_EDGES_PER_REMAINING_EDGES_BUCKET * groupId];
// Synchronize work group
threadgroup_barrier(mem_flags::mem_threadgroup);
// Go through all edges in this work group's bucket as a work group