入れ子並列処理は、OMP_NESTED 環境変数を設定することによって、有効または無効にすることができます。デフォルトでは、入れ子並列処理は無効になっています。
3 つのレベルを持つ、入れ子並列構文の例を次に示します。
使用例 3-1 入れ子並列処理の例#include <omp.h>
#include <stdio.h>
void report_num_threads(int level)
{
#pragma omp single
{
printf("Level %d: number of threads in the team = %d\n",
level, omp_get_num_threads());
}
}
int main()
{
omp_set_dynamic(0);
#pragma omp parallel num_threads(2)
{
report_num_threads(1);
#pragma omp parallel num_threads(2)
{
report_num_threads(2);
#pragma omp parallel num_threads(2)
{
report_num_threads(3);
}
}
}
return(0);
}
入れ子並列処理を有効にして、このプログラムをコンパイルおよび実行すると、次のようなソート済みの結果が出力されます。
% setenv OMP_NESTED TRUE % a.out | sort Level 1: number of threads in the team = 2 Level 2: number of threads in the team = 2 Level 2: number of threads in the team = 2 Level 3: number of threads in the team = 2 Level 3: number of threads in the team = 2 Level 3: number of threads in the team = 2 Level 3: number of threads in the team = 2
入れ子並列処理を無効にしてプログラムを実行すると、次の出力が生成されます。
% setenv OMP_NESTED FALSE % a.out | sort Level 1: number of threads in the team = 2 Level 2: number of threads in the team = 1 Level 2: number of threads in the team = 1 Level 3: number of threads in the team = 1 Level 3: number of threads in the team = 1