GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
mappedMemory.cu
Go to the documentation of this file.
1 
13 #include "../include/mycuda.h"
14 
15 using namespace mycuda;
16 
17 int main() {
18 
19  const int n = 8192;
20  const int gridsize = 32;
21  const int blocksize = 32;
22 
23  cudaSetDeviceFlags( cudaDeviceMapHost );
24 
25  float *x = mapped_malloc<float>(n);
26  float *y = mapped_malloc<float>(n);
27  float *z = mapped_malloc<float>(n);
28 
29  for (int i=0; i<n; i++) {
30  x[i] = 3.0f;
31  y[i] = 2*i + 7.0f;
32  z[i] = 0.0f;
33  }
34 
35  float a=3.0f, b=5.0f;
36  mycuda::aX_plus_bY <<< gridsize, blocksize >>> ( z, a, x, b, y, n );
37 
38 
39  // Show a few elements
41  for (int i=0; i<50; i++) printf( "%8.2f = %8.2f * %8.2f + %8.2f * %8.2f\n",
42  z[i], a, x[i], b, y[i] );
43 
44 
45  // Check results
46  for (int i=0;i<n;i++) assert( fabs( z[i] - (a*x[i] + b*y[i]) ) < 0.00001 );
47 
48  // Free memory
49  mapped_free( x );
50  mapped_free( y );
51  mapped_free( z );
52 }
53 
54 
55 
56 
57