GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
deviceQualifier.cu
Go to the documentation of this file.
1 
9 #include "../include/mycuda.h"
10 
11 const int n=8192;
12 
13 __device__ float x_d[n];
14 __device__ float y_d[n];
15 __device__ float z_d[n];
16 
17 
19 
20 __device__ float XplusY_dev( float x, float y ) {
21  return x+y;
22 }
23 
24 
26 
27 __global__ void XplusY() {
28  int tid = blockIdx.x*blockDim.x + threadIdx.x;
29  int nthreads = blockDim.x*gridDim.x;
30  for (int i=tid; i<n; i+=nthreads) {
31  z_d[i] = XplusY_dev( x_d[i], y_d[i] );
32  }
33 }
34 
35 
36 
37 int main() {
38  const int blocksize=64;
39  const int gridsize=64;
40 
41  float *x_ptr, *y_ptr, *z_ptr;
42  cudaGetSymbolAddress( (void**)&x_ptr, x_d );
43  cudaGetSymbolAddress( (void**)&y_ptr, y_d );
44  cudaGetSymbolAddress( (void**)&z_ptr, z_d );
45 
46  mycuda::seq <<< gridsize, blocksize >>> ( x_ptr, 1.0, 2.0, n );
47  mycuda::seq <<< gridsize, blocksize >>> ( y_ptr, 3.0, n );
48 
49  XplusY <<< gridsize, blocksize >>> ();
50 
51  float *x_h = mycuda::host_malloc<float>( n );
52  float *y_h = mycuda::host_malloc<float>( n );
53  float *z_h = mycuda::host_malloc<float>( n );
54 
55  mycuda::copy_device_to_host( x_h, x_ptr, n );
56  mycuda::copy_device_to_host( y_h, y_ptr, n);
57  mycuda::copy_device_to_host( z_h, z_ptr, n);
58 
59  for (int i=0;i<50;i++) printf( "%8.2f + %8.2f = %8.2f\n", x_h[i], y_h[i], z_h[i]);
60  printf( "\n" );
61 
62 }
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76