-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfinal_code.cu
More file actions
449 lines (368 loc) · 11.1 KB
/
final_code.cu
File metadata and controls
449 lines (368 loc) · 11.1 KB
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
/////////////////////////////////////////////////////////
// Computes the 1-stencil using GPUs.
// We don't check for error here for brevity.
// In your implementation - you must do it!
#define BLOCK_SIZE 1024
#define WARP_SIZE 32
#ifndef k
#define k 3
#endif
#ifndef OUTPUT_PER_THREAD
#define OUTPUT_PER_THREAD 1
#endif
#define LOCAL_REGISTER_SIZE ((1+OUTPUT_PER_THREAD) > (k+31)/32 ? (1+OUTPUT_PER_THREAD) : (k+31)/32)
#ifndef TEST_TIMES
#define TEST_TIMES 5
#endif
#include <cstdlib>
#include <cuda_runtime_api.h>
#include <cuda_runtime.h>
#include <cstring>
#include <cstdio>
#include <cuda.h>
float host_k_stencil (int *A, int *B, int sizeOfA, int withRc);
__global__ void k_stencil (int *A, int *B, int sizeOfA);
__global__ void k_stencil_with_rc (int *A, int *B, int sizeOfA);
float host_one_stencil (int *A, int *B, int sizeOfA, int withRc);
bool ans_check(int *A, int* B, int sizeOfA, int t);
__global__ void one_stencil (int *A, int *B, int sizeOfA);
__global__ void one_stencil_with_rc (int *A, int *B, int sizeOfA);
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__);}
inline void gpuAssert(cudaError_t code, const char*file, int line, bool abort=true)
{
if(code != cudaSuccess)
{
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
void fill_array(int *arr, int size)
{
for (int i = 0 ; i < size ; ++i)
{
arr[i] = i%17;
}
}
int main(int argc, char** argv)
{
int withRc = 0;
if (argc != 3)
{
printf("Usage: %s [RC/NO_RC] [TEST_SIZE > 10000]\n", argv[0]);
exit(-1);
}
if (0 == strcmp("RC", argv[1]))
{
withRc = 1;
}
int test_size = atoi(argv[2]);
int *A = new int[test_size];
int *B = new int[test_size - 2];
fill_array(A, test_size);
fill_array(B, test_size - 2);
float sum = 0;
float min = 0;
float max = 0;
float current = 0;
for (int i = 0 ; i < TEST_TIMES ; ++i)
{
current = host_k_stencil(A, B, test_size, withRc);
if (i == 0)
{
min = current;
max = current;
}
sum += current;
if (min > current)
{
min = current;
}
if (max < current)
{
max = current;
}
#ifdef TEST_MODE
if (!ans_check(A, B, test_size, k))
{
printf("TEST FAILED!\n");
break;
}
#endif
}
sum = sum - min - max;
delete[] A;
delete[] B;
printf("%f\n", sum/(TEST_TIMES-2));
return 0;
}
bool ans_check(int *A, int* B, int sizeOfA, int t)
{
for (int i = 0 ; i < sizeOfA - t - 1 ; ++i)
{
int sum = 0;
for (int j = 0 ; j < t + 1 ; ++j)
{
sum += A[i + j];
}
if (B[i] != sum)
{
return 0;
}
}
return 1;
}
float host_one_stencil (int *A, int *B, int sizeOfA, bool withRc)
{
// Declare device pointer.
int *d_A = NULL;
int *d_B = NULL;
// Create events
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
float ms = 0;
// Allocate memory on device.
gpuErrchk(cudaMalloc((void**)(&d_A), sizeOfA * sizeof(int)));
gpuErrchk(cudaMalloc((void**)(&d_B), (sizeOfA - 2) * sizeof(int)));
// Copy input to device
cudaMemcpy(d_A, A, sizeOfA * sizeof(int), cudaMemcpyHostToDevice);
// We round up the result of (sizeOfA / BLOCK_SIZE).
int threadBlocksNum = (sizeOfA + BLOCK_SIZE - 1) / BLOCK_SIZE;
if (withRc)
{
cudaEventRecord(start);
one_stencil_with_rc<<< threadBlocksNum , BLOCK_SIZE >>>(d_A, d_B, sizeOfA);
cudaEventRecord(stop);
}
else
{
int neededShmem = (BLOCK_SIZE + 2) * sizeof(int);
cudaEventRecord(start);
one_stencil<<< threadBlocksNum , BLOCK_SIZE, neededShmem >>>(d_A, d_B, sizeOfA);
cudaEventRecord(stop);
}
cudaDeviceSynchronize();
gpuErrchk(cudaPeekAtLastError());
// Read output from device
cudaMemcpy(B, d_B, (sizeOfA - 2) * sizeof(int), cudaMemcpyDeviceToHost);
// Measure test results
cudaEventSynchronize(stop);
cudaEventElapsedTime(&ms, start, stop);
// Free memory.
cudaFree(d_A);
cudaFree(d_B);
return ms;
}
// Computes the 1-stencil of A using shared memory
// Writes output into B.
__global__ void one_stencil (int *A, int *B, int sizeOfA)
{
extern __shared__ int s[];
// Id of thread in the block.
int localId = threadIdx.x;
// The first index of output element computed by this block.
int startOfBlock = blockIdx.x * blockDim.x;
// The Id of the thread in the scope of the grid.
int globalId = localId + startOfBlock;
if (globalId >= sizeOfA)
return;
// Fetching into shared memory.
s[localId] = A[globalId];
if (localId < 2 && blockDim.x + globalId < sizeOfA)
{
s[blockDim.x + localId] = A[blockDim.x + globalId];
}
// We must sync before reading from shared memory.
__syncthreads();
// Each thread computes a single output.
if (globalId < sizeOfA - 2)
B[globalId] = s[localId] + s[localId + 1] + s[localId + 2];
}
// Computes the 1-stencil of A with register cache.
// Writes output into B.
__global__ void one_stencil_with_rc (int *A, int *B, int sizeOfA)
{
// Declaring local register cache.
int rc[2];
// Id of thread in the warp.
int localId = threadIdx.x % WARP_SIZE;
// The first index of output element computed by this warp.
int startOfWarp = blockIdx.x * blockDim.x + WARP_SIZE*(threadIdx.x / WARP_SIZE);
// The Id of the thread in the scope of the grid.
int globalId = localId + startOfWarp;
if (globalId >= sizeOfA)
return;
// Fetching into shared memory.
rc[0] = A[globalId];
if (localId < 2 && WARP_SIZE + globalId < sizeOfA)
{
rc[1] = A[WARP_SIZE + globalId];
}
// Each thread computes a single output.
int ac = 0;
int toShare = rc[0];
bool isLastWarp = sizeOfA - startOfWarp < WARP_SIZE;
// The number of threads in the warp which are inactive.
// Possibly bigger than zero only for the last warp.
int inactiveThreadsInWarp = isLastWarp ? startOfWarp + WARP_SIZE - sizeOfA : 0;
// Accessing register cache.
// We use a precomputed active mask.
// This is because otherwise only a subset of active threads return from
// the __activemask() call, which will resemble a wrong picture of
// the currently active threads in the warp.
// notice that the active mask does not change along the following
// loop so we claculate it just once.
// Please refer to the cuda developers guide for futher information.
unsigned mask = //__activemask(); <-- Wrong!
(0xffffffff) >> (inactiveThreadsInWarp);
for (int i = 0 ; i < 3 ; ++i)
{
// Threads decide what value will be published in the following access.
if (localId < i)
toShare = rc[1];
ac += __shfl_sync(mask, toShare, (localId + i) % WARP_SIZE);
}
if (globalId < sizeOfA - 2)
B[globalId] = ac;
}
float host_k_stencil (int *A, int *B, int sizeOfA, int withRc)
{
// Declare device pointer.
int *d_A = NULL;
int *d_B = NULL;
// Create events
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
float ms = 0;
// Allocate memory on device.
gpuErrchk(cudaMalloc((void**)(&d_A), sizeOfA * sizeof(int) + WARP_SIZE*(OUTPUT_PER_THREAD+1)));
gpuErrchk(cudaMalloc((void**)(&d_B), (sizeOfA ) * sizeof(int) + WARP_SIZE*OUTPUT_PER_THREAD));
// Copy input to device
cudaMemcpy(d_A, A, sizeOfA * sizeof(int), cudaMemcpyHostToDevice);
// We round up the result of (sizeOfA / BLOCK_SIZE).
if (withRc == 1)
{
int threadBlocksNum = (sizeOfA + OUTPUT_PER_THREAD*BLOCK_SIZE - 1) / (OUTPUT_PER_THREAD*BLOCK_SIZE);
cudaEventRecord(start);
k_stencil_with_rc<<< threadBlocksNum , BLOCK_SIZE >>>(d_A, d_B, sizeOfA);
cudaEventRecord(stop);
}
else
{
int threadBlocksNum = (sizeOfA + BLOCK_SIZE*OUTPUT_PER_THREAD - 1) / (BLOCK_SIZE*OUTPUT_PER_THREAD);
int neededShmem = (BLOCK_SIZE*OUTPUT_PER_THREAD+ k) * sizeof(int);
cudaEventRecord(start);
k_stencil<<< threadBlocksNum , BLOCK_SIZE, neededShmem >>>(d_A, d_B, sizeOfA);
cudaEventRecord(stop);
}
cudaDeviceSynchronize();
gpuErrchk(cudaPeekAtLastError());
// Read output from device
cudaMemcpy(B, d_B, (sizeOfA - k) * sizeof(int), cudaMemcpyDeviceToHost);
// Measure test results
cudaEventSynchronize(stop);
cudaEventElapsedTime(&ms, start, stop);
// Free memory.
cudaFree(d_A);
cudaFree(d_B);
return ms;
}
__global__ void k_stencil (int *A, int *B, int sizeOfA)
{
extern __shared__ int s[];
// Id of thread in the block.
int localId = threadIdx.x;
// The first index of output element computed by this block.
int startOfBlock = blockIdx.x * blockDim.x * OUTPUT_PER_THREAD;
// The Id of the thread in the scope of the grid.
int globalId = localId + startOfBlock;
if (globalId >= sizeOfA)
return;
// Fetching into shared memory.
for (int i = 0 ; i < OUTPUT_PER_THREAD ; ++i)
{
if (globalId + i*BLOCK_SIZE < sizeOfA)
{
s[localId + i*BLOCK_SIZE] = A[globalId + i*BLOCK_SIZE];
}
}
if (localId < k && blockDim.x*OUTPUT_PER_THREAD + globalId < sizeOfA)
{
s[localId + blockDim.x*OUTPUT_PER_THREAD] = A[blockDim.x*OUTPUT_PER_THREAD + globalId];
}
// We must sync before reading from shared memory.
__syncthreads();
int sum = 0;
for (int j = 0 ; j < OUTPUT_PER_THREAD ; ++j)
{
sum = 0;
if (globalId + j*BLOCK_SIZE >= sizeOfA - k)
return;
for (int i = 0 ; i < k + 1 ; ++i)
{
sum += s[localId + j*BLOCK_SIZE + i];
}
B[globalId + BLOCK_SIZE*j] = sum ;
}
}
// Computes the 1-stencil of A with register cache.
// Writes output into B.
__global__ void k_stencil_with_rc (int *A, int *B, int sizeOfA)
{
int sizeOfB = sizeOfA - k;
// Declaring local register cache.
int rc[LOCAL_REGISTER_SIZE];
// Id of thread in the warp.
int localId = threadIdx.x % WARP_SIZE;
// The first index of output element computed by this warp.
int startOfWarp = (blockIdx.x * blockDim.x + WARP_SIZE*(threadIdx.x / WARP_SIZE))*OUTPUT_PER_THREAD;
// The Id of the thread in the scope of the grid.
int globalId = localId + startOfWarp;
if (globalId >= sizeOfA)
return;
// Fetching into shared memory.
#pragma unroll
for (int i = 0 ; i < OUTPUT_PER_THREAD ; ++i)
{
if (globalId + WARP_SIZE*i >= sizeOfA)
{
continue;
}
rc[i] = A[(int)(globalId + WARP_SIZE*i)];
}
rc[LOCAL_REGISTER_SIZE - 1] = A[OUTPUT_PER_THREAD*WARP_SIZE + globalId];
// Each thread computes a single output.
bool warpHasInactiveThreads = sizeOfA - startOfWarp < WARP_SIZE;
// The number of threads in the warp which are inactive.
// Possibly bigger than zero only for the last warp.
int inactiveThreadsInWarp = warpHasInactiveThreads ? startOfWarp + WARP_SIZE - sizeOfA : 0;
// Accessing register cache.
// We use a precomputed active mask.
// This is because otherwise only a subset of active threads return from
// the __activemask() call, which will resemble a wrong picture of
// the currently active threads in the warp.
// notice that the active mask does not change along the following
// loop so we claculate it just once.
// Please refer to the cuda developers guide for futher information.
unsigned mask = //__activemask(); <-- Wrong!
(0xffffffff) >> (inactiveThreadsInWarp);
#pragma unroll
for (int j = 0 ; j < OUTPUT_PER_THREAD ; ++j)
{
int toShare = rc[j];
int ac = 0;
#pragma unroll
for (int i = 0 ; i < k + 1 ; ++i)
{
// Threads decide what value will be published in the following access.
ac += __shfl_sync(mask, toShare, (localId + i) & (WARP_SIZE - 1));
toShare += (i==localId)*(rc[j+1] - rc[j]);
}
if (globalId + j*WARP_SIZE >= sizeOfB)
{
continue;
}
B[globalId + j*WARP_SIZE] = ac ;
}
}