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

Sharing Data

The rule concerning sharing data that applies to parallel blocks applies to parfor statements as well. The parallel threads of control in a parfor statement (that is, the individual iterations of the loop) should not share mutable data. For example:


{
  int sum = 0; 
  parfor (int i=0; i<N; i++) 
    sum += A[i]; //dangerous sharing 
}

In this example, the mutable variable sum is modified in all N iterations. Recall from Section that such sharing is dangerous.

The loop control variable used in a parfor statement is a special case. This variable must be declared in the parfor statement itself, as is seen in the preceding two examples. Each iteration is then considered to have its own const copy of this loop control variable. The loop control variable cannot be modified within the body of a parfor.

paolo@cs.caltech.edu