title: How to do Matrix Addition with CUDA C
body: <p>I am writing a simpled code about the addition of the elements of 2 matrices A and B; the code is quite simple and it is inspired on the example given in chapter 2 of the <a href="http://docs.nvidia.com/cuda/cuda-c-programming-guide/" rel="noreferrer">CUDA C Programming Guide</a>.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define N 2
  
  __global__ void MatAdd(int A[][N], int B[][N], int C[][N]){
             int i = threadIdx.x;
             int j = threadIdx.y;
  
             C[i][j] = A[i][j] + B[i][j];
         }
  
  
  int main(){
  
  int A[N][N] = {{1,2},{3,4}};
  int B[N][N] = {{5,6},{7,8}};
  int C[N][N] = {{0,0},{0,0}};    
  
  int (*pA)[N], (*pB)[N], (*pC)[N];
  
  cudaMalloc((void**)&amp;pA, (N*N)*sizeof(int));
  cudaMalloc((void**)&amp;pB, (N*N)*sizeof(int));
  cudaMalloc((void**)&amp;pC, (N*N)*sizeof(int));
  
  cudaMemcpy(pA, A, (N*N)*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(pB, B, (N*N)*sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(pC, C, (N*N)*sizeof(int), cudaMemcpyHostToDevice);
  
  int numBlocks = 1;
  dim3 threadsPerBlock(N,N);
  MatAdd&lt;&lt;&lt;numBlocks,threadsPerBlock&gt;&gt;&gt;(A,B,C);
  
  cudaMemcpy(C, pC, (N*N)*sizeof(int), cudaMemcpyDeviceToHost);
  
  int i, j; printf("C = \n");
  for(i=0;i&lt;N;i++){
      for(j=0;j&lt;N;j++){
          printf("%d ", C[i][j]);
      }
      printf("\n");
  }
  
  cudaFree(pA); 
  cudaFree(pB); 
  cudaFree(pC);
  
  printf("\n");
  
  return 0;
  }
  </code></pre>
  
  <p>when i run it i keep getting the initial matrix C = [0 0 ; 0 0] instead of the addition of the elements(i,j) of the 2 matrices A and B; i have previously done another example about the addition of the elements of two arrays and it seems to work fine; however this time i don't know why it does not work.</p>
  
  <p>I believe there's something wrong with the cudaMalloc command by i don't really know what else could it be.  </p>
  
  <p>Any ideas? </p>