1 |
C $Header: /u/gcmpack/MITgcm/eesupp/src/ini_threading_environment.F,v 1.12 2009/06/07 14:54:57 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "CPP_EEOPTIONS.h" |
5 |
#include "PACKAGES_CONFIG.h" |
6 |
|
7 |
CBOP |
8 |
|
9 |
C !ROUTINE: INI_THREADING_ENVIRONMENT |
10 |
|
11 |
C !INTERFACE: |
12 |
SUBROUTINE INI_THREADING_ENVIRONMENT |
13 |
IMPLICIT NONE |
14 |
|
15 |
C !DESCRIPTION: |
16 |
C *==========================================================* |
17 |
C | SUBROUTINE INI\_THREADING\_ENVIRONMENT |
18 |
C | o Initialise multi-threaded environment. |
19 |
C *==========================================================* |
20 |
C | Generally we do not start separate threads here. |
21 |
C | The separate threads a spawned at later on. |
22 |
C | Here we perform initialisation of data-structures |
23 |
C | that indicate which of the nSx x nSy tiles a thread is |
24 |
C | responsible for. |
25 |
C | The multiple threads are spawned in the top level MAIN |
26 |
C | routine. |
27 |
C *==========================================================* |
28 |
|
29 |
C !USES: |
30 |
C == Global data == |
31 |
#include "SIZE.h" |
32 |
#include "EEPARAMS.h" |
33 |
#include "EESUPPORT.h" |
34 |
|
35 |
C !LOCAL VARIABLES: |
36 |
C == Local variables == |
37 |
C bXPerThread - Blocks of size sNx per thread. |
38 |
C byPerThread - Blocks of size sNy per thread. |
39 |
C thId - Thread index. Temporary used in loops |
40 |
C which set per. thread values on a |
41 |
C cartesian grid. |
42 |
C bxLo, bxHi - Work vars. for thread index |
43 |
C byLo, byHi range. bxLo is the lowest i index |
44 |
C that a thread covers, bxHi is the |
45 |
C highest i index. byLo is the lowest |
46 |
C j index, byHi is the highest j index. |
47 |
C I, J - Loop counter |
48 |
C msgBuf - I/O buffer for reporting status information. |
49 |
C myThid - Dummy thread id for use in printed messages |
50 |
C ( this routine "INI_THREADING_ENVIRONMENT" is |
51 |
C called before multi-threading has started.) |
52 |
INTEGER bxPerThread |
53 |
INTEGER byPerThread |
54 |
INTEGER thId |
55 |
INTEGER bxLo, bxHi |
56 |
INTEGER byLo, byHi |
57 |
INTEGER I, J |
58 |
CHARACTER*(MAX_LEN_MBUF) msgBuf |
59 |
INTEGER myThid |
60 |
#ifndef ALLOW_EXCH2 |
61 |
LOGICAL flag |
62 |
#endif |
63 |
CEOP |
64 |
|
65 |
C-- Set default for all threads of having no blocks to |
66 |
C-- work on - except for thread 1. |
67 |
myBxLo(1) = 1 |
68 |
myBxHi(1) = nSx |
69 |
myByLo(1) = 1 |
70 |
myByHi(1) = nSy |
71 |
DO I = 2, MAX_NO_THREADS |
72 |
myBxLo(I) = 0 |
73 |
myBxHi(I) = 0 |
74 |
myByLo(I) = 0 |
75 |
myByHi(I) = 0 |
76 |
ENDDO |
77 |
myThid = 1 |
78 |
commName(COMM_NONE) = 'none' |
79 |
commName(COMM_MSG ) = 'messages' |
80 |
commName(COMM_PUT ) = 'put' |
81 |
commName(COMM_GET ) = 'get' |
82 |
|
83 |
C-- If there are multiple threads allocate different range of the |
84 |
C-- nSx*nSy blocks to each thread. |
85 |
C For now handle simple case of no. blocks nSx = n*nTx and |
86 |
C no. blocks nSy = m*nTy ( where m and n are integer ). This |
87 |
C is handled by simply mapping threads to blocks in sequence |
88 |
C with the x thread index moving fastest. |
89 |
C Later code which sets the thread number of neighboring blocks |
90 |
C needs to be consistent with the code here. |
91 |
nThreads = nTx * nTy |
92 |
IF ( nThreads .GT. MAX_NO_THREADS ) THEN |
93 |
WRITE(msgBuf,'(2A,2I6)') |
94 |
& 'S/R INI_THREADING_ENVIRONMENT:', |
95 |
& ' Total number of threads exceeds MAX_NO_THREADS', |
96 |
& nTx*nTy, MAX_NO_THREADS |
97 |
CALL PRINT_ERROR(msgBuf, myThid) |
98 |
WRITE(msgBuf,'(2A)') |
99 |
& ' Needs to increase MAX_NO_THREADS', |
100 |
& ' in file "EEPARAMS.h" and to re-compile' |
101 |
CALL PRINT_ERROR(msgBuf, myThid) |
102 |
eeBootError = .TRUE. |
103 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
104 |
ENDIF |
105 |
|
106 |
C-- Initialise the barrier mechanisms |
107 |
C BAR2 will eventually replace barrier everywhere. |
108 |
CALL BARRIER_INIT |
109 |
DO I=1, MAX_NO_THREADS |
110 |
CALL BAR2_INIT(I) |
111 |
ENDDO |
112 |
|
113 |
C-- Initialise exchange mechanism |
114 |
CALL EXCH_INIT |
115 |
|
116 |
IF ( nThreads .NE. nTx*nTy ) THEN |
117 |
WRITE(msgBuf,'(A,A,A,I5,A,I5)') |
118 |
& 'S/R INI_THREADING_ENVIRONMENT:', |
119 |
& ' Total number of threads is not the same as nTx*nTy.', |
120 |
& ' nTx * nTy = ',nTx*nTy,' nThreads = ',nThreads |
121 |
CALL PRINT_ERROR(msgBuf, myThid) |
122 |
eeBootError = .TRUE. |
123 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
124 |
ENDIF |
125 |
bxPerThread = nSx/nTx |
126 |
IF ( bxPerThread*nTx .NE. nSx ) THEN |
127 |
WRITE(msgBuf,'(A,A,A)') |
128 |
& 'S/R INI_THREADING_ENVIRONMENT:', |
129 |
& ' Number of blocks in X (nSx)', |
130 |
& ' must be exact multiple of threads in X (nTx).' |
131 |
CALL PRINT_ERROR(msgBuf, myThid) |
132 |
eeBootError = .TRUE. |
133 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
134 |
ENDIF |
135 |
byPerThread = nSy/nTy |
136 |
IF ( byPerThread*nTy .NE. nSy ) THEN |
137 |
WRITE(msgBuf,'(A,A,A)') |
138 |
& 'S/R INI_THREADING_ENVIRONMENT:', |
139 |
& ' Number of blocks in Y (nSy)', |
140 |
& ' must be exact multiple of threads in Y (nTy).' |
141 |
CALL PRINT_ERROR(msgBuf, myThid) |
142 |
eeBootError = .TRUE. |
143 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
144 |
ENDIF |
145 |
IF ( .NOT. eeBootError ) THEN |
146 |
byLo = 1 |
147 |
DO J=1,nTy |
148 |
byHi = byLo+byPerThread-1 |
149 |
bxLo = 1 |
150 |
DO I=1,nTx |
151 |
thId = (J-1)*nTx+I |
152 |
bxHi = bxLo+bxPerThread-1 |
153 |
myBxLo(thId) = bxLo |
154 |
myBxHi(thId) = bxHi |
155 |
myByLo(thId) = byLo |
156 |
myByHi(thId) = byHi |
157 |
bxLo = bxHi+1 |
158 |
ENDDO |
159 |
byLo = byHi+1 |
160 |
ENDDO |
161 |
ENDIF |
162 |
|
163 |
DO thId=1,nThreads |
164 |
CALL INI_COMMUNICATION_PATTERNS( thId ) |
165 |
ENDDO |
166 |
|
167 |
C-- Print mapping of threads to grid points. |
168 |
WRITE(msgBuf,'(A)') |
169 |
&'// ======================================================' |
170 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
171 |
& SQUEEZE_RIGHT , 1) |
172 |
WRITE(msgBuf,'(A)') '// Mapping of tiles to threads' |
173 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
174 |
& SQUEEZE_RIGHT , 1) |
175 |
C o Write list of tiles each thread is responsible for |
176 |
WRITE(msgBuf,'(A)') |
177 |
&'// ======================================================' |
178 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
179 |
& SQUEEZE_RIGHT , 1) |
180 |
DO I=1,nThreads |
181 |
WRITE(msgBuf,'(A,I4,A,4(I4,A1))') |
182 |
& '// -o- Thread',I,', tiles (', |
183 |
& myBxLo(I),':',myBxHi(I),',',myByLo(I),':',myByHi(I),')' |
184 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,SQUEEZE_BOTH , 1) |
185 |
ENDDO |
186 |
WRITE(msgBuf,'(A)') ' ' |
187 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,SQUEEZE_RIGHT , 1) |
188 |
|
189 |
#ifndef ALLOW_EXCH2 |
190 |
C o For each tile print its communication method(s) |
191 |
WRITE(msgBuf,'(A)') |
192 |
&'// ======================================================' |
193 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
194 |
& SQUEEZE_RIGHT , 1) |
195 |
WRITE(msgBuf,'(A)') '// Tile <-> Tile connectvity table' |
196 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
197 |
& SQUEEZE_RIGHT , 1) |
198 |
WRITE(msgBuf,'(A)') |
199 |
&'// ======================================================' |
200 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
201 |
& SQUEEZE_RIGHT , 1) |
202 |
DO J=1,nSy |
203 |
DO I=1,nSx |
204 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6,A)') |
205 |
& '//',' Tile number: ',tileNo(I,J), |
206 |
& ' (process no. = ',myPid,')' |
207 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT , 1) |
208 |
C o West communication details |
209 |
IF ( tileNoW(I,J).NE. NULL_TILE ) THEN |
210 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6,A,A)') |
211 |
& '// WEST: ', |
212 |
& 'Tile = ',tileNoW(I,J), |
213 |
& ', Process = ',tilePidW(I,J), |
214 |
& ', Comm = ',commName(tileCommModeW(I,J)) |
215 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
216 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6)') |
217 |
& '// ', |
218 |
& ' bi = ',tileBiW(I,J), |
219 |
& ', bj = ',tileBjW(I,J) |
220 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
221 |
ELSE |
222 |
WRITE(msgBuf,'(A)') |
223 |
& '// WEST: no neighbor' |
224 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
225 |
ENDIF |
226 |
C o East communication details |
227 |
IF ( tileNoE(I,J).NE. NULL_TILE ) THEN |
228 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6,A,A)') |
229 |
& '// EAST: ', |
230 |
& 'Tile = ',tileNoE(I,J), |
231 |
& ', Process = ',tilePidE(I,J), |
232 |
& ', Comm = ',commName(tileCommModeE(I,J)) |
233 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
234 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6)') |
235 |
& '// ', |
236 |
& ' bi = ',tileBiE(I,J), |
237 |
& ', bj = ',tileBjE(I,J) |
238 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
239 |
ELSE |
240 |
WRITE(msgBuf,'(A)') |
241 |
& '// EAST: no neighbor' |
242 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
243 |
ENDIF |
244 |
C o South communication method |
245 |
IF ( tileNoS(I,J).NE. NULL_TILE ) THEN |
246 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6,A,A)') |
247 |
& '// SOUTH: ', |
248 |
& 'Tile = ',tileNoS(I,J), |
249 |
& ', Process = ',tilePidS(I,J), |
250 |
& ', Comm = ',commName(tileCommModeS(I,J)) |
251 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
252 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6)') |
253 |
& '// ', |
254 |
& ' bi = ',tileBiS(I,J), |
255 |
& ', bj = ',tileBjS(I,J) |
256 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
257 |
ELSE |
258 |
WRITE(msgBuf,'(A)') |
259 |
& '// SOUTH: no neighbor' |
260 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
261 |
ENDIF |
262 |
C o North communication method |
263 |
IF ( tileNoN(I,J).NE. NULL_TILE ) THEN |
264 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6,A,A)') |
265 |
& '// NORTH: ', |
266 |
& 'Tile = ',tileNoN(I,J), |
267 |
& ', Process = ',tilePidN(I,J), |
268 |
& ', Comm = ',commName(tileCommModeN(I,J)) |
269 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
270 |
WRITE(msgBuf,'(A,A,I6.6,A,I6.6)') |
271 |
& '// ', |
272 |
& ' bi = ',tileBiN(I,J), |
273 |
& ', bj = ',tileBjN(I,J) |
274 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
275 |
ELSE |
276 |
WRITE(msgBuf,'(A)') |
277 |
& '// NORTH: no neighbor' |
278 |
CALL PRINT_MESSAGE(msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
279 |
ENDIF |
280 |
ENDDO |
281 |
ENDDO |
282 |
WRITE(msgBuf,'(A)') ' ' |
283 |
CALL PRINT_MESSAGE( msgBuf,standardMessageUnit,SQUEEZE_RIGHT, 1) |
284 |
#endif /* ndef ALLOW_EXCH2 */ |
285 |
|
286 |
C-- Check threading options |
287 |
#ifndef ALLOW_EXCH2 |
288 |
IF ( nThreads.GT.1 .AND. useCubedSphereExchange ) THEN |
289 |
C- multi-threads not working for local arrays; could remove the stop if |
290 |
C we are sure that only shared array (=in common blocks) are exchanged. |
291 |
WRITE(msgBuf,'(2A)') 'EXCH-1 useCubedSphereExchange', |
292 |
& ' unsafe with multi-threads' |
293 |
CALL PRINT_ERROR( msgBuf, myThid ) |
294 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
295 |
ENDIF |
296 |
IF ( nThreads.GT.1 ) THEN |
297 |
flag = .FALSE. |
298 |
DO J=1,nSy |
299 |
DO I=1,nSx |
300 |
flag = flag |
301 |
& .OR. tileCommModeW(I,J).EQ.COMM_GET |
302 |
& .OR. tileCommModeE(I,J).EQ.COMM_GET |
303 |
& .OR. tileCommModeS(I,J).EQ.COMM_GET |
304 |
& .OR. tileCommModeN(I,J).EQ.COMM_GET |
305 |
ENDDO |
306 |
ENDDO |
307 |
IF ( flag ) THEN |
308 |
C- multi-threads not working for local arrays; not safe neither for shared arrays |
309 |
WRITE(msgBuf,'(3A)') 'EXCH-1 using Comm = ', |
310 |
& commName(COMM_GET), ' unsafe with multi-threads' |
311 |
CALL PRINT_ERROR( msgBuf, myThid ) |
312 |
STOP 'ABNORMAL END: S/R INI_THREADING_ENVIRONMENT' |
313 |
ENDIF |
314 |
ENDIF |
315 |
#endif /* ndef ALLOW_EXCH2 */ |
316 |
|
317 |
RETURN |
318 |
END |