开发者

CUDA __threadfence()

开发者 https://www.devze.com 2023-02-15 07:28 出处:网络
I have gone through many forum posts and the NVIDIA documentation,but I couldn\'t understand what __threadfence() does and how to use it. Could someone explain what the purpose of that in开发者_如何转

I have gone through many forum posts and the NVIDIA documentation, but I couldn't understand what __threadfence() does and how to use it. Could someone explain what the purpose of that in开发者_如何转开发trinsic is?


Normally, there are no guarantee that if one block writes something to global memory, the other block will "see" it. There is also no guarantee regarding the ordering of writes to global memory, with an exception of the block that issued it.

There are two exceptions:

  • atomic operations - those are always visible by other blocks
  • threadfence

Imagine, that one block produces some data, and then uses atomic operation to mark a flag that the data is there. But it is possible that the other block, after seeing the flag, still reads incorrect or incomplete data.

The __threadfence function, coming to the rescue, ensures the ordering. All writes before it really happen before all writes after it, as seen from other blocks.

Note that the __threadfence function doesn't necessarily need to stall the current thread until its writes to global memory are visible to all other threads in the grid. Implemented in this naive way, the __threadfence function could hurt performance severely.

As an example, if you do something like:

  1. store your data
  2. __threadfence()
  3. atomically mark a flag

it is guaranteed that if the other block sees the flag, it will also see the data.

Further reading: Cuda Programming Guide, Chapter B.5 (as of version 11.5)

0

精彩评论

暂无评论...
验证码 换一张
取 消