Advertisement
Guest User

Untitled

a guest
Apr 11th, 2015
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. $ cat t712.cu
  2. #include <iostream>
  3.  
  4. #define ROWS 20
  5. #define COLS 10
  6.  
  7. __global__ void kernel(const unsigned int size, float* matrix, const float* vector)
  8. {
  9. // get the current element index for the thread
  10. unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
  11. if (idx < size)
  12. {
  13. // sum the current element with the
  14. matrix[idx] += vector[threadIdx.x];
  15. }
  16. }
  17.  
  18. int main(){
  19.  
  20. float *h_mat, *d_mat, *h_vec, *d_vec;
  21. const unsigned int msz = ROWS*COLS*sizeof(float);
  22. const unsigned int vsz = COLS*sizeof(float);
  23. h_mat = (float *)malloc(msz);
  24. h_vec = (float *)malloc(vsz);
  25. cudaMalloc(&d_mat, msz);
  26. cudaMalloc(&d_vec, vsz);
  27. cudaMemset(d_mat, 0, msz); // set matrix to zero
  28. for (int i=0; i<COLS; i++) h_vec[i] = i;
  29. cudaMemcpy(d_vec, h_vec, vsz, cudaMemcpyHostToDevice);
  30. kernel<<<ROWS,COLS>>>(ROWS*COLS, d_mat, d_vec);
  31. cudaMemcpy(h_mat, d_mat, msz, cudaMemcpyDeviceToHost);
  32. for (int i = 0; i < ROWS; i++){
  33. for (int j = 0; j < COLS; j++) std::cout << h_mat[i*COLS+j] << " ";
  34. std::cout << std::endl;}
  35. return 0;
  36. }
  37. $ nvcc -o t712 t712.cu
  38. $ cuda-memcheck ./t712
  39. ========= CUDA-MEMCHECK
  40. 0 1 2 3 4 5 6 7 8 9
  41. 0 1 2 3 4 5 6 7 8 9
  42. 0 1 2 3 4 5 6 7 8 9
  43. 0 1 2 3 4 5 6 7 8 9
  44. 0 1 2 3 4 5 6 7 8 9
  45. 0 1 2 3 4 5 6 7 8 9
  46. 0 1 2 3 4 5 6 7 8 9
  47. 0 1 2 3 4 5 6 7 8 9
  48. 0 1 2 3 4 5 6 7 8 9
  49. 0 1 2 3 4 5 6 7 8 9
  50. 0 1 2 3 4 5 6 7 8 9
  51. 0 1 2 3 4 5 6 7 8 9
  52. 0 1 2 3 4 5 6 7 8 9
  53. 0 1 2 3 4 5 6 7 8 9
  54. 0 1 2 3 4 5 6 7 8 9
  55. 0 1 2 3 4 5 6 7 8 9
  56. 0 1 2 3 4 5 6 7 8 9
  57. 0 1 2 3 4 5 6 7 8 9
  58. 0 1 2 3 4 5 6 7 8 9
  59. 0 1 2 3 4 5 6 7 8 9
  60. ========= ERROR SUMMARY: 0 errors
  61. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement