# shared memory problems

**URL:** <https://forums.developer.nvidia.com/t/shared-memory-problems/19025>\
**Category:** CUDA Programming and Performance\
**Created:** [October 1, 2010, 5:01am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025 "2010-10-01T05:01:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kelson](https://forums.developer.nvidia.com/u/kelson)\
**Post date:** [October 1, 2010, 5:01am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/1 "2010-10-01T05:01:38Z")

</div>

Hi,

I would like to know if the following program has something wrong.  
I’m trying to do some simple scan but i can’t get the correct result

**global** void reduceKernel1(float\* \_iarray, float\* \_oarray,int N)  
{  
//shared memory array  
extern **shared** float sdata;  
int tid = threadIdx.x;  
int i = blockIdx.x \* blockDim.x + threadIdx.x;

```
//each thread load data to shared memory
if(i < blockDim.x){
	sdata[tid] = _iarray[i];
}
//synchronization 
__syncthreads();

//loop over sdata
for(int j = 1; j < blockDim.x; j *= 2)
{
	if(tid % (2*j) == 0)
	{
		sdata[tid] += sdata[tid + j] ;
		__syncthreads();
	}
	__syncthreads();
}

//write back the data to the output array 
if(tid < ARRAYSIZE )
	_oarray[i] = sdata[tid];
__syncthreads();

```

}

Thank you four your help

---

<div class="post-metadata">

**Author:** ![syoon](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@syoon](https://forums.developer.nvidia.com/u/syoon)\
**Post date:** [October 1, 2010, 4:43pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/2 "2010-10-01T16:43:56Z")

</div>

my experience is that setting memory size for the shared memory before calling kernel is one factor for errors.

> [@](#):
>
> Hi,
> 
> I would like to know if the following program has something wrong.
> 
> I’m trying to do some simple scan but i can’t get the correct result
> 
> **global** void reduceKernel1(float\* \_iarray, float\* \_oarray,int N)
> 
> {
> 
> ```
> //shared memory array 
> 
> extern __shared__ float sdata[];
> 
> int tid = threadIdx.x;
> 
> int i = blockIdx.x * blockDim.x + threadIdx.x;
> 
> //each thread load data to shared memory
> 
> if(i < blockDim.x){
> 
> sdata[tid] = _iarray[i];
> 
> }
> 
> //synchronization 
> 
> __syncthreads();
> 
> //loop over sdata
> 
> for(int j = 1; j < blockDim.x; j *= 2)
> 
> {
> 
> if(tid % (2*j) == 0)
> 
> {
> 
> sdata[tid] += sdata[tid + j] ;
> 
> __syncthreads();
> 
> }
> 
> __syncthreads();
> 
> }
> 
> //write back the data to the output array 
> 
> if(tid < ARRAYSIZE )
> 
> _oarray[i] = sdata[tid];
> 
> __syncthreads();
> 
> ```
> 
> }
> 
> Thank you four your help

---

<div class="post-metadata">

**Author:** ![syoon](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@syoon](https://forums.developer.nvidia.com/u/syoon)\
**Post date:** [October 1, 2010, 4:43pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/3 "2010-10-01T16:43:56Z")

</div>

my experience is that setting memory size for the shared memory before calling kernel is one factor for errors.

> [@](#):
>
> Hi,
> 
> I would like to know if the following program has something wrong.
> 
> I’m trying to do some simple scan but i can’t get the correct result
> 
> **global** void reduceKernel1(float\* \_iarray, float\* \_oarray,int N)
> 
> {
> 
> ```
> //shared memory array 
> 
> extern __shared__ float sdata[];
> 
> int tid = threadIdx.x;
> 
> int i = blockIdx.x * blockDim.x + threadIdx.x;
> 
> //each thread load data to shared memory
> 
> if(i < blockDim.x){
> 
> sdata[tid] = _iarray[i];
> 
> }
> 
> //synchronization 
> 
> __syncthreads();
> 
> //loop over sdata
> 
> for(int j = 1; j < blockDim.x; j *= 2)
> 
> {
> 
> if(tid % (2*j) == 0)
> 
> {
> 
> sdata[tid] += sdata[tid + j] ;
> 
> __syncthreads();
> 
> }
> 
> __syncthreads();
> 
> }
> 
> //write back the data to the output array 
> 
> if(tid < ARRAYSIZE )
> 
> _oarray[i] = sdata[tid];
> 
> __syncthreads();
> 
> ```
> 
> }
> 
> Thank you four your help

---

<div class="post-metadata">

**Author:** ![kelson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kelson](https://forums.developer.nvidia.com/u/kelson)\
**Post date:** [October 1, 2010, 5:21pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/4 "2010-10-01T17:21:38Z")

</div>

> [@](#):
>
> my experience is that setting memory size for the shared memory before calling kernel is one factor for errors.

Sir syoon ,

Thank you for your reply.

Actually I’m trying not to set the shared memory size. This is why it is declared with the extern keyword.

I don’t know if I missunderstand some principles of cuda.

---

<div class="post-metadata">

**Author:** ![kelson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kelson](https://forums.developer.nvidia.com/u/kelson)\
**Post date:** [October 1, 2010, 5:21pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/5 "2010-10-01T17:21:38Z")

</div>

> [@](#):
>
> my experience is that setting memory size for the shared memory before calling kernel is one factor for errors.

Sir syoon ,

Thank you for your reply.

Actually I’m trying not to set the shared memory size. This is why it is declared with the extern keyword.

I don’t know if I missunderstand some principles of cuda.

---

<div class="post-metadata">

**Author:** ![kmccall](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kmccall](https://forums.developer.nvidia.com/u/kmccall)\
**Post date:** [October 1, 2010, 9:43pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/6 "2010-10-01T21:43:59Z")

</div>

[codebox]

```
	if(tid % (2*j) == 0)

	{

		sdata[tid] += sdata[tid + j] ;

		__syncthreads();

	}

	__syncthreads();[/codebox]

```

Having a \_\_syncthreads() within an “if” block that doesn’t evaluate the same for all threads can have unexpected side effects.

---

<div class="post-metadata">

**Author:** ![kmccall](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kmccall](https://forums.developer.nvidia.com/u/kmccall)\
**Post date:** [October 1, 2010, 9:43pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/7 "2010-10-01T21:43:59Z")

</div>

[codebox]

```
	if(tid % (2*j) == 0)

	{

		sdata[tid] += sdata[tid + j] ;

		__syncthreads();

	}

	__syncthreads();[/codebox]

```

Having a \_\_syncthreads() within an “if” block that doesn’t evaluate the same for all threads can have unexpected side effects.

---

<div class="post-metadata">

**Author:** ![syoon](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@syoon](https://forums.developer.nvidia.com/u/syoon)\
**Post date:** [October 1, 2010, 9:50pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/8 "2010-10-01T21:50:12Z")

</div>

I think you already know more than I do…Just in case…I had some problems with shared memory before…

The following is from ‘tera’ on my question on shared memory usage…

==============

the size must be known at compile time.

What you can do is use the dynamical shared memory allocation feature. Declare a\_d as follows:

extern **shared** float a\_d;

and add the required size as third configuration parameter of the kernel invocation:

my\_kernel\<\<\<gridsize, blocksize, blocksize.x_blocksize.y_blocksize.z\*sizeof(float)\>\>\>();

Note that this only works for one variable size array.

==============

if you are sure you did right on this part, please disregard my reply.

with ‘tera’ 's help i am now doing fine using dynamic shared memory…

> [@](#):
>
> Sir syoon ,
> 
> Thank you for your reply.
> 
> Actually I’m trying not to set the shared memory size. This is why it is declared with the extern keyword.
> 
> I don’t know if I missunderstand some principles of cuda.

---

<div class="post-metadata">

**Author:** ![syoon](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@syoon](https://forums.developer.nvidia.com/u/syoon)\
**Post date:** [October 1, 2010, 9:50pm UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/9 "2010-10-01T21:50:12Z")

</div>

I think you already know more than I do…Just in case…I had some problems with shared memory before…

The following is from ‘tera’ on my question on shared memory usage…

==============

the size must be known at compile time.

What you can do is use the dynamical shared memory allocation feature. Declare a\_d as follows:

extern **shared** float a\_d;

and add the required size as third configuration parameter of the kernel invocation:

my\_kernel\<\<\<gridsize, blocksize, blocksize.x_blocksize.y_blocksize.z\*sizeof(float)\>\>\>();

Note that this only works for one variable size array.

==============

if you are sure you did right on this part, please disregard my reply.

with ‘tera’ 's help i am now doing fine using dynamic shared memory…

> [@](#):
>
> Sir syoon ,
> 
> Thank you for your reply.
> 
> Actually I’m trying not to set the shared memory size. This is why it is declared with the extern keyword.
> 
> I don’t know if I missunderstand some principles of cuda.

---

<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:** [October 2, 2010, 1:42am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/10 "2010-10-02T01:42:54Z")

</div>

How do you invoke the kernel - can you post that line of code as well?

---

<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:** [October 2, 2010, 1:42am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/11 "2010-10-02T01:42:54Z")

</div>

How do you invoke the kernel - can you post that line of code as well?

---

<div class="post-metadata">

**Author:** ![kelson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kelson](https://forums.developer.nvidia.com/u/kelson)\
**Post date:** [October 2, 2010, 4:17am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/12 "2010-10-02T04:17:42Z")

</div>

Hi sirs,

Thank for your help.  
You are completely right. I declared the shared memory external but  
was missing to add the required size during the kernel call.

before: reduceKernel1 \<\<\< blocks, threads \>\>\> ();  
after: reduceKernel1 \<\<\< blocks, threads ,ARRAYSIZE \* sizeof(float)\>\>\> ();  
Now everything works normally.

Thank you!

---

<div class="post-metadata">

**Author:** ![kelson](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kelson](https://forums.developer.nvidia.com/u/kelson)\
**Post date:** [October 2, 2010, 4:17am UTC](https://forums.developer.nvidia.com/t/shared-memory-problems/19025/13 "2010-10-02T04:17:42Z")

</div>

Hi sirs,

Thank for your help.  
You are completely right. I declared the shared memory external but  
was missing to add the required size during the kernel call.

before: reduceKernel1 \<\<\< blocks, threads \>\>\> ();  
after: reduceKernel1 \<\<\< blocks, threads ,ARRAYSIZE \* sizeof(float)\>\>\> ();  
Now everything works normally.

Thank you!
