4

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.

George Karanikas
  • 1,244
  • 1
  • 12
  • 38
  • 1
    The only method I can think of is to duplicate the loop into an if-statement... – Mysticial Feb 20 '12 at 18:32
  • That's what I meant in the end as well. My bad, I'm a bit tired. I know that is one solution but it will increase also the complexity and the size of the code. That is why it is my last resort. – George Karanikas Feb 20 '12 at 18:38
  • I think @Mysticial has the right idea. A #pragma normally controls things that happen at compile time, therefore, the code generated for the `for` loop is likely to be different between the two -- you use a run-time condition to select the #pragma by itself. – Jerry Coffin Feb 20 '12 at 18:39
  • You could also write the loop body as a massive multi-line macro. That saves a bit of code-replication. But it's ugly. – Mysticial Feb 20 '12 at 18:39
  • @Mysticial: Why macro? Simple function should work just fine I guess – LihO Feb 20 '12 at 18:51
  • @LihO You can't throw an OpenMP "for" over a function call. – Mysticial Feb 20 '12 at 19:06
  • @Mysticial: I understand you now. What I meant was to replace **body** of the loop with a function, not to replace loop itself. – LihO Feb 20 '12 at 19:37
  • @LihO I didn't think of that way. Yes, it would work if you duplicated the loop, but put the loop-body in a function call. I was thinking on the lines of putting the entire loop in a function/macro. The `#pragma omp parallel for` needs to be followed by a `for`-loop. So my initial thought of putting the entire loop in a function call, of course, won't work. – Mysticial Feb 20 '12 at 19:39

1 Answers1

8

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(....);
}
LihO
  • 41,190
  • 11
  • 99
  • 167
  • 1
    +1 for putting just the loop body in the function call. Add that to your answer since it's buried under the comments. – Mysticial Feb 20 '12 at 19:43
  • @Mysticial: Done. Should be clear enough now. Feel free to edit it if you think it should be edited ;) – LihO Feb 20 '12 at 20:02