Parallel Programming Guide for HP-UX Systems

Data privatization
Privatizing loop variables
Chapter 6 115
Example 6-6 Secondary induction variables
In C, secondary induction variables are sometimes included in
for statements, as shown in the following example:
/* warning: unparallelizable code follows */
#pragma _CNX loop_parallel(ivar=i)
for(i=j=0; i<n;i++,j+=2) {
a[i] = ...;
.
.
.
}
}
Because secondary induction variables must be private to the loop and
must be a function of the primary induction variable, this example
cannot be safely parallelized using loop_parallel(ivar=i). In the
presence of this directive, the secondary induction variable is not
recognized.
To manually parallelize this loop, you must remove j from the for
statement, privatize it, and make it a function of i.
The following example demonstrates how to restructure the loop so that
j is a valid secondary induction variable:
#pragma _CNX loop_parallel(ivar=i)
#pragma _CNX loop_private(j)
for(i=0; i<n; i++) {
j = 2*i;
a[i] = ...;
.
.
.
}
}
This method runs faster than placing j in a critical section because it
requires no synchronization overhead, and the private copy of j used
here can typically be more quickly accessed than a shared variable.
save_last[(
list
)]
A save_last directive or pragma causes the thread that executes the
last iteration of the loop to write back the private (or local) copy of the
variable into the global reference.