开发者

Why doesn't the OpenMP atomic directive support assignment?

开发者 https://www.devze.com 2023-01-18 09:19 出处:网络
The atomic directive in openmp supports stuff like x += expr x *= expr where expr is an expression of scalar type that does not reference x. I get that, but I don\'t get why you can\'t do:

The atomic directive in openmp supports stuff like

x += expr
x *= expr

where expr is an expression of scalar type that does not reference x. I get that, but I don't get why you can't do:

#pragma omp atomic
x = y;

Is this somehow more taxing cpu instruction-wise? Seems to me that both the 开发者_运维百科legal and illegal statement loads the value of x and some other scalar value, changes the register value of x and writes it back. If anyone could explain to me how these instructions are (I assume) fundamentally different I would be very grateful.


Because the suggested atomic assignment does not protect against anything.

Remember that an atomic instruction can be thought of as a critical section that could be (but does not have to be) efficiently implemented by the compiler by using magic hardware. Think about two threads reaching x = y with shared x and private y. After all the threads finish, x is equal to the last thread to execute "wins" and sets x to its y. Wrap the assignment in a critical section and nothing changes, the last thread still "wins". Now, if the threads do something else with x afterwards the slowest thread may not have caught up and even if it has the compiler could legitimately end up using choosing to some cached value for x (i.e. the thread's local y). To avoid this, you would need a barrier (so the winning thread has won) and its implied flush (so the local cache has been invalidated):

x = y;
#pragma omp barrier
\\ do something with shared x...

but I cannot think of a good reason to do this. Why do all the work to find y on many threads if most of them will be (non-deterministically) thrown away?

0

精彩评论

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

关注公众号