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_neicoll_generic.h90 in NEMO/branches/2021/dev_r14312_MPI_Interface/src/OCE/LBC – NEMO

source: NEMO/branches/2021/dev_r14312_MPI_Interface/src/OCE/LBC/lbc_lnk_neicoll_generic.h90 @ 14367

Last change on this file since 14367 was 14367, checked in by smasson, 3 years ago

dev_r14312_MPI_Interface: keep send/recv buffers in memory if smaller than jpi*jpj, #2598

File size: 14.6 KB
Line 
1
2   SUBROUTINE lbc_lnk_neicoll_/**/PRECISION( cdname, ptab, cd_nat, psgn, kfld, kfillmode, pfillval, khls, lsend, lrecv, ld4only )
3      CHARACTER(len=*)              , INTENT(in   ) ::   cdname      ! name of the calling subroutine
4      TYPE(PTR_4d_/**/PRECISION),  DIMENSION(:), INTENT(inout) ::   ptab        ! pointer of arrays on which apply the b.c.
5      CHARACTER(len=1), DIMENSION(:), INTENT(in   ) ::   cd_nat      ! nature of array grid-points
6      REAL(PRECISION),  DIMENSION(:), INTENT(in   ) ::   psgn        ! sign used across the north fold boundary
7      INTEGER                       , INTENT(in   ) ::   kfld        ! number of pt3d arrays
8      INTEGER ,             OPTIONAL, INTENT(in   ) ::   kfillmode   ! filling method for halo over land (default = constant)
9      REAL(PRECISION),      OPTIONAL, INTENT(in   ) ::   pfillval    ! background value (used at closed boundaries)
10      INTEGER ,             OPTIONAL, INTENT(in   ) ::   khls        ! halo size, default = nn_hls
11      LOGICAL, DIMENSION(4),OPTIONAL, INTENT(in   ) ::   lsend, lrecv  ! communication with other 4 proc
12      LOGICAL,              OPTIONAL, INTENT(in   ) ::   ld4only     ! if .T., do only 4-neighbour comm (ignore corners)
13      !
14      INTEGER  ::    ji,  jj,  jk , jl,  jf, jn      ! dummy loop indices
15      INTEGER  ::   ipi, ipj, ipk, ipl, ipf          ! dimension of the input array
16      INTEGER  ::   ip0i, ip1i, im0i, im1i
17      INTEGER  ::   ip0j, ip1j, im0j, im1j
18      INTEGER  ::   ishti, ishtj, ishti2, ishtj2
19      INTEGER  ::   iszS, iszR
20      INTEGER  ::   ierr
21      INTEGER  ::   ihls, idx
22      INTEGER  ::   impi_nc
23      INTEGER  ::   ifill_nfd
24      INTEGER, DIMENSION(4)  ::   iwewe, issnn
25      INTEGER, DIMENSION(8)  ::   isizei, ishtSi, ishtRi, ishtPi
26      INTEGER, DIMENSION(8)  ::   isizej, ishtSj, ishtRj, ishtPj
27      INTEGER, DIMENSION(8)  ::   ifill, iszall
28      INTEGER, DIMENSION(:), ALLOCATABLE  ::   iScnt, iRcnt    ! number of elements to be sent/received
29      INTEGER, DIMENSION(:), ALLOCATABLE  ::   iSdpl, iRdpl    ! displacement in halos arrays
30      LOGICAL, DIMENSION(8)  ::   llsend, llrecv
31      REAL(PRECISION) ::   zland
32      LOGICAL  ::   ll4only                                    ! default: 8 neighbourgs
33      LOGICAL  ::   ll_IdoNFold
34      !!----------------------------------------------------------------------
35      !
36      ! ----------------------------------------- !
37      !     1. local variables initialization     !
38      ! ----------------------------------------- !
39      !
40      ipi = SIZE(ptab(1)%pt4d,1)
41      ipj = SIZE(ptab(1)%pt4d,2)
42      ipk = SIZE(ptab(1)%pt4d,3)
43      ipl = SIZE(ptab(1)%pt4d,4)
44      ipf = kfld
45      !
46      IF( narea == 1 .AND. numcom == -1 ) CALL mpp_report( cdname, ipk, ipl, ipf, ld_lbc = .TRUE. )
47      !
48      ! take care of optional parameters
49      !
50      ihls = nn_hls       ! default definition
51      IF( PRESENT( khls ) )   ihls = khls
52      IF( ihls > n_hlsmax ) THEN
53         WRITE(ctmp1,*) TRIM(cdname), '  is calling lbc_lnk with khls > n_hlsmax : ', khls, '>', n_hlsmax
54         CALL ctl_stop( 'STOP', ctmp1 )
55      ENDIF
56      IF( ipi /= Ni_0+2*ihls ) THEN
57         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with an input array which does not match ihls along i: ', ipi, ihls, Ni_0
58         CALL ctl_stop( 'STOP', ctmp1 )
59      ENDIF
60      IF( ipj /= Nj_0+2*ihls ) THEN
61         WRITE(ctmp1,*) TRIM(cdname), ' is calling lbc_lnk with an input array which does not match ihls along j:', ipj, ihls , Nj_0
62         CALL ctl_stop( 'STOP', ctmp1 )
63      ENDIF
64      !
65      ll4only = .FALSE.    ! default definition
66      IF( PRESENT(ld4only) )   ll4only = ld4only
67      !
68      impi_nc = mpi_nc_com8(ihls)   ! default
69      IF( ll4only )   impi_nc = mpi_nc_com4(ihls)
70      !
71      zland = 0._wp                                     ! land filling value: zero by default
72      IF( PRESENT( pfillval ) )   zland = pfillval      ! set land value
73      !
74      ! define llsend and llrecv: logicals which say if mpi-neibourgs for send or receive exist or not.
75      IF     ( PRESENT(lsend) .AND. PRESENT(lrecv) ) THEN   ! localy defined neighbourgs
76         CALL ctl_stop( 'STOP', 'mpp_nc_generic+BDY not yet implemented')
77!!$         ---> llsend(:) = lsend(:)   ;   llrecv(:) = lrecv(:) ???
78      ELSE IF( PRESENT(lsend) .OR.  PRESENT(lrecv) ) THEN
79         WRITE(ctmp1,*) TRIM(cdname), '  is calling lbc_lnk with only one of the two arguments lsend or lrecv'
80         CALL ctl_stop( 'STOP', ctmp1 )
81      ELSE                                              ! default neighbours
82         llsend(:) = mpiSnei(ihls,:) >= 0
83         IF( ll4only )   llsend(5:8) = .FALSE.          ! exclude corners
84         llrecv(:) = mpiRnei(ihls,:) >= 0
85         IF( ll4only )   llrecv(5:8) = .FALSE.          ! exclude corners
86      ENDIF
87      !
88      ! define ifill: which method should be used to fill each parts (sides+corners) of the halos
89      ! default definition
90      DO jn = 1, 8
91         IF(             llrecv(jn) ) THEN   ;   ifill(jn) = jpfillmpi    ! with an mpi communication
92         ELSEIF(    l_SelfPerio(jn) ) THEN   ;   ifill(jn) = jpfillperio  ! with self-periodicity
93         ELSEIF( PRESENT(kfillmode) ) THEN   ;   ifill(jn) = kfillmode    ! localy defined
94         ELSE                                ;   ifill(jn) = jpfillcst    ! constant value (zland)
95         ENDIF
96      END DO
97      ! take care of "indirect self-periodicity" for the corners
98      DO jn = 5, 8
99         IF(.NOT.l_SelfPerio(jn) .AND. l_SelfPerio(jpwe))   ifill(jn) = jpfillnothing   ! no bi-perio but ew-perio: do corners later
100         IF(.NOT.l_SelfPerio(jn) .AND. l_SelfPerio(jpso))   ifill(jn) = jpfillnothing   ! no bi-perio but ns-perio: do corners later
101      END DO
102      ! north fold treatment
103      ll_IdoNFold = l_IdoNFold .AND. ifill(jpno) /= jpfillnothing
104      IF( ll_IdoNFold ) THEN
105         ifill_nfd = ifill(jpno)             ! if we are here, this means llrecv(jpno) = .false. and l_SelfPerio(jpno) = .false.
106         ifill( (/jpno/) ) = jpfillnothing   ! we do north fold -> do nothing for northern halo
107      ENDIF
108     
109      ! We first define the localization and size of the parts of the array that will be sent (s), received (r)
110      ! or used for periodocity (p). The localization is defined as "the bottom left corner - 1" in i and j directions.
111      ! This is a shift that will be applied later in the do loops to pick-up the appropriate part of the array
112      !
113      ! all definitions bellow do not refer to N[ij][se]0 so we can use it with any local value of ihls
114      !                   !                       ________________________
115      ip0i =          0   !          im0j = inner |__|________________|__|
116      ip1i =       ihls   !   im1j = inner - halo |  |__|__________|__|  |
117      im1i = ipi-2*ihls   !                       |  |  |          |  |  |
118      im0i = ipi - ihls   !                       |  |  |          |  |  |
119      ip0j =          0   !                       |  |  |          |  |  |
120      ip1j =       ihls   !                       |  |__|__________|__|  |
121      im1j = ipj-2*ihls   !           ip1j = halo |__|__|__________|__|__|
122      im0j = ipj - ihls   !              ip0j = 0 |__|________________|__|
123      !                   !                    ip0i ip1i        im1i im0i
124      !
125      iwewe(:) = (/ jpwe,jpea,jpwe,jpea /)   ;   issnn(:) = (/ jpso,jpso,jpno,jpno /)
126      !     sides:     west  east south north      ;   corners: so-we, so-ea, no-we, no-ea
127      isizei(1:4) = (/ ihls, ihls, Ni_0, Ni_0 /)   ;   isizei(5:8) = ihls              ! i- count
128      isizej(1:4) = (/ Nj_0, Nj_0, ihls, ihls /)   ;   isizej(5:8) = ihls              ! j- count
129      ishtSi(1:4) = (/ ip1i, im1i, ip1i, ip1i /)   ;   ishtSi(5:8) = ishtSi( iwewe )   ! i- shift send data
130      ishtSj(1:4) = (/ ip1j, ip1j, ip1j, im1j /)   ;   ishtSj(5:8) = ishtSj( issnn )   ! j- shift send data
131      ishtRi(1:4) = (/ ip0i, im0i, ip1i, ip1i /)   ;   ishtRi(5:8) = ishtRi( iwewe )   ! i- shift received data location
132      ishtRj(1:4) = (/ ip1j, ip1j, ip0j, im0j /)   ;   ishtRj(5:8) = ishtRj( issnn )   ! j- shift received data location
133      ishtPi(1:4) = (/ im1i, ip1i, ip1i, ip1i /)   ;   ishtPi(5:8) = ishtPi( iwewe )   ! i- shift data used for periodicity
134      ishtPj(1:4) = (/ ip1j, ip1j, im1j, ip1j /)   ;   ishtPj(5:8) = ishtPj( issnn )   ! j- shift data used for periodicity
135      !
136      ! -------------------------------- !
137      !     2. Prepare MPI exchanges     !
138      ! -------------------------------- !
139      !
140      ! Allocate local temporary arrays to be sent/received.
141      iszS = COUNT( llsend )
142      iszR = COUNT( llrecv )
143      ALLOCATE( iScnt(iszS), iRcnt(iszR), iSdpl(iszS), iRdpl(iszR) )   ! ok if iszS = 0 or iszR = 0
144      iszall(:) = isizei(:) * isizej(:) * ipk * ipl * ipf
145      iScnt(:) = PACK( iszall, mask = llsend )                                       ! ok if mask = .false.
146      iRcnt(:) = PACK( iszall, mask = llrecv )
147      iSdpl(1) = 0
148      DO jn = 2,iszS
149         iSdpl(jn) = iSdpl(jn-1) + iScnt(jn-1)   ! with _alltoallv: in units of sendtype
150      END DO
151      iRdpl(1) = 0
152      DO jn = 2,iszR
153         iRdpl(jn) = iRdpl(jn-1) + iRcnt(jn-1)   ! with _alltoallv: in units of sendtype
154      END DO
155     
156      ! Allocate buffer arrays to be sent/received if needed
157      iszS = SUM(iszall, mask = llsend)                             ! send buffer size
158      IF( ALLOCATED(BUFFSND) ) THEN
159         IF( SIZE(BUFFSND) < iszS )    DEALLOCATE(BUFFSND)          ! send buffer is too small
160      ENDIF
161      IF( .NOT. ALLOCATED(BUFFSND) )   ALLOCATE( BUFFSND(iszS) )
162      iszR = SUM(iszall, mask = llrecv)                             ! recv buffer size
163      IF( ALLOCATED(BUFFRCV) ) THEN
164         IF( SIZE(BUFFRCV) < iszR )    DEALLOCATE(BUFFRCV)          ! recv buffer is too small
165      ENDIF
166      IF( .NOT. ALLOCATED(BUFFRCV) )   ALLOCATE( BUFFRCV(iszR) )
167
168      ! fill sending buffer with ptab(jf)%pt4d
169      idx = 1
170      DO jn = 1, 8
171         IF( llsend(jn) ) THEN
172            ishti = ishtSi(jn)
173            ishtj = ishtSj(jn)
174            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
175               BUFFSND(idx) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
176               idx = idx + 1
177            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
178         ENDIF
179      END DO
180      !
181      ! ------------------------------------------------ !
182      !     3. Do all MPI exchanges in 1 unique call     !
183      ! ------------------------------------------------ !
184      !
185      IF( ln_timing ) CALL tic_tac(.TRUE.)
186      CALL mpi_neighbor_alltoallv (BUFFSND, iScnt, iSdpl, MPI_TYPE, BUFFRCV, iRcnt, iRdpl, MPI_TYPE, impi_nc, ierr)
187      IF( ln_timing ) CALL tic_tac(.FALSE.)
188      !
189      ! ------------------------- !
190      !     4. Fill all halos     !
191      ! ------------------------- !
192      !
193      idx = 1
194      DO jn = 1, 8
195         ishti = ishtRi(jn)
196         ishtj = ishtRj(jn)
197         SELECT CASE ( ifill(jn) )
198         CASE ( jpfillnothing )               ! no filling
199         CASE ( jpfillmpi   )                 ! fill with data received by MPI
200            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
201               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idx)
202               idx = idx + 1
203            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
204         CASE ( jpfillperio )                 ! use periodicity
205            ishti2 = ishtPi(jn)
206            ishtj2 = ishtPj(jn)
207            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
208               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
209            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
210         CASE ( jpfillcopy  )                 ! filling with inner domain values
211            ishti2 = ishtSi(jn)
212            ishtj2 = ishtSj(jn)
213            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
214               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
215            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
216         CASE ( jpfillcst   )                 ! filling with constant value
217            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
218               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = zland
219            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
220         END SELECT
221      END DO
222
223      DEALLOCATE( iScnt, iRcnt, iSdpl, iRdpl )
224      IF( iszS > jpi*jpj )   DEALLOCATE(BUFFSND)                    ! blocking Send -> can directly deallocate
225      IF( iszR > jpi*jpj )   DEALLOCATE(BUFFRCV)                    ! blocking Recv -> can directly deallocate
226
227      ! potential "indirect self-periodicity" for the corners
228      DO jn = 5, 8
229         IF( .NOT. l_SelfPerio(jn) .AND. l_SelfPerio(jpwe)  ) THEN   ! no bi-perio but ew-perio: corners indirect definition
230            ishti  = ishtRi(jn)
231            ishtj  = ishtRj(jn)
232            ishti2 = ishtPi(jn)   ! use i- shift periodicity
233            ishtj2 = ishtRj(jn)   ! use j- shift recv location: use ew-perio -> ok as filling of the south and north halos now done
234            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
235               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
236            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
237         ENDIF
238         IF( .NOT. l_SelfPerio(jn) .AND. l_SelfPerio(jpso)  ) THEN   ! no bi-perio but ns-perio: corners indirect definition
239            ishti  = ishtRi(jn)
240            ishtj  = ishtRj(jn)
241            ishti2 = ishtRi(jn)   ! use i- shift recv location: use ns-perio -> ok as filling of the west and east halos now done
242            ishtj2 = ishtPj(jn)   ! use j- shift periodicity
243            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
244               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
245            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
246         ENDIF
247      END DO
248      !
249      ! ------------------------------- !
250      !     5. north fold treatment     !
251      ! ------------------------------- !
252      !
253      IF( ll_IdoNFold ) THEN
254         IF( jpni == 1 )  THEN   ;   CALL lbc_nfd( ptab, cd_nat, psgn                  , ihls, ipf )   ! self NFold
255         ELSE                    ;   CALL mpp_nfd( ptab, cd_nat, psgn, ifill_nfd, zland, ihls, ipf )   ! mpi  NFold
256         ENDIF
257      ENDIF
258      !
259   END SUBROUTINE lbc_lnk_neicoll_/**/PRECISION
260
Note: See TracBrowser for help on using the repository browser.