-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_support.hpp
1322 lines (1133 loc) · 32.6 KB
/
gpu_support.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
/****************************************************************************************
*
* BEGIN: NAG COPYRIGHT NOTICE
*
* Copyright the Numerical Algorithms Group Ltd, 2015
*
* The portion of this file between
* BEGIN: NAG COPYRIGHT NOTICE
* and
* END: NAG COPYRIGHT NOTICE
* are copyrighted to the Numerical Algorithms Group Ltd, 2015.
* The whole file was created by the Numerical Algorithms Group Ltd, 2015.
*
**************************************************************************************/
#pragma once
#include <iostream>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <stdlib.h>
#include <assert.h>
//========= MACRO Definitions ======================================================
/**
* Macro to test for CUDA errors and bail
* if we find one
*/
#define CHECK(a) ErrorHandler::checkError( (a), __FILE__, __LINE__ )
/**
* Macro to test whether warp shuffles are available
*/
#define HAS_SHUFFLE 0
#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 300)
#undef HAS_SHUFFLE
#define HAS_SHUFFLE 1
#endif
/**
* Macros to insert timing code in the source files depending on a compiler -D define
*/
#define TIMING_SETUP(evt,n)
#if defined(TIMING)
#undef TIMING_SETUP
#define TIMING_SETUP(evt,n) { for(int i=0; i<n; i++) CHECK( cudaEventCreate( &evt[i],0 ) ); }
#endif
#define TIMING_RECORD(ev)
#if defined(TIMING)
#undef TIMING_RECORD
#define TIMING_RECORD(ev) CHECK( cudaEventRecord((ev),0) );
#endif
#define TIMING_PRINT 0
#if defined(TIMING)
#undef TIMING_PRINT
#define TIMING_PRINT 1
#endif
#define TIMING_CLEANUP(evt,n)
#if defined(TIMING)
#undef TIMING_CLEANUP
#define TIMING_CLEANUP(evt,n) { for(int i=0; i<n; i++) CHECK( cudaEventDestroy( evt[i] ) ); }
#endif
//========= Error Handler class ==================================================
/**
* Utility class to help error handling.
* Users can add their own error handlers
* to the static function pointers
* cudaErrorHandler
* and
* cublasErrorHandler
* If these are non-NULL then they will be called in
* preference to the default behaviour which is to
* print a message to the console and call abort()
*/
struct ErrorHandler {
static const char * cublasGetErrorEnum(cublasStatus_t error) {
switch (error) {
case CUBLAS_STATUS_SUCCESS:
return "CUBLAS_STATUS_SUCCESS";
case CUBLAS_STATUS_NOT_INITIALIZED:
return "CUBLAS_STATUS_NOT_INITIALIZED";
case CUBLAS_STATUS_ALLOC_FAILED:
return "CUBLAS_STATUS_ALLOC_FAILED";
case CUBLAS_STATUS_INVALID_VALUE:
return "CUBLAS_STATUS_INVALID_VALUE";
case CUBLAS_STATUS_ARCH_MISMATCH:
return "CUBLAS_STATUS_ARCH_MISMATCH";
case CUBLAS_STATUS_MAPPING_ERROR:
return "CUBLAS_STATUS_MAPPING_ERROR";
case CUBLAS_STATUS_EXECUTION_FAILED:
return "CUBLAS_STATUS_EXECUTION_FAILED";
case CUBLAS_STATUS_INTERNAL_ERROR:
return "CUBLAS_STATUS_INTERNAL_ERROR";
case CUBLAS_STATUS_NOT_SUPPORTED:
return "CUBLAS_STATUS_NOT_SUPPORTED";
case CUBLAS_STATUS_LICENSE_ERROR:
return "CUBLAS_STATUS_LICENSE_ERROR";
}
return "<unknown>";
}
static void (*cudaErrorHandler)(cudaError_t er, const char *file, int line);
static void (*cublasErrorHandler)(cublasStatus_t er, const char *file, int line);
static void (*otherErrorHandler)(const char *msg, const char *file, int line);
inline static void checkError(cudaError_t er, const char * file, int line) {
#ifdef _DEBUG
// If we don't have an error and we're in
// DEBUG mode, sync with device and get last
// error
if(er==cudaSuccess) {
cudaDeviceSynchronize();
er = cudaGetLastError();
}
#endif
if(er!=cudaSuccess) {
if(cudaErrorHandler) {
cudaErrorHandler(er, file, line);
} else {
std::cerr << "CUDA error in " << file << " at line " << line << ":" << std::endl;
std::cerr << "\t" << cudaGetErrorString(er) << std::endl;
assert(false);
}
}
}
inline static void checkError(cublasStatus_t er, const char * file, int line) {
if(er!=CUBLAS_STATUS_SUCCESS) {
if(cublasErrorHandler) {
cublasErrorHandler(er, file, line);
} else {
std::cerr << "CUBLAS error in " << file << " at line " << line << ":" << std::endl;
std::cerr << "\tCUBLAS returned error " << cublasGetErrorEnum(er) << std::endl;
assert(false);
}
}
}
inline static void checkError(const char *msg, const char * file, int line) {
if(otherErrorHandler) {
otherErrorHandler(msg, file, line);
} else {
std::cerr << "General error in " << file << " at line " << line << ":" << std::endl;
std::cerr << "\t" << msg << std::endl;
assert(false);
}
}
};
//===========================================================================================
/**
* Utility functions for doing things on GPU.
* These must not be available on the host
*/
#ifdef __CUDACC__
// These functions were added at CUDA 6.5
#if 0
static __device__ __inline__ double __shfl_down(double var, unsigned int delta, int width=warpSize)
{
float lo, hi;
asm volatile("mov.b64 {%0,%1}, %2;" : "=f"(lo), "=f"(hi) : "d"(var));
hi = __shfl_down(hi, delta, width);
lo = __shfl_down(lo, delta, width);
asm volatile("mov.b64 %0, {%1,%2};" : "=d"(var) : "f"(lo), "f"(hi));
return var;
}
static __device__ __inline__ double __shfl_xor(double var, int laneMask, int width=warpSize)
{
float lo, hi;
asm volatile("mov.b64 {%0,%1}, %2;" : "=f"(lo), "=f"(hi) : "d"(var));
hi = __shfl_xor(hi, laneMask, width);
lo = __shfl_xor(lo, laneMask, width);
asm volatile("mov.b64 %0, {%1,%2};" : "=d"(var) : "f"(lo), "f"(hi));
return var;
}
#endif
//===========================================================================================
// Reduction functions: either warp reductions, or block reductions
//////////////////////////////////
//
// Kepler - shuffles
//
/////////////////////////////////
/**
* Performs a warp reduction where the warp can be split
* into teams. Reduction is within a team, not between teams.
* Teams all have the same size, which must
* be a power of 2. This function is intended for Kepler cards
*
* Parameters:
* x :
* On Input, a value to reduce
* On Output, the value of the reduction across all threads
* in the team
* teamSz :
* On Input: the number of threads in each team.
* Must be a 2,4,8,16 or 32
*/
template<typename FP>
static __inline__ __device__
void warp_allreduceplus(FP &x, int teamSz=32)
{
for(int i=1; i<teamSz; i*=2) {
FP z = __shfl_xor(x, i);
x += z;
}
}
template<typename FP>
static __inline__ __device__
void warp_allreducemax(FP &x, int teamSz=32)
{
for(int i=1; i<teamSz; i*=2) {
FP z = __shfl_xor(x, i);
x = max(x,z);
}
}
/**
* Warp reduction function as above, but where each
* thread now has C independent elements to reduce
*
* Parameters:
* x[C] :
* On Input, the values to reduce
* On Output, x[i] contains the reduction of all x[i]
* values across the team (0 <= i < C)
* teamSz :
* On Input, the number of threads in each team.
* Must be a 2,4,8,16 or 32
*/
template<typename FP, int C>
static __inline__ __device__
void warp_allreduceplus(FP x[], int teamSz=32)
{
FP z[C];
for(int i=1; i<teamSz; i*=2) {
#pragma unroll
for(int cc=0; cc<C; cc++) {
z[cc] = __shfl_xor(x[cc], i);
}
#pragma unroll
for(int cc=0; cc<C; cc++) {
x[cc] += z[cc];
}
}
}
template<typename FP, int C>
static __inline__ __device__
void warp_allreducemax(FP x[], int teamSz=32)
{
FP z[C];
for(int i=1; i<teamSz; i*=2) {
#pragma unroll
for(int cc=0; cc<C; cc++) {
z[cc] = __shfl_xor(x[cc], i);
}
#pragma unroll
for(int cc=0; cc<C; cc++) {
x[cc] = max(x[cc],z[cc]);
}
}
}
/**
* Warp reduction function where each
* thread has C independent elements to reduce.
* Only "thread 0" in each warp receives the final
* answer. This may be faster than the allreduce
* above.
*
* Parameters:
* x[C] :
* On Input, the values to reduce
* On Output, x[i] for thread 0 in the warp
* contains the reduction of all x[i]
* values across the team (0 <= i < C).
* The other threads in the warp
* have undefined values in x[i]
*/
template<typename FP, int C>
static __inline__ __device__
void warp_reduceplus(FP x[])
{
FP z[C];
for(int i=1; i<warpSize; i*=2) {
#pragma unroll
for(int cc=0; cc<C; cc++) {
z[cc] = __shfl_down(x[cc], i);
}
#pragma unroll
for(int cc=0; cc<C; cc++) {
x[cc] += z[cc];
}
}
}
template<typename FP, int C>
static __inline__ __device__
void warp_reducemax(FP x[])
{
FP z[C];
for(int i=1; i<warpSize; i*=2) {
#pragma unroll
for(int cc=0; cc<C; cc++) {
z[cc] = __shfl_down(x[cc], i);
}
#pragma unroll
for(int cc=0; cc<C; cc++) {
x[cc] = max(x[cc],z[cc]);
}
}
}
template<typename FP, int C>
static __inline__ __device__
void warp_reducemin(FP x[])
{
FP z[C];
for(int i=1; i<warpSize; i*=2) {
#pragma unroll
for(int cc=0; cc<C; cc++) {
z[cc] = __shfl_down(x[cc], i);
}
#pragma unroll
for(int cc=0; cc<C; cc++) {
x[cc] = min(x[cc],z[cc]);
}
}
}
/**
* Warp reduction function.
* Only "thread 0" in each warp receives the final
* answer. This may be faster than the allreduce
* above.
*
* Parameters:
* x :
* On Input, the value to reduce
* On Output, x for thread 0 in the warp
* contains the reduction of all x's
* across the team
* The other threads in the warp
* have undefined values in x
*/
template<typename FP>
static __inline__ __device__
void warp_reduceplus(FP &x)
{
FP z;
#pragma unroll
for(int i=1; i<warpSize; i*=2) {
z = __shfl_down(x, i);
x += z;
}
}
template<typename FP>
static __inline__ __device__
void warp_reducemax(FP &x)
{
FP z;
#pragma unroll
for(int i=1; i<warpSize; i*=2) {
z = __shfl_down(x, i);
x = max(x,z);
}
}
template<typename FP>
static __inline__ __device__
void warp_reducemin(FP &x)
{
FP z;
#pragma unroll
for(int i=1; i<warpSize; i*=2) {
z = __shfl_down(x, i);
x = min(x,z);
}
}
/**
* Performs an exclusive scan up the warp
* and returns the scan value. Consider the
* i-th thread in a warp with i=0, 1, ..., 31
* and suppose that
* x = d_x[i]
* Then this routine computes
*
* int cusum = 0;
* for(int j=0; j<i; j++)
* cusum += d_x[j];
*
* return cusum;
*
* In other words the routine returns the sum of
* all x values for all preceding threads in the warp.
* The sum does not include the x value for this thread.
*/
template<typename FP>
static __inline__ __device__
FP warp_exclusive_scanplus(const FP &x)
{
const int tid = threadIdx.x;
const int laneId = tid & 0x1F;
FP cusum = __shfl_up(x, 1);
// Lane 0 must not initialise cusum
if(laneId == 0) cusum = 0;
for (int i=1; i<=32; i*=2) {
int n = __shfl_up(cusum, i);
if (laneId >= i) cusum += n;
}
return cusum;
}
/**
* Performs an exclusive scan of the true/false predicate
* up the warp and returns the scan value. Consider the
* i-th thread in a warp with i=0, 1, ..., 31
* and suppose that
* predicate = d_predicate[i]
* Then this routine computes
*
* int cusum = 0;
* for(int j=0; j<i; j++)
* cusum += (d_predicate[j] ? 1 : 0);
*
* return cusum;
*
* In other words the routine returns the sum of
* all true predicate values for all preceding threads in the warp.
* The sum does not include the predicate value for this thread.
*/
static __inline__ __device__
int warp_exclusive_scanplus(const bool &predicate)
{
const int tid = threadIdx.x;
const int laneId = tid & 0x1F;
unsigned int ballot = __ballot(predicate);
// A 1 for all preceding threads in the warp
unsigned int mask = (1 << laneId) - 1;
int cusum = __popc( ballot & mask );
return cusum;
}
/**
* Performs an exclusive scan up the block
* and returns the scan value. Consider the
* i-th thread in a block with i=0, 1, ..., blockDim.x-1
* and suppose that
* x = d_x[i]
* Then this routine computes
*
* cusum = 0;
* for(int j=0; j<i; j++)
* cusum += d_x[j];
*
* total = cusum;
* for(int j=i; j<blockDim.x; j++)
* total += d_x[j];
*
* In other words the routine returns the sum of
* all x values for all preceding threads in the block.
* The sum does not include the x value for this thread.
* The value 'total' is the sum over all threads in the block
*
* NB: ALL THREADS MUST CALL THIS SINCE IT HAS __syncthreads
*/
template<typename FP>
static __inline__ __device__
void block_exclusive_scanplus(const FP &x, FP & cusum, FP & total, volatile FP *shmem)
{
cusum = warp_exclusive_scanplus(x);
const int tid = threadIdx.x;
const int nwarps = blockDim.x / warpSize;
const int wid = tid / warpSize;
// This is necessary 'cause we have to wait until all threads
// are finished with the shared mem array
__syncthreads();
// Elect the leader
//int leader = __ffs( __ballot(1) ) - 1;
if(tid % warpSize == warpSize-1)
shmem[wid] = cusum + x;
__syncthreads();
if(tid==0) {
for(int i=1; i<nwarps; i++) {
shmem[i] += shmem[i-1];
}
}
__syncthreads();
FP y = (wid>0 ? shmem[wid-1] : FP(0));
total = shmem[nwarps-1];
cusum += y;
}
/**
* Block reduction function where each
* thread in the block has C independent elements to reduce.
* Only thread 0 in the block receives the final
* answer.
*
* Parameters:
* x[C] :
* On Input, the values to reduce across the thread block
* On Output, threadIdx.x=0 contains the reduced values.
* All other thread in the block have undefined values
* shmem[nwarps*C] :
* Workspace, shared memory with C elements for
* every warp
*/
template<typename FP, int C>
static __inline__ __device__
void block_reduceplus(FP x[], volatile FP * shmem)
{
warp_reduceplus<FP,C>(x);
if(threadIdx.x % warpSize ==0) {
for(int cc=0; cc<C; cc++) {
shmem[C * threadIdx.x/warpSize + cc] = x[cc];
}
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpSize; w++) {
for(int cc=0; cc<C; cc++) {
x[cc] += shmem[C*w + cc];
}
}
}
__syncthreads();
}
template<typename FP, int C>
static __inline__ __device__
void block_reducemax(FP x[], volatile FP * shmem)
{
warp_reducemax<FP,C>(x);
if(threadIdx.x % warpSize ==0) {
for(int cc=0; cc<C; cc++) {
shmem[C * threadIdx.x/warpSize + cc] = x[cc];
}
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpSize; w++) {
for(int cc=0; cc<C; cc++) {
x[cc] = max(x[cc], shmem[C*w + cc] );
}
}
}
__syncthreads();
}
template<typename FP, int C>
static __inline__ __device__
void block_reducemin(FP x[], volatile FP * shmem)
{
warp_reducemin<FP,C>(x);
if(threadIdx.x % warpSize ==0) {
for(int cc=0; cc<C; cc++) {
shmem[C * threadIdx.x/warpSize + cc] = x[cc];
}
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpSize; w++) {
for(int cc=0; cc<C; cc++) {
x[cc] = min(x[cc], shmem[C*w + cc] );
}
}
}
__syncthreads();
}
/**
* Block reduction function. Only thread 0 in
* the block receives the final answer.
*
* Parameters:
* x :
* On Input, the values to reduce across the thread block
* On Output, threadIdx.x=0 contains the reduced values.
* All other thread in the block have undefined values
* shmem[nwarps] :
* Workspace, shared memory with one element for
* every warp
*/
template<typename FP>
static __inline__ __device__
void block_reduceplus(FP &x, volatile FP * shmem)
{
warp_reduceplus<FP,1>(&x);
const int warpsize = 32;
const int tid = threadIdx.x + threadIdx.y*blockDim.x;
const int nthds = blockDim.x*blockDim.y;
if(tid % warpsize ==0) {
shmem[tid/warpsize] = x;
}
__syncthreads();
if(tid==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<nthds / warpsize; w++) {
x += shmem[w];
}
}
__syncthreads();
}
template<typename FP>
static __inline__ __device__
void block_reducemax(FP &x, volatile FP * shmem)
{
warp_reducemax<FP,1>(&x);
const int warpsize = 32;
if(threadIdx.x % warpsize ==0) {
shmem[threadIdx.x/warpsize] = x;
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpsize; w++) {
x = max(x, shmem[w]);
}
}
__syncthreads();
}
template<typename FP>
static __inline__ __device__
void block_reducemin(FP &x, volatile FP * shmem)
{
warp_reducemin<FP,1>(&x);
const int warpsize = 32;
if(threadIdx.x % warpsize ==0) {
shmem[threadIdx.x/warpsize] = x;
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpsize; w++) {
x = min(x, shmem[w]);
}
}
__syncthreads();
}
/*************************** Some specialised reducemax functions *****************************/
/**
* Does a max reduction on the value mx
* while also keeping track of the maximal
* idx.
*
* Parameters :
* mx :
* On Input, the value to reduce
* On Output, the maximal value across all threads in the warp
* idx :
* On Input, an integer associated with mx
* On Output, the integer associated with the maximal mx value
* across all threads in the warp
*/
template<typename FP>
static __inline__ __device__
void warp_allreducemax(FP &mx, int &idx)
{
for(int i=1; i<warpSize; i*=2) {
FP z = __shfl_xor(mx, i);
int zidx = __shfl_xor(idx,i);
if(z > mx) {
mx = z;
idx = zidx;
}
}
}
/**
* Does a max reduction on the value mx
* while also keeping track of the maximal
* idx. Only "thread 0" in the warp gets the
* maximal value and index. This is potentially
* faster than an allreduce
*
* Parameters :
* mx :
* On Input, the value to reduce
* On Output, "thread 0" in the warp has the maximal
* value across all threads in the warp.
* All other threads in the warp have undefined
* values
* idx :
* On Input, an integer associated with mx
* On Output, "thread 0" in the warp has the integer
* associated with the maximal mx value
* across all threads in the warp. All
* other threads in the warp have undefined
* values
*/
template<typename FP>
static __inline__ __device__
void warp_reducemax(FP &mx, int &idx)
{
for(int i=1; i<warpSize; i*=2) {
FP z = __shfl_down(mx, i);
int zidx = __shfl_down(idx,i);
if(z > mx) {
mx = z;
idx = zidx;
}
}
}
/**
* Does a max reduction across all threads in a block on the value mx
* while also keeping track of the maximal
* idx.
*
* Parameters :
* mx :
* On Input, the value to reduce
* On Output, the maximal value across all threads in the block.
* idx :
* On Input, an integer associated with mx
* On Output, the integer associated with the maximal mx value
* across all threads in the block.
* shmem[nwarps] :
* Workspace in shared memory
* ishmem[nwarps] :
* Workspace in shared memory
*/
template<typename FP>
static __inline__ __device__
void block_allreducemax(FP &mx, int &idx, volatile FP * shmem, volatile int * ishmem)
{
warp_reducemax(mx, idx);
const int warpsize = 32;
const int wid = threadIdx.x / warpsize;
const int nwarps = blockDim.x / warpsize;
if(threadIdx.x % warpsize == 0) {
shmem[wid] = mx;
ishmem[wid] = idx;
}
__syncthreads();
if(threadIdx.x == 0) {
for(int i=1; i<nwarps; i++) {
FP x = shmem[i];
if(x > mx) {
mx = x;
idx = ishmem[i];
}
}
shmem[0] = mx;
ishmem[0] = idx;
}
__syncthreads();
mx = shmem[0];
idx = ishmem[0];
__syncthreads();
}
//////////////////////////////////
//
// Fermi - use shared mem
//
/////////////////////////////////
/**
* Performes a warp reduction where the warp can be split
* into teams. Reduction is within a team, not between teams.
* Teams all have the same size, which must
* be a power of 2. This function is indended for Fermi
* cards and requires in input array of shared memory.
*
* Parameters :
* x :
* On Input, a value to reduce
* On Output, the value of the reduction across the team
* shared[teamSz] :
* Workspace, an array of shared memory for this team.
* tid :
* On Input, the id of this thread in the team
* teamSz :
* On Input, the number of threads in each team. Must be
* either 2,4,8,16, or 32
*/
template<typename FP>
static __inline__ __device__
void warp_allreduceplusFermi(FP &x, volatile FP * shared, const int tid=threadIdx.x, int teamSz=32)
{
shared[tid] = x;
if(tid < teamSz/2) {
if(teamSz > 16) shared[tid] += shared[tid + 16];
if(teamSz > 8) shared[tid] += shared[tid + 8];
if(teamSz > 4) shared[tid] += shared[tid + 4];
if(teamSz > 2) shared[tid] += shared[tid + 2];
if(teamSz > 1) shared[tid] += shared[tid + 1];
}
x = shared[0];
}
/**
* As above, but each thread now has C independent elements
* to reduce. This routine is intended for Fermi cards.
*
* Parameters :
* x[C] :
* On Input, the values to reduce
* On Output, x[i] contains the reduction of all x[i] values across
* the team (0 <= i < C)
* shared[teamSz] :
* Workspace, an array of shared memory for this team.
* tid :
* On Input, the id of this thread in the team
* teamSz :
* On Input, the number of threads in each team. Must be
* either 2,4,8,16, or 32
*/
template<typename FP, int C>
static __inline__ __device__
void warp_allreduceplusFermi(FP x[], volatile FP * shared, const int tid = threadIdx.x, int teamSz=32)
{
for(int cc=0; cc<C; cc++) {
warp_allreduceplusFermi<FP>(x[cc], shared, tid, teamSz);
}
}
/**
* Block reduction function where each
* thread in the block has C independent elements to reduce.
* Only thread 0 in the block receives the final
* answer. This routine is for Fermi cards.
*
* NOTE: Must have C <= 32
*
* Parameters:
* x[C] :
* On Input, the values to reduce across the thread block
* On Output, threadIdx.x=0 contains the reduced values.
* All other thread in the block have undefined values
* shmem[nwarps*32] :
* Workspace, shared memory with one element for
* every thread
*/
template<typename FP, int C>
static __inline__ __device__
void block_reduceplusFermi(FP x[], volatile FP * shmem)
{
const int wid = threadIdx.x / warpSize;
const int tid = threadIdx.x % warpSize;
warp_allreduceplusFermi<FP,C>(x, shmem + wid*warpSize, tid);
__syncthreads();
if(tid == 0) {
for(int cc=0; cc<C; cc++) {
shmem[C * wid + cc] = x[cc];
}
}
__syncthreads();
if(threadIdx.x==0) {
// Start reading from 1 so that we don't have to zero x[]
for(int w=1; w<blockDim.x / warpSize; w++) {
for(int cc=0; cc<C; cc++) {
x[cc] += shmem[C*w + cc];
}
}
}
__syncthreads();
}
/**
* Block reduction function.
* Only thread 0 in the block receives the final
* answer. This routine is for Fermi cards.
*
* Parameters:
* x :
* On Input, the values to reduce across the thread block
* On Output, threadIdx.x=0 contains the reduced value.
* All other thread in the block have undefined value
* shmem[nwarps*32] :
* Workspace, shared memory with one element for
* every thread
*/
template<typename FP>
static __inline__ __device__
void block_reduceplusFermi(FP &x, volatile FP * shmem)
{
const int wid = threadIdx.x / warpSize;
const int tid = threadIdx.x % warpSize;
warp_allreduceplusFermi<FP>(x, shmem + wid*warpSize, tid);