OpenMP* のロック

次のようなアノテート付きの C/C++ シリアルコードについて考えてみます。

  int count; 
void Tick() { 
     ANNOTATE_LOCK_ACQUIRE(0); 
          count++; 
     ANNOTATE_LOCK_RELEASE(0);
}

ロックを実装するには、OpenMP* の型、変数、および関数を使用して、より柔軟性のある強力なロックを提供します。例えば単純なロックには、C/C++ では omp_lock_t 型を、Fortran では type=omp_lock_kind を使用します。

次に示すように、C++ クラスの内部でロックをラップできます。

#include <omp.h> 
int count; 
omp_lock_t countMutex; 
struct CountMutexInit { 
     CountMutexInit() { omp_init_nest_lock (&countMutex); } 
     ~CountMutexInit() { omp_destroy_nest_lock(&countMutex); } 
} countMutexInit; 

// 上記のオブジェクト宣言は、コンストラクターと
// デストラクターを介してプログラム開始時に countMutex
// を初期化して、プログラム終了時に破棄します。


struct CountMutexHold { 
     CountMutexHold() { omp_set_nest_lock (&countMutex); } 
     ~CountMutexHold() { omp_unset_nest_lock (&countMutex); } 
}; 

void Tick() { 
     // スコープの出口でアンロック 
     CountMutexHold releaseAtEndOfScope; 
          count++; 
} 
...

次のようなアノテート付きの Fortran シリアルコードについて考えてみます。

program BBB 
integer(kind=4) :: count = 0 
..
contains 
subroutine Tick 
     call annotate_lock_acquire(0) 
          count = count + 1 
     call annotate_lock_release(0) 
end subroutine Tick
..
end program BBB

Fortran コードの簡単なロックには、type=omp_lock_kind を使用します。以下に、use omp_libcountinteger 宣言を追加した並列 Fortran コードを示します。

program BBB 
     use omp_lib 
     integer(kind=4) :: count = 0 
     integer (kind=omp_lock_kind) countMutex 
     call omp_nest_lock_init(countMutex)..

  contains 
     subroutine Tick 
          call omp_set_nest_lock(countMutex) 
               count = count + 1  
          call omp_unset_nest_lock(countMutex) 
     end subroutine Tick..
end program BBB

ヒント

OpenMP* 並列フレームワークを使用してコードを書き直した後、インテル® Advisor でパフォーマンスを解析できます。Vectorization and Code Insights (ベクトル化とコードの調査) パースペクティブを使用して、OpenMP* コードがどの程度ベクトル化されているか解析するか、Offload Modeling (オフロードのモデル化) パースペクティブで GPU でのパフォーマンスをモデル化できます。

関連情報