source: CIVL/examples/cuda/cuda-omp.cu@ 83af34d

1.23 2.0 main test-branch
Last change on this file since 83af34d was e75355b, checked in by Andre Marianiello <andre.marianiello@…>, 11 years ago

Updated Cuda2CIVLTransformTest with a few new tests that are not yet working correctly.

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

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