Advertisement
Guest User

Untitled

a guest
Sep 28th, 2014
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. $ cat t579.cu
  2. #include <stdio.h>
  3.  
  4. __global__ void kernel(int *d_A, size_t pitch, int rows, int cols){
  5. //compute the row
  6. int r = blockIdx.y*blockDim.y+threadIdx.y;
  7. //compute the column
  8. int c = blockIdx.x*blockDim.x+threadIdx.x;
  9.  
  10. if((r < rows) && (c < cols)){
  11. // // update the pointer to point to the beginning of the row
  12. int *Row = (int*)((char*)d_A + r*pitch);
  13. int elem = Row[c];
  14. printf("%d ", elem);
  15. }
  16. }
  17.  
  18. int main(){
  19.  
  20. int *d_A, *A;
  21. size_t pitch;
  22. int rows = 4;
  23. int cols = 4;
  24. A = (int *)malloc(rows*cols*sizeof(int));
  25. for (int i = 0; i < rows*cols; i++) A[i] = i;
  26. cudaMallocPitch((void**)&d_A, &pitch, sizeof(int)*cols, rows);
  27. cudaMemcpy2D(d_A, pitch, A, sizeof(int)*cols, sizeof(int)*cols, rows, cudaMemcpyHostToDevice);
  28. dim3 block(16,16);
  29. dim3 grid(1,1);
  30. kernel<<<grid,block>>>(d_A, pitch, rows, cols);
  31. cudaDeviceSynchronize();
  32. printf("\nDone!\n");
  33. return 0;
  34. }
  35.  
  36. $ nvcc -arch=sm_20 -o t579 t579.cu
  37. $ cuda-memcheck ./t579
  38. ========= CUDA-MEMCHECK
  39. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  40. Done!
  41. ========= ERROR SUMMARY: 0 errors
  42. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement