Parallel Programming Guide for HP-UX Systems

Data privatization
Privatizing loop variables
Chapter 6112
Example 6-1 loop_private
The following is a Fortran example of loop_private:
C$DIR LOOP_PRIVATE(S)
DO I = 1, N
C S IS ONLY CORRECTLY PRIVATE IF AT LEAST
C ONE IF TEST PASSES ON EACH ITERATION:
IF(A(I) .GT. 0) S = A(I)
IF(U(I) .LT. V(I)) S = V(I)
IF(X(I) .LE. Y(I)) S = Z(I)
B(I) = S * C(I) + D(I)
ENDDO
A potential loop-carried dependence on S exists in this example. If none
of the IF tests are true on a given iteration, the value of S must wrap
around from the previous iteration. The LOOP_PRIVATE(S) directive
indicates to the compiler that S does, in fact, get assigned on every
iteration, and therefore it is safe to parallelize this loop.
If on any iteration none of the IF tests pass, an actual LCD exists and
privatizing S results in wrong answers.
Example 6-2 Using loop_private with loop_parallel
Because the compiler does not automatically perform variable
privatization in
loop_parallel loops, you must manually privatize loop data requiring
privatization. This is easily done using the loop_private directive or
pragma.
The following Fortran example shows how loop_private manually
privatizes loop data:
SUBROUTINE PRIV(X,Y,Z)
REAL X(1000), Y(4,1000), Z(1000)
REAL XMFIED(1000)
C$DIR LOOP_PARALLEL, LOOP_PRIVATE(XMFIED, J)
DO I = 1, 4
C INITIALIZE XMFIED; MFY MUST NOT WRITE TO X:
CALL MFY(X, XMFIED)
DO J = 1, 999
IF (XMFIED(J) .GE. Y(I,J)) THEN
Y(I,J) = XMFIED(J) * Z(J)
ELSE
XMFIED(J+1) = XMFIED(J)
ENDIF
ENDDO
ENDDO
END