OpenMPのcriticalコンストラクトには、オプションで名前を指定できる。
int acc = 0; #pragma omp parallel for for (int i = 0; i < 10; i++) { #pragma omp critical (acc) acc += func(i); }
OpenMPランタイムによる排他制御では、同じ名前を付与したコード区間同士が対象となる(上記コードでは名前acc
を付与)。名前を明示指定しない場合、プログラム内でグローバルな排他制御が行われるため、並行実行スケーラビリティ低下に繋がる可能性がある。
OpenMP API Version 2.5仕様*1 §2.7.2 critical Constructより該当箇所を引用。
#pragma omp critical
[(
name)
] new-line
structured-block
Description
An optional name may be used to identify thecritical
construct. Allcritical
constructs without a name are considered to have the same unspecified name. A thread waits at the beginning of acritical
region until no other thread is executing acritical
region with the same name. Thecritical
construct enforces exclusive access with respect to allcritical
constructs with the same name in all threads, not just in the current team.Identifiers used to identify a critical construct have external linkage and are in a name space which is separate from the name spaces used by labels, tags, members, and ordinary identifiers.