Interaction Between the num_threads Clause and omp_set_dynamic
3.5. Interaction Between the num_threads Clause and omp_set_dynamic#
The following example demonstrates the num_threads clause and the effect of the omp_set_dynamic routine on it.
The call to the omp_set_dynamic routine with argument 0 in C/C++, or .FALSE. in Fortran, disables the dynamic adjustment of the number of threads in OpenMP implementations that support it. In this case, 10 threads are provided. Note that in case of an error the OpenMP implementation is free to abort the program or to supply any number of threads available.
//%compiler: clang
//%cflags: -fopenmp
/*
* name: nthrs_dynamic.1
* type: C
*/
#include <omp.h>
int main()
{
omp_set_dynamic(0);
#pragma omp parallel num_threads(10)
{
/* do work here */
}
return 0;
}
!!%compiler: gfortran
!!%cflags: -fopenmp
! name: nthrs_dynamic.1
! type: F-fixed
PROGRAM EXAMPLE
INCLUDE "omp_lib.h" ! or USE OMP_LIB
CALL OMP_SET_DYNAMIC(.FALSE.)
!$OMP PARALLEL NUM_THREADS(10)
! do work here
!$OMP END PARALLEL
END PROGRAM EXAMPLE
The call to the omp_set_dynamic routine with a non-zero argument in C/C++, or .TRUE. in Fortran, allows the OpenMP implementation to choose any number of threads between 1 and 10.
//%compiler: clang
//%cflags: -fopenmp
/*
* name: nthrs_dynamic.2
* type: C
*/
#include <omp.h>
int main()
{
omp_set_dynamic(1);
#pragma omp parallel num_threads(10)
{
/* do work here */
}
return 0;
}
!!%compiler: gfortran
!!%cflags: -fopenmp
! name: nthrs_dynamic.2
! type: F-fixed
PROGRAM EXAMPLE
INCLUDE "omp_lib.h" ! or USE OMP_LIB
CALL OMP_SET_DYNAMIC(.TRUE.)
!$OMP PARALLEL NUM_THREADS(10)
! do work here
!$OMP END PARALLEL
END PROGRAM EXAMPLE
It is good practice to set the dyn-var ICV explicitly by calling the omp_set_dynamic routine, as its default setting is implementation defined.