Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. $ cat noworky.cu
  2. #include <iostream>
  3. using namespace std;
  4. __global__ void add(int *a, int *b, int *c)
  5. {
  6. *c = *a + *b;
  7. }
  8.  
  9. int main(void) {
  10. int a, b, c; // host copies of a, b, c
  11. int *d_a, *d_b, *d_c; // device copies of a, b, c
  12. int size = sizeof(int);
  13.  
  14. // Allocate space for device copies of a, b, c
  15. cudaMalloc((void **)&d_a, size);
  16. cudaMalloc((void **)&d_b, size);
  17. cudaMalloc((void **)&d_c, size);
  18.  
  19. // Setup input values
  20. a = 2;
  21. b = 7;
  22.  
  23.  
  24. cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
  25. cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
  26.  
  27. // Launch add() kernel on GPU
  28. add<<<1,1>>>(d_a, d_b, d_c);
  29.  
  30. // Copy result back to host
  31. cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
  32. cout << "answer is " << c <<endl;
  33. // Cleanup
  34. cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
  35.  
  36.  
  37. //return 0;
  38. }
  39.  
  40. $ nvcc -arch=sm_52 -o noworky noworky.cu
  41. $ ./noworky
  42. answer is 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement