-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnestedHelloWorld.cu
More file actions
61 lines (50 loc) · 1.61 KB
/
nestedHelloWorld.cu
File metadata and controls
61 lines (50 loc) · 1.61 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
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<string.h>
// error check
#define CHECK(call) \
{ \
const cudaError_t error = call;\
if (error != cudaSuccess) { \
printf("[device] Error: %s %d, ", __FILE__, __LINE__); \
printf("code:%d, reason: %s\n", error, cudaGetErrorString(error)); \
exit(-10*error); \
} \
}
__global__ void nestedHelloWorld(int const iSize, int iDepth) {
int tid = threadIdx.x;
printf("[Device] Hello World = (recursion depth: %d), (block: %d), (thread: %d)\n",
iDepth, blockIdx.x, threadIdx.x);
// condition to stop recursive execution
if (iSize == 1) return;
// reduce block size to half
int nthreads = iSize >> 1;
// thread 0 launches child grid recursively
if (tid == 0 && nthreads > 0) {
nestedHelloWorld << <1, nthreads >> > (nthreads, ++iDepth);
printf("[Device] --------> nested execution depth : %d\n", iDepth);
}
}
int main(int argc, char** argv) {
printf("[host] %s Starting...\n", argv[0]);
// set up device
int dev = 0;
cudaDeviceProp deviceProp;
CHECK(cudaGetDeviceProperties(&deviceProp, dev));
printf("[host] Using Device %d: %s\n", dev, deviceProp.name);
CHECK(cudaSetDevice(dev));
// data size
int size = 8;
dim3 block(size, 1);
dim3 grid(1, 1);
printf("[host] Execution configure : grid(%d, %d), block(%d, %d)\n",
grid.x, grid.y, block.x, block.y);
// kernel: nestedHelloWorld
nestedHelloWorld << <grid, block >> > (size, 0);
// reset device
CHECK(cudaDeviceReset());
}
/*
output:
*/