MKL also uses OpenMP for its multi-threaded driver. This means that the number of OpenMP threads affects the number of MKL threads, but in a very complicated way.
First, as an OpenMP code, MKL is also controlled by the usual OpenMP methods for setting the number of threads, for example. OMP_NUM_THREADS and will call omp_set_num_threads . But it also provides redefinition of configuration mechanisms in the form of MKL_NUM_THREADS and mkl_set_num_threads() . This allows you to have a different number of threads in user code and in MKL procedures.
Having configured the required number of threads, you also need to know how MKL behaves in nested parallelism cases. That is, MKL by default runs single-threaded if called from within the active parallel region in user code. MKL provides a MKL_DYNAMIC switch that can override this behavior, but this requires the same OpenMP compiler to be used for user code as for MKL (read that - you must use the Intel compiler), since compatibility between different OpenMP loops is not guaranteed .
Generally speaking, you do not need to set the number of threads to 1 before calling MKL, as this makes it single-threaded, unless the number of MKL threads has been redefined by explicitly setting it. And you have to be careful when calling inside parallel areas when nested parallelism is enabled.
Further reading on controlling the number of threads in MKL is available in the MKL User Guide .
source share