Developer Reference for Intel® oneAPI Math Kernel Library for C

ID 766684
Date 3/22/2024
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 (oneMKL) functions on the current execution thread.

Syntax

int mkl_set_num_threads_local( int nt );

Include Files

  • mkl.h

Input Parameters

Name

Type

Description

nt

int

nt> 0 - The number of threads for Intel® oneAPI Math Kernel Library (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® oneAPI Math Kernel Library (oneMKL) 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 (oneMKL) 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

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
}