gpt4 book ai didi

c++ - OpenMP 原子内存顺序

转载 作者:太空狗 更新时间:2023-10-29 20:13:23 26 4
gpt4 key购买 nike

我开始学习 OpenMP 并发现了 #pragma omp atomic 指令。

我对 C++11 的原子有基本的了解,并且知道您可以将 memory_order 参数传递给原子的方法。如果我错了请纠正我,但我认为如果使用 memory_order_seq_cst 例如这允许使用原子作为同步点。

一些限制较少的内存顺序,如 memory_order_relaxed 只需确保对原子的操作是同步的并且对其他人可见。它与其他内存更新无关。

我想知道 OpenMP 的原子指令使用什么内存顺序。它只会同步对原子的访问,还是充当内存同步点?

我的猜测是它更像是 memory_order_relaxed,因为 critical 在这里提供完全同步。

我欢迎任何好的解释/信息。谢谢。

最佳答案

OpenMP 内存模型一直在发展。直到并包括 OpenMP 3.1,该模型基于“刷新”操作。这些实际上无法与 C++ 内存模型中的任何内容相提并论。粗略地说,刷新大致对应于 atomic_thread_fence(x),其中 x 是 memory_order_seq_cst。但是有一个陷阱,不是如果“两次刷新的两个刷新集的交集是空的”。

OpenMP 4.0 增加seq_cst子句,直接回答问题:

Note – As with other implicit flush regions, Section 1.4.4 on page 20 reduces the ordering that must be enforced. The intent is that, when the analogous operation exists in C++11 or C11, a sequentially consistent atomic construct has the same semantics as a memory_order_seq_cst atomic operation in C++11/C11. Similarly, a non-sequentially consistent atomic construct has the same semantics as a memory_order_relaxed atomic operation in C++11/C11.

有关更多详细信息,请下载 latest version of the spec (此时为 4.0)并阅读 1.4.4 节“OpenMP 内存一致性”和 2.12.6 节“原子构造”。

关于c++ - OpenMP 原子内存顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com