Guest User

Untitled

a guest
Jul 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. cudaMallocPitch((void**)&device_a, &pitch, 2*sizeof(int),2);
  2. cudaMallocPitch((void**)&device_b, &pitch, 2*sizeof(int),2);
  3. cudaMallocPitch((void**)&device_c, &pitch, 2*sizeof(int),2);
  4.  
  5. __global__ void add(int *dev_a ,int *dev_b,int* dec_c)
  6. {
  7. for i=0;i<2;i++)
  8. {
  9. for j=0;j<2;j++)
  10. {
  11. dev_c[i][j]=dev_a[i][j]+dev_b[i][j];
  12. }
  13. }
  14. }
  15.  
  16. __global___ void add(int *dev_a[] ,int *dev_b[], int* dec_c[])
  17. {
  18. for i=0;i<2;i++) {
  19. for j=0;j<2;j++) {
  20. dev_c[i][j]=dev_a[i][j]+dev_b[i][j];
  21. }
  22. }
  23. }
  24.  
  25. int ** h_a = (int **)malloc(2 * sizeof(int *));
  26. cudaMalloc((void**)&h_a[0], 2*sizeof(int));
  27. cudaMalloc((void**)&h_a[1], 2*sizeof(int));
  28.  
  29. int **d_a;
  30. cudaMalloc((void ***)&d_a, 2 * sizeof(int *));
  31. cudaMemcpy(d_a, h_a, 2*sizeof(int *), cudaMemcpyHostToDevice);
  32.  
  33. #include <cstdio>
  34. __global__ void add(int * dev_a[], int * dev_b[], int * dev_c[])
  35. {
  36. for(int i=0;i<2;i++)
  37. {
  38. for(int j=0;j<2;j++)
  39. {
  40. dev_c[i][j]=dev_a[i][j]+dev_b[i][j];
  41. }
  42. }
  43. }
  44.  
  45. inline void GPUassert(cudaError_t code, char * file, int line, bool Abort=true)
  46. {
  47. if (code != 0) {
  48. fprintf(stderr, "GPUassert: %s %s %dn", cudaGetErrorString(code),file,line);
  49. if (Abort) exit(code);
  50. }
  51. }
  52.  
  53. #define GPUerrchk(ans) { GPUassert((ans), __FILE__, __LINE__); }
  54.  
  55. int main(void)
  56. {
  57. const int aa[2][2]={{1,2},{3,4}};
  58. const int bb[2][2]={{5,6},{7,8}};
  59. int cc[2][2];
  60.  
  61. int ** h_a = (int **)malloc(2 * sizeof(int *));
  62. for(int i=0; i<2;i++){
  63. GPUerrchk(cudaMalloc((void**)&h_a[i], 2*sizeof(int)));
  64. GPUerrchk(cudaMemcpy(h_a[i], &aa[i][0], 2*sizeof(int), cudaMemcpyHostToDevice));
  65. }
  66.  
  67. int **d_a;
  68. GPUerrchk(cudaMalloc((void ***)&d_a, 2 * sizeof(int *)));
  69. GPUerrchk(cudaMemcpy(d_a, h_a, 2*sizeof(int *), cudaMemcpyHostToDevice));
  70.  
  71. int ** h_b = (int **)malloc(2 * sizeof(int *));
  72. for(int i=0; i<2;i++){
  73. GPUerrchk(cudaMalloc((void**)&h_b[i], 2*sizeof(int)));
  74. GPUerrchk(cudaMemcpy(h_b[i], &bb[i][0], 2*sizeof(int), cudaMemcpyHostToDevice));
  75. }
  76.  
  77. int ** d_b;
  78. GPUerrchk(cudaMalloc((void ***)&d_b, 2 * sizeof(int *)));
  79. GPUerrchk(cudaMemcpy(d_b, h_b, 2*sizeof(int *), cudaMemcpyHostToDevice));
  80.  
  81. int ** h_c = (int **)malloc(2 * sizeof(int *));
  82. for(int i=0; i<2;i++){
  83. GPUerrchk(cudaMalloc((void**)&h_c[i], 2*sizeof(int)));
  84. }
  85.  
  86. int ** d_c;
  87. GPUerrchk(cudaMalloc((void ***)&d_c, 2 * sizeof(int *)));
  88. GPUerrchk(cudaMemcpy(d_c, h_c, 2*sizeof(int *), cudaMemcpyHostToDevice));
  89.  
  90. add<<<1,1>>>(d_a,d_b,d_c);
  91. GPUerrchk(cudaPeekAtLastError());
  92.  
  93. for(int i=0; i<2;i++){
  94. GPUerrchk(cudaMemcpy(&cc[i][0], h_c[i], 2*sizeof(int), cudaMemcpyDeviceToHost));
  95. }
  96.  
  97. for(int i=0;i<2;i++) {
  98. for(int j=0;j<2;j++) {
  99. printf("(%d,%d):%dn",i,j,cc[i][j]);
  100. }
  101. }
  102.  
  103. return cudaThreadExit();
  104. }
Add Comment
Please, Sign In to add comment