Parallel Programming Guide for HP-UX Systems

Parallel synchronization
Synchronizing code
Chapter 8162
3. Copy output queue into output buffer—the output queue is where
other tasks write their output. It can only be emptied when no
computational task is writing to it. This section of code is protected
by the OUTGATE gate. It can run in parallel with the computational
portions of other tasks, but only one task can access the output queue
at a time.
4. Write out output buffer—no other tasks access the output buffer.
This is done in parallel regardless of what the other tasks are doing.
Next, it is important to look at the subroutine THREAD_WRK, which tasks
2-7 call to perform computations.
SUBROUTINE
>
THREAD_WRK(NIN,NOUT,QIN,QOUT,IN_QIN,OUT_QIN,IN_QOUT,OUT_
QOUT)
INTEGER NIN,NOUT
REAL QIN(1000,1000), QOUT(1000,1000) ! SHARED I/O
QUEUES
INTEGER OUT_QIN, OUT_QOUT
COMMON INGATE,OUTGATE,COMPBAR
C$DIR GATE(INGATE, OUTGATE)
REAL WORK(1000) ! LOCAL THREAD PRIVATE WORK
ARRAY
LOGICAL OUTFLAG, INDONE
OUTFLAG = .FALSE.
C$DIR THREAD_PRIVATE (WORK) ! EVERY THREAD WILL CREATE A
COPY
DO WHILE(.NOT. INDONE() .OR. NIN.GT.0 .OR. OUTFLAG)
C WORK/QOUT EMPTYING LOOP
IF (.NOT. OUTFLAG) THEN ! IF NO PENDING OUTPUT
C$DIR CRITICAL_SECTION (INGATE) ! BLOCK ACCESS TO INPUT
QUE
IF (NIN .GT. 0) THEN ! MORE WORK TO DO
WORK(:) = QIN(:,OUT_QIN)
OUT_QIN = 1 + MOD(OUT_QIN, 1000)
NIN = NIN - 1
OUTFLAG = .TRUE.
C INDICATE THAT INPUT DATA HAS
BEEN RECEIVED
ENDIF
C$DIR END_CRITICAL_SECTION