Intel® Math Kernel Library 2019 Developer Reference - C

Threading Control

Intel® MKL provides functions for OpenMP* threading control, discussed in this section.

Important

If Intel MKL operates within the Intel® Threading Building Blocks (Intel® TBB) execution environment, the environment variables for OpenMP* threading control, such as OMP_NUM_THREADS, and Intel MKL functions discussed in this section have no effect. If the Intel TBB threading technology is used, control the number of threads through the Intel TBB application programming interface. Read the documentation for the tbb::task_scheduler_init class at https://www.threadingbuildingblocks.org/documentation to find out how to specify the number of Intel TBB threads.

If Intel® MKL operates within an OpenMP* execution environment, you can control the number of threads for Intel MKL using OpenMP* run-time library routines and environment variables (see the OpenMP* specification for details). Additionally Intel MKL provides optional threading control functions and environment variables that enable you to specify the number of threads for Intel MKL and to control dynamic adjustment of the number of threads independently of the OpenMP* settings. The settings made with the Intel MKL threading control functions and environment variables do not affect OpenMP* settings but take precedence over them.

If none of the threading control functions is used, Intel MKL environment variables may control Intel MKL threading. For details of those environment variables, see the Intel MKL Developer Guide.

You can specify the number of threads for Intel MKL function domains with the mkl_set_num_threads or mkl_domain_set_num_threads function. While mkl_set_num_threads specifies the number of threads for the entire Intel MKL, mkl_domain_set_num_threads does it for a specific function domain. The following table lists the function domains that support independent threading control. The table also provides named constants to pass to threading control functions as a parameter that specifies the function domain.

Intel MKL Function Domains

Function Domain

Named Constant

Basic Linear Algebra Subroutines (BLAS)

MKL_DOMAIN_BLAS

Fast Fourier Transform (FFT) functions, except Cluster FFT functions

MKL_DOMAIN_FFT

Vector Math (VM) functions

MKL_DOMAIN_VML

Parallel Direct Solver (PARDISO) functions

MKL_DOMAIN_PARDISO

All Intel MKL functions except the functions from the domains where the number of threads is set explicitly

MKL_DOMAIN_ALL

Warning

Do not increase the number of OpenMP threads used for cluster_sparse_solver between the first call and the factorization or solution phase. Because the minimum amount of memory required for out-of-core execution depends on the number of OpenMP threads, increasing it after the initial call can cause incorrect results.

Both mkl_set_num_threads and mkl_domain_set_num_threads functions set the number of threads for all subsequent calls to Intel MKL from all applications threads. Use the mkl_set_num_threads_local function to specify different numbers of threads for Intel MKL on different execution threads of your application. The thread-local settings take precedence over the global settings. However, the thread-local settings may have undesirable side effects (see the description of the mkl_set_num_threads_local function for details).

By default, Intel MKL can adjust the specified number of threads dynamically. For example, Intel MKL may use fewer threads if the size of the computation is not big enough or not create parallel regions when running within an OpenMP* parallel region. Although Intel MKL may actually use a different number of threads from the number specified, the library does not create parallel regions with more threads than specified. If dynamic adjustment of the number of threads is disabled, Intel MKL attempts to use the specified number of threads in internal parallel regions (for more information, see the Intel MKL Developer Guide). Use the mkl_set_dynamic function to control dynamic adjustment of the number of threads.