Parallel Programming Guide for HP-UX Systems

Data privatization
Privatizing loop variables
Chapter 6114
ivar is required in all loop_parallel C loops. Its use is shown in the
following example:
#pragma _CNX loop_parallel(ivar=i)
for(i=0; i<n; i++) {
a[i] = ...;
.
. /* assume no dependences */
.
}
}
Secondary induction variables
Secondary induction variables are variables used to track loop iterations,
even though they do not appear in the Fortran DO statement. They
cannot appear in addition to the primary induction variable in the C for
statement.
Such variables
must
be a function of the primary loop induction variable,
and they cannot be independent. Secondary induction variables must be
assigned loop_private.
Example 6-5 Secondary induction variables
The following Fortran example contains an incorrectly incremented
secondary induction variable:
C WARNING: INCORRECT EXAMPLE!!!!
J = 1
C$DIR LOOP_PARALLEL
DO I = 1, N
J = J + 2 ! WRONG!!!
In this example, J does not produce expected values in each iteration
because multiple threads are overwriting its value with no
synchronization. The compiler cannot privatize J because it is a
loop-carried dependence (LCD). This example is corrected by privatizing
J and making it a function of I, as shown below.
C CORRECT EXAMPLE:
J = 1
C$DIR LOOP_PARALLEL
C$DIR LOOP_PRIVATE(J) ! J IS PRIVATE
DO I = 1, N
J = (2*I)+1 ! J IS PRIVATE
As shown in the preceding example, J is assigned correct values on each
iteration because it is a function of I and is safely privatized.