forked from trantalaiho/Cuda-Arnoldi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda_reduce.h
973 lines (868 loc) · 32 KB
/
cuda_reduce.h
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
#ifndef CUDA_REDUCE_H_
#define CUDA_REDUCE_H_
/*
* cuda_reduce.h
*
* Created on: 27.3.2012
* Author: Teemu Rantalaiho ([email protected])
*
*
* Copyright 2011-2013 Teemu Rantalaiho
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
*/
//#include "cuda_histogram.h"
// Public APIs
template <int nDim,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE, typename OUTPUTTYPE, typename INDEXT>
static inline
cudaError_t
callReduceKernelNDim(
INPUTTYPE input, TRANSFORMFUNTYPE xformObj, SUMFUNTYPE sumfunObj,
INDEXT* starts, INDEXT* ends, OUTPUTTYPE* out,
int multiReduce = 1, // Convenience API - multiple entries per one input-index ran in parallel (simple multidim)
cudaStream_t stream = 0, bool outInDev = false,
void** tmpbuf = NULL,
size_t* tmpbufSize = NULL,
bool accumulate = true);
template <typename TRANSFORMFUNTYPE, typename INDEXTYPE, typename INPUTTYPE, typename SUMFUNTYPE, typename OUTPUTTYPE>
static inline
cudaError_t callReduceKernel(
INPUTTYPE input, TRANSFORMFUNTYPE xformFunctor, SUMFUNTYPE sumFunctor,
INDEXTYPE start, INDEXTYPE end, OUTPUTTYPE* result,
int multiReduce = 1, // Convenience API - multiple entries per one input-index ran in parallel (simple multidim)
cudaStream_t stream = 0, bool outInDev = false,
void** tmpbuf = NULL,
size_t* tmpbufSize = NULL,
bool accumulate = true);
template <typename TRANSFORMFUNTYPE, typename INDEXTYPE, typename INPUTTYPE, typename SUMFUNTYPE, typename MINUSFUNTYPE, typename OUTPUTTYPE>
static inline
cudaError_t callKahanReduceKernel(
INPUTTYPE input, TRANSFORMFUNTYPE xformFunctor, SUMFUNTYPE sumFunctor, MINUSFUNTYPE minusFunctor,
INDEXTYPE start, INDEXTYPE end, OUTPUTTYPE* result, OUTPUTTYPE zero,
int multiReduce = 1, // Convenience API - multiple entries per one input-index ran in parallel (simple multidim)
cudaStream_t stream = 0, bool outInDev = false,
void** tmpbuf = NULL,
size_t* tmpbufSize = NULL,
bool accumulate = true);
template <int nDim,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE, typename MINUSFUNTYPE, typename OUTPUTTYPE, typename INDEXT>
static inline
cudaError_t
callKahanReduceKernelNDim(
INPUTTYPE input, TRANSFORMFUNTYPE xformObj, SUMFUNTYPE sumfunObj, MINUSFUNTYPE minusFunctor,
INDEXT* starts, INDEXT* ends, OUTPUTTYPE* out, OUTPUTTYPE zero,
int multiReduce = 1,
cudaStream_t stream = 0, bool outInDev = false,
void** tmpbuf = NULL,
size_t* tmpbufSize = NULL,
bool accumulate = true);
// End public APIs
#define REDUCE_BLOCK_SIZE_LOG2 7
#define REDUCE_BLOCK_SIZE (1 << REDUCE_BLOCK_SIZE_LOG2)
#define MAX_reduce_STEPS 2048
#define R_ERROR_CHECKS 0
#define R_PRINT_ALL_MALLOCS 0
#if R_PRINT_ALL_MALLOCS
#define PRINT_MALLOC(BUFFER) printf("%p cudaMalloc'd(%s) at line %d\n", (BUFFER), #BUFFER , __LINE__)
#define PRINT_FREE(BUFFER) printf("%p cudaFree'd(%s) at line %d\n", (BUFFER), #BUFFER , __LINE__)
#else
#define PRINT_MALLOC(BUFFER)
#define PRINT_FREE(BUFFER)
#endif
#ifdef UNROLL_NLOG2_CUDA_STEPS
#define REDUCE_UNROLL_LOG2 UNROLL_NLOG2_CUDA_STEPS
#else
#define REDUCE_UNROLL_LOG2 1
#endif
#define REDUCE_UNROLL (1 << REDUCE_UNROLL_LOG2)
#if R_ERROR_CHECKS || R_PRINT_ALL_MALLOCS
#include <stdio.h>
#endif
#define FINAL_SUM_BLOCK_SIZE 64
template <typename SUMFUNTYPE, typename OUTPUTTYPE>
__global__
void finalSumKernel(SUMFUNTYPE sumfunObj, OUTPUTTYPE* blockOut, int maxblocks, OUTPUTTYPE* devout, bool accumulate)
{
int myIdx = threadIdx.x;
OUTPUTTYPE res;
OUTPUTTYPE dvout;
if (devout || !accumulate){
maxblocks--;
if (devout)
dvout = *devout;
}
if (maxblocks < 31 /*blockDim.x*/){
if (threadIdx.x == 0){
res = blockOut[0];
if (devout && accumulate)
res = sumfunObj(res, dvout);
for (int i = 1; i <= maxblocks; i++)
res = sumfunObj(res, blockOut[i]);
if (devout)
*devout = res;
else
blockOut[0] = res;
}
return;
}
if (myIdx <= maxblocks){
res = blockOut[myIdx];
myIdx += FINAL_SUM_BLOCK_SIZE;
}
while (myIdx <= maxblocks)
{
res = sumfunObj(res, blockOut[myIdx]);
myIdx += FINAL_SUM_BLOCK_SIZE;
}
{
__shared__ OUTPUTTYPE shRes[64];
shRes[threadIdx.x] = res;
__syncthreads();
if (threadIdx.x < 32 && (threadIdx.x + 32) <= maxblocks) shRes[threadIdx.x] = sumfunObj(shRes[threadIdx.x + 32], shRes[threadIdx.x]);
__threadfence_block();
if (threadIdx.x < 16) shRes[threadIdx.x] = sumfunObj(shRes[threadIdx.x + 16], shRes[threadIdx.x]);
__threadfence_block();
if (threadIdx.x < 8) shRes[threadIdx.x] = sumfunObj(shRes[threadIdx.x + 8], shRes[threadIdx.x]);
__threadfence_block();
if (threadIdx.x < 4) shRes[threadIdx.x] = sumfunObj(shRes[threadIdx.x + 4], shRes[threadIdx.x]);
__threadfence_block();
if (threadIdx.x < 2) shRes[threadIdx.x] = sumfunObj(shRes[threadIdx.x + 2], shRes[threadIdx.x]);
__threadfence_block();
if (threadIdx.x == 0) res = sumfunObj(shRes[threadIdx.x + 1], shRes[threadIdx.x]);
__threadfence_block();
}
if (threadIdx.x == 0){
if (devout){
if (accumulate)
*devout = sumfunObj(dvout, res);
else
*devout = res;
}
else{
blockOut[0] = res;
}
}
}
template <bool firstPass, bool lastSteps, /*int nMultires,*/
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE, typename OUTPUTTYPE, typename INDEXT>
static inline __device__
void histoKernel_reduceStep(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT myStart, INDEXT end,
OUTPUTTYPE* myRes)
{
if (lastSteps)
{
if (myStart < end)
{
if (firstPass)
*myRes = xformObj(input, myStart, blockIdx.y);
else
*myRes = sumfunObj(xformObj(input, myStart, blockIdx.y), *myRes);
}
}
else
{
if (firstPass)
*myRes = xformObj(input, myStart, blockIdx.y);
else
*myRes = sumfunObj(xformObj(input, myStart, blockIdx.y), *myRes);
}
}
template <bool lastSteps,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename OUTPUTTYPE, typename INDEXT>
__global__
void histoKernel_reduce(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT start, INDEXT end,
OUTPUTTYPE* blockOut, int maxblocks,
int nSteps, bool first)
{
// Take care with extern - In order to have two instances of this template the
// type of the extern variables cannot change
// (ie. cannot use "extern __shared__ OUTPUTTYPE bins[]")
extern __shared__ int cudahistogram_allbinstmp[];
OUTPUTTYPE* allbins = (OUTPUTTYPE*)&(*cudahistogram_allbinstmp);
OUTPUTTYPE myres;
OUTPUTTYPE* ourOut = &blockOut[blockIdx.x + blockIdx.y * gridDim.x];
// Now our block handles a continuos lump from myStart to myStart + nThreads*nSteps
// Let's change it to go in thread-linear order from myStart jumping over all the blocks in each step
// Therefore we start from 'start' + blockId * nThreads + tid, and we walk with stride nThreads * nBlocks
int stride = gridDim.x << REDUCE_BLOCK_SIZE_LOG2;
INDEXT myStart = start + (INDEXT)((blockIdx.x) << REDUCE_BLOCK_SIZE_LOG2) + (INDEXT)threadIdx.x;
// Run loops - unroll 8 steps manually
if (nSteps > 0){
histoKernel_reduceStep<true, lastSteps>(input, xformObj, sumfunObj, myStart, end, &myres);
myStart += stride;
nSteps--;
}
if (nSteps > 0){
int doNSteps = (nSteps) >> REDUCE_UNROLL_LOG2;
if (lastSteps){
while (myStart + (doNSteps * stride << REDUCE_UNROLL_LOG2) >= end){
doNSteps--;
}
}
for (int step = 0; step < doNSteps; step++)
{
#pragma unroll
for (int substep = 0; substep < REDUCE_UNROLL; substep++){
histoKernel_reduceStep<false, false>(input, xformObj, sumfunObj, myStart, end, &myres);
myStart += stride;
}
}
int nStepsLeft = (nSteps) - (doNSteps << REDUCE_UNROLL_LOG2);
for (int step = 0; step < nStepsLeft; step++)
{
histoKernel_reduceStep<false, lastSteps>(input, xformObj, sumfunObj, myStart, end, &myres);
myStart += stride;
}
}
#if 1
{
OUTPUTTYPE result;
if (!first && threadIdx.x == 0)
result = ourOut[0];
allbins[threadIdx.x] = myres;
// In the end combine results:
#if REDUCE_BLOCK_SIZE > 32
__syncthreads();
#endif
if (lastSteps && start + (INDEXT)((blockIdx.x + 1) << REDUCE_BLOCK_SIZE_LOG2) >= end)
{
// Safepath for last steps
if (threadIdx.x == 0){
INDEXT limit = end - (start + (INDEXT)((blockIdx.x) << REDUCE_BLOCK_SIZE_LOG2));
//if (limit == 2) myres = sumfunObj(myres, myres);
for (int tid = 1; tid < limit; tid++)
myres = sumfunObj(allbins[tid], myres);
}
}
else
{
// Example REDUCE_BLOCK_SIZE == 256
#if REDUCE_BLOCK_SIZE >= 128
int limit = REDUCE_BLOCK_SIZE >> 1; // Limit = 128
if (threadIdx.x < limit) // For all i < 128 Add a[i] <- a[i] + a[i+128]
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + limit]);
else
return; // Note - exited warps can't hang execution
limit >>= 1; // Limit = 64
int looplimit = ((REDUCE_BLOCK_SIZE_LOG2 - 2)); // Looplimit = 6
#pragma unroll
for (int loop = 5; loop < looplimit; loop++){ // One iteration of loop
__syncthreads(); // 1: For all i add a[i] <- a[i] + a[i + 64]
if (threadIdx.x < limit)
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + limit]);
limit >>= 1; // Limit = 32
}
#endif
if (threadIdx.x >= 32) return;
__syncthreads();
// Unroll rest manually
#if REDUCE_BLOCK_SIZE > 32
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 32]);
__threadfence_block();
#endif
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 16]);
__threadfence_block();
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 8]);
__threadfence_block();
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 4]);
__threadfence_block();
allbins[threadIdx.x] = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 2]);
__threadfence_block();
myres = sumfunObj(allbins[threadIdx.x], allbins[threadIdx.x + 1]);
}
if (threadIdx.x == 0){
if (first)
result = myres;
else
result = sumfunObj(myres, result);
ourOut[0] = result;
}
}
#endif
}
template <typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename OUTPUTTYPE, typename INDEXT>
static
void callReduceImpl(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT start, INDEXT end,
OUTPUTTYPE* out,
cudaDeviceProp* props,
cudaStream_t stream,
bool outInDev,
int multiReduce,
void** tmpbuf,
size_t* tmpbufSize, bool accumulate)
{
INDEXT size = end - start;
if (end <= start)
{
return;
}
int maxblocks = 64;
if (props) maxblocks = props->multiProcessorCount * 4;
//int maxblocks = 16;
if (size > 2*1024*1024 && multiReduce < 2){
maxblocks *= 2;
}
if ((maxblocks << REDUCE_BLOCK_SIZE_LOG2) >= size){
maxblocks = size >> REDUCE_BLOCK_SIZE_LOG2;
if ((maxblocks << REDUCE_BLOCK_SIZE_LOG2) < size)
maxblocks++;
}
OUTPUTTYPE* tmpOut;
// Check whether user has supplied a temporary buffer - these can be really useful, as
// any allocation or free implies a cpu-gpu synchronization
{
if (tmpbuf && tmpbufSize){
if (*tmpbuf && *tmpbufSize >= (maxblocks*multiReduce + 1) * sizeof(OUTPUTTYPE)){
tmpOut = (OUTPUTTYPE*)(*tmpbuf);
}
else {
if (*tmpbuf){
cudaFree(*tmpbuf);
PRINT_FREE(*tmpbuf);
}
*tmpbufSize = (maxblocks*multiReduce + 1) * sizeof(OUTPUTTYPE);
cudaMalloc(tmpbuf, *tmpbufSize);
PRINT_MALLOC(*tmpbuf);
tmpOut = (OUTPUTTYPE*)(*tmpbuf);
}
}
else {
cudaMalloc((void**)&tmpOut, (maxblocks*multiReduce + 1) * sizeof(OUTPUTTYPE));
PRINT_MALLOC(tmpOut);
}
}
int sharedNeeded;
{
int typesize = sizeof(OUTPUTTYPE);
sharedNeeded = (typesize) << (REDUCE_BLOCK_SIZE_LOG2);
//printf("reduce-bin, generic, Shared needed = %d\n", sharedNeeded);
}
// Determine number of local variables
// reduce_LOCALLIMIT is total local size available for one block:
int nSteps = size / (maxblocks << REDUCE_BLOCK_SIZE_LOG2);
if (nSteps * maxblocks * REDUCE_BLOCK_SIZE < size) nSteps++;
if (nSteps > MAX_reduce_STEPS) nSteps = MAX_reduce_STEPS;
int nFullSteps = size / (nSteps * maxblocks * REDUCE_BLOCK_SIZE);
dim3 grid(maxblocks, multiReduce, 1);
dim3 block = REDUCE_BLOCK_SIZE;
bool first = true;
for (int i = 0; i < nFullSteps; i++)
{
histoKernel_reduce<false><<<grid, block, sharedNeeded, stream>>>(
input, xformObj, sumfunObj, start, end, tmpOut, maxblocks, nSteps, i == 0);
first = false;
start += nSteps * maxblocks * REDUCE_BLOCK_SIZE;
#if R_ERROR_CHECKS
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess)
printf("Cudaerror = %s\n", cudaGetErrorString( error ));
#endif
}
size = end - start;
if (size > 0)
{
// Do what steps we still can do without checks
nSteps = size / (maxblocks << REDUCE_BLOCK_SIZE_LOG2);
if (nSteps * (maxblocks << REDUCE_BLOCK_SIZE_LOG2) < size) nSteps++;
if (nSteps > 0)
{
histoKernel_reduce<true><<<grid, block, sharedNeeded, stream>>>(
input, xformObj, sumfunObj, start, end, tmpOut, maxblocks, nSteps, first);
start += nSteps * maxblocks * REDUCE_BLOCK_SIZE;
first = false;
}
}
#if R_ERROR_CHECKS
{
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess)
printf("Cudaerror = %s\n", cudaGetErrorString( error ));
}
#endif
// Finally put together the result:
#if 1
if (!outInDev && accumulate){
if (stream != 0)
cudaMemcpyAsync(&tmpOut[maxblocks*multiReduce], out, sizeof(OUTPUTTYPE), cudaMemcpyHostToDevice, stream);
else
cudaMemcpy(&tmpOut[maxblocks*multiReduce], out, sizeof(OUTPUTTYPE), cudaMemcpyHostToDevice);
}
// Let's do so that one block handles one bin
grid.x = 1;
grid.y = 1;
//grid.x = nOut >> REDUCE_BLOCK_SIZE_LOG2;
//if ((grid.x << REDUCE_BLOCK_SIZE_LOG2) < nOut) grid.x++;
block.x = FINAL_SUM_BLOCK_SIZE;
finalSumKernel<<<grid, block, 0, stream>>>(sumfunObj, tmpOut, maxblocks*multiReduce, outInDev ? out : NULL, accumulate);
#if R_ERROR_CHECKS
{
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess)
printf("Cudaerror (finalsumkernel) = %s\n", cudaGetErrorString( error ));
}
#endif
// TODO: Use async copy for the results as well?
if (!outInDev){
if (stream != 0)
cudaMemcpyAsync(out, tmpOut, sizeof(OUTPUTTYPE), cudaMemcpyDeviceToHost, stream);
else
cudaMemcpy(out, tmpOut, sizeof(OUTPUTTYPE), cudaMemcpyDeviceToHost);
}
#else
{
int i;
OUTPUTTYPE* h_tmp = (OUTPUTTYPE*)malloc(maxblocks * sizeof(OUTPUTTYPE));
//parallel_copy(h_tmp, MemType_HOST, tmpOut, MemType_DEV, n * nOut * sizeof(OUTPUTTYPE));
cudaMemcpy(h_tmp, tmpOut, maxblocks*sizeof(OUTPUTTYPE), cudaMemcpyDeviceToHost);
{
OUTPUTTYPE res = *out;
for (i = 0; i < maxblocks; i++)
{
res = sumfunObj(res, h_tmp[i]);
}
*out = res;
}
free(h_tmp);
}
#endif
if (!(tmpbuf && tmpbufSize)){
cudaFree(tmpOut);
PRINT_FREE(tmpOut);
}
}
template <typename SUMFUNTYPE, typename OUTPUTTYPE, typename MINUSFUNTYPE>
__global__
void finalKahanKernel(SUMFUNTYPE sumfunObj, OUTPUTTYPE* blockOut, int maxblocks, MINUSFUNTYPE minusFun, OUTPUTTYPE zero)
{
OUTPUTTYPE res;
if (threadIdx.x == 0){
res = zero;
OUTPUTTYPE c = zero;
for (int i = 0; i <= maxblocks; i++){
OUTPUTTYPE y = minusFun(blockOut[i], c);
OUTPUTTYPE t = sumfunObj(res, y);
c = minusFun(minusFun(t, res), y);
res = t;
}
blockOut[0] = res;
}
return;
}
template <bool lastSteps, /*int nMultires,*/
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE, typename OUTPUTTYPE, typename INDEXT, typename MINUSFUNTYPE>
static inline __device__
void kahanKernel_reduceStep(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT myStart, INDEXT end,
OUTPUTTYPE* myRes,
OUTPUTTYPE* c,
MINUSFUNTYPE minusFun)
{
if (lastSteps)
{
if (myStart < end)
{
OUTPUTTYPE y = minusFun(xformObj(input, myStart, blockIdx.y), *c);
OUTPUTTYPE t = sumfunObj(y, *myRes);
*c = minusFun( minusFun( t, *myRes ), y);
*myRes = t;
}
}
else
{
// NOTE: We need the if (1) here to get loop unrolling out of the compiler (at least nvcc 3.2.16)
if (1){
OUTPUTTYPE y = minusFun(xformObj(input, myStart, blockIdx.y), *c);
OUTPUTTYPE t = sumfunObj(y, *myRes);
*c = minusFun( minusFun( t, *myRes ), y);
*myRes = t;
}
}
}
template <bool lastSteps,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename OUTPUTTYPE, typename INDEXT, typename MINUSFUNTYPE>
__global__
void kahanKernel_reduce(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT start, INDEXT end,
OUTPUTTYPE* blockOut, int maxblocks,
int nSteps, bool first,
MINUSFUNTYPE minusFunObj, OUTPUTTYPE zero)
{
// Take care with extern - In order to have two instances of this template the
// type of the extern variables cannot change
// (ie. cannot use "extern __shared__ OUTPUTTYPE bins[]")
extern __shared__ int cudahistogram_allbinstmp[];
OUTPUTTYPE* allbins = (OUTPUTTYPE*)&(*cudahistogram_allbinstmp);
OUTPUTTYPE myres = zero;
OUTPUTTYPE c = zero;
OUTPUTTYPE* ourOut = &blockOut[blockIdx.x + blockIdx.y * gridDim.x];
// Now our block handles a continuos lump from myStart to myStart + nThreads*nSteps
// Let's change it to go in thread-linear order from myStart jumping over all the blocks in each step
// Therefore we start from 'start' + blockId * nThreads + tid, and we walk with stride nThreads * nBlocks
int stride = gridDim.x << REDUCE_BLOCK_SIZE_LOG2;
INDEXT myStart = start + (INDEXT)((blockIdx.x) << REDUCE_BLOCK_SIZE_LOG2) + (INDEXT)threadIdx.x;
// Run loops - unroll 8 steps manually
if (nSteps > 0){
int doNSteps = (nSteps) >> REDUCE_UNROLL_LOG2;
if (lastSteps){
while (myStart + (doNSteps * stride << REDUCE_UNROLL_LOG2) >= end){
doNSteps--;
}
}
for (int step = 0; step < doNSteps; step++)
{
#pragma unroll
for (int substep = 0; substep < REDUCE_UNROLL; substep++){
kahanKernel_reduceStep<false>(input, xformObj, sumfunObj, myStart, end, &myres, &c, minusFunObj);
myStart += stride;
}
}
int nStepsLeft = (nSteps) - (doNSteps << REDUCE_UNROLL_LOG2);
for (int step = 0; step < nStepsLeft; step++)
{
kahanKernel_reduceStep<lastSteps>(input, xformObj, sumfunObj, myStart, end, &myres, &c, minusFunObj);
myStart += stride;
}
}
#if 1
{
OUTPUTTYPE result;
if (!first && threadIdx.x == 0)
result = ourOut[0];
allbins[threadIdx.x] = myres;
// In the end combine results:
#if REDUCE_BLOCK_SIZE > 32
__syncthreads();
#endif
// Safepath for last steps
if (threadIdx.x == 0){
INDEXT limit = end - (start + (INDEXT)((blockIdx.x) << REDUCE_BLOCK_SIZE_LOG2));
if (limit > REDUCE_BLOCK_SIZE) limit = REDUCE_BLOCK_SIZE;
//if (limit == 2) myres = sumfunObj(myres, myres);
for (int tid = 1; tid < limit; tid++){
OUTPUTTYPE y = minusFunObj(allbins[tid], c);
OUTPUTTYPE t = sumfunObj(y, myres);
c = minusFunObj( minusFunObj( t, myres ), y);
myres = t;
//myres = sumfunObj(allbins[tid], myres);
}
}
if (threadIdx.x == 0){
if (first){
result = myres;
} else {
OUTPUTTYPE y = minusFunObj(result, c);
result = sumfunObj(myres, y);
}
ourOut[0] = result;
}
}
#endif
}
template <typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename OUTPUTTYPE, typename INDEXT, typename MINUSFUNTYPE>
static
void callKahanReduceImpl(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT start, INDEXT end,
OUTPUTTYPE* out,
cudaDeviceProp* props,
cudaStream_t stream,
bool outInDev,
MINUSFUNTYPE minusFunObj, OUTPUTTYPE zero,
int multidim,
void** tmpbuf,
size_t* tmpbufSize,
bool accumulate
)
{
INDEXT size = end - start;
if (end <= start)
{
return;
}
int maxblocks = 32;
if (props) maxblocks = props->multiProcessorCount * 4;
//int maxblocks = 16;
if (size > 2*1024*1024 && multidim < 2){
maxblocks *= 2;
}
if ((maxblocks << REDUCE_BLOCK_SIZE_LOG2) >= size){
maxblocks = size >> REDUCE_BLOCK_SIZE_LOG2;
if ((maxblocks << REDUCE_BLOCK_SIZE_LOG2) < size)
maxblocks++;
}
// TODO: Magic constants..
// With low bin-counts and large problems it seems beneficial to use
// more blocks...
/* if (size > 2*4096*4096)
maxblocks *= 2;*/
//printf("maxblocks = %d\n", maxblocks);
OUTPUTTYPE* tmpOut;
cudaMalloc((void**)&tmpOut, (maxblocks*multidim + 1) * sizeof(OUTPUTTYPE));
PRINT_MALLOC(tmpOut);
int sharedNeeded;
{
int typesize = sizeof(OUTPUTTYPE);
sharedNeeded = (typesize) << (REDUCE_BLOCK_SIZE_LOG2);
//printf("reduce-bin, generic, Shared needed = %d\n", sharedNeeded);
}
// Determine number of local variables
// reduce_LOCALLIMIT is total local size available for one block:
int nSteps = size / (maxblocks << REDUCE_BLOCK_SIZE_LOG2);
if (nSteps * maxblocks * REDUCE_BLOCK_SIZE < size) nSteps++;
if (nSteps > MAX_reduce_STEPS) nSteps = MAX_reduce_STEPS;
int nFullSteps = size / (nSteps * maxblocks * REDUCE_BLOCK_SIZE);
dim3 grid(maxblocks, multidim, 1);
dim3 block = REDUCE_BLOCK_SIZE;
bool first = true;
for (int i = 0; i < nFullSteps; i++)
{
kahanKernel_reduce<false><<<grid, block, sharedNeeded, stream>>>(
input, xformObj, sumfunObj, start, end, tmpOut, maxblocks, nSteps, i == 0, minusFunObj, zero);
first = false;
start += nSteps * maxblocks * REDUCE_BLOCK_SIZE;
#if R_ERROR_CHECKS
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess)
printf("Cudaerror = %s\n", cudaGetErrorString( error ));
#endif
}
size = end - start;
if (size > 0)
{
// Do what steps we still can do without checks
nSteps = size / (maxblocks << REDUCE_BLOCK_SIZE_LOG2);
if (nSteps * (maxblocks << REDUCE_BLOCK_SIZE_LOG2) < size) nSteps++;
if (nSteps > 0)
{
kahanKernel_reduce<true><<<grid, block, sharedNeeded, stream>>>(
input, xformObj, sumfunObj, start, end, tmpOut, maxblocks, nSteps, first, minusFunObj, zero);
start += nSteps * maxblocks * REDUCE_BLOCK_SIZE;
first = false;
}
}
#if R_ERROR_CHECKS
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess)
printf("Cudaerror = %s\n", cudaGetErrorString( error ));
#endif
// Finally put together the result:
#if 1
enum cudaMemcpyKind fromOut = outInDev ? cudaMemcpyDeviceToDevice : cudaMemcpyHostToDevice;
enum cudaMemcpyKind toOut = outInDev ? cudaMemcpyDeviceToDevice : cudaMemcpyDeviceToHost;
if (stream != 0)
cudaMemcpyAsync(&tmpOut[maxblocks*multidim], out, sizeof(OUTPUTTYPE), fromOut, stream);
else
cudaMemcpy(&tmpOut[maxblocks*multidim], out, sizeof(OUTPUTTYPE), fromOut);
// Let's do so that one block handles one bin
grid.x = 1;
grid.y = 1;
//grid.x = nOut >> REDUCE_BLOCK_SIZE_LOG2;
//if ((grid.x << REDUCE_BLOCK_SIZE_LOG2) < nOut) grid.x++;
block.x = FINAL_SUM_BLOCK_SIZE;
finalKahanKernel<<<grid, block, 0, stream>>>(sumfunObj, tmpOut, maxblocks*multidim, minusFunObj, zero);
// TODO: Use async copy for the results as well?
if (stream != 0)
cudaMemcpyAsync(out, tmpOut, sizeof(OUTPUTTYPE), toOut, stream);
else
cudaMemcpy(out, tmpOut, sizeof(OUTPUTTYPE), toOut);
#else
{
int i;
OUTPUTTYPE* h_tmp = (OUTPUTTYPE*)malloc(maxblocks * sizeof(OUTPUTTYPE));
//parallel_copy(h_tmp, MemType_HOST, tmpOut, MemType_DEV, n * nOut * sizeof(OUTPUTTYPE));
cudaMemcpy(h_tmp, tmpOut, maxblocks*sizeof(OUTPUTTYPE), cudaMemcpyDeviceToHost);
{
OUTPUTTYPE res = *out;
for (i = 0; i < maxblocks; i++)
{
res = sumfunObj(res, h_tmp[i]);
}
*out = res;
}
free(h_tmp);
}
#endif
cudaFree(tmpOut);
PRINT_FREE(tmpOut);
}
template <typename TRANSFORMFUNTYPE, typename INDEXTYPE, typename INPUTTYPE, typename SUMFUNTYPE, typename OUTPUTTYPE>
cudaError_t callReduceKernel(
INPUTTYPE input,
TRANSFORMFUNTYPE xformFunctor,
SUMFUNTYPE sumFunctor,
INDEXTYPE start, INDEXTYPE end,
OUTPUTTYPE* result,
int multiReduce, // Convenience API - multiple entries per one input-index ran in parallel (simple multidim)
cudaStream_t stream, bool outInDev,
void** tmpbuf,
size_t* tmpbufSize,bool accumulate)
{
callReduceImpl(input, xformFunctor, sumFunctor, start, end, result, NULL, stream, outInDev, multiReduce, tmpbuf, tmpbufSize, accumulate);
return cudaSuccess;
}
template <typename TRANSFORMFUNTYPE, typename INDEXTYPE, typename INPUTTYPE, typename SUMFUNTYPE, typename MINUSFUNTYPE, typename OUTPUTTYPE>
cudaError_t callKahanReduceKernel(
INPUTTYPE input,
TRANSFORMFUNTYPE xformFunctor,
SUMFUNTYPE sumFunctor,
MINUSFUNTYPE minusFunctor,
INDEXTYPE start, INDEXTYPE end,
OUTPUTTYPE* result,
OUTPUTTYPE zero,
int multiReduce, // Convenience API - multiple entries per one input-index ran in parallel (simple multidim)
cudaStream_t stream, bool outInDev,
void** tmpbuf,
size_t* tmpbufSize,
bool accumulate)
{
callKahanReduceImpl(input, xformFunctor, sumFunctor, start, end, result, NULL, stream, outInDev, minusFunctor, zero, multiReduce, tmpbuf, tmpbufSize, accumulate);
return cudaSuccess;
}
template <typename nDimIndexFun, int nDim, typename USERINPUTTYPE, typename INDEXT, typename OUTPUTTYPE>
class wrapReduceInput
{
public:
nDimIndexFun userIndexFun;
INDEXT starts[nDim];
//int ends[nDim];
INDEXT sizes[nDim];
float invSizes[nDim];
__host__ __device__
inline
OUTPUTTYPE operator() (USERINPUTTYPE input, INDEXT i, int multiIndex) const {
INDEXT coords[nDim];
INDEXT tmpi = i;
#pragma unroll
for (int d=0; d < nDim - 1; d++)
{
// Example of how this logic works - imagine a cube of (10,100,1000), and take index 123 456
// newI = 123 456 / 10 = 12 345, offset = 123 456 - 123 450 = 6 (this is our first coordinate!),
// newI = 12 345 / 100 = 123, offset = 12 345 - 12 300 = 45 (this is our second coordinate!),
// newI = 123 / 1000 = 0, offset = 123 - 0 = 123 (this is our last coordinate!)
// Result = [123, 45, 6]
INDEXT newI = (INDEXT)((float)tmpi * invSizes[d]);
INDEXT offset = tmpi - newI * sizes[d];
coords[d] = starts[d] + offset;
tmpi = newI;
}
coords[nDim - 1] = starts[nDim - 1] + tmpi;
// Now just call wrapped functor with right coordinate values
return userIndexFun(input, coords, multiIndex);
}
};
template <int nDim,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename OUTPUTTYPE, typename INDEXT>
cudaError_t
callReduceKernelNDim(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
INDEXT* starts, INDEXT* ends,
OUTPUTTYPE* out,
int multiReduce,
cudaStream_t stream, bool outInDev,
void** tmpbuf,
size_t* tmpbufSize,
bool accumulate)
{
wrapReduceInput<TRANSFORMFUNTYPE, nDim, INPUTTYPE, INDEXT, OUTPUTTYPE> wrapInput;
INDEXT start = 0;
INDEXT size = 1;
for (int d = 0; d < nDim; d++)
{
wrapInput.starts[d] = starts[d];
wrapInput.sizes[d] = ends[d] - starts[d];
wrapInput.invSizes[d] = (float)(1.0 / ((double)wrapInput.sizes[d]));
// Example: starts = [3, 10, 23], sizes = [10, 100, 1000]
// start = 3 * 1 = 3, size = 10
// start = 3 + 10 * 10 = 103, size = 10*100 = 1000
// start = 103 + 1000*23 = 23 103, size = 1000*1000 = 1 000 000
start += starts[d] * size;
size *= wrapInput.sizes[d];
if (ends[d] <= starts[d]) return cudaSuccess;
}
wrapInput.userIndexFun = xformObj;
INDEXT end = start + size;
return callReduceKernel(input, wrapInput, sumfunObj, start, end, out, multiReduce, stream, outInDev, tmpbuf, tmpbufSize, accumulate);
}
template <int nDim,
typename INPUTTYPE, typename TRANSFORMFUNTYPE, typename SUMFUNTYPE,
typename MINUSFUNTYPE, typename OUTPUTTYPE, typename INDEXT>
cudaError_t
callKahanReduceKernelNDim(
INPUTTYPE input,
TRANSFORMFUNTYPE xformObj,
SUMFUNTYPE sumfunObj,
MINUSFUNTYPE minusFunctor,
INDEXT* starts, INDEXT* ends,
OUTPUTTYPE* out,
OUTPUTTYPE zero,
int multiReduce,
cudaStream_t stream, bool outInDev,
void** tmpbuf,
size_t* tmpbufSize,
bool accumulate)
{
wrapReduceInput<TRANSFORMFUNTYPE, nDim, INPUTTYPE, INDEXT, OUTPUTTYPE> wrapInput;
INDEXT start = 0;
INDEXT size = 1;
for (int d = 0; d < nDim; d++)
{
wrapInput.starts[d] = starts[d];
wrapInput.sizes[d] = ends[d] - starts[d];
// Example: starts = [3, 10, 23], sizes = [10, 100, 1000]
// start = 3 * 1 = 3, size = 10
// start = 3 + 10 * 10 = 103, size = 10*100 = 1000
// start = 103 + 1000*23 = 23 103, size = 1000*1000 = 1 000 000
start += starts[d] * size;
size *= wrapInput.sizes[d];
if (ends[d] <= starts[d]) return cudaSuccess;
}
wrapInput.userIndexFun = xformObj;
INDEXT end = start + size;
return callKahanReduceKernel(input, wrapInput, sumfunObj, minusFunctor, start, end, out, zero, multiReduce, stream, outInDev, tmpbuf, tmpbufSize, accumulate);
}
#undef REDUCE_BLOCK_SIZE_LOG2
#undef REDUCE_BLOCK_SIZE
#undef MAX_reduce_STEPS
#undef R_ERROR_CHECKS
#endif // CUDA_REDUCE_H_