Index: NEMO/branches/2021/dev_r14312_MPI_Interface/src/OCE/LBC/lbc_lnk_pt2pt_generic.h90
===================================================================
--- NEMO/branches/2021/dev_r14312_MPI_Interface/src/OCE/LBC/lbc_lnk_pt2pt_generic.h90	(revision 14378)
+++ NEMO/branches/2021/dev_r14312_MPI_Interface/src/OCE/LBC/lbc_lnk_pt2pt_generic.h90	(revision 14379)
@@ -1,3 +1,4 @@
   
+#if ! defined BLOCK_ISEND && ! defined BLOCK_FILL
    SUBROUTINE lbc_lnk_pt2pt_/**/PRECISION( cdname, ptab, cd_nat, psgn, kfld, kfillmode, pfillval, khls, lsend, lrecv, ld4only )
       CHARACTER(len=*)              , INTENT(in   ) ::   cdname      ! name of the calling subroutine
@@ -172,18 +173,7 @@
       !
       DO jn = 1, 2
-         IF( llsend(jn) ) THEN
-            ishti = ishtSi(jn)
-            ishtj = ishtSj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               BUFFSND(idxs) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
-               idxs = idxs + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            ! non-blocking send of the west/east side using local buffer
-            CALL MPI_ISEND( BUFFSND(ishtS(jn)+1), iszall(jn), MPI_TYPE, mpiSnei(ihls,jn), iStag(jn), icomm, nreq_p2p(jn), ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-         ENDIF
+#define BLOCK_ISEND
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_ISEND
       END DO
       !
@@ -193,43 +183,26 @@
       !
       DO jn = 1, 2
-         ishti = ishtRi(jn)
-         ishtj = ishtRj(jn)
-         SELECT CASE ( ifill(jn) )
-         CASE ( jpfillnothing )               ! no filling 
-         CASE ( jpfillmpi   )                 ! fill with data received by MPI
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            !                                 ! blocking receive of the west/east halo in local temporary arrays
-            CALL MPI_RECV( BUFFRCV(ishtR(jn)+1), iszall(jn), MPI_TYPE, mpiRnei(ihls,jn), iRtag(jn), icomm, MPI_STATUS_IGNORE, ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idxr)
-               idxr = idxr + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillperio )                 ! use periodicity
-            ishti2 = ishtPi(jn)
-            ishtj2 = ishtPj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillcopy  )                 ! filling with inner domain values
-            ishti2 = ishtSi(jn)
-            ishtj2 = ishtSj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillcst   )                 ! filling with constant value
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = zland
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         END SELECT
+#define BLOCK_FILL
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_FILL
+      END DO
+      !
+      ! ------------------------------------------------- !
+      !     5. Do north and south MPI_Isend if needed     !
+      ! ------------------------------------------------- !
+      !
+      DO jn = 3, 4
+#define BLOCK_ISEND
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_ISEND
       END DO
       !
       ! ------------------------------- !
-      !     5. north fold treatment     !
+      !     6. north fold treatment     !
       ! ------------------------------- !
       !
-      ! do it before south directions so concerned processes can do it without waiting for the comm with the sourthern neighbor
+      ! Must be done after receiving data from East/West neighbourgs (as it is coded in mpp_nfd, to be changed one day...)
+      ! Do it after MPI_iSend to south/north neighbourgs so they won't wait (too much) to receive their data
+      ! Do if before MPI_Recv from south/north neighbourgs so we have more time to receive data
       !
       IF( ll_IdoNFold ) THEN
@@ -239,25 +212,4 @@
       ENDIF
       !
-      ! ------------------------------------------------- !
-      !     6. Do north and south MPI_Isend if needed     !
-      ! ------------------------------------------------- !
-      !
-      DO jn = 3, 4
-         IF( llsend(jn) ) THEN
-            ishti = ishtSi(jn)
-            ishtj = ishtSj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               BUFFSND(idxs) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
-               idxs = idxs + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            ! non-blocking send of the south/north side using local buffer
-            CALL MPI_ISEND( BUFFSND(ishtS(jn)+1), iszall(jn), MPI_TYPE, mpiSnei(ihls,jn), iStag(jn), icomm, nreq_p2p(jn), ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-         ENDIF
-      END DO
-      !
       ! ------------------------------------- !
       !     7. Fill south and north halos     !
@@ -265,36 +217,7 @@
       !
       DO jn = 3, 4
-         ishti = ishtRi(jn)
-         ishtj = ishtRj(jn)
-         SELECT CASE ( ifill(jn) )
-         CASE ( jpfillnothing )               ! no filling 
-         CASE ( jpfillmpi   )                 ! fill with data received by MPI
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            !                                 ! blocking receive of the south/north halo in local temporary arrays
-            CALL MPI_RECV( BUFFRCV(ishtR(jn)+1), iszall(jn), MPI_TYPE, mpiRnei(ihls,jn), iRtag(jn), icomm, MPI_STATUS_IGNORE, ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idxr)
-               idxr = idxr + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillperio )                 ! use periodicity
-            ishti2 = ishtPi(jn)
-            ishtj2 = ishtPj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillcopy  )                 ! filling with inner domain values
-            ishti2 = ishtSi(jn)
-            ishtj2 = ishtSj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         CASE ( jpfillcst   )                 ! filling with constant value
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = zland
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         END SELECT
+#define BLOCK_FILL
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_FILL
       END DO
       !
@@ -305,34 +228,12 @@
       !
       DO jn = 5, 8
-         IF( llsend(jn) ) THEN
-            ishti = ishtSi(jn)
-            ishtj = ishtSj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               BUFFSND(idxs) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
-               idxs = idxs + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            ! non-blocking send of the corners using local buffer
-            CALL MPI_ISEND( BUFFSND(ishtS(jn)+1), iszall(jn), MPI_TYPE, mpiSnei(ihls,jn), iStag(jn), icomm, nreq_p2p(jn), ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-         ENDIF
+#define BLOCK_ISEND
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_ISEND
       END DO
       DO jn = 5, 8
-         IF( llrecv(jn) ) THEN
-#if ! defined key_mpi_off
-            IF( ln_timing ) CALL tic_tac(.TRUE.)
-            ! blocking receive of the corner halo in local temporary arrays
-            CALL MPI_RECV( BUFFRCV(ishtR(jn)+1), iszall(jn), MPI_TYPE, mpiRnei(ihls,jn), iRtag(jn), icomm, MPI_STATUS_IGNORE, ierr )
-            IF( ln_timing ) CALL tic_tac(.FALSE.)
-#endif
-            ishti = ishtRi(jn)
-            ishtj = ishtRj(jn)
-            DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
-               ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idxr)
-               idxr = idxr + 1
-            END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
-         ENDIF
+#define BLOCK_FILL
+#  include "lbc_lnk_pt2pt_generic.h90"         
+#undef BLOCK_FILL
       END DO
       !
@@ -349,3 +250,55 @@
       !
    END SUBROUTINE lbc_lnk_pt2pt_/**/PRECISION
+#endif
 
+#if defined BLOCK_ISEND
+   IF( llsend(jn) ) THEN
+      ishti = ishtSi(jn)
+      ishtj = ishtSj(jn)
+      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
+         BUFFSND(idxs) = ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl)
+         idxs = idxs + 1
+      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
+#if ! defined key_mpi_off
+      IF( ln_timing ) CALL tic_tac(.TRUE.)
+      ! non-blocking send of the west/east side using local buffer
+      CALL MPI_ISEND( BUFFSND(ishtS(jn)+1), iszall(jn), MPI_TYPE, mpiSnei(ihls,jn), iStag(jn), icomm, nreq_p2p(jn), ierr )
+      IF( ln_timing ) CALL tic_tac(.FALSE.)
+#endif
+   ENDIF
+#endif
+
+#if defined BLOCK_FILL
+   ishti = ishtRi(jn)
+   ishtj = ishtRj(jn)
+   SELECT CASE ( ifill(jn) )
+   CASE ( jpfillnothing )               ! no filling 
+   CASE ( jpfillmpi   )                 ! fill with data received by MPI
+#if ! defined key_mpi_off
+      IF( ln_timing ) CALL tic_tac(.TRUE.)
+      !                                 ! blocking receive of the west/east halo in local temporary arrays
+      CALL MPI_RECV( BUFFRCV(ishtR(jn)+1), iszall(jn), MPI_TYPE, mpiRnei(ihls,jn), iRtag(jn), icomm, MPI_STATUS_IGNORE, ierr )
+      IF( ln_timing ) CALL tic_tac(.FALSE.)
+#endif
+      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
+         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = BUFFRCV(idxr)
+         idxr = idxr + 1
+      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
+   CASE ( jpfillperio )                 ! use periodicity
+      ishti2 = ishtPi(jn)
+      ishtj2 = ishtPj(jn)
+      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
+         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
+      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
+   CASE ( jpfillcopy  )                 ! filling with inner domain values
+      ishti2 = ishtSi(jn)
+      ishtj2 = ishtSj(jn)
+      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
+         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = ptab(jf)%pt4d(ishti2+ji,ishtj2+jj,jk,jl)
+      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
+   CASE ( jpfillcst   )                 ! filling with constant value
+      DO jf = 1, ipf  ;  DO jl = 1, ipl  ;  DO jk = 1, ipk  ;  DO jj = 1,isizej(jn)  ;  DO ji = 1,isizei(jn)
+         ptab(jf)%pt4d(ishti+ji,ishtj+jj,jk,jl) = zland
+      END DO   ;   END DO   ;   END DO   ;   END DO   ;   END DO
+   END SELECT
+#endif
