yohhoyの日記

技術的メモをしていきたい日記

OpenMP flush操作はデータ競合を解決しない

タイトル通り。OpenMP flush指示文のみ利用では、データ競合(data race)を回避できない。また、OpenMP仕様ではデータ競合発生時の動作を規定せず(unspecified)、ベース言語*1および動作システムの仕様に準じる。

OpenMP API Version 4.0仕様*2 1.4 Memory Modeより一部引用(下線部は強調)。

1.4.1 Structure of the OpenMP Memory Model
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.

1.4.4 OpenMP Memory Consistency
Note – Since flush operations by themselves cannot prevent data races, explicit flush operations are only useful in combination with non-sequentially consistent atomic directives.

IWOMP'05 (PDF)The OpenMP Memory Model, 2 The OpenMP Memory Modelより一部引用(下線部は強調)。

There are two aspects of memory system behavior relating to shared memory parallel programs: coherence and consistency. Coherence refers to the behavior of the memory system when a single memory location is accessed by multiple threads. Consistency refers to the ordering of accesses to different memory locations, observable from various threads in the system.
OpenMP doesn't specify any coherence behavior of the memory system. That is left to the underlying base language and computer system. OpenMP does not guarantee anything about the result of memory operations that constitute data races within a program. A data race in this context is defined to be accesses to a single variable by at least two threads, at least one of which is a write, not separated by a synchronization operation. OpenMP does guarantee certain consistency behavior, however. That behavior is based on the OpenMP flush operation.

関連URL

*1:OpenMPのベース言語として、CまたはC++Fortranのいずれかを選択可能。

*2:http://www.openmp.org/mp-documents/OpenMP4.0.0.pdf‎