- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
I tried calling mkl_set_num_threads, but it didn't change the behavior of my program - still 5 running threads.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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 had linked both libiomp5md and libiomp5mt, then removng the latter would improve things.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page