Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1.  
  2. #include "cuda_runtime.h"
  3. #include "device_launch_parameters.h"
  4.  
  5. #include <stdio.h>
  6. #define SIZE (1024*1024)
  7.  
  8. __global__ void addKernel(int *c,int *a, int *b ,int n)
  9. {
  10. int i = threadIdx.x;
  11. printf("1");
  12. if (i < n)
  13. (c[i] = a[i] + b[i]);
  14.  
  15. }
  16.  
  17. int main()
  18. {
  19. int *a, *b, *c;
  20. cudaMallocManaged(&a, SIZE * sizeof(int));
  21. cudaMallocManaged(&b, SIZE * sizeof(int));
  22. cudaMallocManaged(&c, SIZE * sizeof(int));
  23. for (int i = 0; i < SIZE; i++) {
  24. a[i] = i;
  25. b[i] = i;
  26. c[i] = 0;
  27. }
  28.  
  29. addKernel <<< 1, SIZE >>> (a, b, c, SIZE);
  30. cudaDeviceSynchronize();
  31.  
  32. for (int i = 0; i < 20; i++) {
  33. printf("c[%d] = %d\n", i, c[i]);
  34. }
  35. cudaFree(a);
  36. cudaFree(b);
  37. cudaFree(c);
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement