OMP_NUM_THREADS

The OMP_NUM_THREADS environment variable sets the number of threads to use for parallel regions. The syntax of the environment variable is as follows:
Read syntax diagramSkip visual syntax diagram
>>-OMP_NUM_THREADS=--num_list----------------------------------><

num_list
A list of one or more positive integer values separated by commas.

If you do not set the OMP_NUM_THREADS environment variable, the number of processors available is the default value to form a new team for the first encountered parallel construct. By default, any nested constructs are run by one thread.

If num_list contains a single value, dynamic adjustment of the number of threads is enabled (OMP_DYNAMIC is set to true), a parallel construct without a NUM_THREADS clause is encountered, the value is the maximum number of threads that can be used to form a new team for the encountered parallel construct.

If num_list contains a single value, dynamic adjustment of the number of threads is not enabled (OMP_DYNAMIC is set to false), a parallel construct without a NUM_THREADS clause is encountered, the value is the exact number of threads that can be used to form a new team for the encountered parallel construct.

If num_list contains multiple values, dynamic adjustment of the number of threads is enabled (OMP_DYNAMIC is set to true), a parallel construct without a NUM_THREADS clause is encountered, the first value is the maximum number of threads that can be used to form a new team for the encountered parallel construct. After the encountered construct is entered, the first value is removed and the remaining values form a new num_list. The new num_list is in turn used in the same way for any closely nested parallel constructs inside the encountered parallel construct.

If num_list contains multiple values, dynamic adjustment of the number of threads is not enabled (OMP_DYNAMIC is set to false), a parallel construct without a NUM_THREADS clause is encountered, the first value is the exact number of threads that can be used to form a new team for the encountered parallel construct. After the encountered construct is entered, the first value is removed and the remaining values form a new num_list. The new num_list is in turn used in the same way for any closely nested parallel constructs inside the encountered parallel construct.
Note: If the number of parallel regions is equal to or greater than the number of values in num_list, the omp_get_max_threads routine returns the last value of num_list in the parallel region.

If the number of threads requested exceeds the system resources available, the program stops.

The omp_set_num_threads routine sets the first value of num_list. The omp_get_max_threads routine returns the first value of num_list.

If you specify the number of threads for a given parallel region more than once with different settings, the compiler uses the following precedence order to determine which setting takes effect:
  1. The number of threads set using the NUM_THREADS clause takes precedence over that set using the omp_set_num_threads routine.
  2. The number of threads set using the omp_set_num_threads routine takes precedence over that set using the OMP_NUM_THREADS environment variable.
  3. The number of threads set using the OMP_NUM_THREADS environment variable takes precedence over that set using the PARTHDS suboption of the XLSMPOPTS environment variable.
Note: In a given parallel region, the omp_get_max_threads routine returns the first value of num_list, even though the actual number of threads running that parallel region might be different from the first value of num_list.
The following example shows how you can set the OMP_NUM_THREADS environment variable.
export OMP_NUM_THREADS=5,10
export OMP_DYNAMIC=false

! OMP_GET_MAX_THREADS() returns 5 threads 
!$omp parallel
! OMP_GET_MAX_THREADS() returns 10 threads 
  !$omp parallel   
  ! OMP_GET_MAX_THREADS() returns 10 threads
    !$omp parallel     
    ! OMP_GET_MAX_THREADS() returns 10 threads 
    !$omp end parallel 
  !$omp end parallel
!$omp end parallel