source: CIVL/examples/cuda/dot.cu@ 581546a

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

Added test for dot.cu

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

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
3 *
4 * NVIDIA Corporation and its licensors retain all intellectual property and
5 * proprietary rights in and to this software and related documentation.
6 * Any use, reproduction, disclosure, or distribution of this software
7 * and related documentation without an express license agreement from
8 * NVIDIA Corporation is strictly prohibited.
9 *
10 * Please refer to the applicable NVIDIA end user license agreement (EULA)
11 * associated with this source code for terms and conditions that govern
12 * your use of this NVIDIA software.
13 *
14 */
15
16
17//#include "../common/book.h"
18#include <stdio.h>
19#include <stdlib.h>
20#include <cuda.h>
21
22#define HANDLE_ERROR(x) x
23
24#define imin(a,b) (a<b?a:b)
25
26const int N = 8;
27const int threadsPerBlock = 4;
28const int blocksPerGrid =
29 imin(32, (N+threadsPerBlock-1) / threadsPerBlock );
30
31__global__ void dot( float *a, float *b, float *c ) {
32 __shared__ float cache[threadsPerBlock];
33 int tid = threadIdx.x + blockIdx.x * blockDim.x;
34 int cacheIndex = threadIdx.x;
35 float temp = 0;
36
37 while (tid < N) {
38 temp += a[tid] * b[tid];
39 tid += blockDim.x * gridDim.x;
40 }
41 // set the cache values
42 cache[cacheIndex] = temp;
43 // synchronize threads in this block
44 __syncthreads();
45 // for reductions, threadsPerBlock must be a power of 2
46 // because of the following code
47 int i = blockDim.x/2;
48 while (i != 0) {
49 if (cacheIndex < i)
50 cache[cacheIndex] += cache[cacheIndex + i];
51 __syncthreads();
52 i /= 2;
53 }
54
55 if (cacheIndex == 0)
56 c[blockIdx.x] = cache[0];
57}
58
59
60int main( void ) {
61 float *a, *b, c, *partial_c;
62 float *dev_a, *dev_b, *dev_partial_c;
63
64 // allocate memory on the cpu side
65 a = (float*)malloc( N*sizeof(float) );
66 b = (float*)malloc( N*sizeof(float) );
67 partial_c = (float*)malloc( blocksPerGrid*sizeof(float) );
68
69 // allocate the memory on the GPU
70 HANDLE_ERROR( cudaMalloc( (void**)&dev_a,
71 N*sizeof(float) ) );
72 HANDLE_ERROR( cudaMalloc( (void**)&dev_b,
73 N*sizeof(float) ) );
74 HANDLE_ERROR( cudaMalloc( (void**)&dev_partial_c,
75 blocksPerGrid*sizeof(float) ) );
76
77 // fill in the host memory with data
78 for (int i=0; i<N; i++) {
79 a[i] = i;
80 b[i] = i*2;
81 }
82
83 // copy the arrays 'a' and 'b' to the GPU
84 HANDLE_ERROR( cudaMemcpy( dev_a, a, N*sizeof(float),
85 cudaMemcpyHostToDevice ) );
86 HANDLE_ERROR( cudaMemcpy( dev_b, b, N*sizeof(float),
87 cudaMemcpyHostToDevice ) );
88
89 dot<<<blocksPerGrid,threadsPerBlock>>>( dev_a, dev_b,
90 dev_partial_c );
91
92 // copy the array 'c' back from the GPU to the CPU
93 HANDLE_ERROR( cudaMemcpy( partial_c, dev_partial_c,
94 blocksPerGrid*sizeof(float),
95 cudaMemcpyDeviceToHost ) );
96
97 // finish up on the CPU side
98 c = 0;
99 for (int i=0; i<blocksPerGrid; i++) {
100 c += partial_c[i];
101 }
102
103 #define sum_squares(x) (x*(x+1)*(2*x+1)/6)
104 printf( "Does GPU value %.6g = %.6g?\n", c,
105 2 * sum_squares( (float)(N - 1) ) );
106
107 // free memory on the gpu side
108 HANDLE_ERROR( cudaFree( dev_a ) );
109 HANDLE_ERROR( cudaFree( dev_b ) );
110 HANDLE_ERROR( cudaFree( dev_partial_c ) );
111
112 // free memory on the cpu side
113 free( a );
114 free( b );
115 free( partial_c );
116}
Note: See TracBrowser for help on using the repository browser.