source: CIVL/examples/cuda/cuda-omp.cu@ f3527dd

1.23 2.0 main test-branch
Last change on this file since f3527dd was 2321281, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

fixed $elaborate for examples.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2355 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 4.2 KB
Line 
1#ifdef _CIVL
2#include <civlc.cvh>
3#endif
4//http://www.arc.vt.edu/resources/software/cuda/docs/cuda-omp.cu
5
6#include <omp.h>
7#include <cuda.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11
12#ifdef _CIVL
13$input int BLOCKS;
14$input int BLOCK_B;
15$assume(1 <= BLOCKS && BLOCKS <= BLOCK_B);
16$input int THREADS_PER_BLOCK;
17$input int THREADS_B;
18$assume(1 <= THREADS_PER_BLOCK && THREADS_PER_BLOCK <= THREADS_B);
19#else
20#define BLOCKS 64
21#define THREADS_PER_BLOCK 128
22#endif
23
24// A kernel that increments each array element by the value b
25
26__global__ void kernelAddConstant(int *g_a, const int b)
27{
28 int idx = blockIdx.x * blockDim.x + threadIdx.x;
29 g_a[idx] += b;
30}
31
32// Check whether each element was incremented by the value b
33int correctResult(int *data, const int n, const int b)
34{
35 for(int i = 0; i < n; i++)
36 if(data[i] != i + b)
37 return 0;
38 return 1;
39}
40
41int main(int argc, char *argv[])
42{
43#ifdef _CIVL
44 $elaborate(BLOCKS);
45 $elaborate(THREADS_PER_BLOCK);
46#endif
47
48 // Variable which holds number of GPUs
49 int num_gpus = 0;
50
51 // Determine the number of CUDA capable GPUs
52 cudaGetDeviceCount(&num_gpus);
53 if(num_gpus < 1)
54 {
55 printf("No CUDA Capable GPU(s) Detected \n");
56 return 1;
57 }
58
59 // Display the CPU and GPU processor specification
60 int num_procs = omp_get_num_procs();
61 printf("number of host CPUs:\t%d\n", num_procs);
62 printf("number of CUDA devices:\t%d\n", num_gpus);
63 for(int i = 0; i < num_gpus; i++)
64 {
65 cudaDeviceProp dprop;
66 cudaGetDeviceProperties(&dprop, i);
67 printf("\t Device %d is a %s\n", i, dprop.name);
68 }
69
70
71 // Initialize the variables
72 unsigned int n = num_gpus * THREADS_PER_BLOCK * BLOCKS;
73 unsigned int nbytes = n * sizeof(int);
74 int *a = 0; // pointer to data on the CPU
75 int b = 3; // value by which each array array element will be incremented
76 a = (int*)malloc(nbytes);
77
78 if(0 == a)
79 {
80 printf("couldn't allocate CPU memory\n");
81 return 1;
82 }
83
84 for(unsigned int i = 0; i < n; i++)
85 a[i] = i;
86
87 // Set the number of threads to the number of GPUs on the system
88 omp_set_num_threads(num_gpus);
89
90 #pragma omp parallel
91 {
92 unsigned int cpu_thread_id = omp_get_thread_num();
93 unsigned int num_cpu_threads = omp_get_num_threads();
94
95 // Assign and check the GPU device for each thread
96 int gpu_id = -1;
97 cudaSetDevice(cpu_thread_id % num_gpus);
98 cudaGetDevice(&gpu_id);
99
100 printf("CPU thread %d (of %d) uses CUDA device %d\n", cpu_thread_id, num_cpu_threads, gpu_id);
101
102 // Variable on the device associated with this CPU thread
103 int *d_a = 0;
104
105 // Variable for the CPU
106 int *sub_a = a + cpu_thread_id * n / num_cpu_threads;
107
108 unsigned int nbytes_per_kernel = nbytes / num_cpu_threads;
109 dim3 gpu_threads = {THREADS_PER_BLOCK, 1, 1}; // 128 threads per block
110 dim3 gpu_blocks = {(n / (gpu_threads.x * num_cpu_threads)), 1, 1};
111
112 //Allocate memory on the device
113 cudaMalloc((void**)&d_a, nbytes_per_kernel);
114
115 //Initialize the array on the device with zeros
116 cudaMemset(d_a, 0, nbytes_per_kernel);
117
118 //Copy data from host to device
119 cudaMemcpy(d_a, sub_a, nbytes_per_kernel, cudaMemcpyHostToDevice);
120
121 //Launch the kernel
122 kernelAddConstant<<<gpu_blocks, gpu_threads>>>(d_a, b);
123
124 //Copy the result from the device to the host
125 cudaMemcpy(sub_a, d_a, nbytes_per_kernel, cudaMemcpyDeviceToHost);
126
127 //Deallocate the memory on the device
128 cudaFree(d_a);
129
130 }
131
132
133 if(cudaSuccess != cudaGetLastError()) {
134 int err_num = cudaGetLastError();
135 const char * err_str = cudaGetErrorString(err_num);
136 printf("%s\n", err_str);
137 }
138
139
140 //Check for correctness of the result
141 if(correctResult(a, n, b)) {
142#ifdef _CIVL
143 $assert(($true));
144#endif
145 printf("Test PASSED\n");
146 } else
147 printf("Test FAILED\n");
148
149 //Deallocate the CPU memory
150 free(a);
151
152 // deprecated
153 // cudaThreadExit();
154
155 return 0;
156}
157
Note: See TracBrowser for help on using the repository browser.