Parallel Programming Guide for HP-UX Systems
Data privatization
Privatizing loop variables
Chapter 6 117
.
if(atemp > amax) {
.
.
.
In this example, the loop_private variable atemp is conditionally
assigned in the loop. In order for atemp to be truly private, you must be
sure that at least one of the conditions is met so that atemp is assigned
on every iteration.
When the loop terminates, the save_last pragma ensures that atemp
and X contain the values they are assigned on the last iteration. These
values can then be used later in the program. The value of y, however, is
not available once the loop finishes because y is not specified as an
argument to save_last.
Example 6-8 save_last
There are some loop contexts in which the
save_last directive and pragma is misleading.
The following Fortran code provides an example of this:
C$DIR LOOP_PARALLEL
C$DIR LOOP_PRIVATE(S)
C$DIR SAVE_LAST
DO I = 1, N
IF(G(I) .GT. 0) THEN
S = G(I) * G(I)
ENDIF
ENDDO
While it may appear that the last value of S assigned is saved in this
example, you must remember that the SAVE_LAST directive applies only
to the last (Nth) iteration, with no regard for any conditionals contained
in the loop. For SAVE_LAST to be valid here, G(N) must be greater than 0
so that the assignment to S takes place on the final iteration.
Obviously, if this condition is predicted, the loop is more efficiently
written to exclude the IF test, so the presence of a SAVE_LAST in such a
loop is suspect.