Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose OpenMP pragma according to condition

I have a code that I want to optimise that should run in a variaty of threads ammount. After running some tests using different scheduling techniques in a for loop that I have, I came to the conclusion that what suits best is to perform a dynamic scheduling when I have only one thread and guided otherwise. Is that even possible in openMP?

To be more precise I want to be able to do something like the following:

if(omp_get_max_threads()>1)
#pragma omp parallel for .... scheduling(guided)
else
#pragma omp parallel for .... scheduling(dynamic)
for(.....){
  ...
}

If anyone can help me I would appreciate it. The other solution would be to write two times the for loop and use an if condition. But I want to avoid that if it is possible.

like image 807
George Karanikas Avatar asked Feb 21 '23 10:02

George Karanikas


1 Answers

Possible solution is to copy the loop into an if statement and to "extract" loop body into function to avoid breaking DRY principle. Then there will be only one place where you have to change this code if you need to change it in the future:

void foo(....)
{
   ...
}

if(omp_get_max_threads()>1)
{
    #pragma omp parallel for .... scheduling(guided)
    for (.....)
        foo(....);
}
else
{
    #pragma omp parallel for .... scheduling(dynamic)
    for (.....)
        foo(....);
}
like image 187
LihO Avatar answered Feb 24 '23 00:02

LihO