# Warps and Shared Memory

**URL:** <https://forums.developer.nvidia.com/t/warps-and-shared-memory/23970>\
**Category:** CUDA Programming and Performance\
**Created:** [August 15, 2011, 11:01pm UTC](https://forums.developer.nvidia.com/t/warps-and-shared-memory/23970 "2011-08-15T23:01:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fotone](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fotone](https://forums.developer.nvidia.com/u/fotone)\
**Post date:** [August 15, 2011, 11:01pm UTC](https://forums.developer.nvidia.com/t/warps-and-shared-memory/23970/1 "2011-08-15T23:01:32Z")

</div>

Dear Cuda experts,

Sorry for the stupid question, but I can’t understand where I’m wrong.

I have a code in which there are 64 threads in which some value is compute and stored in shared memory. In the last of this threads I would like to re-read the value in the shared memory in order to compare the results.

Something like this:

```auto
__shared__ float results[64]

....

if(threadIdx.x>31 && threadIdx.x<96) {

 nthread=threadIdx.x-32;

 .....(calculations)......

 results[nthread]= xx;

 __syncthreads();

                   <<<<---------BREAK 1

 if(threadIdx.x==95) {

    for(int i=0;i<64;i++) {

       yy = results[i];

                   <<<<---------BREAK 2

    }

 }

}

.....

```

If I “extract” the contains of results using a vector in the global memory in the points indicated with BREAK 1 and BREAK 2, I get different values: in particular the first 32 are wrong while the last 32 are right.

Do you have any idea?

thanks a lot,

g.

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [August 16, 2011, 4:19am UTC](https://forums.developer.nvidia.com/t/warps-and-shared-memory/23970/2 "2011-08-16T04:19:07Z")

</div>

don’t issue \_\_syncthreads() inside a if-then-else because behavior is undefined.

try following code

```auto
__shared__ float results[64]

....

if(threadIdx.x>31 && threadIdx.x<96) {

    nthread=threadIdx.x-32;

    .....(calculations)......

    results[nthread]= xx;

}

__syncthreads();

if(threadIdx.x==95) {

    for(int i=0;i<64;i++) {

       yy = results[i];

                   <<<<---------BREAK 2

    }

}

```

---

<div class="post-metadata">

**Author:** ![RabidCicada](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/rabidcicada/32/365523_2.png) [@RabidCicada](https://forums.developer.nvidia.com/u/RabidCicada)\
**Post date:** [August 16, 2011, 2:02pm UTC](https://forums.developer.nvidia.com/t/warps-and-shared-memory/23970/3 "2011-08-16T14:02:31Z")

</div>

> [@](#):
>
> don’t issue \_\_syncthreads() inside a if-then-else because behavior is undefined.
> 
> try following code
> 
> ```auto
> __shared__ float results[64]
> 
> ....
> 
> if(threadIdx.x>31 && threadIdx.x<96) {
> 
> nthread=threadIdx.x-32;
> 
> .....(calculations)......
> 
> results[nthread]= xx;
> 
> }
> 
> __syncthreads();
> 
> if(threadIdx.x==95) {
> 
> for(int i=0;i<64;i++) {
> 
> yy = results[i];
> 
> <<<<---------BREAK 2
> 
> }
> 
> }
> 
> ```

Yeah. Technically your progam shouldn’t finish because syncthreads should wait for all threads to complete. Clearly all your threads are not giong to hit the syncthreads which should lead to “infinite wait”.
