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

此生再无相见时 提交于 2019-12-08 21:45:54

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35680633/atomic-access-to-non-atomic-memory-location-in-c11-and-openmp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!