Parallel Programming Guide for HP-UX Systems

Data privatization
Privatizing region variables
Chapter 6 121
parallel_private privatizes regions:
REAL A(1000,8), B(1000,8), C(1000,8), AWORK(1000), SUM(8)
INTEGER MYTID
.
.
.
C$DIR PARALLEL(MAX_THREADS = 8)
C$DIR PARALLEL_PRIVATE(I,J,K,L,M,AWORK,MYTID)
IF(NUM_THREADS() .LT. 8) STOP "NOT ENOUGH THREADS; EXITING"
MYTID = MY_THREAD() + 1 !ADD 1 FOR PROPER SUBSCRIPTING
DO I = 1, 1000
AWORK(I) = A(I, MYTID)
ENDDO
DO J = 1, 1000
A(J, MYTID) = AWORK(J) + B(J, MYTID)
ENDDO
DO K = 1, 1000
B(K, MYTID) = B(K, MYTID) * AWORK(K)
C(K, MYTID) = A(K, MYTID) * B(K, MYTID)
ENDDO
DO L = 1, 1000
SUM(MYTID) = SUM(MYTID) + A(L,MYTID) + B(L,MYTID) +
C(L,MYTID)
ENDDO
DO M = 1, 1000
A(M, MYTID) = AWORK(M)
ENDDO
C$DIR END_PARALLEL
This example checks for a certain number of threads and divides up the
work among those threads. The example additionally introduces the
parallel_private variable AWORK.
Each thread initializes its private copy of AWORK to the values contained
in a dimension of the array A at the beginning of the parallel region. This
allows the threads to reference AWORK without regard to thread ID. This
is because no thread can access any other thread’s copy of AWORK.
Because AWORK cannot carry values into or out of the region, it must be
initialized within the region.
Induction variables in region privatization
All induction variables contained in a parallel region must be privatized.
Code contained in the region runs on all available threads. Failing to
privatize an induction variable would allow each thread to update the
same shared variable, creating indeterminate loop counts on every
thread.