Note that there are some explanatory texts on larger screens.

plurals
  1. POCUDA 2D array nvidia
    text
    copied!<p>I am using <code>cudaMallocPitch</code> and <code>cudaMemcpy2D</code> for 2D array. I am not sure that I have coded correct even though I could not get the output correctly. Can any one help please? Can any one debug my error? Thanks in advance. </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;cuda.h&gt; #define siz 4*sizeof(int) __global__ void addmatrix(int *m1,int *m2,size_t pitch) { int r=threadIdx.x; int *r1=m1+r*pitch; int *r2=m2+r*pitch; int c; for(c=1;c&lt;=4;c++) { r1[c]+=r2[c]; } } int main() { int i,j; int **m1_c,**m2_c; int *m1_d,*m2_d; size_t pitch; cudaError_t err; m1_c=(int **)malloc(4*sizeof(int *)); for(i=1;i&lt;=4;i++) { m1_c[i]=(int *)malloc(siz); } m2_c=(int **)malloc(4*sizeof(int *)); for(i=1;i&lt;=4;i++) { m2_c[i]=(int *)malloc(siz); } for(i=1;i&lt;=4;i++) { for(j=1;j&lt;=4;j++) { m1_c[i][j]=rand()%10; m2_c[i][j]=rand()%10; } } for(i=1;i&lt;=4;i++) { for(j=1;j&lt;=4;j++) { printf("%d\t",m1_c[i][j]); } printf("\n"); } printf("\n\n"); for(i=1;i&lt;=4;i++) { for(j=1;j&lt;=4;j++) { printf("%d\t",m2_c[i][j]); } printf("\n"); } err=cudaMallocPitch((void **)&amp;m1_d,&amp;pitch,siz,siz); err=cudaMallocPitch((void **)&amp;m2_d,&amp;pitch,siz,siz); err=cudaMemcpy2D(m1_d,pitch,m1_c,siz,siz,4,cudaMemcpyHostToDevice); err=cudaMemcpy2D(m2_d,pitch,m2_c,siz,siz,4,cudaMemcpyHostToDevice); dim3 grid(1); dim3 block(16); addmatrix&lt;&lt;&lt;grid,block&gt;&gt;&gt;(m1_d,m2_d,siz); cudaMemcpy2D(m1_c,siz,m1_d,pitch,siz,4,cudaMemcpyDeviceToHost); for(i=1;i&lt;=4;i++) { for(j=1;j&lt;=4;j++) { printf("%d\t",m1_c[i][j]); } printf("\n"); } err=cudaFree(m1_d); err=cudaFree(m2_d); err=cudaDeviceReset(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload