Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6956 Discussions

Setting Number of OMP Threads with MKL

noamp
Beginner
1,235 Views
Hi,

My application uses OpenMP and Intel MKL. I want to enable setting the number of threads in run-time, but using
omp_set_num_threads has no effect, and the application always uses all cores, regardless of the value given to omp_set_num_threads.

To pinpoint the problem, I wrote the following simple program:

void main()
{
omp_set_num_threads(2);

#pragma omp parallel for
for (int i = 0; i < 100000; i++)
{
printf("Hello\\n");
Sleep(100);
}
}

I am running the program on a machine with 4 cores.
When I run the program, it uses 2 threads as expected. But if I add Intel MKL settings to the project (without changing anything in the code), the number of running threads is 5. It seems like MKL makes the program use all cores (+ one extra thread?)

Does anyone know how I can set the number of threads in run-time in this configuration?

OS: Windows.
MKL version: 10.1.1.022

Your help is very much appreciated.
Thanks,
Noam.

0 Kudos
7 Replies
Sergey_Solovev__Inte
New Contributor I
1,235 Views

Hi, Noam,
we recommend to use mkl_set_num_threads() to set number of threads. Probably, it can help you. This function allows you to request independently of OpenMP how many threads MKL should use. For more detail, please see MKL manual (Support Functions/ Threading Control Functions)

Thanks,
Sergey

0 Kudos
TimP
Honored Contributor III
1,235 Views
I would not be surprised if the OpenMP library (invoked by the mkl threaded library) had an additional supervisor thread which runs briefly. If you set the KMP_AFFINITY, that additional thread should have no lasting effect on your execution.
0 Kudos
noamp
Beginner
1,235 Views
Thanks guys for your answers.
I tried calling mkl_set_num_threads, but it didn't change the behavior of my program - still 5 running threads.
0 Kudos
Dmitry_B_Intel
Employee
1,235 Views
Hi Noam,

If this tiny program doesn't use MKL then adding MKL setting should not change the behaviour of the program. If it does, then something in the program, or the project, or the building environment, or the way you observe how many threads are runningis wrong. I can suggest one idea:make surethe program #includes "omp.h" before it calls omp_set_num_threads().

Thanks
Dima
0 Kudos
noamp
Beginner
1,235 Views
There is nothing unusual in the project or the building environment. I added MKL settings by right-clicking on the project > Intel MKL Project Settings > Add Intel MKL 10.1.1.022.
I use Task Manager to observe the number of running threads.
My program #includes "omp.h".

I found something that may be useful:
Adding MKL settings adds some MKL libraries as additional dependenices. One of them is libiomp5mt.lib. If I manually remove this library from the dependency list, the problem is fixed. Meaning, my tiny program runs with 2 threads as expected.

If you have more ideas, I'll be happy to hear them.
Thanks,
Noam.
0 Kudos
TimP
Honored Contributor III
1,235 Views
Quoting noamp
There is nothing unusual in the project or the building environment. I added MKL settings by right-clicking on the project > Intel MKL Project Settings > Add Intel MKL 10.1.1.022.
I use Task Manager to observe the number of running threads.
My program #includes "omp.h".

I found something that may be useful:
Adding MKL settings adds some MKL libraries as additional dependenices. One of them is libiomp5mt.lib. If I manually remove this library from the dependency list, the problem is fixed. Meaning, my tiny program runs with 2 threads as expected.


This opens the suspicion that you linked more than one OpenMP library. That would hardly qualify as "nothing unusual." Did you examine your build log to check which libraries were linked? If using mkl_thread, you must not link vcomp.lib, for example. If using mkl sequential rather than mkl_thread, you can't blame MKL for threading behavior, as MKL will not start any threads.
If you had linked both libiomp5md and libiomp5mt, then removng the latter would improve things.
0 Kudos
noamp
Beginner
1,235 Views
I found a workaround that solves the problem.
While omp_set_num_threads() does not work no matter what I try, using num_threads() in the omp parallel clause works just fine:

int get_num_threads()
{
return 2;
}

void main()
{
#pragma omp parallel for num_threads(get_num_threads())
for (int i = 0; i < 100000; i++)
{
printf("Hello\n");
Sleep(100);
}
}

And so I can use get_num_threads to set number of threads in run-time, which is exactly what I wanted.
This took me by surprise - I didn't think I could call a function in the omp parallel clause, as it is a preprocessor directive (what am I missing here?)

Anyway, thanks for everyone who tried to help.
Noam.
0 Kudos
Reply