Read atomic


I am doing a little experiment with some code and I would like to read the following value atomically:

“LoopIndex[BlockIndex]” should be read atomically.

I am not sure how to do that, should I wrap it in AtomicCAS.

My CAS-skills are a bit rusty, an example how to solve it for the problem below would appreciated ! ;)

while (LoopIndex[BlockIndex] < LoopCount)


These are all integers. BlockIndex can be considered safe/constant.

The integer located at the index BlockIndex in the array LoopIndex should be read atomically…

What do you mean by “read atomically”?
Atomic operations perform read-modify-write cycles where the difference to ordinary operations is that no writes from other threads are allowed in between.
So I’m not quite sure what operation an “atomic read” by itself would perform.

An integer consists out of 4 bytes.

I want to make sure that all 4 bytes are read atomically…

Just reading a 32-bit word should be fine without atomic operations. As current GPUs don’t support misaligned access, 32-bit words are always read in a single transaction and there is no risk of an intermitting write from another thread.