Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronizing code
Chapter 8156
Manual synchronization
Ordered and critical sections allow you to isolate dependences in a
structured, semiautomatic manner. The same isolation is accomplished
manually using the functions discussed in “Synchronization functions”
on page 137.
Example 8-9 Critical sections and gates
Below is a simple critical section Fortran example using loop_parallel:
C$DIR LOOP_PARALLEL
DO I = 1, N ! LOOP IS PARALLELIZABLE
.
.
.
C$DIR CRITICAL_SECTION
SUM = SUM + X(I)
C$DIR END_CRITICAL_SECTION
.
.
.
ENDDO
As shown, this example is easily implemented using critical sections. It is
manually implemented in Fortran, using gate functions, as shown below:
C$DIR GATE(CRITSEC)
.
.
.
LOCK = ALLOC_GATE(CRITSEC)
C$DIR LOOP_PARALLEL
DO I = 1, N
.
.
.
LOCK = LOCK_GATE(CRITSEC)
SUM = SUM + X(I)
LOCK = UNLOCK_GATE(CRITSEC)
.
.