- Code: Select all
#pragma omp parallel for collapse(2)
for (int x = 0; x < 3; x ++)
for (int y = 0; y <= x; y ++)
#pragma omp critical(cout)
std::cout << "x = " << x << ", y = " << y << std::endl;
The standard states that the "x" in "y <= x" must be a loop-invariant expression. "x" is loop invariant with respect to the second loop, but I could not find in the standard how this extends to collapsed loops. Still, there is a very good use case that justifies legalizing this construct (namely, to compute all elements on a triangle in parallel).
BTW, neither g++ nor icpc behave as expected; they both skip some iterations.