88

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.

Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75

2 Answers2

56

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.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • 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? – DarioP Jul 25 '13 at 09:33
  • According to this website : http://www.cplusplus.com/reference/iterator/RandomAccessIterator/ it should work... – Jean-Michaël Celerier Jul 25 '13 at 10:02
  • In that website they don't put any pragma before the cycle. Just try to compile it :) – DarioP Jul 25 '13 at 10:37
  • 3
    @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. – Hristo Iliev Jul 25 '13 at 10:45
  • It seems that at least with apple clang 12.0.0, `std::map` has no random access iter, so the `<` relation doesnt work. – oarfish Dec 07 '20 at 10:27
  • @HristoIliev, what is the type of ``it`` in your first code-snippet? Surely it does not compile the way it is now. It is of the same type as the elements of ``v`` ? Thanks – velenos14 Jul 14 '23 at 18:55
  • @velenos14 it is of the type returned by `v.begin()`. In modern C++ that could be `auto`. – Hristo Iliev Jul 17 '23 at 06:01
32

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

Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75