A loop is appropriate for explicit parallelization if:
It is a DO loop, but not a DO WHILE or Fortran 90 array syntax.
The values of array variables for each iteration of the loop do not depend on the values of array variables for any other iteration of the loop.
If the loop changes a scalar, that scalar is not referenced after the loop terminates. Such scalar variables are not guaranteed to have a defined value after the loop terminates, since the compiler does not automatically ensure a proper storeback for them.
For each iteration, any subprogram that is invoked inside the loop does not reference or change values of array variables for any other iteration.
The DO loop index must be an integer.
A private variable or array is private to a single iteration of a loop. The value assigned to a private variable or array in one iteration is not propagated to any other iteration of the loop.
A shared variable or array is shared with all other iterations. The value assigned to a shared variable or array in an iteration is seen by other iterations of the loop.
If an explicitly parallelized loop contains shared references, then you must ensure that sharing does not cause correctness problems. The compiler does no synchronization on updates or accesses to shared variables.
If you specify a variable as private in one loop, and its only initialization is within some other loop, the value of that variable may be left undefined in the loop.
For Sun-style (C$PAR) explicit directives, the compiler uses default rules to determine whether a scalar or array is shared or private. You can override the default rules to specify the attributes of scalars or arrays referenced inside a loop. (With Cray-style !MIC$ directives, all variables that appear in the loop must be explicitly declared either shared or private on the DOALL directive.)
The compiler applies these default rules:
All scalars are treated as private. A processor local copy of the scalar is made in each processor, and that local copy is used within that process.
All array references are treated as shared references. Any write of an array element by one processor is visible to all processors. No synchronization is performed on accesses to shared variables.
If inter-iteration dependencies exist in a loop, then the execution may result in erroneous results. You must ensure that these cases do not arise. The compiler may sometimes be able to detect such a situation at compile time and issue a warning, but it does not disable parallelization of such loops.
Example: Potential problem through equivalence:
equivalence (a(1),y) C$PAR DOALL do i = 1,n y = i a(i) = y end do
In the preceding example, since the scalar variable y has been equivalenced to a(1), it is no longer a private variable, even though the compiler treats it as such by the default scoping rule. Thus, the presence of the DOALL directive might lead to erroneous results when the parallelized i loop is executed.
You can fix the example by using C$PAR DOALL PRIVATE(y).