# matrix multiplication--wrong answer

**URL:** <https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344>\
**Category:** CUDA Programming and Performance\
**Created:** [August 4, 2009, 4:26am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344 "2009-08-04T04:26:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmd22](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@rmd22](https://forums.developer.nvidia.com/u/rmd22)\
**Post date:** [August 4, 2009, 4:26am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/1 "2009-08-04T04:26:56Z")

</div>

I am trying to write a program for matrix multiplication. The two matrices are of 4000X4000 size. The code compiles correctly and runs without any problem, but answer seems to be wrong. I am using Tesla 1060 and host is fedora 10 (64 bit) on 64 bit AMD. Here’s my code, I would appreciate if somebody could help me with it.

#include\<stdio.h\>  
#include\<stdlib.h\>

#define BLOCK\_SIZE 16  
#define N 4000  
**global** void matmul(double \*A, double _B, double C, int wA, int wB, int hA)  
{  
int k;  
int i=blockDim.xblockIdx.x + threadIdx.x;  
int j=blockDim.y_blockIdx.y + threadIdx.y;

if(i \< wA && j \< hA)  
{  
C[j_wB+i]=0;  
for(k=0;k\<wA;k++)  
{  
C[j_wA+i]+=A[j\*wA+k]_B[k_wB+i];  
}  
}  
}

void randomInit(double\* data, int size)  
{  
for (int i = 0; i \< size; i++)  
data[i] = 1.0;  
}

int main(int argc, char_argv[]){  
int Awidth,Aheight,Bwidth,Bheight,Cwidth,Cheight;  
Awidth=N;  
Aheight=N;  
Bwidth=N;  
Bheight=N;  
Cwidth=N;  
Cheight=N;unsigned int size\_A=Awidth_Aheight\*sizeof(double);  
double _h\_A=(double_)malloc(size\_A);

unsigned int size\_B=Bwidth_Bheight_sizeof(double);  
double _h\_B=(double_)malloc(size\_B);

unsigned int size\_C=Cwidth_Cheight_sizeof(double);  
double _h\_C=(double_)malloc(size\_C);

int Awh=Awidth_Aheight;  
int Bwh=Bwidth_Bheight;

randomInit(h\_A,Awh);  
randomInit(h\_B,Bwh);

double _d\_A;  
size\_t Asize=Awidth_Aheight\*sizeof(double);  
cudaMalloc((void\*\*)&d\_A,Asize);

double _d\_B;  
size\_t Bsize=Bwidth_Bheight\*sizeof(double);  
cudaMalloc((void\*\*)&d\_B,Bsize);

double _d\_C;  
size\_t Csize=Cwidth_Cheight\*sizeof(double);  
cudaMalloc((void\*\*)&d\_C,Csize);

cudaMemcpy(d\_A, h\_A, Asize,cudaMemcpyHostToDevice);  
cudaMemcpy(d\_B, h\_B, Bsize,cudaMemcpyHostToDevice);

clock\_t c0,c1;  
c0=clock();  
dim3 dimBlock(BLOCK\_SIZE, BLOCK\_SIZE);  
dim3 dimGrid(ceil(Aheight/double(16)), ceil(Bwidth/double(16)));

matmul\<\<\<dimGrid,dimBlock, Csize\>\>\>(d\_A, d\_B, d\_C, Awidth, Bwidth, Aheight);  
cudaThreadSynchronize();  
cudaMemcpy(h\_C, d\_C, Cwidth_Cheight_sizeof(double), cudaMemcpyDeviceToHost);

c1=clock();  
double gput=((double)(c1-c0))/CLOCKS\_PER\_SEC;

printf(“GPU Matrix multiplication done in %f sec\n”,gput);  
int i;

//Check the answer for first 10 elements of C  
for(i=0;i\<10;i++){  
printf(“\n%f\n”,h\_C[i]);  
}

cudaFree(d\_A);  
cudaFree(d\_B);  
cudaFree(d\_C);  
}

---

<div class="post-metadata">

**Author:** ![David\_Lisin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@David\_Lisin](https://forums.developer.nvidia.com/u/David_Lisin)\
**Post date:** [August 11, 2009, 9:14am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/2 "2009-08-11T09:14:39Z")

</div>

Hi,  
I posted a thread with a matrix multiplication solution. [http://forums.nvidia.com/index.php?showtop](http://forums.nvidia.com/index.php?showtop)…;hl=david+lisin  
I hope this will help you!  
Good luck, David Lisin.

---

<div class="post-metadata">

**Author:** ![David\_Lisin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@David\_Lisin](https://forums.developer.nvidia.com/u/David_Lisin)\
**Post date:** [August 11, 2009, 9:17am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/3 "2009-08-11T09:17:16Z")

</div>

Hi,  
I posted a thread with a matrix multiplication solution. [http://forums.nvidia.com/index.php?showtop](http://forums.nvidia.com/index.php?showtop)…;hl=david+lisin  
I hope this will help you!  
Good luck, David Lisin.

---

<div class="post-metadata">

**Author:** ![David\_Lisin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@David\_Lisin](https://forums.developer.nvidia.com/u/David_Lisin)\
**Post date:** [August 11, 2009, 9:20am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/4 "2009-08-11T09:20:01Z")

</div>

Hi,  
I posted a thread with a matrix multiplication solution. [http://forums.nvidia.com/index.php?showtop](http://forums.nvidia.com/index.php?showtop)…;hl=david+lisin  
I hope this will help you!  
Good luck, David Lisin.

---

<div class="post-metadata">

**Author:** ![rmd22](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@rmd22](https://forums.developer.nvidia.com/u/rmd22)\
**Post date:** [August 18, 2009, 6:23am UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/5 "2009-08-18T06:23:53Z")

</div>

Hey thanks for your reply, but it’s not what I was looking for. I don’t use matlab and what I am trying is to run a basic matrix matrix multiplication program as explained in cuda tutorial and I can not understand where I am making a mistake. That’s why I posted my code here so that someone could point it out. Please help if you can.

---

<div class="post-metadata">

**Author:** ![Abdo](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Abdo](https://forums.developer.nvidia.com/u/Abdo)\
**Post date:** [August 20, 2009, 4:41pm UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/6 "2009-08-20T16:41:20Z")

</div>

> [@](#):
>
> Hey thanks for your reply, but it’s not what I was looking for. I don’t use matlab and what I am trying is to run a basic matrix matrix multiplication program as explained in cuda tutorial and I can not understand where I am making a mistake. That’s why I posted my code here so that someone could point it out. Please help if you can.

Hi rmd22

I have the same problem, the output of the matrix multiplication is zeros. Please, if u know the solution of this problem, can you send me it.

thanks a lot

Abdo

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [August 20, 2009, 5:58pm UTC](https://forums.developer.nvidia.com/t/matrix-multiplication-wrong-answer/11344/7 "2009-08-20T17:58:00Z")

</div>

> [@](#):
>
> I am trying to write a program for matrix multiplication. The two matrices are of 4000X4000 size.
> 
> …
> 
> size\_t Csize=Cwidth_Cheight_sizeof(double);
> 
> …
> 
> matmul\<\<\<dimGrid,dimBlock, Csize\>\>\>(d\_A, d\_B, d\_C, Awidth, Bwidth, Aheight);

It seems to me that the third argument of your execution configuration syntax (i.e., \<\<\<\>\>\>) indicates that you’re trying to launch a kernel with a shared memory allocation of (4000 x 4000 \* 8) bytes, which is too large. Your kernel launch is failing. Your kernel isn’t using shared memory (other than what nvcc uses for kernel arguments), so you should try running without that third argument (Csize).

I’d also recommend checking for errors. There is a section in the Programming Guide called “Error Handling”. It is Section 3.2.9 in the most recent CUDA Programming Guides.

Hope this helps,

Jeremy Furtek
