Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic access to non-atomic memory location in C++11 and OpenMP?

OpenMP, in contrast to C++11, works with atomicity from a perspective of memory operations, not variables. That allows, e.g., to use atomic reads/writes for integers being stored in a vector with unknown size at compile time:

std::vector<int> v;

// non-atomic access (e.g., in a sequential region):
v.resize(n);
...
v.push_back(i);
...

// atomic access in a multi-threaded region:
#pragma omp atomic write // seq_cst
v[k] = ...;
#pragma omp atomic read // seq_cst
... = v[k];

In C++11, this is not possible to achieve. We can kind-of access atomic variables as non-atomic by relaxing memory model, but we cannot resize a vector of atomic elements.

I understand that there are reasons why C++ does not allow to access non-atomic variables by atomic memory operations. But I wonder, why these reasons do not apply for OpenMP as well.

For example, in N4013, it is said that "There is no reasonable way to completely portably apply atomic operations to data not declared as atomic." How it's possible that OpenMP can guarantee such portability and C++ not?

like image 362
Daniel Langr Avatar asked Feb 28 '16 09:02

Daniel Langr


1 Answers

As far as I understand the respective standards, OpenMP has more restrictions on usage than C++11, which allows it to be portable without using special types. For example, OpenMP 4.5 says:

If the storage location designated by x is not size-aligned (that is, if the byte alignment of x is not a multiple of the size of x), then the behavior of the atomic region is implementation defined.

On the other hand, if C++11 uses std::atomic<int>, then the compiler will guarentee the appropriate alignment. In both cases, alignment is required, but OpenMP and C++11 differ in who is responsible for ensuring this is done.

Generally, there are philosophical differences between OpenMP and C++, but it's hard to enumerate all of them. The C++ folks are thinking about portability to everything, whereas OpenMP is targeted at HPC.

like image 183
Jeff Hammond Avatar answered Oct 21 '22 14:10

Jeff Hammond