Developer Reference for Intel® oneAPI Math Kernel Library for Fortran

ID 766686
Date 12/16/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

mkl_set_num_threads_local

Specifies the number of OpenMP* threads for all Intel® oneAPI Math Kernel Library functions on the current execution thread.

Syntax

save_nt = mkl_set_num_threads_local( nt )

Fortran Include Files/Modules
  • Include file: mkl.fi
  • Module (compiled): mkl_service.mod
  • Module (source): mkl_service.f90
Input Parameters

Name

Type

Description

nt

INTEGER*4

nt> 0 - The number of threads for Intel® oneAPI Math Kernel Library 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® oneAPI Math Kernel Library 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® oneAPI Math Kernel Library functions use the global number of threads. You can set the global number of threads using themkl_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* andparallelization of Intel® oneAPI Math Kernel Library 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® oneAPI Math Kernel Library 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® oneAPI Math Kernel Library number of threads
end subroutine my_compute