-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.cu
405 lines (312 loc) · 10.7 KB
/
scan.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
#include <stdio.h>
#include <math.h>
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <helper_functions.h>
#define CUDA_ERROR( err, msg ) { \
if (err != cudaSuccess) {\
printf( "%s: %s in %s at line %d\n", msg, cudaGetErrorString( err ), __FILE__, __LINE__);\
exit( EXIT_FAILURE );\
}\
}
#define NUM_BANKS 32
#define LOG_NUM_BANKS 5
#ifdef ZERO_BANK_CONFLICTS
#define CONFLICT_FREE_OFFSET(n) \
((n) >> NUM_BANKS + (n) >> (2 * LOG_NUM_BANKS))
#else
#define CONFLICT_FREE_OFFSET(n) ((n) >> LOG_NUM_BANKS)
#endif
#define BLOCK_SIZE 1024
size_t size = 1000000;
void sequential_block_scan(int * x, int * y, int len) {
int num_blocks = 1 + (len - 1) / BLOCK_SIZE;
for (int blk = 0; blk < num_blocks; blk++) {
int blk_start = blk * BLOCK_SIZE;
int blk_end = blk_start + BLOCK_SIZE;
if (blk_end > len)
blk_end = len;
y[blk_start] = 0;
for (int i = blk_start + 1; i < blk_end; i++)
y[i] = y[i - 1] + x[i - 1];
}
}
__device__ void seq() {
}
void sequential_full_scan(int * x, int * y, int len) {
y[0] = 0;
for (int i = 1; i < len; i++)
y[i] = y[i - 1] + x[i - 1];
}
static void compare_results(const int *vector1, const int *vector2,
int numElements) {
for (int i = 0; i < numElements; ++i) {
if (fabs(vector1[i] - vector2[i]) > 0) {
for (int x = max(i - 5, 0); x < max(i - 5, 0) + 10; x++) {
if (x == i)
printf(" | ");
printf("(%d,%d)", vector1[x], vector2[x]);
}
fprintf(stderr, "Result verification failed at element %d!\n", i);
exit(EXIT_FAILURE);
}
}
}
__global__ void blockScan(int * g_odata, int * g_idata, int n, int * sums) {
__shared__ int temp[2 * BLOCK_SIZE]; // allocated on invocation
int blockStart = 2 * BLOCK_SIZE * blockIdx.x;
int thid = threadIdx.x;
int offset = 1;
if (2 * thid + blockStart < n) {
temp[2 * thid] = g_idata[2 * thid + blockStart]; // load input into shared memory
temp[2 * thid + 1] = g_idata[2 * thid + 1 + blockStart];
}
for (int d = 2 * BLOCK_SIZE >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (thid < d) {
int ai = offset * (2 * thid + 1) - 1;
int bi = offset * (2 * thid + 2) - 1;
temp[bi] += temp[ai];
}
offset *= 2;
}
if (sums != NULL) {
sums[blockStart / BLOCK_SIZE] = temp[BLOCK_SIZE - 1];
sums[(blockStart / BLOCK_SIZE) + 1] = temp[2 * BLOCK_SIZE - 1]
- temp[BLOCK_SIZE - 1];
}
__syncthreads();
if (thid == 0) {
temp[BLOCK_SIZE - 1] = 0;
}
if (BLOCK_SIZE % thid == 0) {
temp[2 * BLOCK_SIZE - 1] = 0;
}
for (int d = 1; d < 2 * BLOCK_SIZE; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (thid < d) {
int ai = offset * (2 * thid + 1) - 1; //1024
int bi = offset * (2 * thid + 2) - 1; //1025
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
if (2 * thid + blockStart < n) {
g_odata[(2 * thid) + blockStart] = temp[2 * thid]; // write results to device memory
g_odata[(2 * thid + 1) + blockStart] = temp[2 * thid + 1];
}
__syncthreads();
}
__global__ void blockScanWithBCAO(int * g_odata, int * g_idata, int n,
int * sums) {
__shared__ int temp[4 * BLOCK_SIZE]; // allocated on invocation
int blockStart = 2 * BLOCK_SIZE * blockIdx.x;
int thid = threadIdx.x;
int offset = 1;
int ai = thid;
int bi = thid + (BLOCK_SIZE / 2);
int bankOffsetA = CONFLICT_FREE_OFFSET(ai);
int bankOffsetB = CONFLICT_FREE_OFFSET(bi);
if (ai + bankOffsetA + blockStart < n) {
temp[ai + bankOffsetA] = g_idata[ai + blockStart];
temp[bi + bankOffsetB] = g_idata[bi + blockStart];
}
for (int d = 2 * BLOCK_SIZE >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
__syncthreads();
if (thid < d) {
int ai = offset*(2*thid+1)-1;
int bi = offset*(2*thid+2)-1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
temp[bi] += temp[ai];
}
offset *= 2;
}
if (sums != NULL) {
sums[blockStart / BLOCK_SIZE] = temp[BLOCK_SIZE - 1 + CONFLICT_FREE_OFFSET(BLOCK_SIZE - 1)];
sums[(blockStart / BLOCK_SIZE) + 1] = temp[2*BLOCK_SIZE - 1 + CONFLICT_FREE_OFFSET(2*BLOCK_SIZE - 1)]
- temp[BLOCK_SIZE - 1 + CONFLICT_FREE_OFFSET(BLOCK_SIZE - 1)];
}
__syncthreads();
if (thid == 0) {
temp[BLOCK_SIZE - 1 + CONFLICT_FREE_OFFSET(BLOCK_SIZE - 1)] = 0;
}
if (BLOCK_SIZE % thid == 0) {
temp[2*BLOCK_SIZE - 1 + CONFLICT_FREE_OFFSET(2*BLOCK_SIZE - 1)] = 0;
}
for (int d = 1; d < 2 * BLOCK_SIZE; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
__syncthreads();
if (thid < d) {
int ai = offset*(2*thid+1)-1;
int bi = offset*(2*thid+2)-1;
ai += CONFLICT_FREE_OFFSET(ai);
bi += CONFLICT_FREE_OFFSET(bi);
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
if (ai + blockStart < n) {
g_odata[ai + blockStart] = temp[ai + bankOffsetA];
g_odata[bi + blockStart] = temp[bi + bankOffsetB];
}
__syncthreads();
}
__global__ void addToBlock2(int * input, int * output, int n) {
int thid = blockIdx.x * blockDim.x + threadIdx.x;
int numBlocks = 1 + ((n - 1) / (BLOCK_SIZE));
if (thid < numBlocks) {
int blockStart = BLOCK_SIZE * thid;
for (int i = 0; i < BLOCK_SIZE; i++) {
output[i + blockStart] += input[thid];
}
}
__syncthreads();
}
void full_scan_without_BCAO(int * g_odata, int * g_idata, int n) {
int blocksPerGrid = 1 + ((n - 1) / (BLOCK_SIZE * 2));
cudaError_t err = cudaSuccess;
int lvl3 = 0;
if (n / BLOCK_SIZE > BLOCK_SIZE) {
lvl3 = 1;
}
int *d_sum1 = NULL;
err = cudaMalloc((void **) &d_sum1, ((n / BLOCK_SIZE) + 1) * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for d_sum1");
int *d_sum1_scanned = NULL;
err = cudaMalloc((void **) &d_sum1_scanned,
((n / BLOCK_SIZE) + 1) * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for d_sum1_scanned");
if (lvl3 == 0) {
blockScan<<<blocksPerGrid, BLOCK_SIZE>>>(g_odata, g_idata, n, d_sum1);
blockScan<<<blocksPerGrid, BLOCK_SIZE>>>(d_sum1_scanned, d_sum1,
(n / BLOCK_SIZE) + 1, NULL);
addToBlock2<<<blocksPerGrid, BLOCK_SIZE>>>(d_sum1_scanned, g_odata, n);
cudaDeviceSynchronize();
} else {
//Level 3 Scan Needed
int *d_sum2 = NULL;
err = cudaMalloc((void **) &d_sum2,
((n / BLOCK_SIZE) + 1) * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for d_sum1");
int *d_sum2_scanned = NULL;
err = cudaMalloc((void **) &d_sum2_scanned,
((n / BLOCK_SIZE) + 1) * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for d_sum1");
blockScan<<<blocksPerGrid, BLOCK_SIZE>>>(g_odata, g_idata, n, d_sum1);
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "1");
blockScan<<<(blocksPerGrid / (BLOCK_SIZE * 2)), BLOCK_SIZE>>>(
d_sum1_scanned, d_sum1, (n / BLOCK_SIZE) + 1, d_sum2);
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "2");
blockScan<<<1, BLOCK_SIZE>>>(d_sum2_scanned, d_sum2, 1, NULL);
cudaDeviceSynchronize();
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "3");
addToBlock2<<<1 + (blocksPerGrid / BLOCK_SIZE * 2), BLOCK_SIZE>>>(
d_sum2_scanned, d_sum1_scanned, ((n / BLOCK_SIZE) + 1));
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "4");
addToBlock2<<<blocksPerGrid, BLOCK_SIZE>>>(d_sum1_scanned, g_odata, n);
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "5");
cudaDeviceSynchronize();
}
}
void block_scan_without_BCAO(int * g_odata, int * g_idata, int n) {
int blocksPerGrid = 1 + ((size - 1) / (BLOCK_SIZE * 2));
blockScan<<<blocksPerGrid, BLOCK_SIZE>>>(g_odata, g_idata, n, NULL);
}
void block_scan_with_BCAO(int * g_odata, int * g_idata, int n) {
int blocksPerGrid = 1 + ((size - 1) / (BLOCK_SIZE * 2));
blockScanWithBCAO<<<blocksPerGrid, BLOCK_SIZE>>>(g_odata, g_idata, n, NULL);
}
int main(void) {
cudaError_t err = cudaSuccess;
StopWatchInterface * timer = NULL;
sdkCreateTimer(&timer);
double h_msecs;
// Create Device timer event objects
cudaEvent_t start, stop;
float d_msecs;
cudaEventCreate(&start);
cudaEventCreate(&stop);
//Generate array
int *h_inputVector = (int *) malloc(size * sizeof(int));
memset(h_inputVector, '\0', size * sizeof(int));
for (int i = 0; i < size; i++) {
h_inputVector[i] = rand() % 10;
};
//Serial Block Scan
int *h_outputBlockScan = (int *) malloc(size * sizeof(int));
sdkStartTimer(&timer);
sequential_block_scan(h_inputVector, h_outputBlockScan, size);
sdkStopTimer(&timer);
h_msecs = sdkGetTimerValue(&timer);
//Serial Full Scan
int *h_outputFullScan = (int *) malloc(size * sizeof(int));
sdkStartTimer(&timer);
sequential_full_scan(h_inputVector, h_outputFullScan, size);
sdkStopTimer(&timer);
h_msecs = sdkGetTimerValue(&timer);
//Copying input vector to device
int *d_inputVector = NULL;
err = cudaMalloc((void **) &d_inputVector, (size * sizeof(int)));
CUDA_ERROR(err, "Failed to allocate device vector");
err = cudaMemcpy(d_inputVector, h_inputVector, size * sizeof(int),
cudaMemcpyHostToDevice);
CUDA_ERROR(err, "Failed to copy vector from host to device");
//Parallel Block Scan Without BCAO
int *d_parallelBlockScan = NULL;
err = cudaMalloc((void **) &d_parallelBlockScan, size * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for parallel output");
cudaEventRecord(start, 0);
block_scan_without_BCAO(d_parallelBlockScan, d_inputVector, size);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
// wait for device to finish
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "Failed to launch vectorAdd kernel");
err = cudaEventElapsedTime(&d_msecs, start, stop);
CUDA_ERROR(err, "Failed to get elapsed time");
int *h_parallelBlockScan = (int *) malloc(size * sizeof(int));
err = cudaMemcpy(h_parallelBlockScan, d_parallelBlockScan,
size * sizeof(int), cudaMemcpyDeviceToHost);
CUDA_ERROR(err, "Failed to copy parallel output from device to host");
compare_results(h_outputBlockScan, h_parallelBlockScan, size);
printf("Block Scan Without BCAO = %.5fmSecs\n", d_msecs);
//Full Block Scan Without BCAO
int *d_parallelFullScan = NULL;
err = cudaMalloc((void **) &d_parallelFullScan, size * sizeof(int));
CUDA_ERROR(err, "Failed to allocate for d_parallelFullScan");
cudaEventRecord(start, 0);
full_scan_without_BCAO(d_parallelFullScan, d_inputVector, size);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
// wait for device to finish
cudaDeviceSynchronize();
err = cudaGetLastError();
CUDA_ERROR(err, "Failed to launch full_scan kernel");
err = cudaEventElapsedTime(&d_msecs, start, stop);
CUDA_ERROR(err, "Failed to get elapsed time");
int *h_parallelFullScan = (int *) malloc(size * sizeof(int));
err = cudaMemcpy(h_parallelFullScan, d_parallelFullScan, size * sizeof(int),
cudaMemcpyDeviceToHost);
CUDA_ERROR(err, "Failed to copy parallel output from device to host");
compare_results(h_outputFullScan, h_parallelFullScan, size);
printf("Full Scan Without BCAO = %.5fmSecs\n", d_msecs);
}