Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronization tools
Chapter 8 145
Figure 8-1 illustrates this assumption.
Figure 8-1 Ordered parallelization
Here, thread 0 executes first, followed by thread 1, and so on. Each
thread starts its iteration after the preceding iteration has started. A
manually defined ordered section prevents one thread from executing
the code in the ordered section until the previous thread exits the
section. This means that thread 0 cannot enter the section for iteration 9
until thread 7 exits it for iteration 8.
This is efficient only if the loop body contains enough code to keep a
thread busy until all other threads start their consecutive iterations,
thus taking advantage of parallelism.
You may find the max_threads attribute helpful when fine-tuning
loop_parallel(ordered) loops to fully exploit their parallel code.
Examples of synchronizing loop_parallel(ordered) loops are shown
in “Synchronizing code” on page 148.
Critical sections
Critical sections allow you to synchronize simple, nonordered
dependences. You must use the critical_section directive or pragma
to enter a critical section, and the end_critical_section directive or
pragma to exit one.
Critical sections must not contain branches to outside the section. The
two directives must appear in the same procedure, but they do not have
to be in the same procedure as the parallel construct in which they are
used. This means that the directives can exist in a procedure that is
called in parallel.
DO I = 1,100,8
...
ENDDO
DO I = 3,100,8
...
ENDDO
DO I = 2,100,8
...
ENDDO
DO I = 4,100,8
...
ENDDO
DO I = 5,100,8
...
ENDDO
DO I = 6,100,8
...
ENDDO
DO I = 7,100,8
...
ENDDO
DO I = 8,100,8
...
ENDDO
THREAD 0
THREAD 1
THREAD 2 THREAD 3
THREAD 4 THREAD 5
THREAD 6 THREAD 7