-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSubmission.cu
704 lines (561 loc) · 17.4 KB
/
Submission.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
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
/*
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"
// scan.cuh
long sequential_scan(int* output, int* input, int length);
float blockscan(int *output, int *input, int length, bool bcao);
float 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();
/*///////////////////////////////////*/
/* Main.cpp */
/*///////////////////////////////////*/
void test(int N) {
bool canBeBlockscanned = N <= 1024;
time_t t;
srand((unsigned)time(&t));
int *in = new int[N];
for (int i = 0; i < N; i++) {
in[i] = rand() % 10;
}
printf("%i Elements \n", N);
// sequential scan on CPU
int *outHost = new int[N]();
long time_host = sequential_scan(outHost, in, N);
printResult("host ", outHost[N - 1], time_host);
// full scan
int *outGPU = new int[N]();
float time_gpu = scan(outGPU, in, N, false);
printResult("gpu ", outGPU[N - 1], time_gpu);
// full scan with BCAO
int *outGPU_bcao = new int[N]();
float time_gpu_bcao = scan(outGPU_bcao, in, N, true);
printResult("gpu bcao", outGPU_bcao[N - 1], time_gpu_bcao);
if (canBeBlockscanned) {
// basic level 1 block scan
int *out_1block = new int[N]();
float time_1block = blockscan(out_1block, in, N, false);
printResult("level 1 ", out_1block[N - 1], time_1block);
// level 1 block scan with BCAO
int *out_1block_bcao = new int[N]();
float time_1block_bcao = blockscan(out_1block_bcao, in, N, true);
printResult("l1 bcao ", out_1block_bcao[N - 1], time_1block_bcao);
delete[] out_1block;
delete[] out_1block_bcao;
}
printf("\n");
delete[] in;
delete[] outHost;
delete[] outGPU;
delete[] outGPU_bcao;
}
int main()
{
int TEN_MILLION = 10000000;
int ONE_MILLION = 1000000;
int TEN_THOUSAND = 10000;
int elements[] = {
TEN_MILLION * 2,
TEN_MILLION,
ONE_MILLION,
TEN_THOUSAND,
5000,
4096,
2048,
2000,
1000,
500,
100,
64,
8,
5
};
int numElements = sizeof(elements) / sizeof(elements[0]);
for (int i = 0; i < numElements; i++) {
test(elements[i]);
}
return 0;
}
/*///////////////////////////////////*/
/* scan.cu */
/*///////////////////////////////////*/
#define checkCudaError(o, l) _checkCudaError(o, l, __func__)
int THREADS_PER_BLOCK = 512;
int ELEMENTS_PER_BLOCK = THREADS_PER_BLOCK * 2;
long sequential_scan(int* output, int* input, int length) {
long start_time = get_nanos();
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];
}
long end_time = get_nanos();
return end_time - start_time;
}
float blockscan(int *output, int *input, int length, bool bcao) {
int *d_out, *d_in;
const int arraySize = length * sizeof(int);
cudaMalloc((void **)&d_out, arraySize);
cudaMalloc((void **)&d_in, arraySize);
cudaMemcpy(d_out, output, arraySize, cudaMemcpyHostToDevice);
cudaMemcpy(d_in, input, arraySize, cudaMemcpyHostToDevice);
// start timer
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
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);
}
// end timer
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsedTime = 0;
cudaEventElapsedTime(&elapsedTime, start, stop);
cudaMemcpy(output, d_out, arraySize, cudaMemcpyDeviceToHost);
cudaFree(d_out);
cudaFree(d_in);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return elapsedTime;
}
float scan(int *output, int *input, int length, bool bcao) {
int *d_out, *d_in;
const int arraySize = length * sizeof(int);
cudaMalloc((void **)&d_out, arraySize);
cudaMalloc((void **)&d_in, arraySize);
cudaMemcpy(d_out, output, arraySize, cudaMemcpyHostToDevice);
cudaMemcpy(d_in, input, arraySize, cudaMemcpyHostToDevice);
// start timer
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
if (length > ELEMENTS_PER_BLOCK) {
scanLargeDeviceArray(d_out, d_in, length, bcao);
}
else {
scanSmallDeviceArray(d_out, d_in, length, bcao);
}
// end timer
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float elapsedTime = 0;
cudaEventElapsedTime(&elapsedTime, start, stop);
cudaMemcpy(output, d_out, arraySize, cudaMemcpyDeviceToHost);
cudaFree(d_out);
cudaFree(d_in);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return elapsedTime;
}
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;
}
/*
Timings
'level 1' = blockscan
'l1 bcao' = blockscan with bcao
The number before the time is the final element of the scanned array
20000000 Elements
host : 89997032 in 42338 ms
gpu : 89997032 in 16.285631 ms
gpu bcao : 89997032 in 8.554880 ms
10000000 Elements
host : 44983528 in 20749 ms
gpu : 44983528 in 7.860768 ms
gpu bcao : 44983528 in 4.304064 ms
1000000 Elements
host : 4494474 in 2105 ms
gpu : 4494474 in 0.975648 ms
gpu bcao : 4494474 in 0.600416 ms
10000 Elements
host : 45078 in 19 ms
gpu : 45078 in 0.213760 ms
gpu bcao : 45078 in 0.192128 ms
5000 Elements
host : 22489 in 11 ms
gpu : 22489 in 0.169312 ms
gpu bcao : 22489 in 0.148832 ms
4096 Elements
host : 18294 in 9 ms
gpu : 18294 in 0.132672 ms
gpu bcao : 18294 in 0.128480 ms
2048 Elements
host : 9149 in 4 ms
gpu : 9149 in 0.140736 ms
gpu bcao : 9149 in 0.126944 ms
2000 Elements
host : 8958 in 3 ms
gpu : 8958 in 0.178912 ms
gpu bcao : 8958 in 0.214464 ms
1000 Elements
host : 4483 in 2 ms
gpu : 4483 in 0.020128 ms
gpu bcao : 4483 in 0.010784 ms
level 1 : 4483 in 0.018080 ms
l1 bcao : 4483 in 0.010400 ms
500 Elements
host : 2203 in 4 ms
gpu : 2203 in 0.013440 ms
gpu bcao : 2203 in 0.009664 ms
level 1 : 2203 in 0.013280 ms
l1 bcao : 2203 in 0.010176 ms
100 Elements
host : 356 in 0 ms
gpu : 356 in 0.008512 ms
gpu bcao : 356 in 0.009280 ms
level 1 : 356 in 0.008896 ms
l1 bcao : 356 in 0.009056 ms
64 Elements
host : 221 in 0 ms
gpu : 221 in 0.007584 ms
gpu bcao : 221 in 0.008960 ms
level 1 : 221 in 0.007360 ms
l1 bcao : 221 in 0.008352 ms
8 Elements
host : 24 in 0 ms
gpu : 24 in 0.006240 ms
gpu bcao : 24 in 0.007392 ms
level 1 : 24 in 0.006176 ms
l1 bcao : 24 in 0.007424 ms
5 Elements
host : 12 in 0 ms
gpu : 12 in 0.006144 ms
gpu bcao : 12 in 0.007296 ms
level 1 : 12 in 0.006048 ms
l1 bcao : 12 in 0.007328 ms
*/