gpt4 book ai didi

multithreading - 如果其他线程只读取共享数据,OpenMP 是否需要原子写入?

转载 作者:行者123 更新时间:2023-12-01 22:29:29 25 4
gpt4 key购买 nike

我有一个 C++ 中的 openmp 并行循环,其中所有线程都访问一个共享的 double 数组。

  • 每个线程仅在其自己的数组分区中写入。两个线程不能写入同一个数组条目。
  • 每个线程读取其他线程写入的分区。只要 double 值是旧值或更新值(不是由于读取半写入的 double 值而导致的无效值),数据是否已被拥有该分区的线程更新并不重要。<

我是否需要原子写入来确保读取的数据有效(旧的或更新的),或者仅当多个线程尝试在同一位置写入时才需要原子写入?

无论有没有原子写入,它似乎都可以工作,但如果没有原子写入,当然速度更快。

最佳答案

对于正确的可移植程序,您应该使用原子写入和读取。正如 the standard 所指定的:

2.13.6 atomic Construct

[...] To avoid race conditions, all accesses of the locations designated by x that could potentially occur in parallel must be protected with an atomic construct.

更详细:

1.4.1 Structure of the OpenMP Memory Model

[...]

A single access to a variable may be implemented with multiple load or store instructions, and hence is not guaranteed to be atomic with respect to other accesses to the same variable.

[...]

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.

除了原子性之外,还应该考虑可见性:

1.4.3 The Flush Operation

The memory model has relaxed-consistency because a thread’s temporary view of memory is not required to be consistent with memory at all times. A value written to a variable can remain in the thread’s temporary view until it is forced to memory at a later time. Likewise, a read from a variable may retrieve the value from the thread’s temporary view, unless it is forced to read from memory. The OpenMP flush operation enforces consistency between the temporary view and memory.

这意味着,除非您有任何显式或隐式内存刷新,否则无法保证您会看到更新的值。

但是,原子版本并不一定较慢。实现原子操作的编译器知道该体系结构的特定内存模型并可以自由地利用它。事实上,gcc 和 clang 都不是 generate expensive locks用于在 x86 上原子地写入或读取 double,同时执行原子增量或 long double 操作。不幸的是,原子可能仍然会阻碍某些优化 - 但如果您省略原子,这些很可能会导致未指定的结果。不要低估编译器优化:对于严格来说不符合标准的看似合理的程序,很容易出现未定义的行为。

至于内存刷新对性能的影响,取决于您的实际算法需要刷新内存的频率。

关于multithreading - 如果其他线程只读取共享数据,OpenMP 是否需要原子写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41612143/

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