Вопрос

I started learning OpenMP and discovered the #pragma omp atomic directive.

I have basic understanding of C++11's atomics and know that you can pass a memory_order parameter to atomics's method. Correct me if I'm wrong, but I think this allows to use atomics as a synchronization point if using memory_order_seq_cst for example.

Some less restrictive memory order, like memory_order_relaxed just make sure that the operations on the atomic are synchronized and visible to others. It doesnt nothing about other memory updates.

I would like to know what memory order is used by the OpenMP's atomic directive. Will it only synchronize access to the atomic, or will it act as a memory synchronization's point?

My guess would be that it would be more like memory_order_relaxed, because critical's are here to provide a full synchronization.

I welcome any good explanation / informations. Thanks.

Это было полезно?

Решение

The OpenMP memory model has been evolving. Up to and including OpenMP 3.1, the model is based on "flush" operations. These are not really comparable to anything in the C++ memory model. Loosely speaking a flush roughly corresponds to atomic_thread_fence(x), where x is memory_order_seq_cst. But there's a gotcha where it's not that if the "intersection of the two flush-sets of the two flushes is empty".

OpenMP 4.0 adds the seq_cst clause, and answers the question directly:

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.

For more details, download the latest version of the spec (4.0 at this time) and read sections 1.4.4 "OpenMP Memory Consistency" and 2.12.6 "atomic Construct".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top