Intel® Math Kernel Library 2019 Developer Reference - Fortran

mkl_set_num_threads_local

Specifies the number of OpenMP* threads for all Intel MKL functions on the current execution thread.

Syntax

save_nt = mkl_set_num_threads_local( nt )

Fortran Include Files/Modules

Input Parameters

Name

Type

Description

nt

INTEGER*4

nt > 0 - The number of threads for Intel MKL functions to use on the current execution thread.

nt = 0 - A request to reset the thread-local number of threads and use the global number.

Description

This function sets the number of OpenMP threads that Intel MKL functions should request for parallel computation. The number of threads is thread-local, which means that it only affects the current execution thread of the application. If the thread-local number is not set or if this number is set to zero in a call to this function, Intel MKL functions use the global number of threads. You can set the global number of threads using the mkl_set_num_threads or mkl_domain_set_num_threads function.

The thread-local number of threads takes precedence over the global number: if the thread-local number is non-zero, changes to the global number of threads have no effect on the current thread.

CAUTION

If your application is threaded with OpenMP* and parallelization of Intel MKL is based on nested OpenMP parallelism, different OpenMP parallel regions reuse OpenMP threads. Therefore a thread-local setting in one OpenMP parallel region may continue to affect not only the master thread after the parallel region ends, but also subsequent parallel regions. To avoid performance implications of this side effect, reset the thread-local number of threads before leaving the OpenMP parallel region (see Examples for how to do it).

Return Values

Name

Type

Description

save_nt

INTEGER*4

The value of the thread-local number of threads that was used before this function call. Zero means that the global number of threads was used.

Examples

This example shows how to avoid the side effect of a thread-local number of threads by reverting to the global setting:

use omp_lib
use mkl_service
integer(4) :: dummy
…
call mkl_set_num_threads(16)
call my_compute_using_mkl()	! Intel MKL functions use up to 16 threads
!$omp parallel num_threads(2)
  if (0 == omp_get_thread(num))  dummy = mkl_set_num_threads_local(4)
  if (1 == omp_get_thread(num))  dummy = mkl_set_num_threads_local(12)
  call my_compute_using_mkl()	! Intel MKL functions use up to 4 threads on thread 0
					!   and up to 12 threads on thread 1
!$omp end parallel
call my_compute_using_mkl()	! Intel MKL functions use up to 4 threads (!)
dummy = mkl_set_num_threads_local(0)	! make master thread use global setting
call my_compute_using_mkl()	! Now Intel MKL functions use up to 16 threads

This example shows how to avoid the side effect of a thread-local number of threads by saving and restoring the existing setting:

subroutine my_compute(nt)
	use mkl_service
	integer(4) :: nt, save
	save = mkl_set_num_threads_local( nt )	! save the Intel MKL number of threads
	call my_compute_using_mkl()	! Intel MKL functions use up to nt threads on this thread
	save = mkl_set_num_threads_local( save )	! restore the Intel MKL number of threads
end subroutine my_compute