source: CIVL/examples/cuda/cuda-omp.cu@ 139c8d5

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 139c8d5 was 082072f, checked in by Andre Marianiello <andre.marianiello@…>, 11 years ago

Got cuda-omp.cu example verified. Added matMult example from civl-papers directory. Updated CIVL2CudaTransformerTest to run matMult example. Minor changes to cuda.cvl

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

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