The environment variable OMP_MAX_ACTIVE_LEVELS controls the maximum number of nested active parallel regions. A parallel region is active if it is executed by a team consisting of more than one thread. If not set, the default is 4.
Note that setting this environment variable simply controls the maximum number of nested active parallel regions; it does not enable nested parallelism. To enable nested parallelism, OMP_NESTED must be set to TRUE, or omp_set_nested() must be called with an argument that evaluates to true.
The following sample code creates 4 levels of nested parallel regions.
#include <omp.h> #include <stdio.h> #define DEPTH 4 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()); } } void nested(int depth) { if (depth > DEPTH) return; #pragma omp parallel num_threads(2) { report_num_threads(depth); nested(depth+1); } } int main() { omp_set_dynamic(0); omp_set_nested(1); nested(1); return(0); }
The following output shows a possible result from compiling and running the sample code when DEPTH is set to 4. Actual results would depend on how the operating system schedules the threads.
% setenv OMP_NESTED TRUE % setenv OMP_MAX_ACTIVE_LEVELS 4 % 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 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2 Level 4: number of threads in the team = 2
If OMP_MAX_ACTIVE_LEVELS is set to 2, then nested parallel regions at nesting depths of 3 and 4 are executed single-threaded. The following example shows a possible result.
% setenv OMP_NESTED TRUE % setenv OMP_MAX_ACTIVE_LEVELS 2 % 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 = 1 Level 3: 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 Level 4: number of threads in the team = 1 Level 4: number of threads in the team = 1 Level 4: number of threads in the team = 1 Level 4: number of threads in the team = 1