Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
346
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cassert>
  3. #include <cuda.h>
  4.  
  5. #define HANDLE_ERROR(x) assert(x == cudaSuccess);
  6. #define THREADS 1
  7. #define BLK 1000
  8. #define N BLK * THREADS
  9. #define MEMCPY 0
  10.  
  11. using namespace std;
  12.  
  13. __global__ void add(int *da, int *db, int *dc)
  14. {
  15.     int i = blockIdx.x * blockDim.x + threadIdx.x;
  16.     if (i < N)
  17.         dc[i] = da[i] + db[i];
  18. }
  19.  
  20. int main(void)
  21. {
  22.     // ensure device supports mapped pinned memory & set appropriate flag
  23.     cudaDeviceProp deviceProp;
  24.     cudaGetDeviceProperties(&deviceProp, 0);
  25.     if (!deviceProp.canMapHostMemory)
  26.         exit(1);
  27.     cudaSetDeviceFlags(cudaDeviceMapHost);
  28.  
  29.     // valloc for page-aligned memory
  30.     int *a, *b, *c, *da, *db, *dc;
  31.     a = (int*)valloc(N * sizeof(int));
  32.     b = (int*)valloc(N * sizeof(int));
  33.     c = (int*)valloc(N * sizeof(int));
  34.  
  35.     // register host memory & get device pointers to it
  36.     int bytes = (N * sizeof(int) + 4095)/4096;
  37.     HANDLE_ERROR(cudaHostRegister((void*)a, bytes, cudaHostRegisterMapped));
  38.     HANDLE_ERROR(cudaHostRegister((void*)b, bytes, cudaHostRegisterMapped));
  39.     HANDLE_ERROR(cudaHostRegister((void*)c, bytes, cudaHostRegisterMapped));
  40.     HANDLE_ERROR(cudaHostGetDevicePointer((void**)&da, a, 0));
  41.     HANDLE_ERROR(cudaHostGetDevicePointer((void**)&db, b, 0));
  42.     HANDLE_ERROR(cudaHostGetDevicePointer((void**)&dc, c, 0));
  43.  
  44.     // initialise vectors on host end
  45.     for (int i = 0; i < N; i++)
  46.     {
  47.         a[i] = 2*i;
  48.         b[i] = -i;
  49.     }
  50.     add<<<BLK,THREADS>>>(da, db, dc);
  51.  
  52.     // ensure vector addition happened properly
  53.     for (int i = 0; i < N; i++)
  54.     {
  55.         if (c[i] != i)
  56.         {
  57.             cout << "Failed @ idx " << i << ", c[i] = " << c[i] << endl;
  58.             assert(c[i] == i);
  59.         }
  60.     }
  61.    
  62.     // clean up resources
  63.     HANDLE_ERROR(cudaHostUnregister(a));
  64.     HANDLE_ERROR(cudaHostUnregister(b));
  65.     HANDLE_ERROR(cudaHostUnregister(c));
  66.     free(a);
  67.     free(b);
  68.     free(c);
  69.     cout << "Success." << endl;
  70.     return 0;
  71. }
Advertisement
RAW Paste Data Copied
Advertisement