Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronizing code
Chapter 8148
Synchronizing code
Code containing dependences are parallelized by synchronizing the way
the parallel tasks access the dependence. This is done manually using
the gates, barriers and synchronization functions discussed earlier in
this chapter, or semiautomatically using critical and ordered sections,
described in the following sections.
Using critical sections
The critical_section example shown below isolates a single critical
section in a loop, so that the critical_section directive does not
require a gate. In this case, the critical section directives automate
allocation, locking, unlocking and deallocation of the needed gate.
Multiple dependences and dependences in manually-defined parallel
tasks are handled when user-defined gates are used with the directives.
Example 8-4 critical_section
The following Fortran example, however, uses the manual methods of
code synchronization:
REAL GLOBAL_SUM
C$DIR FAR_SHARED(GLOBAL_SUM)
C$DIR GATE(SUM_GATE)
.
.
.
LOCK = ALLOC_GATE(SUM_GATE)
C$DIR BEGIN_TASKS
CONTRIB1 = 0.0
DO J = 1, M
CONTRIB1 = CONTRIB1 + FUNC1(J)
ENDDO
.
.
.
C$DIR CRITICAL_SECTION (SUM_GATE)
GLOBAL_SUM = GLOBAL_SUM + CONTRIB1
C$DIR END_CRITICAL_SECTION
.
.
.