26

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.

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.

5
  • Why would you not be able to? Just don't try to acquire a C++ mutex and wait on it using OpenMP. Commented Dec 24, 2016 at 0:43
  • @brianbeuning Well, I'm unsure, that's why I am asking. There is a comment to the linked question speculating that we "will likely run into problems". I couldn't find any solid answer to the question on the web, that's why I kind of re-raised the question again. Commented Dec 24, 2016 at 0:56
  • 5
    This is a implementation defined behavior and may vary between compilers. However, there's also a more "practical" answer. In most cases, if the standard library, and OpenMP runtime, come from the same vendor of compiler, you are most likely to be fine. For example, using GCC with libstdc++ and libgomp , clang with libc++ and LLVM (Intel) runtime. There might be problems when using a compiler that does not have its own standard library, for example Intel C++ with libstdc++ on Linux or libc++ on macOS. I have seen issues in this case but very rarely.
    – Yan Zhou
    Commented Dec 24, 2016 at 2:22
  • 4
    That said, in most cases you should expect them to work as you expect. But just test it more carefully. Threading facilities such as standard library threads and OpenMP, is more of a OS-specific issue while atomics are more hardware related. It is possible for one part of the tool chain, say the OpenMP in compiler misuse a OS feature and the other part, the standard library make a different assumption of the OS, and they end up don't play nice. Any software can have bugs. But if the different parts come from the same vendor, it might have a better chance been more throughly tested
    – Yan Zhou
    Commented Dec 24, 2016 at 2:26
  • "This makes me to believe that the components for atomic access are kind of orthogonal to the thread library used" - From my understanding, it is not so much they are orthogonal. Rather, they are different primitives that operate at slightly different levels to solve different problems. If I had to characterize them based on my understanding then I'd say they compliment one another. Also see C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?.
    – jww
    Commented Dec 7, 2018 at 2:09

2 Answers 2

14

Update:

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:

  • Data-dependency ordering: atomics and memory model
  • Additions to the standard library
  • C++11 library

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".

Old discussion:

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

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.

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.

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.

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.

1
  • 6
    I asked once a member of the OpenMP ARB and the unofficial response was that interoperability between OpenMP and any other threading paradigm will probably never make it into the specification, but that most vendors will anyway Do The Right Thing (tm) as summarised in the comments by Yan Zhou. In other words, such code will mostly work, but will never be portable. Commented Dec 25, 2016 at 23:06
3

This is currently unspecified by OpenMP 4.5. In practice, you can use C++11 atomic operations with OpenMP threads in most compilers, but there is no formal guarentee that it will work.

Because of the unspecified behavior, GCC did not support C11 atomics (which are nearly identical in semantics to C++11 atomics) and OpenMP threads until recently. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65467 for details.

OpenMP 5.0 made an attempt to address this. The normative language references were updated to C11 and C++11. However, the atomics and memory model from these is "not supported", which means implementation-defined. I wish OpenMP 5.0 said more but it is extremely difficult to define the interaction of OpenMP and ISO language atomics.

2
  • 1
    Now that the OpenMP Version 5.0 Public Comment Draft is available, you may want to update your answer. Commented Jul 21, 2018 at 3:28
  • 1
    @user1494080 Thanks for the reminder. I’ll try to address this next time I’m at my desk. Commented Jul 21, 2018 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.