3
votes

I am trying to parallelize a range-based for loop in OpenMP, by making each iteration of the loop an OpenMP section. I want to do this:

#pragma omp parallel sections 
{

for ( auto x : range ) {
  #pragma omp section
  // LOTS OF CODE HERE
}

}

However, this doesn't work - the compiler complains that the section must be within a #pragma omp sections construct . Am I doing something wrong, or is OpenMP not smart enough to understand that this section is within a for loop?

1
Wjy don't you try with take rather than sections? It would be a more natural code, imho.Harald
Do you mean tasks?v2v1
Oops! Yes! I meant tasks :)Harald

1 Answers

3
votes

OpenMP sections are for somewhat unrelated bits of code that can be executed in parallel. The idea is that within your algorithms, there are different parts which aren't super organized, but which could be executed in any order and even in parallel. This is a very ad-hoc way of parallelizing, which you don't expect to see much in a code.

Anyway, your code isn't standard compliant since all blocks within a sections construct must be enclosed inside a section block (but the first for which this is optional as it is implicitly enclosed in a first sectionblock). Here, if you where to add this implicit #pragma omp section right before your for loop, you'd see how little sense the code would have: the loop is in a section and the body into another...

Here, since you have a for loop, this structures your code very well and should permit you to use a omp parallel for directive. You only need to rewrite a bit your for such as to explicit better the loop boundaries.