In general, the compiler parallelizes a loop if you explicitly direct it to. There are exceptions--some loops the compiler just cannot parallelize.
The following are the primary detectable inhibitors that might prevent explicitly parallelizing a DO loop:
The DO loop is nested inside another DO loop that is parallelized.
This exception holds for indirect nesting, too. If you explicitly parallelize a loop that includes a call to a subroutine, then even if you parallelize loops in that subroutine, those loops are not run in parallel at runtime.
A flow control statement allows jumping out of the DO loop.
The index variable of the loop is subject to side effects, such as being equivalenced.
If you compile with -vpara, you may get a warning message if f77/f90 detects a problem with explicitly parallelizing a loop. f77/f90 may still parallelize the loop. The following list of typical parallelization problems shows those that are ignored by the compiler
Table 10-6 Explicit Parallelization Problems
Problem |
Parallelized |
Message |
---|---|---|
Loop is nested inside another loop that is parallelized. |
No |
No |
Loop is in a subroutine, and a call to the subroutine is in a parallelized loop. |
No |
No |
Jumping out of loop is allowed by a flow control statement. |
No |
Yes |
Index variable of loop is subject to side effects. |
Yes |
No |
Some variable in the loop keeps a loop-carried dependency. |
Yes |
Yes |
I/O statement in the loop--usually unwise, because the order of the output is not predictable. |
Yes |
No |
and those that generate messages with -vpara.
... C$PAR DOALL do 900 i = 1, 1000 ! Parallelized (outer loop) do 200 j = 1, 1000 ! Not parallelized, no warning ... 200 continue 900 continue ... demo% f77 -explicitpar -vpara t6.f
Example: A parallelized loop in a subroutine:
C$PAR DOALL do 100 i = 1, 200 ... call calc (a, x) ... 100 continue ... demo% f77 -explicitpar -vpara t.f |
subroutine calc ( b, y ) ... C$PAR DOALL do 1 m = 1, 1000 ... 1 continue return end |
At runtime, the loop could run in parallel. |
At runtime, both loops do not run in parallel. |
In the preceding example, the loop within the subroutine is not parallelized because the subroutine itself is run in parallel.
Example: Jumping out of a loop:
C$PAR DOALL do i = 1, 1000 ! ¨ Not parallelized, with warning ... if (a(i) .gt. min_threshold ) go to 20 ... end do 20 continue ... demo% f77 -explicitpar -vpara t9.f
Example: An index variable subject to side effects:
equivalence ( a(1), y ) ! ¨ Source of possible side effects ... C$PAR DOALL do i = 1, 2000 ! ¨ Parallelized: no warning, but not safe y = i a(i) = y end do ... demo% f77 -explicitpar -vpara t11.f
Example: A variable in a loop has a loop-carried dependency:
C$PAR DOALL do 100 i = 1, 200 ! Parallelized, with warning y = y * i ! y has a loop-carried dependency a(i) = y 100 continue ... demo% f77 -explicitpar -vpara t12.f