# Problem with shared memory

**URL:** <https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918>\
**Category:** CUDA Programming and Performance\
**Created:** [October 23, 2015, 1:50pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918 "2015-10-23T13:50:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredgentil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fredgentil](https://forums.developer.nvidia.com/u/fredgentil)\
**Post date:** [October 23, 2015, 1:50pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/1 "2015-10-23T13:50:17Z")

</div>

I am new to CUDA and am trying to use shared memory in matrix multiplication and the result comes out always 0 (zero). When using global memory the result is correct. My graphic board is NVS3100.

The values of SIZE and Width = 16 - TILE\_DIM = 4.

With SHARED MEMORY - Problem  
**global** void MatrixMul(int \*Md, int \*Nd, int \*Pd, int Width) {

```
int bx = blockIdx.x; 
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;

__shared__ int Ad;
__shared__ int Bd;

int Row = by*TILE_DIM + ty;
int Col = bx*TILE_DIM + tx;

int PValue = 0;

for (int k = 0; k < Width / 2; k++) {
	Ad[ty][tx] = Md[Row*Width + (k*Width + tx)];
	Bd[ty][tx] = Nd[(k*Width + ty)*Width + Col];
	__syncthreads();
}

for (int k = 0; k<Width; ++k){
	PValue += Ad[tx][k] * Bd[k][ty];
	__syncthreads();
}
Pd[Row*Width+Col] = PValue;

```

}

With GLOBAL MEMORY - OK  
**global** void MatrixMul(int \*Md, int \*Nd, int \*Pd, int Width) {  
int bx = blockIdx.x; int by = blockIdx.y;  
int tx = threadIdx.x; int ty = threadIdx.y;

```
int Row = by*TILE_DIM + ty;
int Col = bx*TILE_DIM + tx;

int PValue = 0;

for (int k = 0; k < Width; ++k) {
	PValue += Md[Row*Width + k] * Nd[k*Width + Col];
}
__syncthreads();

Pd[Row*Width+Col] = PValue;

```

}

Helpme, Plaese!!!

---

<div class="post-metadata">

**Author:** ![little\_jimmy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@little\_jimmy](https://forums.developer.nvidia.com/u/little_jimmy)\
**Post date:** [October 23, 2015, 2:18pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/2 "2015-10-23T14:18:39Z")

</div>

perhaps you are out of shared memory  
what cc is NVS3100?

see if the kernel even runs - either with proper error checking, or by adding a breakpoint on the first kernel line, and using the debugger

or, perhaps your indices are wrong, when using shared memory  
easiest seems to simply add a breakpoint to

PValue += Ad[tx][k] \* Bd[k][ty];

and noting whether the value actually increments, for different threads

---

<div class="post-metadata">

**Author:** ![fredgentil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fredgentil](https://forums.developer.nvidia.com/u/fredgentil)\
**Post date:** [October 23, 2015, 2:54pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/3 "2015-10-23T14:54:19Z")

</div>

The breakpoint inside the Kernel does not stop the execution … :-((

---

<div class="post-metadata">

**Author:** ![little\_jimmy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@little\_jimmy](https://forums.developer.nvidia.com/u/little_jimmy)\
**Post date:** [October 23, 2015, 3:07pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/4 "2015-10-23T15:07:10Z")

</div>

add a breakpoint after the kernel launch, in the host code

if that breakpoint is hit, and the debugger does not jump to the breakpoint in the kernel, it is safe to assume the kernel launch failed  
you can then verify that via proper error checking - cudaGetLastError(), etc

---

<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:** [October 23, 2015, 3:59pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/5 "2015-10-23T15:59:09Z")

</div>

As little jimmy has indicated, you ought to be doing proper cuda error checking, before asking others for help. Not sure what that is? google “proper cuda error checking” and take the first hit.

You can also try running your code as-is with cuda-memcheck.

---

<div class="post-metadata">

**Author:** ![fredgentil](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fredgentil](https://forums.developer.nvidia.com/u/fredgentil)\
**Post date:** [October 23, 2015, 4:52pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/6 "2015-10-23T16:52:18Z")

</div>

I’m using error handling (gpuErrchk) in allocations and transfers Host-\> Devide and Device-\> Host and the error does not occur. I think the error is in the transfer to the shared memory.

---

<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:** [October 23, 2015, 7:53pm UTC](https://forums.developer.nvidia.com/t/problem-with-shared-memory/39918/7 "2015-10-23T19:53:28Z")

</div>

The error checking on allocations and transfers is not sufficient to catch all possible issues. Perhaps you should actually look at proper cuda error checking, and implement it.

Perhaps you should provide a complete code, rather than just the kernel.

Perhaps you should run your code with cuda-memcheck.
