OMP_NUM_THREADS

The OMP_NUM_THREADS environment variable specifies 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 OMP_NUM_THREADS, the number of processors available is the default value to form a new team for the first encountered parallel construct. If nested parallelism is disabled, any nested parallel 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), and 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), and 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), and 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), and 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 function 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 function sets the first value of num_list. The omp_get_max_threads function 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 function.
  2. The number of threads set using the omp_set_num_threads function 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.

Example

export OMP_NUM_THREADS=3,4,5
export OMP_DYNAMIC=false

// omp_get_max_threads() returns 3

#pragma omp parallel
{  
// Three threads running the parallel region  
// omp_get_max_threads() returns 4
  
  #pragma omp parallel if(0)
  {
  // One thread running the parallel region 
  // omp_get_max_threads() returns 5

    #pragma omp parallel
    {  
    // Five threads running the parallel region   
    // omp_get_max_threads() returns 5 
    } 
  }
}


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us