forked from mattdean1/cuda
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprefix_sum.cu
505 lines (402 loc) · 12.8 KB
/
prefix_sum.cu
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
// small change by Lixin to fit pytorch's API
/*
Matt Dean - 1422434 - mxd434
Goals implemented:
- Block scan for arbitrary length small vectors - 'blockscan' function
- Full scan for arbitrary length large vectors - 'scan' function
This function decides whether to perform a small (one block) scan or a full (n-level) scan depending on the length of the input vector
- BCAO for both scans
Hardware:
CPU - Intel Core i5-4670k @ 3.4GHz
GPU - NVIDIA GeForce GTX 760
Timings:
10,000,000 Elements
host : 20749 ms
gpu : 7.860768 ms
gpu bcao : 4.304064 ms
For more results please see the comment at the bottom of this file
Extra work:
Due to the recursive nature of the full scan it can handle n > 3 levels
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "device_functions.h"
#include "prefix_sum.h"
// scan.cuh
void sequential_scan(int* output, int* input, int length);
void blockscan(int *output, int *input, int length, bool bcao);
void scan(int *output, int *input, int length, bool bcao);
void scanLargeDeviceArray(int *output, int *input, int length, bool bcao);
void scanSmallDeviceArray(int *d_out, int *d_in, int length, bool bcao);
void scanLargeEvenDeviceArray(int *output, int *input, int length, bool bcao);
// kernels.cuh
__global__ void prescan_arbitrary(int *output, int *input, int n, int powerOfTwo);
__global__ void prescan_arbitrary_unoptimized(int *output, int *input, int n, int powerOfTwo);
__global__ void prescan_large(int *output, int *input, int n, int* sums);
__global__ void prescan_large_unoptimized(int *output, int *input, int n, int *sums);
__global__ void add(int *output, int length, int *n1);
__global__ void add(int *output, int length, int *n1, int *n2);
// utils.h
void _checkCudaError(const char *message, cudaError_t err, const char *caller);
void printResult(const char* prefix, int result, long nanoseconds);
void printResult(const char* prefix, int result, float milliseconds);
bool isPowerOfTwo(int x);
int nextPowerOfTwo(int x);
long get_nanos();
/*///////////////////////////////////*/
/* New API */
/*///////////////////////////////////*/
void PrefixSumCUDA(
const at::Tensor grid_cnt,
int num_grids,
at::Tensor grid_off) {
scan(
grid_off.contiguous().data_ptr<int>(),
grid_cnt.contiguous().data_ptr<int>(),
num_grids,
true
);
return;
}
void PrefixSumCPU(
const at::Tensor grid_cnt,
int num_grids,
at::Tensor grid_off) {
sequential_scan(
grid_off.contiguous().data_ptr<int>(),
grid_cnt.contiguous().data_ptr<int>(),
num_grids
);
return;
}
/*///////////////////////////////////*/
/* scan.cu */
/*///////////////////////////////////*/
#define checkCudaError(o, l) _checkCudaError(o, l, __func__)
int THREADS_PER_BLOCK = 512;
int ELEMENTS_PER_BLOCK = THREADS_PER_BLOCK * 2;
void sequential_scan(int* output, int* input, int length) {
output[0] = 0; // since this is a prescan, not a scan
for (int j = 1; j < length; ++j)
{
output[j] = input[j - 1] + output[j - 1];
}
return;
}
void blockscan(int *d_out, int *d_in, int length, bool bcao) {
int powerOfTwo = nextPowerOfTwo(length);
if (bcao) {
prescan_arbitrary<<<1, (length + 1) / 2, 2 * powerOfTwo * sizeof(int)>>>(d_out, d_in, length, powerOfTwo);
}
else {
prescan_arbitrary_unoptimized<<<1, (length + 1) / 2, 2 * powerOfTwo * sizeof(int)>>>(d_out, d_in, length, powerOfTwo);
}
return;
}
void scan(int *d_out, int *d_in, int length, bool bcao) {
if (length > ELEMENTS_PER_BLOCK) {
scanLargeDeviceArray(d_out, d_in, length, bcao);
}
else {
scanSmallDeviceArray(d_out, d_in, length, bcao);
}
return;
}
void scanLargeDeviceArray(int *d_out, int *d_in, int length, bool bcao) {
int remainder = length % (ELEMENTS_PER_BLOCK);
if (remainder == 0) {
scanLargeEvenDeviceArray(d_out, d_in, length, bcao);
}
else {
// perform a large scan on a compatible multiple of elements
int lengthMultiple = length - remainder;
scanLargeEvenDeviceArray(d_out, d_in, lengthMultiple, bcao);
// scan the remaining elements and add the (inclusive) last element of the large scan to this
int *startOfOutputArray = &(d_out[lengthMultiple]);
scanSmallDeviceArray(startOfOutputArray, &(d_in[lengthMultiple]), remainder, bcao);
add<<<1, remainder>>>(startOfOutputArray, remainder, &(d_in[lengthMultiple - 1]), &(d_out[lengthMultiple - 1]));
}
}
void scanSmallDeviceArray(int *d_out, int *d_in, int length, bool bcao) {
int powerOfTwo = nextPowerOfTwo(length);
if (bcao) {
prescan_arbitrary<<<1, (length + 1) / 2, 2 * powerOfTwo * sizeof(int)>>>(d_out, d_in, length, powerOfTwo);
}
else {
prescan_arbitrary_unoptimized<<<1, (length + 1) / 2, 2 * powerOfTwo * sizeof(int)>>>(d_out, d_in, length, powerOfTwo);
}
}
void scanLargeEvenDeviceArray(int *d_out, int *d_in, int length, bool bcao) {
const int blocks = length / ELEMENTS_PER_BLOCK;
const int sharedMemArraySize = ELEMENTS_PER_BLOCK * sizeof(int);
int *d_sums, *d_incr;
cudaMalloc((void **)&d_sums, blocks * sizeof(int));
cudaMalloc((void **)&d_incr, blocks * sizeof(int));
if (bcao) {
prescan_large<<<blocks, THREADS_PER_BLOCK, 2 * sharedMemArraySize>>>(d_out, d_in, ELEMENTS_PER_BLOCK, d_sums);
}
else {
prescan_large_unoptimized<<<blocks, THREADS_PER_BLOCK, 2 * sharedMemArraySize>>>(d_out, d_in, ELEMENTS_PER_BLOCK, d_sums);
}
const int sumsArrThreadsNeeded = (blocks + 1) / 2;
if (sumsArrThreadsNeeded > THREADS_PER_BLOCK) {
// perform a large scan on the sums arr
scanLargeDeviceArray(d_incr, d_sums, blocks, bcao);
}
else {
// only need one block to scan sums arr so can use small scan
scanSmallDeviceArray(d_incr, d_sums, blocks, bcao);
}
add<<<blocks, ELEMENTS_PER_BLOCK>>>(d_out, ELEMENTS_PER_BLOCK, d_incr);
cudaFree(d_sums);
cudaFree(d_incr);
}
/*///////////////////////////////////*/
/* kernels.cu */
/*///////////////////////////////////*/
#define SHARED_MEMORY_BANKS 32
#define LOG_MEM_BANKS 5
// There were two BCAO optimisations in the paper - this one is fastest
#define CONFLICT_FREE_OFFSET(n) ((n) >> LOG_MEM_BANKS)
__global__ void prescan_arbitrary(int *output, int *input, int n, int powerOfTwo)
{
extern __shared__ int temp[];// allocated on invocation
int threadID = threadIdx.x;
int ai = threadID;
int bi = threadID + (n / 2);
int bankOffsetA = CONFLICT_FREE_OFFSET(ai);
int bankOffsetB = CONFLICT_FREE_OFFSET(bi);
if (threadID < n) {
temp[ai + bankOffsetA] = input[ai];
temp[bi + bankOffsetB] = input[bi];
}
else {
temp[ai + bankOffsetA] = 0;
temp[bi + bankOffsetB] = 0;
}
int offset = 1;
for (int d = powerOfTwo >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
temp[bi] += temp[ai];
}
offset *= 2;
}
if (threadID == 0) {
temp[powerOfTwo - 1 + CONFLICT_FREE_OFFSET(powerOfTwo - 1)] = 0; // clear the last element
}
for (int d = 1; d < powerOfTwo; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
__syncthreads();
if (threadID < n) {
output[ai] = temp[ai + bankOffsetA];
output[bi] = temp[bi + bankOffsetB];
}
}
__global__ void prescan_arbitrary_unoptimized(int *output, int *input, int n, int powerOfTwo) {
extern __shared__ int temp[];// allocated on invocation
int threadID = threadIdx.x;
if (threadID < n) {
temp[2 * threadID] = input[2 * threadID]; // load input into shared memory
temp[2 * threadID + 1] = input[2 * threadID + 1];
}
else {
temp[2 * threadID] = 0;
temp[2 * threadID + 1] = 0;
}
int offset = 1;
for (int d = powerOfTwo >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
temp[bi] += temp[ai];
}
offset *= 2;
}
if (threadID == 0) { temp[powerOfTwo - 1] = 0; } // clear the last element
for (int d = 1; d < powerOfTwo; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
__syncthreads();
if (threadID < n) {
output[2 * threadID] = temp[2 * threadID]; // write results to device memory
output[2 * threadID + 1] = temp[2 * threadID + 1];
}
}
__global__ void prescan_large(int *output, int *input, int n, int *sums) {
extern __shared__ int temp[];
int blockID = blockIdx.x;
int threadID = threadIdx.x;
int blockOffset = blockID * n;
int ai = threadID;
int bi = threadID + (n / 2);
int bankOffsetA = CONFLICT_FREE_OFFSET(ai);
int bankOffsetB = CONFLICT_FREE_OFFSET(bi);
temp[ai + bankOffsetA] = input[blockOffset + ai];
temp[bi + bankOffsetB] = input[blockOffset + bi];
int offset = 1;
for (int d = n >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
temp[bi] += temp[ai];
}
offset *= 2;
}
__syncthreads();
if (threadID == 0) {
sums[blockID] = temp[n - 1 + CONFLICT_FREE_OFFSET(n - 1)];
temp[n - 1 + CONFLICT_FREE_OFFSET(n - 1)] = 0;
}
for (int d = 1; d < n; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
__syncthreads();
output[blockOffset + ai] = temp[ai + bankOffsetA];
output[blockOffset + bi] = temp[bi + bankOffsetB];
}
__global__ void prescan_large_unoptimized(int *output, int *input, int n, int *sums) {
int blockID = blockIdx.x;
int threadID = threadIdx.x;
int blockOffset = blockID * n;
extern __shared__ int temp[];
temp[2 * threadID] = input[blockOffset + (2 * threadID)];
temp[2 * threadID + 1] = input[blockOffset + (2 * threadID) + 1];
int offset = 1;
for (int d = n >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
temp[bi] += temp[ai];
}
offset *= 2;
}
__syncthreads();
if (threadID == 0) {
sums[blockID] = temp[n - 1];
temp[n - 1] = 0;
}
for (int d = 1; d < n; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (threadID < d)
{
int ai = offset * (2 * threadID + 1) - 1;
int bi = offset * (2 * threadID + 2) - 1;
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
__syncthreads();
output[blockOffset + (2 * threadID)] = temp[2 * threadID];
output[blockOffset + (2 * threadID) + 1] = temp[2 * threadID + 1];
}
__global__ void add(int *output, int length, int *n) {
int blockID = blockIdx.x;
int threadID = threadIdx.x;
int blockOffset = blockID * length;
output[blockOffset + threadID] += n[blockID];
}
__global__ void add(int *output, int length, int *n1, int *n2) {
int blockID = blockIdx.x;
int threadID = threadIdx.x;
int blockOffset = blockID * length;
output[blockOffset + threadID] += n1[blockID] + n2[blockID];
}
/*///////////////////////////////////*/
/* utils.cpp */
/*///////////////////////////////////*/
void _checkCudaError(const char *message, cudaError_t err, const char *caller) {
if (err != cudaSuccess) {
fprintf(stderr, "Error in: %s\n", caller);
fprintf(stderr, message);
fprintf(stderr, ": %s\n", cudaGetErrorString(err));
exit(0);
}
}
void printResult(const char* prefix, int result, long nanoseconds) {
printf(" ");
printf(prefix);
printf(" : %i in %ld ms \n", result, nanoseconds / 1000);
}
void printResult(const char* prefix, int result, float milliseconds) {
printf(" ");
printf(prefix);
printf(" : %i in %f ms \n", result, milliseconds);
}
// from https://stackoverflow.com/a/3638454
bool isPowerOfTwo(int x) {
return x && !(x & (x - 1));
}
// from https://stackoverflow.com/a/12506181
int nextPowerOfTwo(int x) {
int power = 1;
while (power < x) {
power *= 2;
}
return power;
}
// from https://stackoverflow.com/a/36095407
// Get the current time in nanoseconds
long get_nanos() {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return (long)ts.tv_sec * 1000000000L + ts.tv_nsec;
}