Openmp Set_Num_Threads() Is Not Working

OpenMP set_num_threads() is not working

Besides calling omp_get_num_threads() outside of the parallel region in your case, calling omp_set_num_threads() still doesn't guarantee that the OpenMP runtime will use exactly the specified number of threads. omp_set_num_threads() is used to override the value of the environment variable OMP_NUM_THREADS and they both control the upper limit of the size of the thread team that OpenMP would spawn for all parallel regions (in the case of OMP_NUM_THREADS) or for any consequent parallel region (after a call to omp_set_num_threads()). There is something called dynamic teams that could still pick smaller number of threads if the run-time system deems it more appropriate. You can disable dynamic teams by calling omp_set_dynamic(0) or by setting the environment variable OMP_DYNAMIC to false.

To enforce a given number of threads you should disable dynamic teams and specify the desired number of threads with either omp_set_num_threads():

omp_set_dynamic(0);     // Explicitly disable dynamic teams
omp_set_num_threads(4); // Use 4 threads for all consecutive parallel regions
#pragma omp parallel ...
{
... 4 threads used here ...
}

or with the num_threads OpenMP clause:

omp_set_dynamic(0);     // Explicitly disable dynamic teams
// Spawn 4 threads for this parallel region only
#pragma omp parallel ... num_threads(4)
{
... 4 threads used here ...
}

OpenMP with C and gcc omp_set_num_threads() has no effect

Call omp_set_num_threads(4); before you actually use it with #pragma omp parallel.

set_num_threads inside parallel not working

TL;DR You can't change the number of threads in a parallel region.

Remember this is a pool of threads, which get forked at the beginning of the parallel region. Inside they are not even synchronized (if you dont tell them too), thus OpenMP would need to terminate some of them at an unknown position - obviously a bad idea.

Your #pragma omp single makes the following code section be executed by a single thread, thus no need to set it via omp_set_num_threads.
BUT it doesnt change your pool, it just advises the compiler to schedule the following section to one thread - while the rest ignores it.

To show this behavior e.g. for university purposes i would suggest to print out only the thread id in parallel and single part. That way you can already tell it's working or not.

windows - visual studio 2013 : OpenMP: omp_set_num_threads() not working

Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.

setx OMP_NUM_THREADS 16

Is there any way to force openmp to get desired number of threads?

#pragma omp num_threads(6) reduction(+ : sum)  //default(none) 
{
//OMP_NUM_THREADS=6;
omp_set_num_threads(6);

This is wrong, you need to set the number of threads before the parallel region. Furthermore, you are missing the parallel clause there. So you can either do:

omp_set_num_threads(6)
#pragma omp parallel reduction(+ : sum)
{
...
}

or

#pragma omp parallel num_threads(6) reduction(+ : sum)

You do not need both.

The first approach (i.e., omp_set_num_threads) sets the number of threads to be used by all subsequent parallel regions, whereas the second approach num_threads explicitly set the number of threads to be used only by the enclosing parallel region.

If it still does not work then you have to explicitly disable dynamic teams:

By adding the following call:

omp_set_dynamic(0);
#pragma omp parallel ...
{
...
}

From source one can read:

Summary The omp_set_dynamic routine enables or disables dynamic
adjustment of the number of threads available
for the execution of
subsequent parallel regions by setting the value of the dyn-var ICV.

How can I set the number of OpenMP threads from within the program?

There are two ways1 one can use to set the number of threads from within the program:

Option #1

Use num_threads clause in a directive that opens a parallel region:

#pragma omp parallel num_threads(number_of_threads)

Option #2

Use omp_set_num_threads API function before a parallel region begins:

#include <omp.h>

// ...
omp_set_num_threads(number_of_threads);
#pragma omp parallel

Note: Both options take priority over OMP_NUM_THREADS environment variable, but num_threads clause has precedence over omp_set_num_threads.

Why setenv fails to have any effect?

This is covered in the OpenMP specification (emphasis mine):

Chapter 4

Environment Variables

[...] Modifications to the environment variables after the program has started, even if modified by the program itself, are ignored by the OpenMP implementation. However, the settings of some of the ICVs can be modified during the execution of the OpenMP program by the use of the appropriate directive clauses or OpenMP API routines. [...]


1) There is a third run-time option that allows to alter the number of threads executing a parallel region that follows by resetting it to 1 (master thread only) or to the number from num_threads clause or omp_set_num_threads call, which is an if clause in a directive the clause belongs to.



Related Topics



Leave a reply



Submit