混合 C++11 原子和 OpenMP [英] Mixing C++11 atomics and OpenMP

查看:41
本文介绍了混合 C++11 原子和 OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenMP 有自己对原子访问的支持,但是,首选 C++11 原子至少有两个原因:它们明显更加灵活并且它们是标准的一部分.另一方面,OpenMP 比 C++11 线程库更强大.

OpenMP has its own support for atomic access, however, there are at least two reasons for preferring C++11 atomics: they are significantly more flexible and they are part of the standard. On the other hand, OpenMP is more powerful than the C++11 thread library.

该标准在两个不同的章节中指定了原子操作库线程支持库.这让我相信原子访问的组件与使用的线程库是正交的.我真的可以将 C++11 原子和 OpenMP 结合起来吗?

The standard specifies the atomic operations library and the thread support library in two distinct chapters. This makes me to believe that the components for atomic access are kind of orthogonal to the thread library used. Can I indeed combine C++11 atomics and OpenMP?

Stack Overflow 上有一个非常类似的问题;然而,三年来基本上没有答案,因为它的答案没有回答实际问题.

There is a very similar question on Stack Overflow; however, it has been basically unanswered for three years, since its answer does not answer the actual question.

推荐答案

更新:

OpenMP 5.0 定义了与 C++11 及更高版本的交互.其中,它表示使用以下功能可能会导致未指定的行为:

OpenMP 5.0 defines the interactions to C++11 and further. Among others, it says that using the following features may result in unspecified behavior:

  • 数据依赖排序:原子和内存模型
  • 对标准库的补充
  • C++11 库

很明显,混合 C++11 原子和 OpenMP 5.0 将导致未指定的行为.至少标准本身承诺OpenMP 规范的未来版本有望解决 [这些] 功能".

So clearly, mixing C++11 atomics and OpenMP 5.0 will result in unspecified behavior. At least the standard itself promises that "future versions of the OpenMP specification are expected to address [these] features".

旧讨论:

有趣的是,OpenMP 4.5 标准 (2.13.6) 对 C++11 原子的引用相当模糊,或更具体的std::memory_order:

Interestingly, the OpenMP 4.5 standard (2.13.6) has a rather vague reference to C++11 atomics, or more specific std::memory_order:

意图是,当类似的操作存在于 C++11 或C11,顺序一致的原子结构具有相同的语义作为 C++11/C11 中的 memory_order_seq_cst 原子操作.同样,一个非顺序一致的原子结构具有相同的语义C++11/C11 中的 memory_order_relaxed 原子操作.

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.

不幸的是,这只是一个音符,没有任何东西可以说明他们在一起玩得很好.特别是,即使是最新的 OpenMP 5.0 预览版仍然将 C++98 称为 C++ 的唯一规范参考.所以从技术上讲,OpenMP 甚至不支持 C++11.

Unfortunately this is only a note, there is nothing that defines that they are playing nicely together. In particular, even the latest OpenMP 5.0 preview still refers to C++98 as the only normative reference for C++. So technically, OpenMP doesn't even support C++11 itself.

除此之外,它可能在实践中的大部分时间都有效.我同意使用 std::atomic 如果与 OpenMP 一起使用比 C++11 线程更少潜在的麻烦.但如果有什么麻烦,可能就不那么明显了.最坏的情况是原子不会以原子方式运行,即使我很难想象可能发生这种情况的现实场景.归根结底,这可能不值得,最安全的做法是坚持使用纯 OpenMP 或纯 C++11 线程/原子.

That aside, it will probably work most of the time in practice. I would agree that using std::atomic has less potential for trouble if used together with OpenMP than C++11 threading. But if there is any trouble, it may not be as obvious. Worst case would be a atomic that doesn't operate atomically, even though I have serious trouble imagining a realistic scenario where this may happen. At the end of the day, it may not be worth it and the safest thing is to stick with pure OpenMP or pure C++11 thread/atomics.

也许 Hristo 对此有话要说,同时请查看这个答案以获得更一般的讨论.虽然有点过时,但恐怕它仍然有效.

Maybe Hristo has something to say about this, in the mean time check out this answer for a more general discussion. While a bit dated, I'm afraid it still holds.

这篇关于混合 C++11 原子和 OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆