1 |
C $Header: /u/gcmpack/MITgcm/pkg/exch2/exch2_rx1_cube_ad.template,v 1.9 2010/05/06 23:28:44 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "CPP_EEOPTIONS.h" |
5 |
|
6 |
#undef Dbg |
7 |
|
8 |
CBOP |
9 |
C !ROUTINE: EXCH_RX_CUBE_AD |
10 |
|
11 |
C !INTERFACE: |
12 |
SUBROUTINE EXCH2_RX1_CUBE_AD( |
13 |
U array, |
14 |
I signOption, fieldCode, |
15 |
I myOLw, myOLe, myOLs, myOLn, myNz, |
16 |
I exchWidthX, exchWidthY, |
17 |
I cornerMode, myThid ) |
18 |
|
19 |
C !DESCRIPTION: |
20 |
C Scalar field (1 component) AD-Exchange: |
21 |
C Tile-edge overlap-region of a 1 component scalar field is added to |
22 |
C corresponding near-edge interior data point and then zero out. |
23 |
|
24 |
C !USES: |
25 |
IMPLICIT NONE |
26 |
|
27 |
C == Global data == |
28 |
#include "SIZE.h" |
29 |
#include "EEPARAMS.h" |
30 |
#include "EESUPPORT.h" |
31 |
#include "W2_EXCH2_SIZE.h" |
32 |
#include "W2_EXCH2_TOPOLOGY.h" |
33 |
#include "W2_EXCH2_BUFFER.h" |
34 |
|
35 |
C !INPUT/OUTPUT PARAMETERS: |
36 |
C array :: Array with edges to exchange. |
37 |
C signOption :: Flag controlling whether field sign depends on orientation |
38 |
C :: (signOption not yet implemented but needed for SM exch) |
39 |
C fieldCode :: field code (position on staggered grid) |
40 |
C myOLw,myOLe :: West and East overlap region sizes. |
41 |
C myOLs,myOLn :: South and North overlap region sizes. |
42 |
C exchWidthX :: Width of data regi exchanged in X. |
43 |
C exchWidthY :: Width of data region exchanged in Y. |
44 |
C cornerMode :: halo-corner-region treatment: update/ignore corner region |
45 |
C myThid :: Thread number of this instance of S/R EXCH... |
46 |
|
47 |
INTEGER myOLw, myOLe, myOLs, myOLn, myNz |
48 |
_RX array(1-myOLw:sNx+myOLe, |
49 |
& 1-myOLs:sNy+myOLn, |
50 |
& myNZ, nSx, nSy) |
51 |
LOGICAL signOption |
52 |
CHARACTER*2 fieldCode |
53 |
INTEGER exchWidthX |
54 |
INTEGER exchWidthY |
55 |
INTEGER cornerMode |
56 |
INTEGER myThid |
57 |
|
58 |
C !LOCAL VARIABLES: |
59 |
C e2_msgHandles :: Synchronization and coordination data structure used to |
60 |
C :: coordinate access to e2Bufr1_RX or to regulate message |
61 |
C :: buffering. In PUT communication sender will increment |
62 |
C :: handle entry once data is ready in buffer. Receiver will |
63 |
C :: decrement handle once data is consumed from buffer. |
64 |
C :: For MPI MSG communication MPI_Wait uses handle to check |
65 |
C :: Isend has cleared. This is done in routine after receives. |
66 |
C note: a) current implementation does not use e2_msgHandles for communication |
67 |
C between threads: all-threads barriers are used (see CNH note below). |
68 |
C For a 2-threads synchro communication (future version), |
69 |
C e2_msgHandles should be shared (in common block, moved to BUFFER.h) |
70 |
C b) 1rst dim=2 so that it could be used also by exch2_rx2_cube. |
71 |
INTEGER bi, bj |
72 |
C Variables for working through W2 topology |
73 |
INTEGER e2_msgHandles( 2, W2_maxNeighbours, nSx, nSy ) |
74 |
INTEGER thisTile, farTile, N, nN, oN |
75 |
INTEGER tIlo, tIhi, tJlo, tJhi, tKlo, tKhi |
76 |
INTEGER tIStride, tJStride, tKStride |
77 |
INTEGER i1Lo, i1Hi, j1Lo, j1Hi, k1Lo, k1Hi |
78 |
LOGICAL updateCorners |
79 |
|
80 |
#ifdef ALLOW_USE_MPI |
81 |
INTEGER iBufr, nri, nrj |
82 |
C MPI stuff (should be in a routine call) |
83 |
INTEGER mpiStatus(MPI_STATUS_SIZE) |
84 |
INTEGER mpiRc |
85 |
INTEGER wHandle |
86 |
#endif |
87 |
CEOP |
88 |
|
89 |
updateCorners = cornerMode .EQ. EXCH_UPDATE_CORNERS |
90 |
C- Tile size of array to exchange: |
91 |
i1Lo = 1-myOLw |
92 |
i1Hi = sNx+myOLe |
93 |
j1Lo = 1-myOLs |
94 |
j1Hi = sNy+myOLn |
95 |
k1Lo = 1 |
96 |
k1Hi = myNz |
97 |
|
98 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
99 |
|
100 |
C Prevent anyone to access shared buffer while an other thread modifies it |
101 |
CALL BAR2( myThid ) |
102 |
|
103 |
C-- Extract from buffer (either from level 1 if local exch, |
104 |
C or level 2 if coming from an other Proc) |
105 |
C AD: = fill buffer from my-tile-edge overlap-region, level 1 or 2 depending |
106 |
C AD: on local (to this Proc) or remote Proc tile destination |
107 |
DO bj=myByLo(myThid), myByHi(myThid) |
108 |
DO bi=myBxLo(myThid), myBxHi(myThid) |
109 |
thisTile=W2_myTileList(bi,bj) |
110 |
nN=exch2_nNeighbours(thisTile) |
111 |
DO N=1,nN |
112 |
CALL EXCH2_GET_SCAL_BOUNDS( |
113 |
I fieldCode, exchWidthX, updateCorners, |
114 |
I thisTile, N, |
115 |
O tIlo, tiHi, tjLo, tjHi, |
116 |
O tiStride, tjStride, |
117 |
I myThid ) |
118 |
tKLo=1 |
119 |
tKHi=myNz |
120 |
tKStride=1 |
121 |
|
122 |
C From buffer, get my points |
123 |
C (tIlo:tIhi:tiStride,tJlo:tJhi,tJStride,tKlo:tKhi,tKStride) in "array": |
124 |
C Note: when transferring data within a process: |
125 |
C o e2Bufr entry to read is entry associated with opposing send record |
126 |
C o e2_msgHandle entry to read is entry associated with opposing send record. |
127 |
CALL EXCH2_AD_GET_RX1( |
128 |
I tIlo, tIhi, tiStride, |
129 |
I tJlo, tJhi, tjStride, |
130 |
I tKlo, tKhi, tkStride, |
131 |
I thisTile, N, bi, bj, |
132 |
I e2BufrRecSize, W2_maxNeighbours, nSx, nSy, |
133 |
O iBuf1Filled(N,bi,bj), |
134 |
O e2Bufr1_RX, |
135 |
U array(1-myOLw,1-myOLs,1,bi,bj), |
136 |
I i1Lo, i1Hi, j1Lo, j1Hi, k1Lo, k1Hi, |
137 |
U e2_msgHandles, |
138 |
I W2_myCommFlag(N,bi,bj), myThid ) |
139 |
ENDDO |
140 |
ENDDO |
141 |
ENDDO |
142 |
|
143 |
C Wait until all threads finish filling buffer |
144 |
CALL BAR2( myThid ) |
145 |
|
146 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
147 |
|
148 |
#ifdef ALLOW_USE_MPI |
149 |
C AD: all MPI part is acting on buffer and is identical to forward code, |
150 |
C AD: except a) the buffer level: send from lev.2, receive into lev.1 |
151 |
C AD: b) the length of transferred buffer (<- match the ad_put/ad_get) |
152 |
|
153 |
_BEGIN_MASTER( myThid ) |
154 |
|
155 |
C-- Send my data (in buffer, level 2) to target Process |
156 |
DO bj=1,nSy |
157 |
DO bi=1,nSx |
158 |
thisTile=W2_myTileList(bi,bj) |
159 |
nN=exch2_nNeighbours(thisTile) |
160 |
DO N=1,nN |
161 |
C- Skip the call if this is an internal exchange |
162 |
IF ( W2_myCommFlag(N,bi,bj) .EQ. 'M' ) THEN |
163 |
CALL EXCH2_SEND_RX1( |
164 |
I thisTile, N, |
165 |
I e2BufrRecSize, |
166 |
I iBuf1Filled(N,bi,bj), |
167 |
I e2Bufr1_RX(1,N,bi,bj,2), |
168 |
O e2_msgHandles(1,N,bi,bj), |
169 |
I W2_myCommFlag(N,bi,bj), myThid ) |
170 |
ENDIF |
171 |
ENDDO |
172 |
ENDDO |
173 |
ENDDO |
174 |
|
175 |
C-- Receive data (in buffer, level 1) from source Process |
176 |
DO bj=1,nSy |
177 |
DO bi=1,nSx |
178 |
thisTile=W2_myTileList(bi,bj) |
179 |
nN=exch2_nNeighbours(thisTile) |
180 |
DO N=1,nN |
181 |
C- Skip the call if this is an internal exchange |
182 |
IF ( W2_myCommFlag(N,bi,bj) .EQ. 'M' ) THEN |
183 |
farTile=exch2_neighbourId(N,thisTile) |
184 |
oN = exch2_opposingSend(N,thisTile) |
185 |
CALL EXCH2_GET_SCAL_BOUNDS( |
186 |
I fieldCode, exchWidthX, updateCorners, |
187 |
I farTile, oN, |
188 |
O tIlo, tiHi, tjLo, tjHi, |
189 |
O tiStride, tjStride, |
190 |
I myThid ) |
191 |
nri = 1 + (tIhi-tIlo)/tiStride |
192 |
nrj = 1 + (tJhi-tJlo)/tjStride |
193 |
iBufr = nri*nrj*myNz |
194 |
C Receive from neighbour N to fill buffer and later on the array |
195 |
CALL EXCH2_RECV_RX1( |
196 |
I thisTile, N, |
197 |
I e2BufrRecSize, |
198 |
I iBufr, |
199 |
O e2Bufr1_RX(1,N,bi,bj,1), |
200 |
I W2_myCommFlag(N,bi,bj), myThid ) |
201 |
ENDIF |
202 |
ENDDO |
203 |
ENDDO |
204 |
ENDDO |
205 |
|
206 |
C-- Clear message handles/locks |
207 |
DO bj=1,nSy |
208 |
DO bi=1,nSx |
209 |
thisTile=W2_myTileList(bi,bj) |
210 |
nN=exch2_nNeighbours(thisTile) |
211 |
DO N=1,nN |
212 |
C Note: In a between process tile-tile data transport using |
213 |
C MPI the sender needs to clear an Isend wait handle here. |
214 |
C In a within process tile-tile data transport using true |
215 |
C shared address space/or direct transfer through commonly |
216 |
C addressable memory blocks the receiver needs to assert |
217 |
C that he has consumed the buffer the sender filled here. |
218 |
c farTile=exch2_neighbourId(N,thisTile) |
219 |
IF ( W2_myCommFlag(N,bi,bj) .EQ. 'M' ) THEN |
220 |
wHandle = e2_msgHandles(1,N,bi,bj) |
221 |
CALL MPI_Wait( wHandle, mpiStatus, mpiRc ) |
222 |
ELSEIF ( W2_myCommFlag(N,bi,bj) .EQ. 'P' ) THEN |
223 |
ELSE |
224 |
ENDIF |
225 |
ENDDO |
226 |
ENDDO |
227 |
ENDDO |
228 |
|
229 |
_END_MASTER( myThid ) |
230 |
C Everyone waits until master-thread finishes receiving |
231 |
CALL BAR2( myThid ) |
232 |
|
233 |
#endif /* ALLOW_USE_MPI */ |
234 |
|
235 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
236 |
|
237 |
C-- Post sends into buffer (buffer level 1): |
238 |
C- AD: = get exch-data from buffer (level 1), formerly in source tile |
239 |
C AD: overlap region, and add to my tile near-Edge interior |
240 |
DO bj=myByLo(myThid), myByHi(myThid) |
241 |
DO bi=myBxLo(myThid), myBxHi(myThid) |
242 |
thisTile=W2_myTileList(bi,bj) |
243 |
nN=exch2_nNeighbours(thisTile) |
244 |
DO N=1,nN |
245 |
farTile=exch2_neighbourId(N,thisTile) |
246 |
oN = exch2_opposingSend(N,thisTile) |
247 |
CALL EXCH2_GET_SCAL_BOUNDS( |
248 |
I fieldCode, exchWidthX, updateCorners, |
249 |
I farTile, oN, |
250 |
O tIlo, tiHi, tjLo, tjHi, |
251 |
O tiStride, tjStride, |
252 |
I myThid ) |
253 |
tKLo=1 |
254 |
tKHi=myNz |
255 |
tKStride=1 |
256 |
C- Put my points in buffer for neighbour N to fill points |
257 |
C (tIlo:tIhi:tiStride,tJlo:tJhi,tJStride,tKlo:tKhi,tKStride) |
258 |
C in its copy of "array". |
259 |
CALL EXCH2_AD_PUT_RX1( |
260 |
I tIlo, tIhi, tiStride, |
261 |
I tJlo, tJhi, tjStride, |
262 |
I tKlo, tKhi, tkStride, |
263 |
I thisTile, N, |
264 |
I e2BufrRecSize, |
265 |
I e2Bufr1_RX(1,N,bi,bj,1), |
266 |
U array(1-myOLw,1-myOLs,1,bi,bj), |
267 |
I i1Lo, i1Hi, j1Lo, j1Hi, k1Lo, k1Hi, |
268 |
O e2_msgHandles(1,N,bi,bj), |
269 |
I W2_myCommFlag(N,bi,bj), myThid ) |
270 |
ENDDO |
271 |
ENDDO |
272 |
ENDDO |
273 |
|
274 |
RETURN |
275 |
END |
276 |
|
277 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
278 |
|
279 |
CEH3 ;;; Local Variables: *** |
280 |
CEH3 ;;; mode:fortran *** |
281 |
CEH3 ;;; End: *** |