New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
lbc_lnk_pt2pt_async.h90 in NEMO/branches/2021/dev_r14447_HPC-07_Irrmann_try_new_pt2pt/src/OCE/LBC – NEMO

source: NEMO/branches/2021/dev_r14447_HPC-07_Irrmann_try_new_pt2pt/src/OCE/LBC/lbc_lnk_pt2pt_async.h90 @ 14891

Last change on this file since 14891 was 14891, checked in by dguibert, 3 years ago

add lbc_lnk_pt2pt_async

Introduce a new pt2pt scheme allowing to overlap communication and computation
by

  1. calling irecv
  2. compute on whole domain using a dummy subroutine
  3. calling isend

... usual unpack

File size: 16.6 KB
Line 
1! vim: ft=fortran :
2#if ! defined BLOCK_ISEND && ! defined BLOCK_IRECV && ! defined BLOCK_FILL
3   SUBROUTINE lbc_lnk_pt2pt_async/**/PRECISION( cdname, loop_fct, ptab, cd_nat, psgn, kfld, kfillmode, pfillval, khls, lsend, lrecv, ld4only )
4      CHARACTER(len=*)              , INTENT(in   ) ::   cdname      ! name of the calling subroutine
5      interface
6        subroutine loop_fct(i0, i1, j0, j1, k0, k1, buf)
7          integer, intent(in) :: i0, i1, j0, j1, k0, k1
8          REAL*8, dimension(:,:,:,:,:,:), optional, intent(out) :: buf
9        end subroutine loop_fct
10      end interface
11      TYPE(PTR_4d_/**/PRECISION),  DIMENSION(:), INTENT(inout) ::   ptab        ! pointer of arrays on which apply the b.c.
12      CHARACTER(len=1), DIMENSION(:), INTENT(in   ) ::   cd_nat      ! nature of array grid-points
13      REAL(PRECISION),  DIMENSION(:), INTENT(in   ) ::   psgn        ! sign used across the north fold boundary
14      INTEGER                       , INTENT(in   ) ::   kfld        ! number of pt3d arrays
15      INTEGER ,             OPTIONAL, INTENT(in   ) ::   kfillmode   ! filling method for halo over land (default = constant)
16      REAL(PRECISION),      OPTIONAL, INTENT(in   ) ::   pfillval    ! background value (used at closed boundaries)
17      INTEGER ,             OPTIONAL, INTENT(in   ) ::   khls        ! halo size, default = nn_hls
18      LOGICAL, DIMENSION(4),OPTIONAL, INTENT(in   ) ::   lsend, lrecv  ! communication with other 4 proc
19      LOGICAL,              OPTIONAL, INTENT(in   ) ::   ld4only     ! if .T., do only 4-neighbour comm (ignore corners)
20      !
21      INTEGER  ::     ji,   jj,  jk,  jl,  jf, jn     ! dummy loop indices
22      INTEGER  ::    ipi,  ipj, ipk, ipl, ipf         ! dimension of the input array
23      INTEGER  ::   ip0i, ip1i, im0i, im1i
24      INTEGER  ::   ip0j, ip1j, im0j, im1j
25      INTEGER  ::   ishti, ishtj, ishti2, ishtj2
26      INTEGER  ::   ifill_nfd, icomm, ierr
27      INTEGER  ::   ihls, idxs, idxr, iszS, iszR
28      INTEGER, DIMENSION(4)  ::   iwewe, issnn
29      INTEGER, DIMENSION(8)  ::   isizei, ishtSi, ishtRi, ishtPi
30      INTEGER, DIMENSION(8)  ::   isizej, ishtSj, ishtRj, ishtPj
31      INTEGER, DIMENSION(8)  ::   ifill, iszall, ishtS, ishtR
32      INTEGER, DIMENSION(8)  ::   ireq             ! mpi_request id
33      INTEGER, DIMENSION(8)  ::   iStag, iRtag     ! Send and Recv mpi_tag id
34      REAL(PRECISION) ::   zland
35      LOGICAL, DIMENSION(8)  ::   llsend, llrecv
36      LOGICAL  ::   ll4only                                        ! default: 8 neighbourgs
37      LOGICAL  ::   ll_IdoNFold
38      !!----------------------------------------------------------------------
39      !
40      ! ----------------------------------------- !
41      !     1. local variables initialization     !
42      ! ----------------------------------------- !
43      !
44      ipi = SIZE(ptab(1)%pt4d,1)
45      ipj = SIZE(ptab(1)%pt4d,2)
46      ipk = SIZE(ptab(1)%pt4d,3)
47      ipl = SIZE(ptab(1)%pt4d,4)
48      ipf = kfld
49      !
50      IF( narea == 1 .AND. numcom == -1 ) CALL mpp_report( cdname, ipk, ipl, ipf, ld_lbc = .TRUE. )
51      !
52      idxs = 1   ! initalize index for send buffer
53      idxr = 1   ! initalize index for recv buffer
54      icomm = mpi_comm_oce        ! shorter name
55      !
56      ! take care of optional parameters
57      !
58      ihls = nn_hls   ! default definition
59      IF( PRESENT( khls ) )   ihls = khls
60      IF( ihls > n_hlsmax ) THEN
61         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with khls > n_hlsmax : ', khls, '>', n_hlsmax
62         CALL ctl_stop( 'STOP', ctmp1 )
63      ENDIF
64      IF( ipi /= Ni_0+2*ihls ) THEN
65         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with an input array which does not match ihls along i: ', ipi, ihls, Ni_0
66         CALL ctl_stop( 'STOP', ctmp1 )
67      ENDIF
68      IF( ipj /= Nj_0+2*ihls ) THEN
69         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with an input array which does not match ihls along j:', ipj, ihls , Nj_0
70         CALL ctl_stop( 'STOP', ctmp1 )
71      ENDIF
72      !
73      ll4only = .FALSE.    ! default definition
74      IF( PRESENT(ld4only) )   ll4only = ld4only
75      !
76      zland = 0._wp                                     ! land filling value: zero by default
77      IF( PRESENT( pfillval ) )   zland = pfillval      ! set land value
78      !
79      ! define llsend and llrecv: logicals which say if mpi-neibourgs for send or receive exist or not.
80      IF     ( PRESENT(lsend) .AND. PRESENT(lrecv) ) THEN   ! localy defined neighbourgs
81         llsend(1:4) = lsend(1:4)   ;   llrecv(1:4) = lrecv(1:4)
82      ELSE IF( PRESENT(lsend) .OR.  PRESENT(lrecv) ) THEN
83         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with only one of the two arguments lsend or lrecv'
84         CALL ctl_stop( 'STOP', ctmp1 )
85      ELSE                                              ! default neighbours
86         llsend(:) = mpiSnei(ihls,:) >= 0
87         IF( ll4only )   llsend(5:8) = .FALSE.          ! exclude corners
88         llrecv(:) = mpiRnei(ihls,:) >= 0
89         IF( ll4only )   llrecv(5:8) = .FALSE.          ! exclude corners
90      ENDIF
91      !
92      ! define ifill: which method should be used to fill each parts (sides+corners) of the halos
93      ! default definition
94      DO jn = 1, 4
95         IF(             llrecv(jn) ) THEN   ;   ifill(jn) = jpfillmpi    ! with an mpi communication
96         ELSEIF(    l_SelfPerio(jn) ) THEN   ;   ifill(jn) = jpfillperio  ! with self-periodicity
97         ELSEIF( PRESENT(kfillmode) ) THEN   ;   ifill(jn) = kfillmode    ! localy defined
98         ELSE                                ;   ifill(jn) = jpfillcst    ! constant value (zland)
99         ENDIF
100      END DO
101      DO jn = 5, 8
102         IF(             llrecv(jn) ) THEN   ;   ifill(jn) = jpfillmpi    ! with an mpi communication
103         ELSE                                ;   ifill(jn) = jpfillnothing! do nothing
104         ENDIF
105      END DO
106         !
107      ! north fold treatment
108      ll_IdoNFold = l_IdoNFold .AND. ifill(jpno) /= jpfillnothing
109      IF( ll_IdoNFold ) THEN
110         ifill_nfd = ifill(jpno)             ! if we are here, this means llrecv(jpno) = .false. and l_SelfPerio(jpno) = .false.
111         ifill( (/jpno/) ) = jpfillnothing   ! we do north fold -> do nothing for northern halo
112      ENDIF
113
114      ! We first define the localization and size of the parts of the array that will be sent (s), received (r)
115      ! or used for periodocity (p). The localization is defined as "the bottom left corner - 1" in i and j directions.
116      ! This is a shift that will be applied later in the do loops to pick-up the appropriate part of the array
117      !
118      ! all definitions bellow do not refer to N[ij][se]0 so we can use it with any local value of ihls
119      !                   !                       ________________________
120      ip0i =          0   !          im0j = inner |__|__|__________|__|__|
121      ip1i =       ihls   !   im1j = inner - halo |__|__|__________|__|__|
122      im1i = ipi-2*ihls   !                       |  |  |          |  |  |
123      im0i = ipi - ihls   !                       |  |  |          |  |  |
124      ip0j =          0   !                       |  |  |          |  |  |
125      ip1j =       ihls   !                       |__|__|__________|__|__|
126      im1j = ipj-2*ihls   !           ip1j = halo |__|__|__________|__|__|
127      im0j = ipj - ihls   !              ip0j = 0 |__|__|__________|__|__|
128      !                   !                    ip0i ip1i        im1i im0i
129      !
130      iwewe(:) = (/ jpwe,jpea,jpwe,jpea /)   ;   issnn(:) = (/ jpso,jpso,jpno,jpno /)
131      !cd     sides:     west  east south north      ;   corners: so-we, so-ea, no-we, no-ea
132      isizei(1:4) = (/ ihls, ihls,  ipi,  ipi /)   ;   isizei(5:8) = ihls              ! i- count
133      isizej(1:4) = (/  ipj,  ipj, ihls, ihls /)   ;   isizej(5:8) = ihls              ! j- count
134      ishtSi(1:4) = (/ ip1i, im1i, ip0i, ip0i /)   ;   ishtSi(5:8) = ishtSi( iwewe )   ! i- shift send data
135      ishtSj(1:4) = (/ ip0j, ip0j, ip1j, im1j /)   ;   ishtSj(5:8) = ishtSj( issnn )   ! j- shift send data
136      ishtRi(1:4) = (/ ip0i, im0i, ip0i, ip0i /)   ;   ishtRi(5:8) = ishtRi( iwewe )   ! i- shift received data location
137      ishtRj(1:4) = (/ ip0j, ip0j, ip0j, im0j /)   ;   ishtRj(5:8) = ishtRj( issnn )   ! j- shift received data location
138      ishtPi(1:4) = (/ im1i, ip1i, ip0i, ip0i /)   ;   ishtPi(5:8) = ishtPi( iwewe )   ! i- shift data used for periodicity
139      ishtPj(1:4) = (/ ip0j, ip0j, im1j, ip1j /)   ;   ishtPj(5:8) = ishtPj( issnn )   ! j- shift data used for periodicity
140      !
141      ! -------------------------------- !
142      !     2. Prepare MPI exchanges     !
143      ! -------------------------------- !
144      !
145      iStag = (/ 1, 2, 3, 4, 5, 6, 7, 8 /)   ! any value but each one must be different
146      ! define iRtag with the corresponding iStag, e.g. data received at west where sent at east.
147      iRtag(jpwe) = iStag(jpea)   ;   iRtag(jpea) = iStag(jpwe)   ;   iRtag(jpso) = iStag(jpno)   ;   iRtag(jpno) = iStag(jpso)
148      iRtag(jpsw) = iStag(jpne)   ;   iRtag(jpse) = iStag(jpnw)   ;   iRtag(jpnw) = iStag(jpse)   ;   iRtag(jpne) = iStag(jpsw)
149      !
150      iszall(:) = isizei(:) * isizej(:) * ipk * ipl * ipf
151      ishtS(1) = 0
152      DO jn = 2, 8
153         ishtS(jn) = ishtS(jn-1) + iszall(jn-1) * COUNT( (/llsend(jn-1)/) )
154      END DO
155      ishtR(1) = 0
156      DO jn = 2, 8
157         ishtR(jn) = ishtR(jn-1) + iszall(jn-1) * COUNT( (/llrecv(jn-1)/) )
158      END DO
159
160      ! Allocate buffer arrays to be sent/received if needed
161      iszS = SUM(iszall, mask = llsend)                             ! send buffer size
162      IF( ALLOCATED(BUFFSND) ) THEN
163         CALL mpi_waitall(8, nreq_p2p, MPI_STATUSES_IGNORE, ierr)   ! wait for Isend from the PREVIOUS call
164         IF( SIZE(BUFFSND) < iszS )    DEALLOCATE(BUFFSND)          ! send buffer is too small
165      ENDIF
166      IF( .NOT. ALLOCATED(BUFFSND) )   ALLOCATE( BUFFSND(iszS) )
167      iszR = SUM(iszall, mask = llrecv)                             ! recv buffer size
168      IF( ALLOCATED(BUFFRCV) ) THEN
169         IF( SIZE(BUFFRCV) < iszR )    DEALLOCATE(BUFFRCV)          ! recv buffer is too small
170      ENDIF
171      IF( .NOT. ALLOCATED(BUFFRCV) )   ALLOCATE( BUFFRCV(iszR) )
172      !
173      ! default definition when no communication is done. understood by mpi_waitall
174      nreq_p2p(:) = MPI_REQUEST_NULL   ! WARNING: Must be done after the call to mpi_waitall just above
175      ireq(:) = MPI_REQUEST_NULL
176      !
177      ! ------------------------------------------------------------- !
178      !     3. Do east, west, south and north MPI_Irecv if needed     !
179      ! ------------------------------------------------------------- !
180      !
181      DO jn = 1, 4 ! TODO maybe to 8 for corners
182#define BLOCK_IRECV
183#  include "lbc_lnk_pt2pt_async.h90"
184#undef BLOCK_IRECV
185      END DO
186
187      ! ------------------------------------------------------------- !
188      !     4. Compute whole domain                                   !
189      ! ------------------------------------------------------------- !
190      !
191      call loop_fct( 2, jpi-1 &
192                   , 2, jpj-1 & !
193                   , 1, jpkm1 & ! TODO check if always jpkm1
194                   )
195      !
196      ! ------------------------------------------------------------- !
197      !     3. Do east, west, south and north MPI_Isend if needed     !
198      ! ------------------------------------------------------------- !
199      !
200      DO jn = 1, 4
201#define BLOCK_ISEND
202#  include "lbc_lnk_pt2pt_async.h90"
203#undef BLOCK_ISEND
204      END DO
205
206      !
207      ! ----------------------------------- !
208      !     5. Fill east and west halos     !
209      ! ----------------------------------- !
210      !
211      DO jn = 1, 2
212#define BLOCK_FILL
213#  include "lbc_lnk_pt2pt_async.h90"
214#undef BLOCK_FILL
215      END DO
216      !
217      ! ------------------------------- !
218      !     7. north fold treatment     !
219      ! ------------------------------- !
220      !
221      ! Must be done after receiving data from East/West neighbourgs (as it is coded in mpp_nfd, to be changed one day...)
222      ! Do it after MPI_iSend to south/north neighbourgs so they won't wait (too much) to receive their data
223      ! Do if before MPI_Recv from south/north neighbourgs so we have more time to receive data
224      !
225      IF( ll_IdoNFold ) THEN
226         IF( jpni == 1 )  THEN   ;   CALL lbc_nfd( ptab, cd_nat, psgn                  , ihls, ipf )   ! self NFold
227         ELSE                    ;   CALL mpp_nfd( ptab, cd_nat, psgn, ifill_nfd, zland, ihls, ipf )   ! mpi  NFold
228         ENDIF
229      ENDIF
230      !
231      ! ------------------------------------- !
232      !     7. Fill south and north halos     !
233      ! ------------------------------------- !
234      !
235      DO jn = 3, 4
236#define BLOCK_FILL
237#  include "lbc_lnk_pt2pt_async.h90"
238#undef BLOCK_FILL
239      END DO
240!!      !
241!!      ! ----------------------------------------------- !
242!!      !     8. Specific problem in corner treatment     !
243!!      !              ( very rate case... )              !
244!!      ! ----------------------------------------------- !
245!!      !
246!!      DO jn = 5, 8
247!!#define BLOCK_ISEND
248!!#  include "lbc_lnk_pt2pt_async.h90"
249!!#undef BLOCK_ISEND
250!!      END DO
251!!      DO jn = 5, 8
252!!#define BLOCK_FILL
253!!#  include "lbc_lnk_pt2pt_async.h90"
254!!#undef BLOCK_FILL
255!!      END DO
256      !
257      ! -------------------------------------------- !
258      !     9. deallocate local temporary arrays     !
259      !        if they areg larger than jpi*jpj      !  <- arbitrary max size...
260      ! -------------------------------------------- !
261      !
262      IF( iszR > jpi*jpj )   DEALLOCATE(BUFFRCV)                    ! blocking receive -> can directly deallocate
263      IF( iszS > jpi*jpj ) THEN
264         CALL mpi_waitall(8, nreq_p2p, MPI_STATUSES_IGNORE, ierr)   ! must wait before deallocate send buffer
265         DEALLOCATE(BUFFSND)
266      ENDIF
267      !
268   END SUBROUTINE lbc_lnk_pt2pt_/**/PRECISION
269#endif
270
271#if defined BLOCK_ISEND
272   IF( llsend(jn) ) THEN
273      ishti = ishtSi(jn)
274      ishtj = ishtSj(jn)
275      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
276         BUFFSND(idxs) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
277         idxs = idxs + 1
278      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
279#if ! defined key_mpi_off
280      IF( ln_timing ) CALL tic_tac(.TRUE.)
281      ! non-blocking send of the west/east side using local buffer
282      CALL MPI_ISEND( BUFFSND(ishtS(jn)+1), iszall(jn), MPI_TYPE, mpiSnei(ihls,jn), iStag(jn), icomm, nreq_p2p(jn), ierr )
283      IF( ln_timing ) CALL tic_tac(.FALSE.)
284#endif
285   ENDIF
286#endif
287
288#if defined BLOCK_IRECV
289   ishti = ishtRi(jn)
290   ishtj = ishtRj(jn)
291   SELECT CASE ( ifill(jn) )
292   CASE ( jpfillnothing )               ! no filling
293   CASE ( jpfillmpi   )                 ! fill with data received by MPI
294#if ! defined key_mpi_off
295      IF( ln_timing ) CALL tic_tac(.TRUE.)
296      CALL MPI_IRECV( BUFFRCV(ishtR(jn)+1), iszall(jn), MPI_TYPE, mpiRnei(ihls,jn), iRtag(jn), icomm, ireq(jn), ierr )
297      IF( ln_timing ) CALL tic_tac(.FALSE.)
298#endif
299END SELECT
300#endif
301
302#if defined BLOCK_FILL
303   ishti = ishtRi(jn)
304   ishtj = ishtRj(jn)
305   SELECT CASE ( ifill(jn) )
306   CASE ( jpfillnothing )               ! no filling
307   CASE ( jpfillmpi   )                 ! fill with data received by MPI
308#if ! defined key_mpi_off
309      IF( ln_timing ) CALL tic_tac(.TRUE.)
310      !                                 ! waiting receive of the west/east halo in local temporary arrays
311      call MPI_WAIT(iref(jn), MPI_STATUS_IGNORE, ier)
312      IF( ln_timing ) CALL tic_tac(.FALSE.)
313#endif
314      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
315         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idxr)
316         idxr = idxr + 1
317      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
318   CASE ( jpfillperio )                 ! use periodicity
319      ishti2 = ishtPi(jn)
320      ishtj2 = ishtPj(jn)
321      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
322         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
323      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
324   CASE ( jpfillcopy  )                 ! filling with inner domain values
325      ishti2 = ishtSi(jn)
326      ishtj2 = ishtSj(jn)
327      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
328         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
329      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
330   CASE ( jpfillcst   )                 ! filling with constant value
331      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
332         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = zland
333      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
334   END SELECT
335#endif
Note: See TracBrowser for help on using the repository browser.