OpenMP and MKL threading

fortran, intel-mkl, openmp

Solution

MKL also uses OpenMP for its multithreaded driver. This means that the number of OpenMP threads does affect the number of MKL threads, but in a very intricate way.

First, being OpenMP code, MKL is also controlled by the usual OpenMP ways to set the number of threads, e.g. `OMP_NUM_THREADS` and calls to `omp_set_num_threads`. But it also provides override configuration mechanisms in the form of `MKL_NUM_THREADS` and `mkl_set_num_threads()`. This allows one to have different number of threads in the user code and in the MKL routines.

Having configured the desired number of threads, one should also know how MKL behaves in nested parallelism cases. That is, MKL would by default run single-threaded if called from inside an active `parallel` region in the user code. MKL provides the `MKL_DYNAMIC` switch that can override this behaviour but it requires that the same OpenMP compiler is used for the user code as for MKL (read that - you must use Intel's compiler) as no compatibility is guaranteed between different OpenMP runtimes.

Generally speaking, you do not need to set the number of threads to 1 before calling into MKL, as this would make it single-threaded, unless the number of MKL threads was overridden by configuring it explicitly. And you should be careful when calling it from inside `parallel` regions when nested parallelism is enabled.

Further read about controlling the number of threads in MKL is available in MKL's User Guide:

- Using Additional Threading Control (mirror of otherwise dead link)

- Techniques to Set the Number of Threads

Problem

I have a code in Fortran which uses `DGESVD` from MKL and runs on 8 cores with Intel compiler. The code is accelerated via OpenMP. Also I know that OpenMP and MKL has their own setting to set the number of threads (`omp_num_threads` and `mkl_num_threads`). I want to know the optimum number of threads. Am I supposed to set the `OMP_NUM_THREADS=1` before calling the LAPACK routine? Does the number of OpenMP threads affect MKL number of threads?

Original source