# Write to shared memory

**URL:** <https://forums.developer.nvidia.com/t/write-to-shared-memory/10499>\
**Category:** CUDA Programming and Performance\
**Created:** [June 30, 2009, 2:41am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499 "2009-06-30T02:41:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [June 30, 2009, 2:41am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/1 "2009-06-30T02:41:46Z")

</div>

Recently I started to learn CUDA programming, and be attracted at once. Now I want to program a FFT code with CUDA. Before this, I tested a 2-dimension using CUFFT lib, it aquire a good performance in about 25000000 clock ticks( one third for memory copy). But my code do 1-dimension FFT will cost the same time. I checked it, found that it’s very time-consuming while writing shared memory.

To avoid bank conflict, I set stride to be a big odd number, where’s my fault? Can you help me and give me some advice? thank you!

[codebox]#define MATRIX\_SIZE (1\<\<9)

#define BLOCK\_SIZE (1\<\<9)

**global**

void FFT\_2D\_Radix2(DATA\_TYPE\* dg\_buffer, int N )

{

int tid, rev, pos, pre, stride = 33;

tid = threadIdx.x;

rev = bit\_reverse3(tid, tail\_zero\_nums(N));

**shared** DATA\_TYPE s\_DataR[MATRIX\_SIZE]; // 512\*4 = 2kB

**shared** DATA\_TYPE s\_DataI[MATRIX\_SIZE]; // 512\*4 = 2kB

**shared** DATA\_TYPE s\_CosTable[MATRIX\_SIZE]; // 512\*4 = 2kB

**shared** DATA\_TYPE s\_SinTable[MATRIX\_SIZE]; // 512\*4 = 2kB

pos = tid \* stride % MATRIX\_SIZE;

s\_DataR[pos] = dg\_buffer[blockIdx.x \* BLOCK\_SIZE + rev]; //------------------------------------time-consuming

s\_DataI[pos] = dg\_buffer[N\*N + blockIdx.x \* BLOCK\_SIZE + rev]; //-----------------------------------time-consuming

float theta = GV\_2PI / N;

s\_SinTable[pos] = \_\_sinf( theta \* tid );

s\_CosTable[pos] = \_\_cosf( theta \* tid );

\_\_syncthreads();

int step, w;

for(step = 1;step\<N;step=step\*2)

{

if(tid & step)

{

w = ( tid & ( step - 1 ) ) \* stride % MATRIX\_SIZE;

DATA\_TYPE tempR = s\_DataR[pos] \* s\_CosTable[w] + s\_DataI[pos] \* s\_SinTable[w];

DATA\_TYPE tempI = s\_DataI[pos] \* s\_CosTable[w] - s\_DataR[pos] \* s\_SinTable[w];

pre = ( tid - step ) \* stride % MATRIX\_SIZE;

s\_DataR[pos] = s\_DataR[pre] - tempR; //-----------------------------------------------time-consuming

s\_DataI[pos] = s\_DataI[pre] - tempI; //-----------------------------------------------time-consuming

s\_DataR[pre] += tempR; //-----------------------------------------------time-consuming

s\_DataI[pre] += tempI; //-----------------------------------------------time-consuming

}

\_\_syncthreads();

}

}[/codebox]

---

<div class="post-metadata">

**Author:** ![Nico](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Nico](https://forums.developer.nvidia.com/u/Nico)\
**Post date:** [June 30, 2009, 8:46am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/2 "2009-06-30T08:46:55Z")

</div>

Quote from the cuda programming guide:

> [@](#):
>
> Integer division and **modulo operation** are particularly costly and should be avoided
> 
> if possible or replaced with bitwise operations whenever possible: If n is a power of
> 
> 2, (i/n) is equivalent to (i\>\>log2(n)) and (i%n) is equivalent to (i&(n-1));
> 
> the compiler will perform these conversions if n is literal.

N.

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [June 30, 2009, 9:57am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/3 "2009-06-30T09:57:00Z")

</div>

Thank you, Nico! I’ll remember these skills.

But the bottleneck is store data to shared memory. As tested, transfer data from/to host will cost 7,500,000 clock ticks.

And CUFFT execution costs about 17,000,000 ticks.

In each kernel, My code read Global Memory 2 times, read Shared Memory many times(this seems not time-consuming),  
and write Shared Memory many times(about 40 times, this costs a lot of time).

Read/Write Shared memory cost about 11,000,000 ticks.

Is that Writing will cost order of magnitude times of Reading? Or maybe my code has bank conflict?

---

<div class="post-metadata">

**Author:** ![Nico](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Nico](https://forums.developer.nvidia.com/u/Nico)\
**Post date:** [June 30, 2009, 10:18am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/4 "2009-06-30T10:18:51Z")

</div>

What DATA\_TYPE are you using?

N.

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [July 2, 2009, 5:01am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/5 "2009-07-02T05:01:05Z")

</div>

float

> [@](#):
>
> What DATA\_TYPE are you using?
> 
> N.

---

<div class="post-metadata">

**Author:** ![Nico](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Nico](https://forums.developer.nvidia.com/u/Nico)\
**Post date:** [July 2, 2009, 7:25am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/6 "2009-07-02T07:25:14Z")

</div>

> [@](#):
>
> s\_DataR[pos] = dg\_buffer[blockIdx.x \* BLOCK\_SIZE + rev]; //------------------------------------time-consuming
> 
> s\_DataI[pos] = dg\_buffer[N\*N + blockIdx.x \* BLOCK\_SIZE + rev]; //-----------------------------------time-consuming

Those global memory accesses probably aren’t coalesced.

> [@](#):
>
> DATA\_TYPE tempR = s\_DataR[pos] \* s\_CosTable[w] + s\_DataI[pos] \* s\_SinTable[w];
> 
> DATA\_TYPE tempI = s\_DataI[pos] \* s\_CosTable[w] - s\_DataR[pos] \* s\_SinTable[w];

Here are a number of bank conflicts, depending on the value of step.

If you commented out the writes to shared memory in the loop for ‘timing purposes’, then the compiler probably optimized the loop and threw away the calculation of tempR and templ entirely.

But if you uncomment them, then these calculations need to be performed (which are IMO the slow parts of the loop because the pos and pre indices do not generate any bank conflicts)

N.

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [July 2, 2009, 8:19am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/7 "2009-07-02T08:19:43Z")

</div>

Now I comment the other code, just compare the followings, and the results were also strange:

[codebox]

**global**

void FFT\_2D\_Radix2(DATA\_TYPE\* dg\_buffer, int N )

{

```
int tid, rev, pos, pre, stride = 33;

tid = threadIdx.x;

rev = bit_reverse3(tid, tail_zero_nums(N));

__shared__ DATA_TYPE s_DataR[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_DataI[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_CosTable[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_SinTable[MATRIX_SIZE]; // 512*4 = 2kB

float theta = GV_2PI / N;

pos = (tid * stride) & (MATRIX_SIZE - 1);

DATA_TYPE tempR, tempI;

tempR = dg_buffer[blockIdx.x * BLOCK_SIZE + rev];

tempI = dg_buffer[N*N + blockIdx.x * BLOCK_SIZE + rev];

__threadfence_block();

s_DataR[pos] = tempR;

s_DataI[pos] = tempI;

s_SinTable[pos] = __sinf( theta * tid );

s_CosTable[pos] = __cosf( theta * tid );

```

}

[/codebox]

timing: 11,500,000 ticks

[codebox]

**global**

void FFT\_2D\_Radix2(DATA\_TYPE\* dg\_buffer, int N )

{

```
int tid, rev, pos, pre, stride = 33;

tid = threadIdx.x;

rev = bit_reverse3(tid, tail_zero_nums(N));

__shared__ DATA_TYPE s_DataR[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_DataI[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_CosTable[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_SinTable[MATRIX_SIZE]; // 512*4 = 2kB

float theta = GV_2PI / N;

pos = (tid * stride) & (MATRIX_SIZE - 1);

DATA_TYPE tempR, tempI;

tempR = dg_buffer[blockIdx.x * BLOCK_SIZE + rev];

tempI = dg_buffer[N*N + blockIdx.x * BLOCK_SIZE + rev];

__threadfence_block();

s_DataR[pos] = tid;

s_DataI[pos] = tid;

s_SinTable[pos] = __sinf( theta * tid );

s_CosTable[pos] = __cosf( theta * tid );

```

}

[/codebox]

timing: 8,160,000 ticks

[codebox]

**global**

void FFT\_2D\_Radix2(DATA\_TYPE\* dg\_buffer, int N )

{

```
int tid, rev, pos, pre, stride = 33;

tid = threadIdx.x;

rev = bit_reverse3(tid, tail_zero_nums(N));

__shared__ DATA_TYPE s_DataR[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_DataI[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_CosTable[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_SinTable[MATRIX_SIZE]; // 512*4 = 2kB

float theta = GV_2PI / N;

pos = (tid * stride) & (MATRIX_SIZE - 1);

DATA_TYPE tempR, tempI;

tempR = dg_buffer[blockIdx.x * BLOCK_SIZE + rev];

tempI = dg_buffer[N*N + blockIdx.x * BLOCK_SIZE + rev];

__threadfence_block();

s_SinTable[pos] = __sinf( theta * tid );

s_CosTable[pos] = __cosf( theta * tid );

```

}

[/codebox]

timing: 8,140,000 ticks

[codebox]

**global**

void FFT\_2D\_Radix2(DATA\_TYPE\* dg\_buffer, int N )

{

```
int tid, rev, pos, pre, stride = 33;

tid = threadIdx.x;

rev = bit_reverse3(tid, tail_zero_nums(N));

__shared__ DATA_TYPE s_DataR[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_DataI[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_CosTable[MATRIX_SIZE]; // 512*4 = 2kB

__shared__ DATA_TYPE s_SinTable[MATRIX_SIZE]; // 512*4 = 2kB

float theta = GV_2PI / N;

pos = (tid * stride) & (MATRIX_SIZE - 1);

DATA_TYPE tempR, tempI;

__threadfence_block();

s_DataR[pos] = tempR;

s_DataI[pos] = tempI;

s_SinTable[pos] = __sinf( theta * tid );

s_CosTable[pos] = __cosf( theta * tid );

```

}

[/codebox]

timing: 8,220,000

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [July 2, 2009, 8:59am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/8 "2009-07-02T08:59:01Z")

</div>

OK, all are because of optimization.

Thank you, Nico!

> [@](#):
>
> Those global memory accesses probably aren’t coalesced.
> 
> Here are a number of bank conflicts, depending on the value of step.
> 
> If you commented out the writes to shared memory in the loop for ‘timing purposes’, then the compiler probably optimized the loop and threw away the calculation of tempR and templ entirely.
> 
> But if you uncomment them, then these calculations need to be performed (which are IMO the slow parts of the loop because the pos and pre indices do not generate any bank conflicts)
> 
> N.

---

<div class="post-metadata">

**Author:** ![cvnguyen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cvnguyen](https://forums.developer.nvidia.com/u/cvnguyen)\
**Post date:** [July 2, 2009, 9:25am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/9 "2009-07-02T09:25:32Z")

</div>

> [@](#):
>
> s\_DataR[pos] = dg\_buffer[blockIdx.x \* BLOCK\_SIZE + rev]; //------------------------------------time-consuming
> 
> s\_DataI[pos] = dg\_buffer[N\*N + blockIdx.x \* BLOCK\_SIZE + rev]; //-----------------------------------time-consuming

Reading the global memory in bit-reverse pattern is not coalesced at all. This task would be dominant in your kernel.

You can read the input data from global memory in linear pattern first and then reorganize them in bit-reverse pattern. Otherwise, you can use the reverse butterfly algorithm.

---

<div class="post-metadata">

**Author:** ![cvnguyen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cvnguyen](https://forums.developer.nvidia.com/u/cvnguyen)\
**Post date:** [July 2, 2009, 9:48am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/10 "2009-07-02T09:48:22Z")

</div>

> [@](#):
>
> ```auto
> for(step = 1;step<N;step=step*2)
> 
> {
> 
> if(tid & step)
> 
> {
> 
> ...............
> 
> pre = ( tid - step ) * stride % MATRIX_SIZE;
> 
> s_DataR[pos] = s_DataR[pre] - tempR;	//-----------------------------------------------time-consuming 
> 
> s_DataI[pos] = s_DataI[pre] - tempI; //-----------------------------------------------time-consuming 
> 
> s_DataR[pre] += tempR; //-----------------------------------------------time-consuming 
> 
> s_DataI[pre] += tempI; //-----------------------------------------------time-consuming 
> 
> }
> 
> __syncthreads();
> 
> }
> 
> }
> 
> ```

For step \< 32, there is only 50% warp occupancy inside the above IF.

I think your ‘pre’ and ‘pos’ will be also free of bank conflict without the stride.

---

<div class="post-metadata">

**Author:** ![cvnguyen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cvnguyen](https://forums.developer.nvidia.com/u/cvnguyen)\
**Post date:** [July 2, 2009, 9:50am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/11 "2009-07-02T09:50:23Z")

</div>

> [@](#):
>
> do you have any introduction( like PDF ) about reverse butterfly algorithm?
> 
> thank you!

Your algorithm is:

[External Image](http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/images/figure8.gif "Image hosted on another site. Click to open in a new tab.")

The reverse is:

[External Image](http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/images/figure13.gif "Image hosted on another site. Click to open in a new tab.")

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [July 2, 2009, 9:57am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/12 "2009-07-02T09:57:39Z")

</div>

ok, thanks!

---

<div class="post-metadata">

**Author:** ![GoGdizzY](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GoGdizzY](https://forums.developer.nvidia.com/u/GoGdizzY)\
**Post date:** [July 2, 2009, 10:05am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/13 "2009-07-02T10:05:05Z")

</div>

Sorry, another question!

what is the benifit of the Reverse Algorithm?

It still need to re-arrange the order of the arrays after butterfly transforming.

> [@](#):
>
> Your algorithm is:
> 
> ![](https://global.discourse-cdn.com/nvidia/original/3X/5/0/50c533eac59a4f1bf8100145f4cfb1f925b42d71.gif)
> 
> The reverse is:
> 
> ![](https://global.discourse-cdn.com/nvidia/original/3X/4/2/427b482153d0ad700ceb443966d04d79c5201121.gif)

---

<div class="post-metadata">

**Author:** ![cvnguyen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cvnguyen](https://forums.developer.nvidia.com/u/cvnguyen)\
**Post date:** [July 2, 2009, 10:19am UTC](https://forums.developer.nvidia.com/t/write-to-shared-memory/10499/14 "2009-07-02T10:19:08Z")

</div>

> [@](#):
>
> Sorry, another question!
> 
> what is the benifit of the Reverse Algorithm?
> 
> It still need to re-arrange the order of the arrays after butterfly transforming.

Well, both of them are similar, but I found that some people prefer one to the other.

You can make your global reads coalesced as follows:

```auto
tmp = global_data[tid];

shared_data[bit_reverse(tid)] = tmp;

```
