Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMp set number of threads for parallel loop depending on variable

Tags:

c++

openmp

Is there a way to set number of threads in OpenMP parallel for region based on the value of a variable? Initially for the whole application number of threads = nofCores. On my AMD FX 8350, nofCores =8. For this area if the variable is 3 then I only need 3 threads. If variable>cores then number of threads should remain equal to nofCores.

I do not want to set the number of threads globally for all the application. Just for this specific parallel loop.

Sorry if this is a naive question, but I am a newbie in OpenMP.

like image 370
Alexandros Avatar asked Oct 08 '13 12:10

Alexandros


People also ask

How do I limit the number of threads in OpenMP?

The OMP_THREAD_LIMIT environment variable sets the maximum number of OpenMP threads to use in a contention group by setting the thread-limit-var ICV. The value of this environment variable must be a positive integer.

How many threads are created by pragma OMP parallel for?

OpenMP parallel for loops However, the #pragma omp parallel for directive instructs the compiler to generate code that splits the iterations of the loop among multiple threads. For example, if we have 4 CPU cores, OpenMP typically uses 4 threads.

How many threads does default OpenMP use?

When run, an OpenMP program will use one thread (in the sequential sections), and several threads (in the parallel sections). There is one thread that runs from the beginning to the end, and it's called the master thread.


1 Answers

Sure .. just tack this on to your parallel for directive:

#pragma parallel for num_threads(variable)
for( ... )
like image 109
eduffy Avatar answered Sep 26 '22 04:09

eduffy