collapsed loops & loop-independent variables

General OpenMP discussion

collapsed loops & loop-independent variables

Postby romein » Wed Aug 07, 2013 5:52 am

Is it legal to collapse a parallel loop where the iteration count of the second loop depends on the first loop, as in:

Code: Select all
#include <iostream>

int main()
#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;

  return 0;

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.
Posts: 1
Joined: Fri Aug 02, 2013 6:02 am

Re: collapsed loops & loop-independent variables

Postby MarkB » Mon Aug 12, 2013 4:34 am

This case is not legal, I'm afraid. The relevant part of the standard is on page 56, lines 1-3 in Version 4.0:

"The iteration count for each associated loop is computed before entry to the outermost loop. If execution of any associated loop changes any of the values used to compute any of the iteration counts, then the behavior is unspecified."

This is a reasonable use case, but allowing it as a special case would be quite awkward to specify: the general case is not tractable!
Posts: 746
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests