yohhoyの日記

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

criticalコンストラクトへの名前指定

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 the critical construct. All critical constructs without a name are considered to have the same unspecified name. A thread waits at the beginning of a critical region until no other thread is executing a critical region with the same name. The critical construct enforces exclusive access with respect to all critical 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.