# Matrix Addition

**URL:** <https://forums.developer.nvidia.com/t/matrix-addition/26908>\
**Category:** CUDA Programming and Performance\
**Created:** [June 14, 2012, 2:32am UTC](https://forums.developer.nvidia.com/t/matrix-addition/26908 "2012-06-14T02:32:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfshark](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wolfshark](https://forums.developer.nvidia.com/u/wolfshark)\
**Post date:** [June 14, 2012, 2:32am UTC](https://forums.developer.nvidia.com/t/matrix-addition/26908/1 "2012-06-14T02:32:30Z")

</div>

Hi, I am very fresh in learning CUDA and I need some help adding matrices. So far i have this as my adding function:

#define N 3  
const dim3 threadsPerBlock(N, N);  
const dim3 numBlocks(N / threadsPerBlock.x, N / threadsPerBlock.y);

**global** void compute(int a[N][N], int b[N][N], int c[N][N])  
{  
int i = blockIdx.x \* blockDim.x + threadIdx.x;  
int j = blockIdx.y \* blockDim.y + threadIdx.y;  
if (i \< N && j \< N)  
c[i][j] = a[i][j] + b[i][j];  
}

It is very similar to the NVIDIA programming guide example.  
From there i have:

int main(void)  
{  
int a[N][N], b[N][N], c[N][N];  
int dev\_a[N][N], dev\_b[N][N], dev\_c[N][N];

```
cudaMalloc( (void**)&dev_a, (N*N)*sizeof(int) );
cudaMalloc( (void**)&dev_b, (N*N)*sizeof(int) );
cudaMalloc( (void**)&dev_c, (N*N)*sizeof(int) );

    //THEN I FILL THE MATRICES UP WITH RANDOM NUMBERS

```

and finish off with this:

cudaMemcpy(dev\_a, a, (N\*N)_sizeof(int), cudaMemcpyHostToDevice);  
cudaMemcpy(dev\_b, b, (N_N)\*sizeof(int), cudaMemcpyHostToDevice);

```
compute<<<numBlocks,threadsPerBlock>>>(dev_a,dev_b,dev_c);

cudaMemcpy(c,dev_c, (N*N)*sizeof(int), cudaMemcpyDeviceToHost);

```

The addition is not happening. Can anyone see where i went wrong? Thanks.

---

<div class="post-metadata">

**Author:** ![sjiagc](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sjiagc](https://forums.developer.nvidia.com/u/sjiagc)\
**Post date:** [June 14, 2012, 3:21am UTC](https://forums.developer.nvidia.com/t/matrix-addition/26908/2 "2012-06-14T03:21:55Z")

</div>

Hi wolfshark,

Pointers to device memory can not be defined in this style:  
int dev\_a[N][N], dev\_b[N][N], dev\_c[N][N];

It defines 2D array, not pointers, you should write like this:  
int \*pdev\_a, \*pdev\_b, \*pdev\_c;

Best regards!

---

<div class="post-metadata">

**Author:** ![Gert-Jan](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Gert-Jan](https://forums.developer.nvidia.com/u/Gert-Jan)\
**Post date:** [June 14, 2012, 8:17am UTC](https://forums.developer.nvidia.com/t/matrix-addition/26908/3 "2012-06-14T08:17:53Z")

</div>

Looks quite good to me, as sjiagc said, device pointers need to be changed.

This is how I should do it (not tested). I prefer 1-D arrays, and calculate the indices myself. And always check the results of cuda…() functions, it will save you a lot of debugging time.

```auto
#define N 1024

const dim3 threadsPerBlock(16, 16);

const dim3 numBlocks(N / threadsPerBlock.x, N / threadsPerBlock.y); // think about rounding when N is not a multiple of threadsPerBlock

__global__ void compute(int *a, int *b, int *c)

{

    int i = blockIdx.x * blockDim.x + threadIdx.x; 

    int j = blockIdx.y * blockDim.y + threadIdx.y;

    //if (i < N && j < N) // not (yet) needed, will always evaluate to true in this example

    c[j*N+i] = a[j*N+i] + b[j*N+i];

} 

int main(void)

{

    cudaError_t err;

    int *a, *b, *c;

    int *dev_a, *dev_b, *dev_c;

a = (int*)malloc(N*N*sizeof(int)); // malloc because N is 'large'

    b = (int*)malloc(N*N*sizeof(int));

    c = (int*)malloc(N*N*sizeof(int));

err = cudaMalloc( (void**)&dev_a, (N*N)*sizeof(int) );

    if(err != cudaSuccess) return -1;

    err = cudaMalloc( (void**)&dev_b, (N*N)*sizeof(int) );

    if(err != cudaSuccess) return -1;

    err = cudaMalloc( (void**)&dev_c, (N*N)*sizeof(int) );

    if(err != cudaSuccess) return -1;

//THEN I FILL THE MATRICES UP WITH RANDOM NUMBERS

    //and finish off with this:

err = cudaMemcpy(dev_a, a, (N*N)*sizeof(int), cudaMemcpyHostToDevice);

    if(err != cudaSuccess) return -1;

    err = cudaMemcpy(dev_b, b, (N*N)*sizeof(int), cudaMemcpyHostToDevice);

    if(err != cudaSuccess) return -1;

compute<<<numBlocks,threadsPerBlock>>>(dev_a, dev_b, dev_c);

err = cudaMemcpy(c,dev_c, (N*N)*sizeof(int), cudaMemcpyDeviceToHost);

    if(err != cudaSuccess) return -1;

    return 0;

}

```
