1 |
C $Header: /u/gcmpack/MITgcm/eesupp/src/exch_rx_send_put_y.template,v 1.14 2010/05/17 02:28:06 jmc Exp $ |
2 |
C $Name: $ |
3 |
#include "CPP_EEOPTIONS.h" |
4 |
|
5 |
CBOP |
6 |
C !ROUTINE: EXCH_RX_SEND_PUT_Y |
7 |
|
8 |
C !INTERFACE: |
9 |
SUBROUTINE EXCH_RX_SEND_PUT_Y( array, |
10 |
I myOLw, myOLe, myOLs, myOLn, myNz, |
11 |
I exchWidthX, exchWidthY, |
12 |
I thesimulationMode, thecornerMode, myThid ) |
13 |
IMPLICIT NONE |
14 |
C !DESCRIPTION: |
15 |
C *==========================================================* |
16 |
C | SUBROUTINE SEND_PUT_Y |
17 |
C | o "Send" or "put" Y edges for RX array. |
18 |
C *==========================================================* |
19 |
C | Routine that invokes actual message passing send or |
20 |
C | direct "put" of data to update Y faces of an XY[R] array. |
21 |
C *==========================================================* |
22 |
|
23 |
C !USES: |
24 |
C == Global variables == |
25 |
#include "SIZE.h" |
26 |
#include "EEPARAMS.h" |
27 |
#include "EESUPPORT.h" |
28 |
#include "EXCH.h" |
29 |
|
30 |
C !INPUT/OUTPUT PARAMETERS: |
31 |
C == Routine arguments == |
32 |
C array :: Array with edges to exchange. |
33 |
C myOLw :: West, East, North and South overlap region sizes. |
34 |
C myOLe |
35 |
C myOLn |
36 |
C myOLs |
37 |
C exchWidthX :: Width of data region exchanged. |
38 |
C exchWidthY |
39 |
C theSimulationMode :: Forward or reverse mode exchange ( provides |
40 |
C support for adjoint integration of code. ) |
41 |
C Note - the reverse mode for an assignment |
42 |
C is an accumulation. This means that |
43 |
C put implementations that do leary things |
44 |
C like writing to overlap regions in a |
45 |
C remote process need to be even more |
46 |
C careful. You need to be pretty careful |
47 |
C in forward mode too! |
48 |
C theCornerMode :: Flag indicating whether corner updates are |
49 |
C needed. |
50 |
C myThid :: Thread number of this instance of S/R EXCH... |
51 |
C eBl :: Edge buffer level |
52 |
INTEGER myOLw |
53 |
INTEGER myOLe |
54 |
INTEGER myOLs |
55 |
INTEGER myOLn |
56 |
INTEGER myNz |
57 |
_RX array(1-myOLw:sNx+myOLe, |
58 |
& 1-myOLs:sNy+myOLn, |
59 |
& myNZ, nSx, nSy) |
60 |
INTEGER exchWidthX |
61 |
INTEGER exchWidthY |
62 |
INTEGER theSimulationMode |
63 |
INTEGER theCornerMode |
64 |
INTEGER myThid |
65 |
|
66 |
C !LOCAL VARIABLES: |
67 |
C == Local variables == |
68 |
C i, j, k, jMin, jMax, iB - Loop counters and extents |
69 |
C bi, bj |
70 |
C biS, bjS - South tile indices |
71 |
C biN, bjN - North tile indices |
72 |
C eBl - Current exchange buffer level |
73 |
C theProc, theTag, theType, - Variables used in message building |
74 |
C theSize |
75 |
C southCommMode - Working variables holding type |
76 |
C northCommMode of communication a particular |
77 |
C tile face uses. |
78 |
INTEGER i, j, k, jMin, jMax, iMin, iMax, iB |
79 |
INTEGER bi, bj, biS, bjS, biN, bjN |
80 |
INTEGER eBl |
81 |
INTEGER northCommMode |
82 |
INTEGER southCommMode |
83 |
#ifdef ALLOW_USE_MPI |
84 |
INTEGER theProc, theTag, theType, theSize, mpiRc |
85 |
# ifdef ALLOW_AUTODIFF_OPENAD_AMPI |
86 |
INTEGER mpiStatus(MPI_STATUS_SIZE) |
87 |
INTEGER pReqI |
88 |
# endif |
89 |
#endif |
90 |
|
91 |
C-- Write data to exchange buffer |
92 |
C Various actions are possible depending on the communication mode |
93 |
C as follows: |
94 |
C Mode Action |
95 |
C -------- --------------------------- |
96 |
C COMM_NONE Do nothing |
97 |
C |
98 |
C COMM_MSG Message passing communication ( e.g. MPI ) |
99 |
C Fill south send buffer from this tile. |
100 |
C Send data with tag identifying tile and direction. |
101 |
C Fill north send buffer from this tile. |
102 |
C Send data with tag identifying tile and direction. |
103 |
C |
104 |
C COMM_PUT "Put" communication ( UMP_, shmemput, etc... ) |
105 |
C Fill south receive buffer of south-neighbor tile |
106 |
C Fill north receive buffer of north-neighbor tile |
107 |
C Sync. memory |
108 |
C Write data-ready Ack for north edge of south-neighbor |
109 |
C tile |
110 |
C Write data-ready Ack for south edge of north-neighbor |
111 |
C tile |
112 |
C Sync. memory |
113 |
CEOP |
114 |
|
115 |
#ifdef ALLOW_AUTODIFF_OPENAD_AMPI |
116 |
# ifdef ALLOW_USE_MPI |
117 |
IF ( usingMPI ) THEN |
118 |
_BEGIN_MASTER(myThid) |
119 |
DO bj=1,nSy |
120 |
DO bi=1,nSx |
121 |
CALL ampi_awaitall ( |
122 |
& exchNReqsY(1,bi,bj) , |
123 |
& exchReqIdY(1,1,bi,bj) , |
124 |
& mpiStatus , |
125 |
& mpiRC ) |
126 |
ENDDO |
127 |
ENDDO |
128 |
_END_MASTER(myThid) |
129 |
ENDIF |
130 |
# endif |
131 |
#endif |
132 |
|
133 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
134 |
|
135 |
C Prevent anyone to access shared buffer while an other thread modifies it |
136 |
_BARRIER |
137 |
|
138 |
C Fill shared buffers from array values |
139 |
DO bj=myByLo(myThid),myByHi(myThid) |
140 |
DO bi=myBxLo(myThid),myBxHi(myThid) |
141 |
|
142 |
eBl = exchangeBufLevel(1,bi,bj) |
143 |
southCommMode = _tileCommModeS(bi,bj) |
144 |
northCommMode = _tileCommModeN(bi,bj) |
145 |
biS = _tileBiS(bi,bj) |
146 |
bjS = _tileBjS(bi,bj) |
147 |
biN = _tileBiN(bi,bj) |
148 |
bjN = _tileBjN(bi,bj) |
149 |
iMin = 1 |
150 |
iMax = sNx |
151 |
IF ( theCornerMode .EQ. EXCH_UPDATE_CORNERS ) THEN |
152 |
iMin = 1-exchWidthX |
153 |
iMax = sNx+exchWidthX |
154 |
ENDIF |
155 |
|
156 |
C >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<< |
157 |
|
158 |
IF ( _theSimulationMode .EQ. FORWARD_SIMULATION ) THEN |
159 |
|
160 |
C o Send or Put south edge |
161 |
jMin = 1 |
162 |
jMax = 1+exchWidthY-1 |
163 |
IF ( southCommMode .EQ. COMM_MSG ) THEN |
164 |
iB = 0 |
165 |
DO k=1,myNz |
166 |
DO j=jMin,jMax |
167 |
DO i=iMin,iMax |
168 |
iB = iB + 1 |
169 |
southSendBuf_RX(iB,eBl,bi,bj) = array(i,j,k,bi,bj) |
170 |
ENDDO |
171 |
ENDDO |
172 |
ENDDO |
173 |
ELSEIF ( southCommMode .EQ. COMM_PUT ) THEN |
174 |
iB = 0 |
175 |
DO k=1,myNz |
176 |
DO j=jMin,jMax |
177 |
DO i=iMin,iMax |
178 |
iB = iB + 1 |
179 |
northRecvBuf_RX(iB,eBl,biS,bjS) = array(i,j,k,bi,bj) |
180 |
ENDDO |
181 |
ENDDO |
182 |
ENDDO |
183 |
ELSEIF ( southCommMode .NE. COMM_NONE |
184 |
& .AND. southCommMode .NE. COMM_GET ) THEN |
185 |
STOP ' S/R EXCH: Invalid commS mode.' |
186 |
ENDIF |
187 |
|
188 |
C o Send or Put north edge |
189 |
jMin = sNy-exchWidthY+1 |
190 |
jMax = sNy |
191 |
IF ( northCommMode .EQ. COMM_MSG ) THEN |
192 |
iB = 0 |
193 |
DO k=1,myNz |
194 |
DO j=jMin,jMax |
195 |
DO i=iMin,iMax |
196 |
iB = iB + 1 |
197 |
northSendBuf_RX(iB,eBl,bi,bj) = array(i,j,k,bi,bj) |
198 |
ENDDO |
199 |
ENDDO |
200 |
ENDDO |
201 |
ELSEIF ( northCommMode .EQ. COMM_PUT ) THEN |
202 |
iB = 0 |
203 |
DO k=1,myNz |
204 |
DO j=jMin,jMax |
205 |
DO i=iMin,iMax |
206 |
iB = iB + 1 |
207 |
southRecvBuf_RX(iB,eBl,biN,bjN) = array(i,j,k,bi,bj) |
208 |
ENDDO |
209 |
ENDDO |
210 |
ENDDO |
211 |
ELSEIF ( northCommMode .NE. COMM_NONE |
212 |
& .AND. northCommMode .NE. COMM_GET ) THEN |
213 |
STOP ' S/R EXCH: Invalid commN mode.' |
214 |
ENDIF |
215 |
|
216 |
C >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<< |
217 |
|
218 |
ELSEIF ( _theSimulationMode .EQ. REVERSE_SIMULATION ) THEN |
219 |
|
220 |
C o Send or Put south edge |
221 |
jMin = 1-exchWidthY |
222 |
jMax = 0 |
223 |
IF ( southCommMode .EQ. COMM_MSG ) THEN |
224 |
iB = 0 |
225 |
DO k=1,myNz |
226 |
DO j=jMin,jMax |
227 |
DO i=iMin,iMax |
228 |
iB = iB + 1 |
229 |
southSendBuf_RX(iB,eBl,bi,bj) = array(i,j,k,bi,bj) |
230 |
array(i,j,k,bi,bj) = 0.0 |
231 |
ENDDO |
232 |
ENDDO |
233 |
ENDDO |
234 |
ELSEIF ( southCommMode .EQ. COMM_PUT ) THEN |
235 |
iB = 0 |
236 |
DO k=1,myNz |
237 |
DO j=jMin,jMax |
238 |
DO i=iMin,iMax |
239 |
iB = iB + 1 |
240 |
northRecvBuf_RX(iB,eBl,biS,bjS) = array(i,j,k,bi,bj) |
241 |
array(i,j,k,bi,bj) = 0.0 |
242 |
ENDDO |
243 |
ENDDO |
244 |
ENDDO |
245 |
ELSEIF ( southCommMode .NE. COMM_NONE |
246 |
& .AND. southCommMode .NE. COMM_GET ) THEN |
247 |
STOP ' S/R EXCH: Invalid commS mode.' |
248 |
ENDIF |
249 |
|
250 |
C o Send or Put north edge |
251 |
jMin = sNy+1 |
252 |
jMax = sNy+exchWidthY |
253 |
IF ( northCommMode .EQ. COMM_MSG ) THEN |
254 |
iB = 0 |
255 |
DO k=1,myNz |
256 |
DO j=jMin,jMax |
257 |
DO i=iMin,iMax |
258 |
iB = iB + 1 |
259 |
northSendBuf_RX(iB,eBl,bi,bj) = array(i,j,k,bi,bj) |
260 |
array(i,j,k,bi,bj) = 0.0 |
261 |
ENDDO |
262 |
ENDDO |
263 |
ENDDO |
264 |
ELSEIF ( northCommMode .EQ. COMM_PUT ) THEN |
265 |
iB = 0 |
266 |
DO k=1,myNz |
267 |
DO j=jMin,jMax |
268 |
DO i=iMin,iMax |
269 |
iB = iB + 1 |
270 |
southRecvBuf_RX(iB,eBl,biN,bjN) = array(i,j,k,bi,bj) |
271 |
array(i,j,k,bi,bj) = 0.0 |
272 |
ENDDO |
273 |
ENDDO |
274 |
ENDDO |
275 |
ELSEIF ( northCommMode .NE. COMM_NONE |
276 |
& .AND. northCommMode .NE. COMM_GET ) THEN |
277 |
STOP ' S/R EXCH: Invalid commN mode.' |
278 |
ENDIF |
279 |
|
280 |
ENDIF |
281 |
|
282 |
ENDDO |
283 |
ENDDO |
284 |
|
285 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
286 |
C-- Signal completetion ( making sure system-wide memory state is |
287 |
C-- consistent ). |
288 |
|
289 |
C ** NOTE ** We are relying on being able to produce strong-ordered |
290 |
C memory semantics here. In other words we assume that there is a |
291 |
C mechanism which can ensure that by the time the Ack is seen the |
292 |
C overlap region data that will be exchanged is up to date. |
293 |
IF ( exchNeedsMemSync ) CALL MEMSYNC |
294 |
|
295 |
DO bj=myByLo(myThid),myByHi(myThid) |
296 |
DO bi=myBxLo(myThid),myBxHi(myThid) |
297 |
eBl = exchangeBufLevel(1,bi,bj) |
298 |
biS = _tileBiS(bi,bj) |
299 |
bjS = _tileBjS(bi,bj) |
300 |
biN = _tileBiN(bi,bj) |
301 |
bjN = _tileBjN(bi,bj) |
302 |
southCommMode = _tileCommModeS(bi,bj) |
303 |
northCommMode = _tileCommModeN(bi,bj) |
304 |
IF ( southCommMode.EQ.COMM_PUT ) northRecvAck(eBl,biS,bjS) = 1 |
305 |
IF ( northCommMode.EQ.COMM_PUT ) southRecvAck(eBl,biN,bjN) = 1 |
306 |
IF ( southCommMode.EQ.COMM_GET ) northRecvAck(eBl,biS,bjS) = 1 |
307 |
IF ( northCommMode.EQ.COMM_GET ) southRecvAck(eBl,biN,bjN) = 1 |
308 |
ENDDO |
309 |
ENDDO |
310 |
|
311 |
C-- Make sure "ack" setting is seen system-wide. |
312 |
C Here strong-ordering is not an issue but we want to make |
313 |
C sure that processes that might spin on the above Ack settings |
314 |
C will see the setting. |
315 |
C ** NOTE ** On some machines we wont spin on the Ack setting |
316 |
C ( particularly the T90 ), instead we will use s system barrier. |
317 |
C On the T90 the system barrier is very fast and switches out the |
318 |
C thread while it waits. On most machines the system barrier |
319 |
C is much too slow and if we own the machine and have one thread |
320 |
C per process preemption is not a problem. |
321 |
IF ( exchNeedsMemSync ) CALL MEMSYNC |
322 |
|
323 |
C Wait until all threads finish filling buffer |
324 |
_BARRIER |
325 |
|
326 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
327 |
|
328 |
#ifdef ALLOW_USE_MPI |
329 |
IF ( usingMPI ) THEN |
330 |
C-- Send buffer data: Only Master Thread do proc communication |
331 |
_BEGIN_MASTER(myThid) |
332 |
|
333 |
DO bj=1,nSy |
334 |
DO bi=1,nSx |
335 |
|
336 |
eBl = exchangeBufLevel(1,bi,bj) |
337 |
southCommMode = _tileCommModeS(bi,bj) |
338 |
northCommMode = _tileCommModeN(bi,bj) |
339 |
biS = _tileBiS(bi,bj) |
340 |
bjS = _tileBjS(bi,bj) |
341 |
biN = _tileBiN(bi,bj) |
342 |
bjN = _tileBjN(bi,bj) |
343 |
theType = _MPI_TYPE_RX |
344 |
theSize = sNx*exchWidthY*myNz |
345 |
IF ( theCornerMode .EQ. EXCH_UPDATE_CORNERS ) THEN |
346 |
theSize = (sNx+2*exchWidthX)*exchWidthY*myNz |
347 |
ENDIF |
348 |
|
349 |
IF ( southCommMode .EQ. COMM_MSG ) THEN |
350 |
C Send buffer data (copied from south edge) |
351 |
theProc = tilePidS(bi,bj) |
352 |
theTag = _tileTagSendS(bi,bj) |
353 |
# ifndef ALLOW_AUTODIFF_OPENAD_AMPI |
354 |
exchNreqsY(1,bi,bj) = exchNreqsY(1,bi,bj)+1 |
355 |
CALL MPI_Isend( southSendBuf_RX(1,eBl,bi,bj), theSize, |
356 |
& theType, theProc, theTag, MPI_COMM_MODEL, |
357 |
& exchReqIdY(exchNreqsY(1,bi,bj),1,bi,bj), |
358 |
& mpiRc ) |
359 |
# else |
360 |
pReqI=exchNReqsY(1,bi,bj)+1 |
361 |
CALL ampi_isend_RX( |
362 |
& southSendBuf_RX(1,eBl,bi,bj), |
363 |
& theSize, |
364 |
& theType, |
365 |
& theProc, |
366 |
& theTag, |
367 |
& MPI_COMM_MODEL, |
368 |
& exchReqIdY(pReqI,1,bi,bj), |
369 |
& exchNReqsY(1,bi,bj), |
370 |
& mpiStatus, |
371 |
& mpiRc ) |
372 |
# endif /* ALLOW_AUTODIFF_OPENAD_AMPI */ |
373 |
c northRecvAck(eBl,biS,bjS) = 1 |
374 |
ENDIF |
375 |
|
376 |
IF ( northCommMode .EQ. COMM_MSG ) THEN |
377 |
C Send buffer data (copied from north edge) |
378 |
theProc = tilePidN(bi,bj) |
379 |
theTag = _tileTagSendN(bi,bj) |
380 |
#ifndef ALLOW_AUTODIFF_OPENAD_AMPI |
381 |
exchNreqsY(1,bi,bj) = exchNreqsY(1,bi,bj)+1 |
382 |
CALL MPI_Isend( northSendBuf_RX(1,eBl,bi,bj), theSize, |
383 |
& theType, theProc, theTag, MPI_COMM_MODEL, |
384 |
& exchReqIdY(exchNreqsY(1,bi,bj),1,bi,bj), |
385 |
& mpiRc ) |
386 |
# else |
387 |
pReqI=exchNReqsY(1,bi,bj)+1 |
388 |
CALL ampi_isend_RX( |
389 |
& northSendBuf_RX(1,eBl,bi,bj) , |
390 |
& theSize , |
391 |
& theType , |
392 |
& theProc , |
393 |
& theTag , |
394 |
& MPI_COMM_MODEL , |
395 |
& exchReqIdY(pReqI,1,bi,bj) , |
396 |
& exchNReqsY(1,bi,bj) , |
397 |
& mpiStatus , |
398 |
& mpiRc ) |
399 |
# endif /* ALLOW_AUTODIFF_OPENAD_AMPI */ |
400 |
c southRecvAck(eBl,biN,bjN) = 1 |
401 |
ENDIF |
402 |
|
403 |
ENDDO |
404 |
ENDDO |
405 |
|
406 |
_END_MASTER(myThid) |
407 |
|
408 |
ENDIF |
409 |
#endif /* ALLOW_USE_MPI */ |
410 |
|
411 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
412 |
|
413 |
RETURN |
414 |
END |