# shared memory problem

**URL:** <https://forums.developer.nvidia.com/t/shared-memory-problem/16168>\
**Category:** CUDA Programming and Performance\
**Created:** [April 21, 2010, 1:34am UTC](https://forums.developer.nvidia.com/t/shared-memory-problem/16168 "2010-04-21T01:34:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultralight](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ultralight](https://forums.developer.nvidia.com/u/ultralight)\
**Post date:** [April 21, 2010, 1:34am UTC](https://forums.developer.nvidia.com/t/shared-memory-problem/16168/1 "2010-04-21T01:34:17Z")

</div>

I am now solving a matrix- vector multiplication problem and try to compare shared memory with globe memory. The problem is described like this: y=A_x, A is a m_n matrix, and x is a n\*1 vector.  
For the kernel program implemented by globe memory, I have :

**global** void mv\_kernel(float \*Yd, float _Ad, float xd, int m, int n)  
{  
float sum = 0;  
int idx=threadIdx.x+blockIdx.xblockDim.x;  
if (idx\>=m) return;  
for (int j=0;j\<n;j++)  
sum += Ad[idx_n+j]\*xd[j];  
Yd[idx]=sum;  
}

And for the kernel program implemented by shared memory, I have:

**global** void mv\_kernel(float _Yd, float Ad, float xd, int m,  
int n)  
{  
float sum = 0;  
 **shared** float xds[512];  
int idx=threadIdx.x+blockIdx.xblockDim.x;  
if (idx\>=m) return;  
for (int i=0;i\<(Nâ€1)/512+1;i++) {  
if ((threadIdx.x+i512)\<N)  
xds[threadIdx] = xd[threadIdx.x+i_512];  
\_\_syncthreads();  
for (int j=0;j\<512;j++)  
if ((i_512+j\<n))  
sum += Ad[idx_n+j+i\*512]\*xds[j];  
\_\_syncthreads();  
}  
Yd[idx]=sum;  
}  
In the shared memory example, the shared memory for Xd is cut into chunks in order to avoid surpassing the memory limit.

It is expected that shared memory example should be faster than globe memory example. However, when I implement both of these programs, I find the globe memory example is always a little bit faster than shared memory example.  
Do I do something wrong?

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [April 21, 2010, 1:47am UTC](https://forums.developer.nvidia.com/t/shared-memory-problem/16168/2 "2010-04-21T01:47:34Z")

</div>

Coalescing accesses to Ad would be more important than coalescing reads of xd.

---

<div class="post-metadata">

**Author:** ![Exitios](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/exitios/32/12049_2.png) [@Exitios](https://forums.developer.nvidia.com/u/Exitios)\
**Post date:** [April 21, 2010, 3:01am UTC](https://forums.developer.nvidia.com/t/shared-memory-problem/16168/3 "2010-04-21T03:01:17Z")

</div>

Kindly stop creating duplicate threads, particularly when your concern has already been addressed by another member. Please continue to use this thread for your issue.
