Developer Reference for Intel® oneAPI Math Kernel Library for C
mkl_set_num_threads_local
Specifies the number of OpenMP* threads for all Intel® oneAPI Math Kernel Library (oneMKL) functions on the current execution thread.
Syntax
intmkl_set_num_threads_local ( intnt ) ;
Include Files
mkl.h
Input Parameters
Name |
Type |
Description |
|---|---|---|
nt |
int |
nt > 0 - The number of threads for Intel® oneMKL 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 (oneMKL) 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® oneMKL 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.
Return Values
Name |
Type |
Description |
|---|---|---|
save_nt |
int |
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:
#include "omp.h"
#include "mkl.h"
...
mkl_set_num_threads(16);
my_compute_using_mkl(); // Intel MKL functions use up to 16 threads
#pragma omp parallel num_threads(2)
{
if (0 == omp_get_thread_num())
mkl_set_num_threads_local(4);
else
mkl_set_num_threads_local(12);
my_compute_using_mkl(); // Intel MKL functions use up to 4 threads on thread 0
// and up to 12 threads on thread 1
}
my_compute_using_mkl(); // Intel MKL functions use up to 4 threads (!)
mkl_set_num_threads_local( 0 ); // make master thread use global setting
my_compute_using_mkl(); // 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:
#include "mkl.h"
void my_compute( int nt )
{
int save = mkl_set_num_threads_local( nt ); // save the Intel® oneAPI Math Kernel Library (oneMKL) number of threads
my_compute_using_mkl(); // Intel MKL functions use up to nt threads on this thread
mkl_set_num_threads_local( save ); // restore the Intel® oneAPI Math Kernel Library (oneMKL) number of threads
}