Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronizing code
Chapter 8 157
.
ENDDO
LOCK = FREE_GATE(CRITSEC)
As shown, the manual implementation requires declaring, allocating,
and deallocating a gate, which must be locked on entry into the critical
section using the LOCK_GATE function and unlocked on exit using
UNLOCK_GATE.
Example 8-10 Conditionally lock critical sections
Another advantage of manually deļ¬ned critical sections is the ability to
conditionally lock them. This allows the task that wishes to execute the
section to proceed with other work if the lock cannot be acquired. This
construct is useful, for example, in situations where one thread is
performing I/O for several other parallel threads.
While a processing thread is reading from the input queue, the queue is
locked, and the I/O thread can move on to do output. While a processing
thread is writing to the output queue, the I/O thread can do input. This
allows the I/O thread to keep as busy as possible while the parallel
computational threads execute their (presumably large) computational
code.
This situation is illustrated in the following Fortran example. Task 1
performs I/O for the 7 other tasks, which perform parallel computations
by calling the THREAD_WRK subroutine:
COMMON INGATE,OUTGATE,COMPBAR
C$DIR GATE (INGATE, OUTGATE)
C$DIR BARRIER (COMPBAR)
REAL DIN(:), DOUT(:) ! I/O BUFFERS FOR TASK 1
ALLOCATABLE DIN, DOUT ! THREAD 0 WILL ALLOCATE
REAL QIN(1000,1000), QOUT(1000,1000) ! SHARED I/O
QUEUES
INTEGER NIN/0/,NOUT/0/ ! QUEUE ENTRY COUNTERS
C CIRCULAR BUFFER POINTERS:
INTEGER
IN_QIN/1/,OUT_QIN/1/,IN_QOUT/1/,OUT_QOUT/1/
COMMON /DONE/ DONEIN, DONECOMP
LOGICAL DONECOMP, DONEIN
C SIGNALS FOR COMPUTATION DONE
AND INPUT DONE
LOGICAL COMPDONE, INDONE
C FUNCTIONS TO RETURN DONECOMP