# Volatile - when to use? (Regarding registers)

**URL:** <https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993>\
**Category:** CUDA Programming and Performance\
**Created:** [April 13, 2011, 2:51pm UTC](https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993 "2011-04-13T14:51:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phw\_89](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@phw\_89](https://forums.developer.nvidia.com/u/phw_89)\
**Post date:** [April 13, 2011, 2:51pm UTC](https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993/1 "2011-04-13T14:51:04Z")

</div>

I’m aware of the ‘volatile trick’ to reduce register usage in certain situations.

(See: [Topic 1](http://forums.nvidia.com/index.php?showtopic=89573), [Topic 2](http://forums.nvidia.com/index.php?showtopic=99209) )

However, I’m slightly confused as to when using the volatile keyword will increase performance, and when using it will decrease performance. (Assume we have 100% occupancy already).

My understanding is the volatile keyword forces the variable to be stored in a register and then every time the variable is used in the code it is fetched from the register. If a variable isn’t declared volatile then it may or may not be inlined or stored in a register. Is this correct?

Basically, I’ve seen people saying that using volatile on every variable will result in a performance hit. How do I know which variables will benefit from being volatile?

Is it the ones that are used the most? If so, how many times does the variable have to be used to gain any benefit from being volatile?

Or will variables that are computed from a global read (i.e. float tmp = A[i]; where A is a global array) always benefit from being volatile?

Lot’s of questions I know [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") There just doesn’t seem to be much concrete info on this…

---

<div class="post-metadata">

**Author:** ![wlangdon](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wlangdon](https://forums.developer.nvidia.com/u/wlangdon)\
**Post date:** [April 13, 2011, 3:25pm UTC](https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993/2 "2011-04-13T15:25:47Z")

</div>

hmm that not how I read it. I think volitile tells the compiler it is _not_ to optimise  
variables (especially shared memory) by placing them in registers because another thread  
may update the variable. (The update would be ignored if the register was used instead).  
I ended up using volitile on all shared memory because I could never be sure that replacing  
it with a register was safe (after all I was using shared memory inorder to communicate between  
threads). In my view any small performance gain by risking the compiler doing the wrong thing is not worth the debugging effort. Use volitle on all pointers to shared memory.  
Bill

---

<div class="post-metadata">

**Author:** ![sidxavier](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@sidxavier](https://forums.developer.nvidia.com/u/sidxavier)\
**Post date:** [March 15, 2012, 5:59pm UTC](https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993/3 "2012-03-15T17:59:13Z")

</div>

> [@](#):
>
> hmm that not how I read it. I think volitile tells the compiler it is _not_ to optimise
> 
> variables (especially shared memory) by placing them in registers because another thread
> 
> may update the variable. (The update would be ignored if the register was used instead).
> 
> I ended up using volitile on all shared memory because I could never be sure that replacing
> 
> it with a register was safe (after all I was using shared memory inorder to communicate between
> 
> threads). In my view any small performance gain by risking the compiler doing the wrong thing is not worth the debugging effort. Use volitle on all pointers to shared memory.
> 
> Bill

Suppose I am implementing a global worklist - I add to that worklist using the list tail and read (delete)from work list using Head of list.

None of my threads re-read any particular location of list however I do re-read Tail and Head values repeatedly. Should I be define these global variable (tail and head) as volatile?

In that case on the CPU code side - how do i use cudamemcpy or Atomics on them -? By type casting?

Sid.

---

<div class="post-metadata">

**Author:** ![silbmarks](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@silbmarks](https://forums.developer.nvidia.com/u/silbmarks)\
**Post date:** [March 17, 2012, 8:46pm UTC](https://forums.developer.nvidia.com/t/volatile-when-to-use-regarding-registers/21993/4 "2012-03-17T20:46:11Z")

</div>

volatile should be used when the data can be changed outside the current thread without memory fences (for writes) or synchronization (for reads and writes). Otherwise the compiler is free to optimize the reads/writes to the variable by caching the data in a local register. In particular if you access your shmem data only after \_\_syncthreads there is no need to use volatile.

In your example, if you synchronize the accesses to the list via some global atomic variable, and then change the tail/head from different threads without \_\_synchtreads, you MUST declar head/tail as volatile since otherwise the updates will not be properly read by other threads.

If you worklist is on a GPU and you modify the items from the CPU, then it’s a whole different story and volatile is insufficient, you have to use PTX assembly to read through to bypass the cache.

> [@](#):
>
> Suppose I am implementing a global worklist - I add to that worklist using the list tail and read (delete)from work list using Head of list.
> 
> None of my threads re-read any particular location of list however I do re-read Tail and Head values repeatedly. Should I be define these global variable (tail and head) as volatile?
> 
> In that case on the CPU code side - how do i use cudamemcpy or Atomics on them -? By type casting?
> 
> Sid.
