55 |
|
|
56 |
C !DESCRIPTION: |
C !DESCRIPTION: |
57 |
C *==========================================================* |
C *==========================================================* |
58 |
C | SUBROUTINE BARRIER |
C | SUBROUTINE BARRIER |
59 |
C | o Barrier routine that uses "busy waiting". |
C | o Barrier routine that uses "busy waiting". |
60 |
C *==========================================================* |
C *==========================================================* |
61 |
C | This routine provides a pure fortran mechanism to |
C | This routine provides a pure fortran mechanism to |
62 |
C | synchronise multiple threads in a multi-threaded code. |
C | synchronise multiple threads in a multi-threaded code. |
63 |
C | No thread can leave this routine before all the threads |
C | No thread can leave this routine before all the threads |
64 |
C | have entered it. |
C | have entered it. |
65 |
C | Notes |
C | Notes |
66 |
C | ===== |
C | ===== |
67 |
C | The door and key variables are assumed to have been |
C | The door and key variables are assumed to have been |
68 |
C | initialized once an initial state of key = INVALID |
C | initialized once an initial state of key = INVALID |
69 |
C | and door = SHUT. |
C | and door = SHUT. |
70 |
C | We use the routine FOOL\_THE\_COMPILER to stop compilers |
C | We use the routine FOOL\_THE\_COMPILER to stop compilers |
71 |
C | generating code which might simply set and test a |
C | generating code which might simply set and test a |
72 |
C | register value. Shared-memory systems only maintain |
C | register value. Shared-memory systems only maintain |
73 |
C | coherency over process caches and not registers. |
C | coherency over process caches and not registers. |
74 |
C | Also we have to be a bit careful regarding sequential |
C | Also we have to be a bit careful regarding sequential |
75 |
C | consistency - or lack of it. At the moment the code |
C | consistency - or lack of it. At the moment the code |
76 |
C | assumes a total store order memory model, which some |
C | assumes a total store order memory model, which some |
77 |
C | machines do not have! However, I have yet to find a |
C | machines do not have! However, I have yet to find a |
78 |
C | problem with this I think because the tolerances in |
C | problem with this I think because the tolerances in |
79 |
C | terms of memory ordering i.e. a little bit of reordering |
C | terms of memory ordering i.e. a little bit of reordering |
80 |
C | probably will not break the barrier mechanism! |
C | probably will not break the barrier mechanism! |
81 |
C | On non-cache coherent systems e.g. T3E we need to use |
C | On non-cache coherent systems e.g. T3E we need to use |
82 |
C | a library function to do barriers. |
C | a library function to do barriers. |
83 |
C | Note - The PANIC tests can be removed for working code |
C | Note - The PANIC tests can be removed for working code |
84 |
C | I have left them in without an ifdef option |
C | I have left them in without an ifdef option |
85 |
C | because without them programming errors can |
C | because without them programming errors can |
86 |
C | lead to infinitely spinning code. If you are |
C | lead to infinitely spinning code. If you are |
87 |
C | confident that your code is OK then removing |
C | confident that your code is OK then removing |
88 |
C | them may increase performance. Do not remove these |
C | them may increase performance. Do not remove these |
89 |
C | lines to make your code "work" If the code is |
C | lines to make your code "work" If the code is |
90 |
C | stopping in these PANIC blocks then something is |
C | stopping in these PANIC blocks then something is |
91 |
C | wrong with your program and it needs to be fixed. |
C | wrong with your program and it needs to be fixed. |
92 |
C *==========================================================* |
C *==========================================================* |
93 |
|
|
94 |
C !USES: |
C !USES: |
104 |
|
|
105 |
C !LOCAL VARIABLES: |
C !LOCAL VARIABLES: |
106 |
C === Local variables === |
C === Local variables === |
107 |
C nDone :: Counter for number of threads that have |
C nDone :: Counter for number of threads that have |
108 |
C completed a section. |
C completed a section. |
109 |
C I :: Loop counter |
C I :: Loop counter |
110 |
INTEGER nDone |
INTEGER nDone |
119 |
C$OMP BARRIER |
C$OMP BARRIER |
120 |
bCount(myThid) = bCount(myThid) + 1 |
bCount(myThid) = bCount(myThid) + 1 |
121 |
IF ( masterSet(myThid) .NE. 0 ) THEN |
IF ( masterSet(myThid) .NE. 0 ) THEN |
122 |
PRINT *, 'BARRIER called for master reg myThid == ', |
PRINT *, 'BARRIER called for master reg myThid == ', |
123 |
& myThid, masterSet(myThid) |
& myThid, masterSet(myThid) |
124 |
ENDIF |
ENDIF |
125 |
Cdbg C$OMP BARRIER |
Cdbg C$OMP BARRIER |
141 |
C-- Check that thread number is expected range |
C-- Check that thread number is expected range |
142 |
IF ( myThid .LT. 1 .OR. myThid .GT. nThreads ) THEN |
IF ( myThid .LT. 1 .OR. myThid .GT. nThreads ) THEN |
143 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
144 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
145 |
& myThid, ' nThreads = ', nThreads |
& myThid, ' nThreads = ', nThreads |
146 |
STOP 'ABNROMAL END: S/R BARRIER' |
STOP 'ABNROMAL END: S/R BARRIER' |
147 |
ENDIF |
ENDIF |
149 |
C-- When every threads key1 is valid thread 1 will open door1. |
C-- When every threads key1 is valid thread 1 will open door1. |
150 |
IF ( key1(1,myThid) .EQ. VALID ) THEN |
IF ( key1(1,myThid) .EQ. VALID ) THEN |
151 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
152 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
153 |
& myThid, ' key1 already validated' |
& myThid, ' key1 already validated' |
154 |
STOP 'ABNROMAL END: S/R BARRIER' |
STOP 'ABNROMAL END: S/R BARRIER' |
155 |
ENDIF |
ENDIF |
161 |
DO I=1,nThreads |
DO I=1,nThreads |
162 |
if ( key1(1,I) .EQ. VALID ) nDone = nDone+1 |
if ( key1(1,I) .EQ. VALID ) nDone = nDone+1 |
163 |
ENDDO |
ENDDO |
164 |
CALL FOOL_THE_COMPILER( key1 ) |
CALL FOOL_THE_COMPILER( key1(1,1) ) |
165 |
IF ( nDone .LT. nThreads ) GOTO 10 |
IF ( nDone .LT. nThreads ) GOTO 10 |
166 |
door1 = OPEN |
door1 = OPEN |
167 |
ELSE |
ELSE |
189 |
C-- When every threads key2 is valid thread 1 will open door2. |
C-- When every threads key2 is valid thread 1 will open door2. |
190 |
C Notes |
C Notes |
191 |
C ===== |
C ===== |
192 |
C I think that to work with any memory model ( i.e. relaxed, |
C I think that to work with any memory model ( i.e. relaxed, |
193 |
C partial store, total store) the variables key1, key2 and key3 |
C partial store, total store) the variables key1, key2 and key3 |
194 |
C might need to be set to invalid by thread 1. |
C might need to be set to invalid by thread 1. |
195 |
C |
C |
196 |
IF ( key2(1,myThid) .EQ. VALID ) THEN |
IF ( key2(1,myThid) .EQ. VALID ) THEN |
197 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
198 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
199 |
& myThid, ' key2 already validated' |
& myThid, ' key2 already validated' |
200 |
STOP 'ABNROMAL END: S/R BARRIER' |
STOP 'ABNROMAL END: S/R BARRIER' |
201 |
ENDIF |
ENDIF |
207 |
DO I=1,nThreads |
DO I=1,nThreads |
208 |
if ( key2(1,I) .EQ. VALID ) nDone = nDone+1 |
if ( key2(1,I) .EQ. VALID ) nDone = nDone+1 |
209 |
ENDDO |
ENDDO |
210 |
CALL FOOL_THE_COMPILER( key2 ) |
CALL FOOL_THE_COMPILER( key2(1,1) ) |
211 |
IF ( nDone .LT. nThreads ) GOTO 20 |
IF ( nDone .LT. nThreads ) GOTO 20 |
212 |
door2 = OPEN |
door2 = OPEN |
213 |
ELSE |
ELSE |
226 |
IF ( myThid .EQ. 1 ) THEN |
IF ( myThid .EQ. 1 ) THEN |
227 |
door1 = SHUT |
door1 = SHUT |
228 |
ENDIF |
ENDIF |
229 |
|
|
230 |
|
|
231 |
C-- When every threads key3 is valid thread 1 will open door3. |
C-- When every threads key3 is valid thread 1 will open door3. |
232 |
IF ( key3(1,myThid) .EQ. VALID ) THEN |
IF ( key3(1,myThid) .EQ. VALID ) THEN |
233 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! CATASTROPHIC ERROR' |
234 |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
WRITE(*,*) '!!!!!!! PANIC !!!!!!! in S/R BARRIER myThid = ', |
235 |
& myThid, ' key3 already validated' |
& myThid, ' key3 already validated' |
236 |
STOP 'ABNROMAL END: S/R BARRIER' |
STOP 'ABNROMAL END: S/R BARRIER' |
237 |
ENDIF |
ENDIF |
243 |
DO I=1,nThreads |
DO I=1,nThreads |
244 |
if ( key3(1,I) .EQ. VALID ) nDone = nDone+1 |
if ( key3(1,I) .EQ. VALID ) nDone = nDone+1 |
245 |
ENDDO |
ENDDO |
246 |
CALL FOOL_THE_COMPILER( key3 ) |
CALL FOOL_THE_COMPILER( key3(1,1) ) |
247 |
IF ( nDone .LT. nThreads ) GOTO 30 |
IF ( nDone .LT. nThreads ) GOTO 30 |
248 |
door3 = OPEN |
door3 = OPEN |
249 |
ELSE |
ELSE |
266 |
CcnhDebugStarts |
CcnhDebugStarts |
267 |
C WRITE(myThid,*) ' Barrier exited ' |
C WRITE(myThid,*) ' Barrier exited ' |
268 |
CcnhDebugEnds |
CcnhDebugEnds |
269 |
|
|
270 |
RETURN |
RETURN |
271 |
END |
END |
272 |
|
|