# atomicCAS() doesn't work!

**URL:** <https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828>\
**Category:** CUDA Programming and Performance\
**Created:** [July 20, 2010, 2:47pm UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828 "2010-07-20T14:47:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiulioPU](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GiulioPU](https://forums.developer.nvidia.com/u/GiulioPU)\
**Post date:** [July 20, 2010, 2:47pm UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828/1 "2010-07-20T14:47:32Z")

</div>

I am trying to use atomicCAS() to sum elements stored in global memory inside a kernel(the vector is the result of a parallel reduction…), but it doesnt work!

the code is easy:

[codebox] **device** int lock=0;

**device** \_ float square\_norm=0;

**global** mykernel(…){

…

if(tid==0){

do{}while(atomicCAS(&lock,0,1));//setlock

square\_norm += temp[0]

\_\_threadfence();//waitforwritecompletion

lock=0;//freelock

}

}

[/codebox]

Do you find where is the problem?

Thanks

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [July 20, 2010, 4:50pm UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828/2 "2010-07-20T16:50:05Z")

</div>

This can deadlock due to warp-divergence and the code’s reliance on un-defined behavior.  
All these have been discussed long long time back. Try searching…

---

<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:** [July 21, 2010, 4:19am UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828/3 "2010-07-21T04:19:27Z")

</div>

> [@](#):
>
> I am trying to use atomicCAS() to sum elements stored in global memory inside a kernel(the vector is the result of a parallel reduction…), but it doesnt work!
> 
> the code is easy:
> 
> [codebox]
> 
> **device** int lock=0;
> 
> **device** \_ float square\_norm=0;
> 
> **global** mykernel(…){
> 
> …
> 
> if(tid==0){
> 
> do{}while(atomicCAS(&lock,0,1));//setlock
> 
> square\_norm += temp[0]
> 
> \_\_threadfence();//waitforwritecompletion
> 
> lock=0;//freelock
> 
> }
> 
> }
> 
> [/codebox]

(1)

don’t not use “lock=0” when fee lock.

try

```auto
atomicCAS(&lock,1,0)); // free lock

```

(2) if above modification does not work, then try to allocate lock outside the kernel.

---

<div class="post-metadata">

**Author:** ![GiulioPU](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@GiulioPU](https://forums.developer.nvidia.com/u/GiulioPU)\
**Post date:** [July 21, 2010, 8:44am UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828/4 "2010-07-21T08:44:24Z")

</div>

Whou! I have found the answer(…maybe…) in a topic after 20 replies!

[url=“[http://forums.nvidia.com/index.php?showtopic=98444](http://forums.nvidia.com/index.php?showtopic=98444)”][The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtopic=98444%5B/url%5D)

LSChien, thanks for your reply but I guess your code doesnt work, check the above topic.

Sarnath, PLEASE :( can you post the final WORKING code of a spinlock in CUDA? Many thanks!

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [July 22, 2010, 5:31pm UTC](https://forums.developer.nvidia.com/t/atomiccas-doesnt-work/17828/5 "2010-07-22T17:31:35Z")

</div>

GiulioPU,  
Unfortunately, I dont have that code now… I think ‘tmurray’ posted it in that topic… Check out…  
Its difficult and tiresome…You may need to spend 1 or 2 days to get it working. Good Luck!
