-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc_gpu.hpp
995 lines (911 loc) · 24.5 KB
/
misc_gpu.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
#ifndef __HEADER_MISC_GPU_HPP__
#define __HEADER_MISC_GPU_HPP__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <type_traits>
#if defined(__NVCC__)
#include <cuda.h>
#include <cuda_runtime.h>
#include <cooperative_groups.h>
#endif
#if defined(__HIPCC__)
#include <hip/hip_runtime.h>
#include <hip/amd_detail/amd_hip_cooperative_groups.h>
#endif
#undef COOPERATIVE_GROUP_STANDARD
#if defined(__HIPCC__)
#include <hip/hip_version.h>
#if HIP_VERSION<50700000
#define COOPERATIVE_GROUP_STANDARD 1
#else
//
// 5.7.0 has a strange behavior on compilation, where cooperative group
// would be wrong in class inheritance or friendship.
// 5.6.x prior is OK
//
#if HIP_VERSION<60000000
#define COOPERATIVE_GROUP_STANDARD 0
#else
#define COOPERATIVE_GROUP_STANDARD 1
#endif
//
//
#endif
#endif
#if !defined(COOPERATIVE_GROUP_STANDARD)
#define COOPERATIVE_GROUP_STANDARD 1
#endif
extern __shared__ __align__(sizeof(float)*4) char __shmem[];
#define __SHMEM__(...) (&(((T *)__shmem)[(threadIdx.x&(unsigned)(-tile_size))<<1]))
#if defined(__NVCC__)
#define _if_ asm volatile ("// if branch is here."); if
#endif
#if defined(__HIPCC__)
#define _if_ if
#endif
// NOTE::
// __NVCC__ is on when nvcc compiler is used,
// __CUDA_ARCH__ is on when nvcc targets to compile
// the device functions, and
// __HIPCC__ is on when hipcc compiler is used.
//
// Machine epsilon
// to avoid the conflition to refere to std::numeric_limits<T>::epsilon()
// in a CUDA device function
//
#if defined(__NVCC__)
template < typename T > __host__ __device__
T constexpr machine_epsilon() noexcept { return nextafter(T(1),T(2))-T(1); }
template <> __host__ __device__
double constexpr machine_epsilon <double> () noexcept { return DBL_EPSILON; }
template <> __host__ __device__
float constexpr machine_epsilon <float> () noexcept { return FLT_EPSILON; }
#endif
#if defined(__HIPCC__)
template < typename T > __host__ __device__
T constexpr machine_epsilon() noexcept { return std::numeric_limits<T>::epsilon(); }
#endif
__host__ __device__ __forceinline__
double __Volatile__(const double x) {
double x_ = x;
#if defined(__NVCC__)
asm volatile ("": "+d"(x_));
#endif
return x_;
}
__host__ __device__ __forceinline__
float __Volatile__(const float x) {
float x_ = x;
#if defined(__NVCC__)
asm volatile ("": "+f"(x_));
#endif
return x_;
}
template < class T >
__host__ __device__ __forceinline__
T __MASK__(const T x, const bool cond) {
const T ZERO = static_cast<T>(0);
#if defined(__NVCC__)
asm volatile ("// MASK");
#endif
return cond ? x : ZERO;
}
template < class T >
__host__ __device__ __forceinline__
void __UPDATE__(T &x, const T y, const bool cond) {
#if defined(__NVCC__)
asm volatile ("// UPDATE");
#endif
x = cond ? y : x;
}
template < class T >
__host__ __device__ __forceinline__
void __Cond_swap__(T &x, T &y, const bool cond) {
T z = x; x = cond ? y : z; y = cond ? z : y;
}
template < class T >
__host__ __device__ __forceinline__
T Add(const T x, const T y) {
return __Volatile__(x) + __Volatile__(y);
}
template < class T >
__host__ __device__ __forceinline__
T Sub(const T x, const T y) {
return __Volatile__(x) - __Volatile__(y);
}
template < >
__host__ __device__ __forceinline__
float Sub(const float x, const float y) {
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
return __fsub_rn( x, y );
#else
return __Volatile__(x) - __Volatile__(y);
#endif
}
template < >
__host__ __device__ __forceinline__
double Sub(const double x, const double y) {
#if defined(__CUDA_ARCH__) || defined(__HIPCC__)
return __dsub_rn( x, y );
#else
return __Volatile__(x) - __Volatile__(y);
#endif
}
template < class T >
__host__ __device__ __forceinline__
T Mul(const T x, const T y) {
return __Volatile__(x) * __Volatile__(y);
}
template < class T >
__host__ __device__ __forceinline__
T Div(const T x, const T y) {
return __Volatile__(x) / __Volatile__(y);
}
__host__ __device__ __forceinline__
float Div(const float x, const float y) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ( "div.rn.f32 %0, %1, %2;" : "=f"(z) : "f"(x), "f"(y) );
return ( z );
#else
return __Volatile__(x) / __Volatile__(y);
#endif
}
__host__ __device__ __forceinline__
int Div(const int x, const int y) {
return (int)((unsigned)x /(unsigned)y);
}
__host__ __device__ __forceinline__
double Min(const double x, const double y) {
return fmin(x,y);
}
__host__ __device__ __forceinline__
float Min(const float x, const float y) {
return fminf(x,y);
}
__host__ __device__ __forceinline__
int Min(const int x, const int y) {
return (x<y)?x:y;
}
__host__ __device__ __forceinline__
double Max(const double x, const double y) {
return fmax(x,y);
}
__host__ __device__ __forceinline__
float Max(const float x, const float y) {
return fmaxf(x,y);
}
__host__ __device__ __forceinline__
int Max(const int x, const int y) {
return (x>y)?x:y;
}
__host__ __device__ __forceinline__
double Abs(const double x) {
return fabs(x);
}
__host__ __device__ __forceinline__
float Abs(const float x) {
return fabsf(x);
}
__host__ __device__ __forceinline__
double Sqrt(const double x) {
return sqrt(x);
}
__host__ __device__ __forceinline__
float Sqrt(const float x) {
return sqrtf(x);
}
__host__ __device__ __forceinline__
double Rsqrt(const double x) {
#if defined(__CUDA_ARCH__)
double z;
asm volatile ("rsqrt.f64 %0, %1;" : "=d"(z) : "d"(x) );
return (z);
#else
#if defined(__HIPCC__)
return rsqrt(x);
#else
return 1.0e0/sqrt(x);
#endif
#endif
}
__host__ __device__ __forceinline__
float Rsqrt(const float x) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ("rsqrt.f32 %0, %1;" : "=f"(z) : "f"(x) );
return (z);
#else
#if defined(__HIPCC__)
return rsqrtf(x);
#else
return 1.0f/sqrtf(x);
#endif
#endif
}
__host__ __device__ __forceinline__
float FastSqrt(const float x) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ("sqrt.approx.ftz.f32 %0, %1;" : "=f"(z) : "f"(x) );
return (z);
#else
return Sqrt(x);
#endif
}
__host__ __device__ __forceinline__
double FastSqrt(const double x) {
return Sqrt(x);
}
__host__ __device__ __forceinline__
float Neg(const float x) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ( "neg.f32 %0, %1;" : "=f"(z) : "f"(x) );
return ( z );
#else
return ( -x );
#endif
}
__host__ __device__ __forceinline__
double Neg(const double x) {
#if defined(__CUDA_ARCH__)
double z;
asm volatile ( "neg.f64 %0, %1;" : "=d"(z) : "d"(x) );
return ( z );
#else
return ( -x );
#endif
}
__device__ __forceinline__
float Reciprocal(const float x) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ( "rcp.rn.f32 %0, %1;" : "=f"(z) : "f"(x) );
return ( z );
#else
return static_cast<float>(1) / x;
#endif
}
__device__ __forceinline__
double Reciprocal(const double x) {
#if defined(__CUDA_ARCH__)
double z;
asm volatile ( "rcp.rn.f64 %0, %1;" : "=d"(z) : "d"(x) );
return ( z );
#else
return static_cast<double>(1) / x;
#endif
}
__host__ __device__ __forceinline__
float Sign(const float x, const float y) {
#if defined(__CUDA_ARCH__)
float z;
asm volatile ( "copysign.f32\t%0, %1, %2;" : "=f"(z) : "f"(y), "f"(x) );
return z;
#else
return copysignf(x, y);
#endif
}
__host__ __device__ __forceinline__
double Sign(const double x, const double y) {
#if defined(__CUDA_ARCH__)
double z;
asm volatile ( "copysign.f64\t%0, %1, %2;" : "=d"(z) : "d"(y), "d"(x) );
return z;
#else
return copysign(x, y);
#endif
}
__host__ __device__ __forceinline__
float flip0to1(const float x)
{
#if defined(__CUDA_ARCH__)
float y = x;
asm volatile (
"// flip0to1\n\t"
"{.reg.pred\t%pp;\n\t"
"setp.equ.f32\t%pp, %0, 0f00000000;\n\t"
"@%pp mov.b32\t%0, 0f3F800000;\n\t"
"}" : "+f"(y) );
#else
float y = (x == (float)0.) ? (float)1. : x;
#endif
return y;
}
__host__ __device__ __forceinline__
double flip0to1(const double x)
{
#if defined(__CUDA_ARCH__)
double y = x;
asm volatile (
"// flip0to1\n\t"
"{.reg.pred\t%pp;\n\t"
"setp.equ.f64\t%pp, %0, 0d0000000000000000;\n\t"
"@%pp mov.b64\t%0, 0d3FF0000000000000;\n\t"
"}" : "+d"(y) );
#else
double y = (x == (double)0.) ? (double)1. : x;
#endif
return y;
}
template < class T >
__host__ __device__ __forceinline__
T pythag1(const T x)
{
// Borges 2020, ACM TOMS 1014
const T ONE = static_cast<T>(1);
const T x_sq = x * x;
const T sigma = x_sq + ONE;
const T h = FastSqrt(sigma);
const T sigma_e = Sub(sigma,x_sq) - ONE;
const T tau = fma(x,x,-x_sq) - sigma_e + fma(-h,h,sigma);
const T HF = ONE / 2;
const T r = fma(Div(tau,flip0to1(h)),HF,h);
return r;
}
template < class T >
__host__ __device__ __forceinline__
T pythag(const T x, const T y)
{
// Borges 2020, ACM TOMS 1014
const T ONE = static_cast<T>(1);
const T x_sq = x * x;
const T y_sq = y * y;
const T sigma = x_sq + y_sq;
const T h = FastSqrt(sigma);
const T sigma_e = Sub(sigma,x_sq) - y_sq;
const T tau = fma(y,y,-y_sq) + fma(x,x,-x_sq) - sigma_e + fma(-h,h,sigma);
const T HF = ONE / 2;
const T r = fma(Div(tau,flip0to1(h)),HF,h);
return r;
}
#if defined(__NVCC__)
__device__ __forceinline__
unsigned long __global_timer__(void)
{
unsigned long r;
asm volatile ( "mov.u64 %0, %globaltimer;" : "=l"(r) );
return r;
}
#endif
#if defined(__NVCC__)
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __noinline__
void prefetch_mat_cg(const int len, const T *a) {
asm volatile ("// prefetch in");
const int myid = threadIdx.x % tile_size + 1;
const int L = 32;
size_t a_ = (size_t)a + L*(myid-1);
const size_t a_end = (size_t)a + sizeof(T)*(len);
#pragma unroll 1
while ( true ) {
_if_ ( a_ >= a_end ) break;
asm volatile (
"{ .reg.b32 %t;\n\t"
"ld.global.cg.b32\t%t,[%0];\n\t"
"}" :: "l"(a_) );
a_ += (L*tile_size);
}
asm volatile ("// prefetch out");
}
#endif
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void sync_over_cg(void) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
__cooperative_group__.sync();
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void bcast_over_cg(T &x, const int root) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
x = __cooperative_group__.shfl(x, (root)-1);
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void sum_over_cg(T &x_) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_;
for(int lane=1; lane<tile_size; lane<<=1) {
x += __cooperative_group__.shfl_xor(x, lane);
}
x_ = x;
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void max_over_cg(T &x_) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_;
for(int lane=1; lane<tile_size; lane<<=1) {
x = Max(x, __cooperative_group__.shfl_xor(x, lane));
}
x_ = x;
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void min_over_cg(T &x_) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_;
for(int lane=1; lane<tile_size; lane<<=1) {
x = Min(x, __cooperative_group__.shfl_xor(x, lane));
}
x_ = x;
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
T red2_over_cg(const T x_, const T y_, const int km) {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_; T y = y_;
const bool eee = ((myid & 0x1) != 0);
__Cond_swap__(x, y, eee);
x = y + __cooperative_group__.shfl_xor(x, 1);
for(int lane=2; lane<tile_size; lane<<=1) {
x += __cooperative_group__.shfl_xor(x, lane);
}
x = __MASK__( x, myid<=2 );
x = __cooperative_group__.shfl(x, km);
return x;
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void sum2_over_cg(T &x_, T &y_) {
if (tile_size<=1) {
sum_over_cg<T,tile_size>(x_);
sum_over_cg<T,tile_size>(y_);
} else {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_; T y = y_;
const bool eee = ((myid & 0x1) != 0);
__Cond_swap__(x, y, eee);
x = y + __cooperative_group__.shfl_xor(x, 1);
for(int lane=2; lane<tile_size; lane<<=1) {
x += __cooperative_group__.shfl_xor(x, lane);
}
y = __cooperative_group__.shfl_xor(x, 1);
__Cond_swap__(x, y, !eee);
x_ = x; y_ = y;
}
}
template < class T = int, int tile_size = WARP_GPU_SIZE >
__device__ __forceinline__
void sum4_over_cg(T &x_, T &y_, T &z_, T &w_) {
if (tile_size<=2) {
sum2_over_cg<T,tile_size>(x_,y_);
sum2_over_cg<T,tile_size>(z_,w_);
} else {
#if COOPERATIVE_GROUP_STANDARD
auto const __cooperative_group__ =
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block());
#else
cooperative_groups::thread_block_tile_base<tile_size> __cooperative_group__;
#endif
const int myid = __cooperative_group__.thread_rank()+1;
T x = x_; T y = y_; T z = z_; T w = w_;
const bool eee = ((myid & 0x1) != 0);
__Cond_swap__(x, y, eee);
x = y + __cooperative_group__.shfl_xor(x, 1);
__Cond_swap__(z, w, eee);
z = w + __cooperative_group__.shfl_xor(z, 1);
const bool fff = ((myid & 0x2) != 0);
__Cond_swap__(x, z, fff);
x = z + __cooperative_group__.shfl_xor(x, 2);
for(int lane=4; lane<tile_size; lane<<=1) {
x += __cooperative_group__.shfl_xor(x, lane);
}
z = __cooperative_group__.shfl_xor(x, 2);
__Cond_swap__(x, z, !fff);
y = __cooperative_group__.shfl_xor(x, 1);
__Cond_swap__(x, y, !eee);
w = __cooperative_group__.shfl_xor(z, 1);
__Cond_swap__(w, z, !eee);
x_ = x; y_ = y; z_ = z; w_ = w;
}
}
__device__ __forceinline__ void
sync_over_warp(void)
{
#if defined(__NVCC__)
__syncwarp();
#endif
#if defined(__HIPCC__)
int constexpr tile_size = WARP_GPU_SIZE;
#if COOPERATIVE_GROUP_STANDARD
cooperative_groups::tiled_partition<tile_size>(cooperative_groups::this_thread_block()).sync();
#else
cooperative_groups::thread_block_tile_base<tile_size>::sync();
#endif
#endif
}
#if defined(__NVCC__)
#define SYNC_IF_NEEDED(...) _if_(tile_size<WARP_GPU_SIZE)sync_over_warp()
#else
#define SYNC_IF_NEEDED(...) /* */
#endif
template <class T>
__device__ __forceinline__ void
bcast_over_warp(T &x_, const int root)
{
#if defined(__NVCC__)
T x = x_;
x = __shfl_sync( 0xffffffff, x, root-1, WARP_GPU_SIZE );
x_ = x;
#endif
#if defined(__HIPCC__)
T x = x_;
x = __shfl( x, root-1, WARP_GPU_SIZE );
x_ = x;
#endif
}
#if defined(__NVCC__)
template <>
__device__ __forceinline__ void
bcast_over_warp(float &x_, const int root)
{
float x = x_;
asm volatile (
"{.reg.pred\t%pp;\n\t"
"shfl.sync.idx.b32\t%0|%pp, %0, %1, 31, -1;\n\t"
"}"
: "+f"(x) : "r"(root-1) );
x_ = x;
}
#endif
template <class T>
__device__ __forceinline__ void
bcast2_over_warp(T &x_, T &y_, const int root)
{
bcast_over_warp(x_, root);
bcast_over_warp(y_, root);
}
#if defined(__NVCC__)
template <>
__device__ __forceinline__ void
bcast2_over_warp(float &x_, float &y_, const int root)
{
float x = x_;
float y = y_;
asm volatile (
"{.reg.pred\t%pp;\n\t"
"shfl.sync.idx.b32\t%0|%pp, %0, %2, 31, -1;\n\t"
"shfl.sync.idx.b32\t%1|%pp, %1, %2, 31, -1;\n\t"
"}"
: "+f"(x), "+f"(y) : "r"(root-1) );
x_ = x;
y_ = y;
}
#endif
template <class T>
__device__ __inline__ void
sum_over_warp(T &x_)
{
#if defined(__CUDA_ARCH__)
T x = x_;
x += __shfl_xor_sync( 0xffffffff, x, 1, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 2, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 4, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 8, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 16, 32 );
x_ = x;
#endif
#if defined(__HIPCC__)
T x = x_;
x += __shfl_xor( x, 1, WARP_GPU_SIZE );
x += __shfl_xor( x, 2, WARP_GPU_SIZE );
x += __shfl_xor( x, 4, WARP_GPU_SIZE );
x += __shfl_xor( x, 8, WARP_GPU_SIZE );
x += __shfl_xor( x, 16, WARP_GPU_SIZE );
#if WARP_GPU_SIZE == 64
x += __shfl_xor( x, 32, 64 );
#endif
x_ = x;
#endif
}
#if defined(__NVCC__)
template <>
__device__ __inline__ void
sum_over_warp(float &x_)
{
float x = x_;
asm volatile (
"{.reg.f32\t%ftemp;\n\t"
".reg.pred\t%pp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 1, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 2, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 4, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 8, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 16, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"}"
: "+f"(x) );
x_ = x;
}
#endif
template <class T>
__device__ __inline__ void
sum2_over_warp(T &x_, T &y_)
{
T x = x_;
T y = y_;
const bool eee = (threadIdx.x & 0x1);
{
const T t = (eee ? x : y);
y = (eee ? y : x);
x = t;
}
#if defined(__CUDA_ARCH__)
x = y + __shfl_xor_sync( 0xffffffff, x, 1, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 2, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 4, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 8, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 16, 32 );
y = __shfl_xor_sync( 0xffffffff, x, 1, 32 );
#else
x = y + __shfl_xor( x, 1 , WARP_GPU_SIZE );
x += __shfl_xor( x, 2 , WARP_GPU_SIZE );
x += __shfl_xor( x, 4 , WARP_GPU_SIZE );
x += __shfl_xor( x, 8 , WARP_GPU_SIZE );
x += __shfl_xor( x, 16, WARP_GPU_SIZE );
#if WARP_GPU_SIZE == 64
x += __shfl_xor( x, 32, 64 );
#endif
y = __shfl_xor( x, 1, WARP_GPU_SIZE );
#endif
{
const T t = (eee ? y : x);
y = (eee ? x : y);
x = t;
}
x_ = x;
y_ = y;
}
#if defined(__NVCC__)
template <>
__device__ __inline__ void
sum2_over_warp(float &x_, float &y_)
{
float x = x_;
float y = y_;
const bool eee = (threadIdx.x & 0x1);
__Cond_swap__(x, y, eee);
asm volatile (
"{.reg.f32\t%ftemp;\n\t"
".reg.pred\t%pp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 1, 31, -1;\n\t"
"add.f32\t%0, %1, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 2, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 4, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 8, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%ftemp|%pp, %0, 16, 31, -1;\n\t"
"add.f32\t%0, %0, %ftemp;\n\t"
"shfl.sync.bfly.b32\t%1|%pp, %0, 1, 31, -1;\n\t"
"}"
: "+f"(x), "+f"(y) );
#if 0
z = w + __shfl_xor_sync( 0xffffffff, z, 1, 32 );
z += __shfl_xor_sync( 0xffffffff, z, 2, 32 );
z += __shfl_xor_sync( 0xffffffff, z, 4, 32 );
z += __shfl_xor_sync( 0xffffffff, z, 8, 32 );
z += __shfl_xor_sync( 0xffffffff, z, 16, 32 );
w = __shfl_xor_sync( 0xffffffff, z, 1, 32 );
#endif
__Cond_swap__(x, y, !eee);
x_ = x;
y_ = y;
}
#endif
template <class T>
__device__ __inline__ void
sum4_over_warp(T &x_, T &y_, T &z_, T &w_)
{
T x = x_;
T y = y_;
T z = z_;
T w = w_;
#if defined(__CUDA_ARCH__)
const bool eee = (threadIdx.x & 0x1);
__Cond_swap__(x, y, eee);
x = y + __shfl_xor_sync( 0xffffffff, x, 1, 32 );
__Cond_swap__(z, w, eee);
z = w + __shfl_xor_sync( 0xffffffff, z, 1, 32 );
const bool fff = (threadIdx.x & 0x2);
__Cond_swap__(x, z, fff);
x = z + __shfl_xor_sync( 0xffffffff, x, 2, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 4, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 8, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 16, 32 );
z = __shfl_xor_sync( 0xffffffff, x, 2, 32 );
__Cond_swap__(x, z, !fff);
y = __shfl_xor_sync( 0xffffffff, x, 1, 32 );
__Cond_swap__(x, y, !eee);
w = __shfl_xor_sync( 0xffffffff, z, 1, 32 );
__Cond_swap__(z, w, !eee);
#endif
#if defined(__HIPCC__)
const bool eee = (threadIdx.x & 0x1);
__Cond_swap__(x, y, eee);
x = y + __shfl_xor( x, 1, WARP_GPU_SIZE );
__Cond_swap__(z, w, eee);
z = w + __shfl_xor( z, 1, WARP_GPU_SIZE );
const bool fff = (threadIdx.x & 0x2);
__Cond_swap__(x, z, fff);
x = z + __shfl_xor( x, 2, WARP_GPU_SIZE );
x += __shfl_xor( x, 4, WARP_GPU_SIZE );
x += __shfl_xor( x, 8, WARP_GPU_SIZE );
x += __shfl_xor( x, 16, WARP_GPU_SIZE );
#if WARP_GPU_SIZE == 64
x += __shfl_xor( x, 32, 64 );
#endif
z = __shfl_xor( x, 2, WARP_GPU_SIZE );
__Cond_swap__(x, z, !fff);
y = __shfl_xor( x, 1, WARP_GPU_SIZE );
__Cond_swap__(x, y, !eee);
w = __shfl_xor( z, 1, WARP_GPU_SIZE );
__Cond_swap__(z, w, !eee);
#endif
x_ = x;
y_ = y;
z_ = z;
w_ = w;
}
#if defined(__NVCC__)
template <class T>
__device__ __inline__ void
max_over_warp(T &x_)
{
T x = x_;
x = Max(x, __shfl_xor_sync( 0xffffffff, x, 1, 32 ));
x = Max(x, __shfl_xor_sync( 0xffffffff, x, 2, 32 ));
x = Max(x, __shfl_xor_sync( 0xffffffff, x, 4, 32 ));
x = Max(x, __shfl_xor_sync( 0xffffffff, x, 8, 32 ));
x = Max(x, __shfl_xor_sync( 0xffffffff, x, 16, 32 ));
x_ = x;
}
template <class T>
__device__ __inline__ void
min_over_warp(T &x_)
{
T x = x_;
x = Min(x, __shfl_xor_sync( 0xffffffff, x, 1, 32 ));
x = Min(x, __shfl_xor_sync( 0xffffffff, x, 2, 32 ));
x = Min(x, __shfl_xor_sync( 0xffffffff, x, 4, 32 ));
x = Min(x, __shfl_xor_sync( 0xffffffff, x, 8, 32 ));
x = Min(x, __shfl_xor_sync( 0xffffffff, x, 16, 32 ));
x_ = x;
}
#endif
// only neccessary for _small kernel
#if defined(__NVCC__)
template <class T>
__device__ __inline__ void
sum_over_warp(T &x_, const int bit)
{
T x = x_;
const int nlz=1<<bit;
#pragma unroll
for(int lane=1; lane<nlz; lane<<=1) {
x += __shfl_xor_sync( 0xffffffff, x, lane, nlz );
}
x_ = x;
}
#endif
template <class T>
__device__ __inline__ void
red2_over_warp(T &x_, const T y_)
{
T x = x_;
T y = y_;
const bool eee = (threadIdx.x & 0x1);
{
const T t = (eee ? x : y);
y = (eee ? y : x);
x = t;
}
#if defined(__CUDA_ARCH__)
x = y + __shfl_xor_sync( 0xffffffff, x, 1, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 2, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 4, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 8, 32 );
x += __shfl_xor_sync( 0xffffffff, x, 16, 32 );
#endif
#if defined(__HIPCC__)
x = y + __shfl_xor( x, 1, WARP_GPU_SIZE );
x += __shfl_xor( x, 2, WARP_GPU_SIZE );
x += __shfl_xor( x, 4, WARP_GPU_SIZE );
x += __shfl_xor( x, 8, WARP_GPU_SIZE );
x += __shfl_xor( x, 16, WARP_GPU_SIZE );
#if WARP_GPU_SIZE == 64
x += __shfl_xor( x, 32, WARP_GPU_SIZE );
#endif
#endif
x_ = x;
}
#if defined(__NVCC__)
template <class T>
__device__ __inline__ void
find_maxloc_over_warp(T x_, int &pos)
{
T x = Abs(x_); int p = threadIdx.x & 0x1f;
for(int lane=1; lane<32; lane<<=1) {
T y = __shfl_xor_sync( 0xffffffff, x, lane, 32 );
if ( y > x ) { x = y; p ^= lane; }
}
}
template <class T>
__device__ __noinline__ void
matcpy_(const int nm, const int n, T * wk_, T *a_)
{
const int myid = threadIdx.x % 32 + 1;
__syncwarp();
#define a(row,col) (*(a_+(row-1)+(col-1)*nm))
#define wk(row,col) (*(wk_+(row-1)+(col-1)*nm))
for (int i=1; i<=n%4; i++) {
T * wki_ptr = &wk(myid,i);
T * aki_ptr = &a(myid,i);
for (int k=myid; k<=nm; k+=32, wki_ptr+=32, aki_ptr+=32) {
*wki_ptr = *aki_ptr;
}
}
for (int i=1+n%4; i<=n; i+=4) {
T * wki_ptr = &wk(myid,i);
T * aki_ptr = &a(myid,i);
for (int k=myid; k<=nm; k+=32, wki_ptr+=32, aki_ptr+=32) {
for(int I=0; I<4; I++) wki_ptr[I*nm] = aki_ptr[I*nm];
}
}
#undef a
#undef wk
__syncwarp();
}
#endif
#endif