Guest User

Untitled

a guest
Nov 3rd, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const int N = 16;
  4. const int blocksize = 16;
  5.  
  6. __global__
  7. void hello(char *a, int *b)
  8. {
  9.     a[threadIdx.x] += b[threadIdx.x];
  10. }
  11.  
  12. int main()
  13. {
  14.     char a[N] = "Hello \0\0\0\0\0\0";
  15.     int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  16.  
  17.     char *ad;
  18.     int *bd;
  19.     const int csize = N*sizeof(char);
  20.     const int isize = N*sizeof(int);
  21.  
  22.     printf("%s", a);
  23.  
  24.     cudaMalloc( (void**)&ad, csize );
  25.     cudaMalloc( (void**)&bd, isize );
  26.     cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice );
  27.     cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice );
  28.    
  29.     dim3 dimBlock( blocksize, 1 );
  30.     dim3 dimGrid( 1, 1 );
  31.     hello<<<dimGrid, dimBlock>>>(ad, bd);
  32.     cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost );
  33.     cudaFree( ad );
  34.     cudaFree( bd );
  35.    
  36.     printf("%s\n", a);
  37.     return EXIT_SUCCESS;
  38. }
Add Comment
Please, Sign In to add comment