-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisect_transmit.hpp
1040 lines (907 loc) · 32.4 KB
/
bisect_transmit.hpp
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
#pragma once
//
// Requires multiple dimension launch with Y dimention equal to 'BiNRanks'
//
template <typename T, int NRanks, int SubGroupSize>
class BisectPTransmit {
constexpr static int BiNRanks = NRanks / 2;
constexpr static int NPeers = BiNRanks -1;
constexpr static int nReg128B = 128 / SubGroupSize / 4;
constexpr static int firstElem = 0;
constexpr static int lastElem = nReg128B -1;
// Configurations
constexpr static auto CommReadCacheCtrl = CacheCtrl::L1UC_L3C;
constexpr static auto CommWriteCacheCtrl = CacheCtrl::L1UC_L3WB;
constexpr static auto PrefetchCacheCtrl = CacheCtrl::DEFAULT;
protected:
using message_t = sycl::vec<uint32_t, nReg128B>;
// transaction of 128-byte is not atomic across HBM channel
constexpr static int nChan8B = 8 / sizeof(message_t);
constexpr static int lastDataChannel = SubGroupSize -nChan8B;
constexpr static int firstFlagChannel = SubGroupSize/2 -1;
constexpr static int lastFlagChannel = SubGroupSize -1;
constexpr static int wireCapacity = (SubGroupSize-nChan8B) * sizeof(message_t);
constexpr static int wireTransSize = SubGroupSize * sizeof(message_t);
constexpr static size_t wireElems = wireCapacity /sizeof(T);
constexpr static size_t wireTransElems = wireTransSize /sizeof(T);
public:
//
// sectionSize represents each temporary buffer section for each rank.
// configurable, in bytes.
//
constexpr static size_t nSlot = 8;
constexpr static size_t ringSize = wireTransSize * nSlot;
constexpr static size_t maxLaunch = 64 * 64; // 64 wire, 64 SS
typedef T (* ringPtr)[nSlot][wireTransElems];
private:
// factor basic communication into another class
template <int unroll> static inline void loadInput(
message_t (&v)[unroll], T* src, int nElt
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // TODO: diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
if (off < nElt) { // TODO: condition branch !
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize>(v[i], src + off);
#else
(void)off;
#endif
}}}
}
template <int unroll> static inline void preload(T *ptr) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireTransSize + local_off;
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscPrefetch<T, sizeof(message_t)/sizeof(T),
SubGroupSize, PrefetchCacheCtrl>(ptr + off);
#else
(void)off;
#endif
}
}
template <int unroll> static inline void loadInput(
message_t (&v)[unroll], T* src
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize>(v[i], src + off);
#else
(void)off;
#endif
}}
}
template <int unroll>
static inline void insertFlags(
message_t (& messages)[unroll], uint32_t flag
) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
if constexpr (SubGroupSize == 16) {
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(1, 7)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(1, 15)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
} else {
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(0, 15)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i])) : "rw"(flag)
);
}
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(0, 31)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i])) : "rw"(flag)
);
}
}
#else
// Add flags at the middle and tail
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
if (lid == firstFlagChannel || lid == lastFlagChannel) {
# pragma unroll
for (int i = 0; i < unroll; ++ i)
messages[i][lastElem] = flag;
}
#endif
}
template <int unroll>
static inline void shuffleData(message_t (& messages)[unroll]) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
if constexpr (SubGroupSize == 16) {
asm volatile ("\n"
"mov (M1, 1) %0(0, 15)<1> %0(1, 7)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
} else {
asm volatile ("\n"
"mov (M1, 1) %0(0, 30)<1> %0(0, 15)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
}
}
#endif
}
template <int unroll>
static inline void restoreData(message_t (& messages)[unroll]) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
if constexpr (SubGroupSize == 16) {
asm volatile ("\n"
"mov (M1, 1) %0(1, 7)<1> %0(0, 15)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
} else {
asm volatile ("\n"
"mov (M1, 1) %0(0, 15)<1> %0(0, 30)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
}
}
#endif
}
template <int unroll> static inline void storeOutput(
T* dst, message_t (&v)[unroll]
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: Diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize>(dst + off, v[i]);
#else
(void)off; (void)local_off;
#endif
}}
}
template <int unroll> static inline void storeOutput(
T* dst, message_t (&v)[unroll], int nElt
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: Fixed diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
if (off < nElt) { // XXX: runtime condition
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize>(dst + off, v[i]);
#endif
}}}
}
// We always push 128-byte packages
template <int unroll>
static inline void sendMessages(T* ptr, message_t (&messages)[unroll]) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
# pragma unroll
for (int u = 0; u < unroll; ++ u) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize, CommWriteCacheCtrl>(
ptr + u * wireTransElems + local_off,
messages[u]
);
#else
(void) lid; (void) local_off;
#endif
}
}
template <int unroll>
static inline bool recvMessages(message_t (&messages)[unroll], T* ptr, uint32_t flag) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
bool retry = false;
# pragma unroll
for (int u = 0; u < unroll; ++ u) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize, CommReadCacheCtrl>(
messages[u],
ptr + u * wireTransElems + local_off
);
#else
(void) lid; (void) local_off;
#endif
retry |= (lid == firstFlagChannel && messages[u][lastElem] != flag)
|| (lid == lastFlagChannel && messages[u][lastElem] != flag);
}
return retry;
}
template <int unroll> static inline void accumMessages(
message_t (&v)[unroll], message_t (&m)[unroll]
) {
#if defined(__SYCL_DEVICE_ONLY__)
using math_t = sycl::vec<T, sizeof(message_t)/sizeof(T)>;
# pragma unroll
for (int u = 0; u < unroll; ++ u)
v[u] = sycl::bit_cast<message_t>(
sycl::bit_cast<math_t>(m[u]) + sycl::bit_cast<math_t>(v[u])
);
#endif
}
public:
template <int unroll> inline void PreloadNext(
size_t inputOffset
) {
// given ioForPeers vs. ioForFar is stride with multiple of 1024
// Presume loss of L1 for accessing each other
auto wireId = sycl::ext::oneapi::experimental::
this_nd_item<1>().get_global_id(0) / SubGroupSize;
auto y_id = wireId % BiNRanks;
preload<unroll>(ioForPeers + y_id * workElems + inputOffset/sizeof(T));
preload<unroll>(ioForFar + y_id * workElems + inputOffset/sizeof(T));
}
template <int unroll> inline void scatterFar(
size_t inputOffset, size_t tStep, ssize_t workLeft
) {
auto inputOffInType = inputOffset / sizeof(T);
auto flag = seqNo + tStep / nSlot;
auto nelems = workLeft / sizeof(T);
constexpr auto eltPerPack = unroll * wireElems;
auto wireId = sycl::ext::oneapi::experimental::
this_nd_item<1>().get_global_id(0) / SubGroupSize;
auto y_id = wireId % BiNRanks;
auto *ptr = ioForFar + y_id * workElems + inputOffInType;
message_t messages[unroll];
if (nelems < eltPerPack) {
loadInput(messages, ptr, nelems);
} else {
loadInput(messages, ptr);
}
shuffleData(messages);
insertFlags(messages, flag);
auto* dst = farScatterSink[wireId][tStep % nSlot];
sendMessages(dst, messages);
}
template <int unroll> inline void pollFarGatherOutput(
size_t inputOffset, size_t tStep, ssize_t workLeft
) {
auto inputOffInType = inputOffset / sizeof(T);
auto flag = seqNo + tStep / nSlot;
auto nelems = workLeft / sizeof(T);
auto wireId = sycl::ext::oneapi::experimental::
this_nd_item<1>().get_global_id(0) / SubGroupSize;
auto y_id = wireId % BiNRanks;
constexpr auto eltPerPack = unroll * wireElems;
message_t messages[unroll];
bool retry;
do {
retry = false;
retry |= recvMessages(
messages, localFarGatherSink[wireId][tStep % nSlot], flag
);
} while(sycl::any_of_group(sycl::ext::oneapi::experimental::this_sub_group(), retry));
// if (sg.get_local_id()[0] == 15)
// cout<<messages[0]<<sycl::endl<<sycl::flush;
restoreData(messages);
if (nelems < eltPerPack)
storeOutput(ioForFar + y_id * workElems + inputOffInType, messages, nelems);
else
storeOutput(ioForFar + y_id * workElems + inputOffInType, messages);
}
template <int unroll> inline void closeUnifiedPollReduceScatterGather(
size_t inputOffset, size_t tStep, ssize_t workLeft
) {
auto wireId = sycl::ext::oneapi::experimental::
this_nd_item<1>().get_global_id(0) / SubGroupSize;
auto y_id = wireId % BiNRanks;
auto wireId_g = wireId / BiNRanks * BiNRanks;
auto inputOffInType = inputOffset / sizeof(T);
auto flag = seqNo + tStep / nSlot;
auto nelems = workLeft / sizeof(T);
constexpr auto eltPerPack = unroll * wireElems;
message_t v[unroll];
auto* ioPtr = ioForPeers + y_id * workElems + inputOffInType;
if (nelems < eltPerPack)
loadInput(v, ioPtr, nelems);
else
loadInput(v, ioPtr);
message_t messages[unroll];
bool retry;
do {
retry = false;
retry |= recvMessages(
messages, localFarScatterSink[wireId][tStep%nSlot], flag
);
} while(sycl::any_of_group(sycl::ext::oneapi::experimental::this_sub_group(), retry));
shuffleData(v);
accumMessages(v, messages);
//------------------------- sub-group diverge 3:1 -------------------
if (y_id != l_rank) {
insertFlags(v, flag);
sendMessages(scatterSink[y_id][wireId_g][tStep%nSlot], v); // 1. xNPeers <scatter>
bool retry;
do {
retry = false;
retry |= recvMessages(v, localGatherSink[wireId][tStep%nSlot], flag);
} while(sycl::any_of_group(
sycl::ext::oneapi::experimental::this_sub_group(), retry)
); // 4. xNPeers waits for <gather>
}
if (y_id == l_rank) {
# pragma unroll
for (int i =0; i < NPeers; ++ i) {
bool retry;
do {
retry = false;
retry |= recvMessages(
messages, localScatterSink[i][wireId_g][tStep%nSlot], flag);
} while (sycl::any_of_group(
sycl::ext::oneapi::experimental::this_sub_group(), retry)
); // 2. wait for <scatter> xNPeers
accumMessages(v, messages);
}
insertFlags(v, flag);
# pragma unroll
for (int i = 0; i < NPeers; ++ i) // 3. signal <gather>
sendMessages(gatherSink[i][wireId_g][tStep%nSlot], v);
}
//-------------------------group converge-------------------
sendMessages(farGatherSink[wireId][tStep%nSlot], v);
restoreData(v);
if (nelems < eltPerPack)
storeOutput(ioPtr, v, nelems);
else
storeOutput(ioPtr, v);
}
protected:
BisectPTransmit(
T* input,
T* scatterBuf, T* gatherBuf,
T* const peerBuf0[], T* const peerBuf1[],
size_t workSize, int rank, uint32_t seqNo
) : workElems(workSize/sizeof(T)), l_rank(rank/2), seqNo(seqNo)
{
auto pairRank = rank ^ 1;
auto* pairBuf0 = peerBuf0[pairRank];
auto* pairBuf1 = peerBuf1[pairRank];
auto ioClosePart = [&](T* p) {
return (rank & 1) ? (T *)((uintptr_t)p + workSize * BiNRanks) : p;
};
auto ioFarPart = [&](T* p) {
return (rank & 1) ? p : (T *)((uintptr_t)p + workSize * BiNRanks);
};
auto ipcClosePart = [&](T *p) {
return p;
};
auto ipcFarPart = [&](T* p) {
return (T *)((uintptr_t)p + ringSize * maxLaunch);
};
ioForPeers = ioClosePart(input);
ioForFar = ioFarPart(input);
farScatterSink = reinterpret_cast<ringPtr>(ipcFarPart(pairBuf0));
farGatherSink = reinterpret_cast<ringPtr>(ipcFarPart(pairBuf1));
localFarScatterSink = reinterpret_cast<ringPtr>(ipcFarPart(scatterBuf));
localFarGatherSink = reinterpret_cast<ringPtr>(ipcFarPart(gatherBuf));
localGatherSink = reinterpret_cast<ringPtr>(ipcClosePart(gatherBuf));
// Indicated by y-id
for (int i = 0; i < BiNRanks; ++ i) {
// even: 0, 2, 4, 6
// odd: 1, 3, 5, 7
auto r_index = 2 * i + (rank & 1);
scatterSink[i] = reinterpret_cast<ringPtr>(
(uintptr_t)ipcClosePart(peerBuf0[r_index]) + l_rank * ringSize);
}
// Indicated by next?
for (int i = 0; i < NPeers; ++ i) {
int l_next = (l_rank + i + 1) % BiNRanks;
int next = (rank + i * 2 + 2) % (2 * BiNRanks);
localScatterSink[i] = reinterpret_cast<ringPtr>(
(uintptr_t)ipcClosePart(scatterBuf) + l_next * ringSize);
gatherSink[i] = reinterpret_cast<ringPtr>(
(uintptr_t)ipcClosePart(peerBuf1[next]) + l_rank * ringSize);
}
}
// --------------------Input/Output buffer-------------------
// Input partitions
T* ioForPeers;
T* ioForFar;
ssize_t workElems;
// ---------------------IPC buffers-------------------------
ringPtr farScatterSink;
ringPtr farGatherSink;
// ----------------Sinks, written by remotes----------------
ringPtr localFarScatterSink;
ringPtr localFarGatherSink;
int l_rank;
uint32_t seqNo;
ringPtr scatterSink[BiNRanks];
ringPtr gatherSink[NPeers];
ringPtr localScatterSink[NPeers];
ringPtr localGatherSink;
};
template <typename T, int NRanks, int SubGroupSize>
class BisectPPTransmit {
constexpr static int BiNRanks = NRanks / 2;
constexpr static int NPeers = BiNRanks -1;
constexpr static int nReg128B = 128 / SubGroupSize / 4;
constexpr static int firstElem = 0;
constexpr static int lastElem = nReg128B -1;
constexpr static auto CommReadCacheCtrl = CacheCtrl::L1UC_L3C;
constexpr static auto CommWriteCacheCtrl = CacheCtrl::L1UC_L3WB;
constexpr static auto PrefetchCacheCtrl = CacheCtrl::L1UC_L3C;
public:
//
// sectionSize will be renamed later, it represent each temporary buffer
// section for each rank. configurable, in bytes
//
constexpr static size_t sectionSize = 0x100000;
constexpr static size_t sectionElems = sectionSize / sizeof(T);
constexpr static size_t scratchSize = alignUp(sectionSize * NRanks * 2, 0x200000);
protected:
using message_t = sycl::vec<uint32_t, nReg128B>;
// transaction of 128-byte is not atomic across HBM channel
constexpr static int nChan8B = 8 / sizeof(message_t);
constexpr static int lastDataChannel = SubGroupSize -nChan8B;
constexpr static int firstFlagChannel = SubGroupSize/2 -1;
constexpr static int lastFlagChannel = SubGroupSize -1;
constexpr static int wireCapacity = (SubGroupSize-nChan8B) * sizeof(message_t);
constexpr static int wireTransSize = SubGroupSize * sizeof(message_t);
constexpr static size_t wireElems = wireCapacity /sizeof(T);
constexpr static size_t wireTransElems = wireTransSize /sizeof(T);
public:
static int getNextSequenceNo(size_t workSize, int currSeq) {
return divUp(workSize, wireCapacity) * wireTransSize / sectionSize;
}
static inline size_t ringOffset(size_t sinkOffset) {
return sinkOffset % sectionSize;
}
static inline int seqDelta(size_t sinkOffset) {
return sinkOffset / sectionSize;
}
private:
// factor basic communication into another class
template <int unroll> inline void loadInput(
message_t (&v)[unroll], T* src, int nElt
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // TODO: diverge [0 -----14 ][15] [0]
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
if (off < nElt) { // TODO: condition branch !
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize>(v[i], src + off);
#else
(void)off;
#endif
}}}
}
template <int unroll> inline void loadInput(
message_t (&v)[unroll], T* src
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize>(v[i], src + off);
#else
(void)off;
#endif
}}
}
template <int unroll>
inline void insertFlags(
message_t (& messages)[unroll], uint32_t flag
) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
if constexpr (SubGroupSize == 16) {
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(1, 7)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(1, 15)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
} else {
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(0, 15)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
asm volatile (
"mov (M1, 1) %0(0, 31)<1> %1(0, 0)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
: "rw"(flag)
);
}
}
#else
// Add flags at the middle and tail
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
if (lid == firstFlagChannel || lid == lastFlagChannel) {
# pragma unroll
for (int i = 0; i < unroll; ++ i)
messages[i][lastElem] = flag;
}
#endif
}
template <int unroll>
static inline void shuffleData(message_t (& messages)[unroll]) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
if constexpr (SubGroupSize == 16) {
asm volatile ("\n"
"mov (M1, 1) %0(0, 15)<1> %0(1, 7)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
} else {
asm volatile ("\n"
"mov (M1, 1) %0(0, 30)<1> %0(0, 15)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
}
}
#endif
}
template <int unroll>
static inline void restoreData(message_t (& messages)[unroll]) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
if constexpr (SubGroupSize == 16) {
asm volatile ("\n"
"mov (M1, 1) %0(1, 7)<1> %0(0, 15)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
} else {
asm volatile ("\n"
"mov (M1, 1) %0(0, 15)<1> %0(0, 30)<0;1,0>\n"
: "+rw"(reinterpret_cast<typename message_t::vector_t &>(messages[i]))
:
);
}
}
#endif
}
template <int unroll> inline void storeOutput(
T* dst, message_t (&v)[unroll]
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: Diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize>(dst + off, v[i]);
#else
(void)off; (void)local_off;
#endif
}}
}
template <int unroll> inline void storeOutput(
T* dst, message_t (&v)[unroll], int nElt
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
if (lid < lastDataChannel) { // XXX: Fixed diverge
# pragma unroll
for (int i = 0; i < unroll; ++ i) {
auto off = i * wireElems + local_off;
if (off < nElt) { // XXX: runtime condition
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize>(dst + off, v[i]);
#endif
}}}
}
// We always push 128-byte packages
template <int unroll>
inline void sendMessages(T* ptr, message_t (&messages)[unroll]) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
# pragma unroll
for (int u = 0; u < unroll; ++ u) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscStore<SubGroupSize, CommWriteCacheCtrl>(
ptr + u * wireTransElems + local_off,
messages[u]
);
#else
(void) lid; (void) local_off;
#endif
}
}
template <int unroll>
inline bool recvMessages(message_t (&messages)[unroll], T* ptr, uint32_t flag) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto lid = sg.get_local_id()[0];
int local_off = lid * sizeof(message_t) / sizeof(T);
bool retry = false;
# pragma unroll
for (int u = 0; u < unroll; ++ u) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
lscLoad<SubGroupSize, CommReadCacheCtrl>(
messages[u],
ptr + u * wireTransElems + local_off
);
#else
(void) lid; (void) local_off;
#endif
retry |= (lid == firstFlagChannel && messages[u][lastElem] != flag)
|| (lid == lastFlagChannel && messages[u][lastElem] != flag);
}
return retry;
}
template <int unroll> inline void accumMessages(
message_t (&v)[unroll], message_t (&m)[unroll]
) {
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
using math_t = sycl::vec<T, sizeof(message_t)/sizeof(T)>;
# pragma unroll
for (int u = 0; u < unroll; ++ u)
v[u] = sycl::bit_cast<message_t>(
sycl::bit_cast<math_t>(m[u]) + sycl::bit_cast<math_t>(v[u])
);
#endif
}
public:
// [0, 1] -> [[0, 1], [2, 3]]
template <int unroll> inline void scatterFar(
size_t inputOffset, size_t sinkOffset, ssize_t workLeft
) {
auto inputOffInType = inputOffset / sizeof(T);
auto sinkOffInType = ringOffset(sinkOffset) / sizeof(T);
auto flag = seqNo + seqDelta(sinkOffset);
auto nelems = workLeft / sizeof(T);
constexpr auto eltPerPack = unroll * wireElems;
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto y_id = sg.get_group_id()[0] % 2;
auto *ptr0 = ioForFar[y_id * 2] + inputOffInType;
auto *ptr1 = ioForFar[y_id * 2 + 1] + inputOffInType;
message_t messages0[unroll];
message_t messages1[unroll];
if (nelems < eltPerPack) {
loadInput(messages0, ptr0, nelems);
loadInput(messages1, ptr1, nelems);
} else {
loadInput(messages0, ptr0);
loadInput(messages1, ptr1);
}
shuffleData(messages0);
shuffleData(messages1);
insertFlags(messages0, flag);
insertFlags(messages1, flag);
auto* dst0 = farScatterSink[y_id * 2] + sinkOffInType;
auto* dst1 = farScatterSink[y_id * 2 + 1] + sinkOffInType;
sendMessages(dst0, messages0);
sendMessages(dst1, messages1);
}
// [0, 1] -> [[0, 1], [2, 3]]
template <int unroll> inline void pollFarGatherOutput(
size_t inputOffset, size_t sinkOffset, ssize_t workLeft
) {
auto inputOffInType = inputOffset / sizeof(T);
auto sinkOffInType = ringOffset(sinkOffset) / sizeof(T);
auto flag = seqNo + seqDelta(sinkOffset);
auto nelems = workLeft / sizeof(T);
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto y_id = sg.get_group_id()[0] % 2;
constexpr auto eltPerPack = unroll * wireElems;
message_t messages0[unroll];
bool retry;
do {
retry = false;
retry |= recvMessages(
messages0, localFarGatherSink[y_id * 2] + sinkOffInType, flag);
} while(sycl::any_of_group(sg, retry));
message_t messages1[unroll];
do {
retry = false;
retry |= recvMessages(
messages1, localFarGatherSink[y_id * 2 + 1] + sinkOffInType, flag);
} while(sycl::any_of_group(sg, retry));
// if (sg.get_local_id()[0] == 15)
// cout<<messages[0]<<sycl::endl<<sycl::flush;
restoreData(messages0);
restoreData(messages1);
if (nelems < eltPerPack) {
storeOutput(ioForFar[y_id * 2] + inputOffInType, messages0, nelems);
storeOutput(ioForFar[y_id * 2 + 1] + inputOffInType, messages1, nelems);
} else {
storeOutput(ioForFar[y_id * 2] + inputOffInType, messages0);
storeOutput(ioForFar[y_id * 2 + 1] + inputOffInType, messages1);
}
}
// [0, 1, 2, 3]
template <int unroll> inline void closeUnifiedPollReduceScatterGather(
size_t inputOffset, size_t sinkOffset, ssize_t workLeft
) {
auto sg = sycl::ext::oneapi::experimental::this_sub_group();
auto y_id = sg.get_group_id()[0] % BiNRanks;
auto inputOffInType = inputOffset / sizeof(T);
auto sinkOffInType = ringOffset(sinkOffset) / sizeof(T);
auto flag = seqNo + seqDelta(sinkOffset);
auto nelems = workLeft / sizeof(T);
constexpr auto eltPerPack = unroll * wireElems;
message_t v[unroll];
auto* ioPtr = ioForPeers[y_id] + inputOffInType;
if (nelems < eltPerPack)
loadInput(v, ioPtr, nelems);
else
loadInput(v, ioPtr);
message_t messages[unroll];
bool retry;
do {
retry = false;
retry |= recvMessages(
messages, localFarScatterSink[y_id] + sinkOffInType, flag);
} while(sycl::any_of_group(sg, retry));
shuffleData(v);
accumMessages(v, messages);
//------------------------- group diverge 3:1 -------------------
if (y_id != l_rank) {
insertFlags(v, flag);
sendMessages(scatterSink[y_id] + sinkOffInType, v); // 1. xNPeers <scatter>
bool retry;
do {
retry = false;
retry |= recvMessages(
v, localGatherSink[y_id] + sinkOffInType, flag);
} while(sycl::any_of_group(sg, retry)); // 4. xNPeers waits for <gather>
} else {
# pragma unroll
for (int i =0; i < NPeers; ++ i) {
bool retry;
do {
retry = false;
retry |= recvMessages(
messages, localScatterSink[i] + sinkOffInType, flag);
} while (sycl::any_of_group(sg, retry)); // 2. wait for <scatter> xNPeers
accumMessages(v, messages);
}
insertFlags(v, flag);
# pragma unroll
for (int i = 0; i < NPeers; ++ i) // 3. signal <gather>
sendMessages(gatherSink[i] + sinkOffInType, v);
}
//-------------------------group converge-------------------
sendMessages(farGatherSink[y_id] + sinkOffInType, v);
restoreData(v);
if (nelems < eltPerPack)
storeOutput(ioPtr, v, nelems);
else
storeOutput(ioPtr, v);
}
protected:
BisectPPTransmit(
T* input,
T* scatterBuf, T* gatherBuf,
T* const peerBuf0[], T* const peerBuf1[],
size_t workSize, int rank, uint32_t seqNo
) : l_rank(rank/2), seqNo(seqNo)
{
auto pairRank = rank ^ 1;
auto* pairBuf0 = peerBuf0[pairRank];
auto* pairBuf1 = peerBuf1[pairRank];
auto ioClosePart = [&](T* p) {
return (rank & 1) ? (T *)((uintptr_t)p + workSize * BiNRanks) : p;
};
auto ioFarPart = [&](T* p) {
return (rank & 1) ? p : (T *)((uintptr_t)p + workSize * BiNRanks);
};
auto ipcClosePart = [&](T *p) {
return p;
};
auto ipcFarPart = [&](T* p) {
return (T *)((uintptr_t)p + sectionSize * BiNRanks);
};
// Indicated by y-id
for (int i = 0; i < BiNRanks; ++ i) {
ioForPeers[i] = (T *)((uintptr_t)ioClosePart(input) + i * workSize);
ioForFar[i] = (T *)((uintptr_t)ioFarPart(input) + i * workSize);
farScatterSink[i] = (T *)((uintptr_t)ipcFarPart(pairBuf0)
+ i * sectionSize);
farGatherSink[i] = (T *)((uintptr_t)ipcFarPart(pairBuf1)
+ i * sectionSize);
localFarScatterSink[i] = (T *)((uintptr_t)ipcFarPart(scatterBuf)
+ i * sectionSize);
localFarGatherSink[i] = (T *)((uintptr_t)ipcFarPart(gatherBuf)
+ i * sectionSize);
// Will jump over rank slot, XXX: be careful
if (l_rank != i) {
// even: 0, 2, 4, 6
// odd: 1, 3, 5, 7
auto r_index = 2 * i + (rank & 1);
scatterSink[i] = (T *)((uintptr_t)ipcClosePart(peerBuf0[r_index])