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

查看:129
本文介绍了混合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.

该标准在两个不同的章节中指定了 atomic操作库线程支持库.这使我相信用于原子访问的组件与所使用的线程库正交.我确实可以将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?

关于堆栈溢出,有一个非常类似的问题;但是,由于它的答案不能回答实际的问题,因此基本上已经有三年没有答案了.

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.

顺便说一句,它实际上可能在大多数时间都有效.我同意与C ++ 11线程一起使用std::atomic与OpenMP一起使用时,发生故障的可能性较小.但是,如果有任何麻烦,它可能不会那么明显.即使我在设想可能发生这种情况的现实情况时遇到严重麻烦,但最坏的情况是无法自动进行原子操作的原子.归根结底,这可能不值得,最安全的方法是坚持使用纯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天全站免登陆