GPU Workshop Sample Code
 All Data Structures Namespaces Files Functions Variables Macros Pages
simpleMath.cu
Go to the documentation of this file.
1 
4 
5 #include "../include/mycuda.h"
6 
7 using namespace mycuda;
8 
9 
10 
11 int main()
12 {
13  const int n = 8192;
14  const int gridsize = 32;
15  const int blocksize = 32;
16 
17 
18  float *x_h = host_malloc<float>(n);
19  float *y_h = host_malloc<float>(n);
20  float *z_h = host_malloc<float>(n);
21 
22  float *x_d = device_malloc<float>(n);
23  float *y_d = device_malloc<float>(n);
24  float *z_d = device_malloc<float>(n);
25 
26 
27  float a=2.0f, b=1.0f;
28  fill <<< gridsize, blocksize >>> ( x_d, 3.0f, n );
29  seq <<< gridsize, blocksize >>> ( y_d, 5.0f, 2.0f );
30 
31  copy_device_to_host( x_h, x_d, n );
32  copy_device_to_host( y_h, y_d, n );
33 
34  exp_X <<< gridsize, blocksize >>> ( x_d, x_d, n );
35  aX_plus_bY <<< gridsize, blocksize >>> ( z_d, a, x_d, b, y_d, n );
36 
37  copy_device_to_host( z_h, z_d, n );
38 
39 
40  // Show a few elements
41  for (int i=0; i<50; i++) printf( "%8.2f = %8.2f * exp(%8.2f) + %8.2f * %8.2f\n",
42  z_h[i], a, x_h[i], b, y_h[i] );
43 
44 
45  // Check results
46  for (int i=0;i<n;i++) assert( fabs( z_h[i] - (a*exp(x_h[i]) + b*y_h[i]) ) < 0.00001 );
47 
48  // Free memory
49  device_free( x_d );
50  device_free( y_d );
51  device_free( z_d );
52  host_free(x_h);
53  host_free(y_h);
54  host_free(z_h);
55 }
56 
57 
58 
59 
60