# Bug in CUDA SDK Sample 'Matrix Multiplication (CUBLAS)'?

**URL:** <https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296>\
**Category:** CUDA Programming and Performance\
**Created:** [August 1, 2014, 4:15pm UTC](https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296 "2014-08-01T16:15:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![appletking](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@appletking](https://forums.developer.nvidia.com/u/appletking)\
**Post date:** [August 1, 2014, 4:15pm UTC](https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296/1 "2014-08-01T16:15:26Z")

</div>

Hey guys,

is it just me or is there a bug in the CUDA SDK Sample ‘Matrix Multiplication (CUBLAS)’ of CUDA v6.0 ([CUDA Samples :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cuda-samples/index.html#matrix-multiplication--cublas-))?

There we want to compute A\*B=C, where the dimensions of A, B, and C are defined as multiples of  
A: 4x2, B: 4x2, C: 4x2 (see lines 206-211). Obviously the dimensions don’t match at all and I was wondering why this even works.

From my understanding, you have to change these lines to multiples of  
A: 4x2, B: 2x4, C: 4x4 (or some other constants, which match the dimension constraints).

In addition we have to change the last argument of the function cublasSgemm (the leading dimension of C) to matrix\_size.uiWC. With these changes, everything works fine (also with other constants).

Want do you think? Is there a error in my reasoning? Sorry, if this topic is already discussed elsewhere. I didn’t found any mention of this problem in the web.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [August 1, 2014, 7:02pm UTC](https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296/2 "2014-08-01T19:02:42Z")

</div>

It’s not a bug. The sample is confusing.

If you parse out the parameters that are being passed to cublasSgemm on line 285, and study the formula here:

[url][cuBLAS :: CUDA Toolkit Documentation](http://docs.nvidia.com/cuda/cublas/index.html#cublas-level-3-function-reference%5B/url%5D)

you’ll understand what is going on. Note the parameters passed and default values:

m: matrix\_size.uiWB = 320  
n: matrix\_size.uiHA = 640  
k: matrix\_size.uiWA = 320

also note that the matrix labelled d\_A is being passed as the “B” matrix in the formula, and the matrix labelled d\_B is being passed as the “A” matrix in the formula. Combining these we have:

Formula: C=α op(A)op(B)+β C where: op(A)m× k , op(B)k× n and Cm× n (op() = no transpose here)

Sgemm: C=α d\_B(mxk) \* d\_A(kxn) +β C

So, in effect, only “half” of d\_B is being used, since it’s allocated at 320x640 but only 320x320 is being used. Note that something similar is happening in matrixMulCPU, based on the size parameters passed.

As a proof-point, change this line of code:

```
matrix_size.uiHB = 4 * block_size * iSizeMultiple;

```

to this:

```
matrix_size.uiHB = 2 * block_size * iSizeMultiple;

```

and you will see that the program continues to function normally (with a d\_B matrix that is 320x320).

---

<div class="post-metadata">

**Author:** ![appletking](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@appletking](https://forums.developer.nvidia.com/u/appletking)\
**Post date:** [October 15, 2014, 9:00am UTC](https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296/3 "2014-10-15T09:00:50Z")

</div>

Thanks for the reply. After long time I found time to deal with this problem again. I agree with your explanation.

---

<div class="post-metadata">

**Author:** ![bnsh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bnsh](https://forums.developer.nvidia.com/u/bnsh)\
**Post date:** [May 10, 2015, 5:59am UTC](https://forums.developer.nvidia.com/t/bug-in-cuda-sdk-sample-matrix-multiplication-cublas/34296/4 "2015-05-10T05:59:29Z")

</div>

I think the only real reason this works, is because uiHB is not actually being used, and 4 is _more_ than is necessary to represent the output C. Proof of this, is that changing uiHB and uiWA to anything else will cause it to fail. So, if you change say, matrix\_size.uiHB and matrix\_size.uiWA both to 7 say, then it fails. Because, the stride for matrix C should actually be matrix\_size.uiWB (or alternatively, and probably more clearly, matrix\_size.uiWC)

So, basically change line 272 and 285 from

```auto
checkCudaErrors(cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, matrix_size.uiWB, matrix_size.uiHA, matrix_size.uiWA, &alpha, d_B, matrix_size.uiWB, d_A, matrix_size.uiWA, &beta, d_C, matrix_size.uiWA));

```

to

```auto
checkCudaErrors(cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, matrix_size.uiWB, matrix_size.uiHA, matrix_size.uiWA, &alpha, d_B, matrix_size.uiWB, d_A, matrix_size.uiWA, &beta, d_C, matrix_size.uiWB));

```
