Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Switching between threaded and non-threaded libraries

mullervki
Beginner
353 Views

Hello,

I have an application that uses Intel's MKL libraries. In some sections of this application calls to MKL are made in parallel areas. In these areas, I want to use the sequential version of the MKL libraries; at times I am not in a parallel area in my application, so I would like to use the threadedversion of the MKL libraries here.

I understand I can only link my application with either the threaded or the sequential libraries. Given that I need to use the threaded version in certain places, I assume I must link with the threaded version. I set my environment variables OMP_NUM_THREADS and MKL_NUM_THREADS to 1. My understanding is that I should be able to call mkl_set_num_threads(n), where "n" is either 1 - in areas where I want to use MKL in sequential mode - or ">1", in threaded MKL calls.

But if I bring the Windows task manager I notice that calling mkl_set_num_threads(1) when I'm using the threaded version of the MKL library still starts many threads. If I use the sequential version, on the other hand, this is not a problem. It appears that mkl_set_num_threads does not have the effect I expected.

If I run my application entirely in sequential mode, then even if I call mkl_set_num_threads(1) I still see multiple threads running when MKL is being called. Since I know the threading is not coming from my application, it must be from MKL.

Is there a way to force MKL to run in sequential mode - even if I'm linking with the threaded MKL library?

Thanks.

0 Kudos
4 Replies
VipinKumar_E_Intel
353 Views

Have you tried checking the # of actual threads in your application using mkl_get_max_threads()?

Can you disable dynamic threading using MKL_DYNAMIC to FALSE and setting the MKL_NUM_THREADS=1 even when you use threaded MKL functions?

 --vipin

 

mullervki
Beginner
353 Views

Vipin,

Thanks for the reply. I'll give your suggestion a try. I'm not familiar with MKL_DYNAMIC (I'm rather new to this issue). I assume this is an environment variable to be set to FALSE also.

I did some reading on MKL_DYNAMIC (https://software.intel.com/en-us/node/528547) and was surprised by the comment "Note also that if Intel MKL is called in a parallel region, it will use only one thread by default."

Does this mean I have no need to call mkl_set_num_threads at all? If you are familiar with the underlying reasons for the comment above, could you elaborate on it? Thanks.

VipinKumar_E_Intel
353 Views

  Hi,

   Yes, that's the environment variable.

    That note is about when you call an MKL routine from a parallel region, say an OpenMP parallel region to have nested parallelism like, here

  #pragma omp parallel for

   for (int i; i < n; i++) {
       mkl_function();

  }

  by default, mkl will use single thread.  If you still want to have nested parallelism, then you can try with MKL_DYNAMC to FALSE, and manually set the # of threads than MKL to decide it. Also, please note, you are using the same omp compiler as MKL is using as mentioned there.

Vipin

 

 

 

mullervki
Beginner
353 Views

Vipin,

You've been most helpful. Thanks.

I'm multi-threading the application using Windows threads, not OpenMP. I think I may have found the problem: running the application from the command line seems to work as expected if the environment variables are set at the command prompt. So I started Visual Studio (devenv) from the command line to make sure the environment variables were properly set (I chose not to change the environment variable globally at this point).

Reply