Previous: Loop Unraveling Up: Structured Parallel Loops: parfor Next: Examples

Pitfalls

  1. The loop control variable must be declared in the parfor statement. Variables that come from a higher scope cannot be used as loop control variables in a parallel loop. The following example is a compile-time error:
    
    {
      int i; 
      parfor (i=0; i<N; i++)  //ERROR
        { ... }
    }
    

  2. The (sequential) body of a parfor represents a nested level of scoping within the parallel composition. Declarations are therefore permitted at this level.
    
    parfor (int i=0; i<N; i++)  { 
      int x, y = i*2;  
      x = f(); 
      g(x,y);
    }
    

  3. Nesting sequential loops within parallel loops must be done with care. Consider the following sequential code:
    
    {
      int i,j;
      for (i=0; i<N; i++)
        for (j=0; j<N; j++)
          { ... }
    }
    

    The following parallelization of this code is incorrect:

    
    {
      int j;
      parfor (int i=0; i<N; i++)
        for (j=0; j<N; j++)        //Error: j is a shared mutable
          { ... }
    }
    

    This code is incorrect because the mutable variable j is shared between the concurrently executing iterations of the parallel loop. All sequential loops nested within a parallel loop should declare their loop control variables.

  4. In general, the creation and deletion of parallel threads of control can be a relatively expensive operation. If the amount of computation performed by each iteration is small, such a program could exhibit significant performance degradation. This cost puts a practical limitation on the number of iterations composed in parallel by a parfor statement.

  5. No gotos into, out of, or between iterations of a parfor are permitted. No break, continue, goto, or return statements are permitted at the parfor level of scope.

The current implementation restrictions are the same as those for parallel blocks (see Section ).

paolo@cs.caltech.edu