# Threading Control Functions

Intel® MKL provides optional threading control via service functions that take precedence over setting new environment variables with the same purpose (see Intel® MKL User's Guide for details).

These functions enable you to specify the number of threads for Intel MKL independently of the OpenMP* settings and prevailing over OpenMP* equivalents. Although Intel MKL may actually use the number of threads that differs from the one suggested, the controls will also enable you to instruct the library to try using the suggested number in the event of undetectable threading behavior in the application calling the library.

See the following examples of Fortran and C usage:

## Fortran Usage

`call mkl_set_num_threads( foo )`
`ret = mkl_domain_set_num_threads( num, MKL_BLAS )`
`call mkl_set_dynamic ( 1 )`
`num = mkl_get_max_threads()`
`num = mkl_domain_get_max_threads( MKL_BLAS );`
`ret = mkl_get_dynamic()`

## C Usage

`#include “mkl.h” // Mandatory to make these definitions work!`
`mkl_set_num_threads(num); // Equivalent to omp_set_num_threads( int )`
`return_code = mkl_domain_set_num_threads( num, MKL_FFT );`
`mkl_set_dynamic( 1 );`
`num = mkl_get_max_threads();`
`num = mkl_domain_get_max_threads( MKL_FFT );`
`return_code = mkl_get_dynamic();`

### Note

Always remember to add `#include “mkl.h”` to use the C usage syntax.

- mkl_set_num_threads
  Suggests the number of threads to use.
- mkl_domain_set_num_threads
  Suggests the number of threads for a particular function domain.
- mkl_set_dynamic
  Enables Intel MKL to dynamically change the number of threads.
- mkl_get_max_threads
  Inquires about the number of threads targeted for parallelism.
- mkl_domain_get_max_threads
  Inquires about the number of threads targeted for parallelism in different domains.
- mkl_get_dynamic
  Returns current value of MKL_DYNAMIC variable.

Parent topic: Support Functions