在 C + + 11基于范围的 for 循环中使用 OpenMP?

这样做是否有任何相反的迹象? 或者这种行为是否有明确的规定?

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

因为 OpenMP 规范似乎只对 c + + 98有效,但我猜可能会有更多的不兼容性,因为 C + + 11线程,这里没有使用。我还是想确认一下。

21819 次浏览

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.

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