ここでは、@HighPerformanceMark の回答をより明確な方法 (つまり、ステートメントをサポートするためのコード スケッチ) に言い換えてみます。アイデアを修正するために、ループのシリアルバージョンが次のようになっているとしましょう。
for(int ii = 0; ii < max; ii++)
{
// Do a lot of work based on ii
if(ii==0)
{
//////////////////////////////////////////////////
// Do something special for the first iteration //
//////////////////////////////////////////////////
}
}
以前の回答で簡単に提案されたのは、この単純なロジックで並列化することでした
// Move the loop body into a function
void loopIteration(const size_t ii) {
// Do a lot of work based on ii
}
#pragma omp parallel
{
// Single directive: the first one enters, the other wait at the implicit barrier
#pragma omp single
{
loopIteration(0);
//////////////////////////////////////////////////
// Do something special for the first iteration //
//////////////////////////////////////////////////
} // single: implicit barrier
// Loop work-sharing construct on the remaining iterations
#pragma omp for
for(int ii = 1; ii < max; ii++)
{
loopIteration(ii);
} // for: implicit barrier
} // parallel
主なアイデアは、ループ本体を関数に移動してコードの重複を回避し、ループから最初の反復を明示的に展開することです。