gpt4 book ai didi

c++ - 在并行 omp 循环中同时写入同一内​​存

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:36 24 4
gpt4 key购买 nike

我想实现以下函数,将数组的某些元素标记为 1。

void mark(std::vector<signed char>& marker)
{
#pragma omp parallel for schedule(dynamic, M)
for (int i = 0; i < marker.size; i++)
marker[i] = 0;
#pragma omp parallel for schedule(dynamic, M)
for (int i = 0; i < marker.size; i++)
marker[getIndex(i)] = 1; // is it ok ?
}

如果我们尝试在不同的线程中同时将同一个元素的值设置为 1 会发生什么?它通常会设置为 1 还是此循环可能会导致意外行为?

最佳答案

This answer一个基本部分是错误的(强调我的):

If you write with different threads to the very same location, you get a race condition. This is not necessarily undefined behaviour, but nevertheless it need to be avoided.

看看 OpenMP standard ,第 1.4.1 节说(也强调我的):

If multiple threads write without synchronization to the same memory unit, including cases due to atomicity considerations as described above, then a data race occurs. Similarly, if at least one thread reads from a memory unit and at least one thread writes without synchronization to that same memory unit, including cases due to atomicity considerations as described above, then a data race occurs. If a data race occurs then the result of the program is unspecified.

从技术上讲,OP 片段处于未定义行为领域。这意味着在从程序中删除 UB 之前,无法保证程序的行为。

最简单的方法是使用原子操作来保护内存访问:

#pragma omp parallel for schedule(dynamic, M)
for (int i = 0; i < marker.size; i++)
#pragma omp atomic write seq_cst
marker[getIndex(i)] = 1;

但这可能会以一种合理的方式阻碍性能(正如@schorsch312 正确指出的那样)。

关于c++ - 在并行 omp 循环中同时写入同一内​​存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com