92

Is there any counter-indication to doing this ? Or is the behavior well specified?

#pragma omp parallel for
for(auto x : stl_container)
{
   ...
}

Because it seems that OpenMP specification is only valid for c++98 but I guess there might be more incompatibilities due to C++11 threads, which are not used here. I wanted to be sure, still.

1
  • 1
    + Good question. Want to know that, too. Commented Jul 25, 2013 at 3:35

2 Answers 2

57

The OpenMP 4.0 specification was finalised and published several days ago here. It still mandates that parallel loops should be in the canonical form (§2.6, p.51):

for (init-expr; test-expr; incr-expr) structured-block

The standard allows for containers that provide random-access iterators to be used in all of the expressions, e.g.:

#pragma omp parallel for
for (it = v.begin(); it < v.end(); it++)
{
   ...
}

If you still insist on using the C++11 syntactic sugar, and if it takes a (comparatively) lot of time to process each element of stl_container, then you could use the single-producer tasking pattern:

#pragma omp parallel
{
   #pragma omp single
   {
      for (auto x : stl_container)
      {
         #pragma omp task
         {
            // Do something with x, e.g.
            compute(x);
         }
      }
   }
}

Tasking induces certain overhead so it would make no sense to use this pattern if compute(x); takes very little time to complete.

Sign up to request clarification or add additional context in comments.

7 Comments

I think that iterators are the way to go by now, but if you want your code to compile with gcc you need to substitute != with < otherwise you get an "invalid controlling predicate" error. By the way, do you know why?
According to this website : cplusplus.com/reference/iterator/RandomAccessIterator it should work...
In that website they don't put any pragma before the cycle. Just try to compile it :)
@DarioP, the != is an Intel-ism - the standard does not allow it to be used, but icpc is smart enough to deduce what's going on in the loop. I had it corrected. That's also one of the reasons why random-access iterators are required - the usual iterators only provide the == and != operators.
It seems that at least with apple clang 12.0.0, std::map has no random access iter, so the < relation doesnt work.
|
37

OpenMP 5.0 adds the following line on page 99, which makes a lot of range-based for loops OK !

2.12.1.3 A range-based for loop with random access iterator has a canonical loop form.

Source : https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-5.0.pdf

4 Comments

OpenMP 5.0 also provides initial support for C++14 and C++17. Not all of the features in those base language are supported, however.
Ok, but, which compiler does support OpenMP 5.0?
At least GCC 9+, Clang 10+ should support this feature.
What about Visual C++ 2019? I really like the "syntactic sugar", but cannot test it on Windows until I commit and someone else tries to build it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.