










      subroutine MessPass3D_tile (Istr,Iend,Jstr,Jend, A, nmax)      


      use Agrif_Util
      interface
        subroutine Sub_Loop_MessPass3D_tile(Istr,Iend,Jstr,Jend,A,nmax,N
     &ORTH_INTER,SOUTH_INTER,EAST_INTER,WEST_INTER,p_NW,CORNER_NW,p_SE,C
     &ORNER_SE,p_NE,CORNER_NE,p_SW,CORNER_SW,p_N,NORTH_INTER2,p_S,SOUTH_
     &INTER2,p_E,EAST_INTER2,p_W,WEST_INTER2,buf_rev3_3D,ibuf_revS_3D,bu
     &f_rev1_3D,buf_snd3_3D,ibuf_sndS_3D,buf_snd1_3D,jbuf_revE_3D,jbuf_r
     &evW_3D,jbuf_sndE_3D,jbuf_sndW_3D,buf_rev2_3D,ibuf_revN_3D,buf_rev4
     &_3D,buf_snd2_3D,ibuf_sndN_3D,buf_snd4_3D,Mmmpi,jj,Lmmpi,ii,padd_E,
     &Mm,padd_X,Lm)
          implicit none
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      logical, parameter :: use_host_device = .TRUE.
      integer(4), parameter :: Npts = 2
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      integer(4) :: p_NW
      logical :: CORNER_NW
      integer(4) :: p_SE
      logical :: CORNER_SE
      integer(4) :: p_NE
      logical :: CORNER_NE
      integer(4) :: p_SW
      logical :: CORNER_SW
      integer(4) :: p_N
      logical :: NORTH_INTER2
      integer(4) :: p_S
      logical :: SOUTH_INTER2
      integer(4) :: p_E
      logical :: EAST_INTER2
      integer(4) :: p_W
      logical :: WEST_INTER2
      integer(4) :: Mmmpi
      integer(4) :: jj
      integer(4) :: Lmmpi
      integer(4) :: ii
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real, allocatable, dimension(:) :: buf_rev3_3D
      real, allocatable, dimension(:) :: ibuf_revS_3D
      real, allocatable, dimension(:) :: buf_rev1_3D
      real, allocatable, dimension(:) :: buf_snd3_3D
      real, allocatable, dimension(:) :: ibuf_sndS_3D
      real, allocatable, dimension(:) :: buf_snd1_3D
      real, allocatable, dimension(:) :: jbuf_revE_3D
      real, allocatable, dimension(:) :: jbuf_revW_3D
      real, allocatable, dimension(:) :: jbuf_sndE_3D
      real, allocatable, dimension(:) :: jbuf_sndW_3D
      real, allocatable, dimension(:) :: buf_rev2_3D
      real, allocatable, dimension(:) :: ibuf_revN_3D
      real, allocatable, dimension(:) :: buf_rev4_3D
      real, allocatable, dimension(:) :: buf_snd2_3D
      real, allocatable, dimension(:) :: ibuf_sndN_3D
      real, allocatable, dimension(:) :: buf_snd4_3D
      integer(4) :: nmax
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
        end subroutine Sub_Loop_MessPass3D_tile

      end interface
      integer(4) :: nmax
      real, dimension(-1: Agrif_tabvars_i(2)%iarray0+2+ Agrif_tabvars_i(
     &13)%iarray0,-1: Agrif_tabvars_i(4)%iarray0+2+ Agrif_tabvars_i(14)%
     &iarray0,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      

        call Sub_Loop_MessPass3D_tile(Istr,Iend,Jstr,Jend,A,nmax, Agrif_
     &tabvars_l(13)%larray0, Agrif_tabvars_l(14)%larray0, Agrif_tabvars_
     &l(11)%larray0, Agrif_tabvars_l(12)%larray0, Agrif_tabvars_i(186)%i
     &array0, Agrif_tabvars_l(16)%larray0, Agrif_tabvars_i(185)%iarray0,
     & Agrif_tabvars_l(18)%larray0, Agrif_tabvars_i(187)%iarray0, Agrif_
     &tabvars_l(17)%larray0, Agrif_tabvars_i(184)%iarray0, Agrif_tabvars
     &_l(15)%larray0, Agrif_tabvars_i(183)%iarray0, Agrif_tabvars_l(9)%l
     &array0, Agrif_tabvars_i(182)%iarray0, Agrif_tabvars_l(10)%larray0,
     & Agrif_tabvars_i(181)%iarray0, Agrif_tabvars_l(7)%larray0, Agrif_t
     &abvars_i(180)%iarray0, Agrif_tabvars_l(8)%larray0, Agrif_tabvars(3
     &14)%array1, Agrif_tabvars(310)%array1, Agrif_tabvars(315)%array1, 
     &Agrif_tabvars(316)%array1, Agrif_tabvars(311)%array1, Agrif_tabvar
     &s(317)%array1, Agrif_tabvars(306)%array1, Agrif_tabvars(307)%array
     &1, Agrif_tabvars(308)%array1, Agrif_tabvars(309)%array1, Agrif_tab
     &vars(318)%array1, Agrif_tabvars(312)%array1, Agrif_tabvars(319)%ar
     &ray1, Agrif_tabvars(320)%array1, Agrif_tabvars(313)%array1, Agrif_
     &tabvars(321)%array1, Agrif_tabvars_i(8)%iarray0, Agrif_tabvars_i(1
     &79)%iarray0, Agrif_tabvars_i(7)%iarray0, Agrif_tabvars_i(178)%iarr
     &ay0, Agrif_tabvars_i(14)%iarray0, Agrif_tabvars_i(4)%iarray0, Agri
     &f_tabvars_i(13)%iarray0, Agrif_tabvars_i(2)%iarray0)

      end


      subroutine Sub_Loop_MessPass3D_tile(Istr,Iend,Jstr,Jend,A,nmax,NOR
     &TH_INTER,SOUTH_INTER,EAST_INTER,WEST_INTER,p_NW,CORNER_NW,p_SE,COR
     &NER_SE,p_NE,CORNER_NE,p_SW,CORNER_SW,p_N,NORTH_INTER2,p_S,SOUTH_IN
     &TER2,p_E,EAST_INTER2,p_W,WEST_INTER2,buf_rev3_3D,ibuf_revS_3D,buf_
     &rev1_3D,buf_snd3_3D,ibuf_sndS_3D,buf_snd1_3D,jbuf_revE_3D,jbuf_rev
     &W_3D,jbuf_sndE_3D,jbuf_sndW_3D,buf_rev2_3D,ibuf_revN_3D,buf_rev4_3
     &D,buf_snd2_3D,ibuf_sndN_3D,buf_snd4_3D,Mmmpi,jj,Lmmpi,ii,padd_E,Mm
     &,padd_X,Lm)

      use Agrif_Types, only : Agrif_tabvars


      include 'mpif.h'
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      logical, parameter :: use_host_device = .TRUE.
      integer(4), parameter :: Npts = 2
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      integer(4) :: p_NW
      logical :: CORNER_NW
      integer(4) :: p_SE
      logical :: CORNER_SE
      integer(4) :: p_NE
      logical :: CORNER_NE
      integer(4) :: p_SW
      logical :: CORNER_SW
      integer(4) :: p_N
      logical :: NORTH_INTER2
      integer(4) :: p_S
      logical :: SOUTH_INTER2
      integer(4) :: p_E
      logical :: EAST_INTER2
      integer(4) :: p_W
      logical :: WEST_INTER2
      integer(4) :: Mmmpi
      integer(4) :: jj
      integer(4) :: Lmmpi
      integer(4) :: ii
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real, allocatable, dimension(:) :: buf_rev3_3D
      real, allocatable, dimension(:) :: ibuf_revS_3D
      real, allocatable, dimension(:) :: buf_rev1_3D
      real, allocatable, dimension(:) :: buf_snd3_3D
      real, allocatable, dimension(:) :: ibuf_sndS_3D
      real, allocatable, dimension(:) :: buf_snd1_3D
      real, allocatable, dimension(:) :: jbuf_revE_3D
      real, allocatable, dimension(:) :: jbuf_revW_3D
      real, allocatable, dimension(:) :: jbuf_sndE_3D
      real, allocatable, dimension(:) :: jbuf_sndW_3D
      real, allocatable, dimension(:) :: buf_rev2_3D
      real, allocatable, dimension(:) :: ibuf_revN_3D
      real, allocatable, dimension(:) :: buf_rev4_3D
      real, allocatable, dimension(:) :: buf_snd2_3D
      real, allocatable, dimension(:) :: ibuf_sndN_3D
      real, allocatable, dimension(:) :: buf_snd4_3D
      integer(4) :: nmax
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend

                                            
                             
                                            

                                                             
                     
                        

                                                            
                                           
                                                               
                                    
                                      
                                                          

                       

                                    

                        
                              

                       
                          

                                                
                                             
                          

                        

                                 

                                                
                        

                                                              
                      
                              

                                       
                                                                     
                                          
                         

                             

                             

                            

                            

                                             

                            

                                     

                                                               

                         

                                                      
                                                      
                                                                  
                               

                               

                              

                              

                               

                              

                              

                                

                             

                                                                  
                                                                       
                                                                 
                                 
                                
                                                                       
                                                                 
                                                                 
                                                                 
                                                        
                                                                       
                                                                 
                                                                 
            
                                              
                                                   
                     
                              
                               
                                 
                                                  
                                                                       
                                                                 
                                                                 
                        
                      
                           
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                    
                           
                                      
                             
                                                 
                                                                       
                                                                 
                     
                                                      
                                                                       
                                                                 
              
                                          
                                                                       
                                                                 
       
                                                           
      real(8) :: Cu_Nbq_X
      real(8) :: Cu_Nbq_Y
      real(8) :: Cu_Nbq_Z
                                             
                                                                       
                                                                 
              
                                                  
                                                                       
                                                                 
              
                                  
                                           
                                                  
                                                                   
                                                               
                                                      
                                                                       
                                                                 
                      
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                        
                                
                                                                       
                                                                 
                                                        

                                                                       
                                                                 
                  
                                                                       
                                                                 
                                                                 
                                                                 
                  

                          

              
                             

                  
                               

                 
                                

                       
                                   

!$AGRIF_DO_NOT_TREAT
      INTEGER*4 :: ocean_grid_comm
      common /cpl_comm/ ocean_grid_comm
!$AGRIF_END_DO_NOT_TREAT
                       
                                                     
                                           
      integer(4) :: sub_X
      integer(4) :: size_X
      integer(4) :: sub_E
      integer(4) :: size_E
                                                                       
                                                                 
                                                                 
                         
                                                                       
                                                                 
                                                                 
                                           
                                                                       
                                                                 
                                                                 
                                                                 
                                              
                                                              
      integer(4) :: size_Z
      integer(4) :: sub_X_3D
      integer(4) :: size_X_3D
      integer(4) :: sub_E_3D
      integer(4) :: size_E_3D
                                                                       
                                                                 
                                                                 
                                                 
                                                                       
                                                                 
                                                                 
                                         
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                        
                               
      integer(4) :: ipts
      integer(4) :: jpts
                        

                     
                                                 
                                                                       
                                                                 
                                              
      integer(4) :: i
      integer(4) :: j
      integer(4) :: k
      integer(4) :: isize
      integer(4) :: jsize
      integer(4) :: ksize
      integer(4), dimension(1:8) :: req
      integer(4), dimension(1:MPI_STATUS_SIZE,1:8) :: status
      integer(4) :: ierr
                                 
      integer(4) :: iter
      integer(4) :: mdii
      integer(4) :: mdjj
                                                 
      integer(4) :: imin
      integer(4) :: imax
      integer(4) :: ishft
      integer(4) :: jmin
      integer(4) :: jmax
      integer(4) :: jshft
      if (ii.eq.0 .and. Istr.eq.1) then
        imin=Istr-1
      else
        imin=Istr
      endif
      if (ii.eq.NP_XI-1 .and. Iend.eq.Lmmpi) then
        imax=Iend+1
      else
        imax=Iend
      endif
      ishft=imax-imin+1
      if (jj.eq.0 .and. Jstr.eq.1) then
        jmin=Jstr-1
      else
        jmin=Jstr
      endif
      if (jj.eq.NP_ETA-1 .and. Jend.eq.Mmmpi) then
        jmax=Jend+1
      else
        jmax=Jend
      endif
      jshft=jmax-jmin+1
      size_Z=Npts*Npts*(N+1)
      sub_X=(Lm+NSUB_X-1)/NSUB_X
      size_X=(N+1)*Npts*(sub_X+2*Npts)
      sub_E=(Mm+NSUB_E-1)/NSUB_E
      size_E=(N+1)*Npts*(sub_E+2*Npts)
      if(.not. allocated(buf_snd4_3D)) then
      Allocate(buf_snd4_3D(size_Z),  ibuf_sndN_3D(size_X),
     &         buf_snd2_3D(size_Z),
     &         buf_rev4_3D(size_Z),  ibuf_revN_3D(size_X),
     &         buf_rev2_3D(size_Z),
     &        jbuf_sndW_3D(size_E),                     
     &                                             jbuf_sndE_3D(size_E),
     &        jbuf_revW_3D(size_E),                     
     &                                             jbuf_revE_3D(size_E),
     &         buf_snd1_3D(size_Z),  ibuf_sndS_3D(size_X),
     &         buf_snd3_3D(size_Z),
     &         buf_rev1_3D(size_Z),  ibuf_revS_3D(size_X),
     &         buf_rev3_3D(size_Z))
      endif
      ksize=Npts*Npts*nmax
      isize=Npts*ishft*nmax
      jsize=Npts*jshft*nmax
      do iter=0,1
        mdii=mod(ii+iter,2)
        mdjj=mod(jj+iter,2)
        if (mdii.eq.0) then
          if ((WEST_INTER2)) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do j=jmin,jmax
                do ipts=1,Npts
                  jbuf_sndW_3D(k+nmax*(j-jmin+(ipts-1)*jshft))=
     &                  A(ipts,j,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(jbuf_revW_3D,jbuf_sndW_3D)
            call MPI_Irecv (jbuf_revW_3D, jsize, MPI_DOUBLE_PRECISION,
     &                         p_W, 2, MPI_COMM_WORLD, req(1), ierr)
            call MPI_Send  (jbuf_sndW_3D, jsize, MPI_DOUBLE_PRECISION,
     &                         p_W, 1, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        else
          if (EAST_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do j=jmin,jmax
                do ipts=1,Npts
                  jbuf_sndE_3D(k+nmax*(j-jmin+(ipts-1)*jshft))=
     &                                       A(Lmmpi-Npts+ipts,j,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(jbuf_revE_3D,jbuf_sndE_3D)
            call MPI_Irecv (jbuf_revE_3D, jsize, MPI_DOUBLE_PRECISION,
     &                        p_E, 1, MPI_COMM_WORLD, req(2), ierr)
            call MPI_Send  (jbuf_sndE_3D, jsize, MPI_DOUBLE_PRECISION,
     &                        p_E, 2, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        endif
        if (mdjj.eq.0) then
          if (SOUTH_INTER2) then
!$acc parallel loop if(compute_on_device)
!$acc& default(present) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do i=imin,imax
                  ibuf_sndS_3D(k+nmax*(i-imin+(jpts-1)*ishft))=
     &                          A(i,jpts,k)
                enddo
              enddo
            enddo
!$acc update host(ibuf_sndS_3D(1:isize)) if(.not.use_host_device)
!$acc host_data if(compute_on_device.and.use_host_device)
!$acc&   use_device(ibuf_revS_3D,ibuf_sndS_3D)
            call MPI_Irecv (ibuf_revS_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_S, 4, MPI_COMM_WORLD, req(3), ierr)
            call MPI_Send  (ibuf_sndS_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_S, 3, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        else
          if (NORTH_INTER2) then
!$acc parallel loop if(compute_on_device)
!$acc& default(present) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do i=imin,imax
                  ibuf_sndN_3D(k+nmax*(i-imin+(jpts-1)*ishft))=
     &                                         A(i,Mmmpi-Npts+jpts,k)
                enddo
              enddo
            enddo
!$acc update host(ibuf_sndN_3D(1:isize)) if(.not.use_host_device)
!$acc host_data if(compute_on_device.and.use_host_device)
!$acc&   use_device(ibuf_revN_3D,ibuf_sndN_3D)
            call MPI_Irecv (ibuf_revN_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_N, 3, MPI_COMM_WORLD, req(4), ierr)
            call MPI_Send  (ibuf_sndN_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_N, 4, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        endif
        if (mdii.eq.0) then
          if (CORNER_SW) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd1_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                 A(ipts,jpts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev1_3D,buf_snd1_3D)
            call MPI_Irecv (buf_rev1_3D, ksize, MPI_DOUBLE_PRECISION, 
     &                                                             p_SW,
     &                                6, MPI_COMM_WORLD, req(5),ierr)
            call MPI_Send  (buf_snd1_3D, ksize, MPI_DOUBLE_PRECISION, 
     &                                                             p_SW,
     &                                5, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        else
          if (CORNER_NE) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd2_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                            A(Lmmpi+ipts-Npts,Mmmpi+jpts-Npts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev2_3D,buf_snd2_3D)
            call MPI_Irecv (buf_rev2_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NE, 5, MPI_COMM_WORLD, req(6),ierr)
            call MPI_Send  (buf_snd2_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NE, 6, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        endif
        if (mdii.eq.1) then
          if (CORNER_SE) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd3_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                                A(Lmmpi+ipts-Npts,jpts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev3_3D,buf_snd3_3D)
            call MPI_Irecv (buf_rev3_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_SE, 8, MPI_COMM_WORLD, req(7),ierr)
            call MPI_Send  (buf_snd3_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_SE, 7, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        else
          if (CORNER_NW) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd4_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                                A(ipts,Mmmpi+jpts-Npts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev4_3D,buf_snd4_3D)
            call MPI_Irecv (buf_rev4_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NW, 7, MPI_COMM_WORLD, req(8),ierr)
            call MPI_Send  (buf_snd4_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NW, 8, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        endif
      enddo
      if (WEST_INTER2) then
        call MPI_Wait (req(1),status(1,1),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
             A(ipts-Npts,j,k)=
     &          jbuf_revW_3D(k+nmax*(j-jmin+(ipts-1)*jshft))
            enddo
          enddo
        enddo
      endif
      if (WEST_INTER .and. .not. WEST_INTER2) then
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(ipts-Npts,j,k)=A(ipts,j,k)
            enddo
          enddo
        enddo
      endif
      if (EAST_INTER2) then
        call MPI_Wait (req(2),status(1,2),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(Lmmpi+ipts,j,k)=
     &                         jbuf_revE_3D(k+nmax*(j-jmin+(ipts-1)
     &                         *jshft))
            enddo
          enddo
        enddo
      endif
      if (EAST_INTER .and. .not. EAST_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(Lmmpi+ipts,j,k)=A(Lmmpi+ipts-Npts,j,k)
            enddo
          enddo
        enddo
      endif
      if (SOUTH_INTER2) then
        call MPI_Wait (req(3),status(1,3),ierr)
!$acc update device(ibuf_revS_3D(1:isize)) if(.not.use_host_device)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do i=imin,imax
              A(i,jpts-Npts,k)=ibuf_revS_3D(k+nmax*(i-imin+(jpts-1)
     &                        *ishft))
            enddo
          enddo
        enddo
      endif
      if (SOUTH_INTER .and. .not. SOUTH_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do i=imin,imax
            do jpts=1,Npts
              A(i,jpts-Npts,k)=A(i,jpts,k)
            enddo
          enddo
        enddo
      endif
      if (NORTH_INTER2) then
        call MPI_Wait (req(4),status(1,4),ierr)
!$acc update device(ibuf_revN_3D(1:isize)) if(.not.use_host_device)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do i=imin,imax
              A(i,Mmmpi+jpts,k)=
     &          ibuf_revN_3D(k+nmax*(i-imin+(jpts-1)*ishft))
            enddo
          enddo
        enddo
      endif
      if (NORTH_INTER .and. .not. NORTH_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do i=imin,imax
            do jpts=1,Npts
              A(i,Mmmpi+jpts,k)=A(i,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
      endif
      if (CORNER_SW) then
        call MPI_Wait (req(5),status(1,5),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,jpts-Npts,k)=
     &                           buf_rev1_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_SW  .and.
     &   SOUTH_INTER .and.  WEST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,jpts-Npts,k)=
     &                           A(ipts,jpts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_NE) then
        call MPI_Wait (req(6),status(1,6),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,Mmmpi+jpts,k)=
     &                           buf_rev2_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_NE  .and.
     &   NORTH_INTER .and.  EAST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,Mmmpi+jpts,k)=
     &              A(Lmmpi+ipts-Npts,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_SE) then
        call MPI_Wait (req(7),status(1,7),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,jpts-Npts,k)=
     &                           buf_rev3_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_SE .and.
     &   SOUTH_INTER .and.  EAST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
               A(Lmmpi+ipts,jpts-Npts,k)=
     &                           A(Lmmpi+ipts-Npts,jpts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_NW) then
        call MPI_Wait (req(8),status(1,8),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,Mmmpi+jpts,k)=
     &                           buf_rev4_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_NW  .and.
     &   NORTH_INTER .and.  WEST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,Mmmpi+jpts,k)=A(ipts,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
       endif
      return
       
      

      end subroutine Sub_Loop_MessPass3D_tile

      subroutine MessPass3D_3pts_tile (Istr,Iend,Jstr,Jend, A, nmax)      


      use Agrif_Util
      interface
        subroutine Sub_Loop_MessPass3D_3pts_tile(Istr,Iend,Jstr,Jend,A,n
     &max,NORTH_INTER,SOUTH_INTER,EAST_INTER,WEST_INTER,p_NW,CORNER_NW,p
     &_SE,CORNER_SE,p_NE,CORNER_NE,p_SW,CORNER_SW,p_N,NORTH_INTER2,p_S,S
     &OUTH_INTER2,p_E,EAST_INTER2,p_W,WEST_INTER2,buf_rev3_3D,ibuf_revS_
     &3D,buf_rev1_3D,buf_snd3_3D,ibuf_sndS_3D,buf_snd1_3D,jbuf_revE_3D,j
     &buf_revW_3D,jbuf_sndE_3D,jbuf_sndW_3D,buf_rev2_3D,ibuf_revN_3D,buf
     &_rev4_3D,buf_snd2_3D,ibuf_sndN_3D,buf_snd4_3D,Mmmpi,jj,Lmmpi,ii,pa
     &dd_E,Mm,padd_X,Lm)
          implicit none
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      logical, parameter :: use_host_device = .TRUE.
      integer(4), parameter :: Npts = 3
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      integer(4) :: p_NW
      logical :: CORNER_NW
      integer(4) :: p_SE
      logical :: CORNER_SE
      integer(4) :: p_NE
      logical :: CORNER_NE
      integer(4) :: p_SW
      logical :: CORNER_SW
      integer(4) :: p_N
      logical :: NORTH_INTER2
      integer(4) :: p_S
      logical :: SOUTH_INTER2
      integer(4) :: p_E
      logical :: EAST_INTER2
      integer(4) :: p_W
      logical :: WEST_INTER2
      integer(4) :: Mmmpi
      integer(4) :: jj
      integer(4) :: Lmmpi
      integer(4) :: ii
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real, allocatable, dimension(:) :: buf_rev3_3D
      real, allocatable, dimension(:) :: ibuf_revS_3D
      real, allocatable, dimension(:) :: buf_rev1_3D
      real, allocatable, dimension(:) :: buf_snd3_3D
      real, allocatable, dimension(:) :: ibuf_sndS_3D
      real, allocatable, dimension(:) :: buf_snd1_3D
      real, allocatable, dimension(:) :: jbuf_revE_3D
      real, allocatable, dimension(:) :: jbuf_revW_3D
      real, allocatable, dimension(:) :: jbuf_sndE_3D
      real, allocatable, dimension(:) :: jbuf_sndW_3D
      real, allocatable, dimension(:) :: buf_rev2_3D
      real, allocatable, dimension(:) :: ibuf_revN_3D
      real, allocatable, dimension(:) :: buf_rev4_3D
      real, allocatable, dimension(:) :: buf_snd2_3D
      real, allocatable, dimension(:) :: ibuf_sndN_3D
      real, allocatable, dimension(:) :: buf_snd4_3D
      integer(4) :: nmax
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
        end subroutine Sub_Loop_MessPass3D_3pts_tile

      end interface
      integer(4) :: nmax
      real, dimension(-1: Agrif_tabvars_i(2)%iarray0+2+ Agrif_tabvars_i(
     &13)%iarray0,-1: Agrif_tabvars_i(4)%iarray0+2+ Agrif_tabvars_i(14)%
     &iarray0,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend
      

        call Sub_Loop_MessPass3D_3pts_tile(Istr,Iend,Jstr,Jend,A,nmax, A
     &grif_tabvars_l(13)%larray0, Agrif_tabvars_l(14)%larray0, Agrif_tab
     &vars_l(11)%larray0, Agrif_tabvars_l(12)%larray0, Agrif_tabvars_i(1
     &86)%iarray0, Agrif_tabvars_l(16)%larray0, Agrif_tabvars_i(185)%iar
     &ray0, Agrif_tabvars_l(18)%larray0, Agrif_tabvars_i(187)%iarray0, A
     &grif_tabvars_l(17)%larray0, Agrif_tabvars_i(184)%iarray0, Agrif_ta
     &bvars_l(15)%larray0, Agrif_tabvars_i(183)%iarray0, Agrif_tabvars_l
     &(9)%larray0, Agrif_tabvars_i(182)%iarray0, Agrif_tabvars_l(10)%lar
     &ray0, Agrif_tabvars_i(181)%iarray0, Agrif_tabvars_l(7)%larray0, Ag
     &rif_tabvars_i(180)%iarray0, Agrif_tabvars_l(8)%larray0, Agrif_tabv
     &ars(314)%array1, Agrif_tabvars(310)%array1, Agrif_tabvars(315)%arr
     &ay1, Agrif_tabvars(316)%array1, Agrif_tabvars(311)%array1, Agrif_t
     &abvars(317)%array1, Agrif_tabvars(306)%array1, Agrif_tabvars(307)%
     &array1, Agrif_tabvars(308)%array1, Agrif_tabvars(309)%array1, Agri
     &f_tabvars(318)%array1, Agrif_tabvars(312)%array1, Agrif_tabvars(31
     &9)%array1, Agrif_tabvars(320)%array1, Agrif_tabvars(313)%array1, A
     &grif_tabvars(321)%array1, Agrif_tabvars_i(8)%iarray0, Agrif_tabvar
     &s_i(179)%iarray0, Agrif_tabvars_i(7)%iarray0, Agrif_tabvars_i(178)
     &%iarray0, Agrif_tabvars_i(14)%iarray0, Agrif_tabvars_i(4)%iarray0,
     & Agrif_tabvars_i(13)%iarray0, Agrif_tabvars_i(2)%iarray0)

      end


      subroutine Sub_Loop_MessPass3D_3pts_tile(Istr,Iend,Jstr,Jend,A,nma
     &x,NORTH_INTER,SOUTH_INTER,EAST_INTER,WEST_INTER,p_NW,CORNER_NW,p_S
     &E,CORNER_SE,p_NE,CORNER_NE,p_SW,CORNER_SW,p_N,NORTH_INTER2,p_S,SOU
     &TH_INTER2,p_E,EAST_INTER2,p_W,WEST_INTER2,buf_rev3_3D,ibuf_revS_3D
     &,buf_rev1_3D,buf_snd3_3D,ibuf_sndS_3D,buf_snd1_3D,jbuf_revE_3D,jbu
     &f_revW_3D,jbuf_sndE_3D,jbuf_sndW_3D,buf_rev2_3D,ibuf_revN_3D,buf_r
     &ev4_3D,buf_snd2_3D,ibuf_sndN_3D,buf_snd4_3D,Mmmpi,jj,Lmmpi,ii,padd
     &_E,Mm,padd_X,Lm)

      use Agrif_Types, only : Agrif_tabvars


      include 'mpif.h'
      integer(4), parameter :: LLm0 = 41
      integer(4), parameter :: MMm0 = 42
      integer(4), parameter :: N = 32
      integer(4), parameter :: N_sl = 0
      integer(4), parameter :: NP_XI = 1
      integer(4), parameter :: NP_ETA = 4
      integer(4), parameter :: NNODES = NP_XI*NP_ETA
      integer(4), parameter :: NPP = 1
      integer(4), parameter :: NSUB_X = 1
      integer(4), parameter :: NSUB_E = 1
      integer(4), parameter :: NWEIGHT = 1000
      integer(4), parameter :: Ntides = 8
      integer(4), parameter :: stdout = 6
      integer(4), parameter :: Np = N+1
      integer(4), parameter :: NpHz = (N+1+N_sl)
      integer(4), parameter :: NSA = 28
      real, parameter :: Vtransform = 2
      integer(4), parameter :: itemp = 1
      integer(4), parameter :: ntrc_temp = 1
      integer(4), parameter :: ntrc_salt = 1
      integer(4), parameter :: ntrc_pas = 0
      integer(4), parameter :: ntrc_bio = 0
      integer(4), parameter :: ntrc_subs = 0
      integer(4), parameter :: ntrc_substot = 0
      integer(4), parameter :: ntrc_sed = 0
      integer(4), parameter :: NTA = itemp+ntrc_salt
      integer(4), parameter :: NT = itemp+ntrc_salt+ntrc_pas+ntrc_bio+nt
     &rc_sed
      integer(4), parameter :: NTot = NT
      integer(4), parameter :: isalt = itemp+1
      integer(4), parameter :: ntrc_diabio = 0
      integer(4), parameter :: ntrc_diats = 0
      integer(4), parameter :: ntrc_diauv = 0
      integer(4), parameter :: ntrc_diavrt = 0
      integer(4), parameter :: ntrc_diaek = 0
      integer(4), parameter :: ntrc_diapv = 0
      integer(4), parameter :: ntrc_diaeddy = 0
      integer(4), parameter :: ntrc_surf = 0
      real, parameter :: pi = 3.14159265358979323846D0
      real, parameter :: deg2rad = pi/180.D0
      real, parameter :: rad2deg = 180.D0/pi
      real, parameter :: Eradius = 6371315.0D0
      real, parameter :: Erotation = 7.292115090D-5
      real, parameter :: day2sec = 86400.D0
      real, parameter :: sec2day = 1.D0/86400.D0
      real, parameter :: year2day = 365.25D0
      real, parameter :: day2year = 1.D0/365.25D0
      real, parameter :: jul_off = 2440000.D0
      real, parameter :: g = 9.81D0
      real, parameter :: Cp = 3985.0D0
      real, parameter :: vonKar = 0.41D0
      real, parameter :: spval = -999.0D0
      logical, parameter :: mask_val = .true.
      logical, parameter :: use_host_device = .TRUE.
      integer(4), parameter :: Npts = 3
      logical :: NORTH_INTER
      logical :: SOUTH_INTER
      logical :: EAST_INTER
      logical :: WEST_INTER
      integer(4) :: p_NW
      logical :: CORNER_NW
      integer(4) :: p_SE
      logical :: CORNER_SE
      integer(4) :: p_NE
      logical :: CORNER_NE
      integer(4) :: p_SW
      logical :: CORNER_SW
      integer(4) :: p_N
      logical :: NORTH_INTER2
      integer(4) :: p_S
      logical :: SOUTH_INTER2
      integer(4) :: p_E
      logical :: EAST_INTER2
      integer(4) :: p_W
      logical :: WEST_INTER2
      integer(4) :: Mmmpi
      integer(4) :: jj
      integer(4) :: Lmmpi
      integer(4) :: ii
      integer(4) :: padd_E
      integer(4) :: Mm
      integer(4) :: padd_X
      integer(4) :: Lm
      real, allocatable, dimension(:) :: buf_rev3_3D
      real, allocatable, dimension(:) :: ibuf_revS_3D
      real, allocatable, dimension(:) :: buf_rev1_3D
      real, allocatable, dimension(:) :: buf_snd3_3D
      real, allocatable, dimension(:) :: ibuf_sndS_3D
      real, allocatable, dimension(:) :: buf_snd1_3D
      real, allocatable, dimension(:) :: jbuf_revE_3D
      real, allocatable, dimension(:) :: jbuf_revW_3D
      real, allocatable, dimension(:) :: jbuf_sndE_3D
      real, allocatable, dimension(:) :: jbuf_sndW_3D
      real, allocatable, dimension(:) :: buf_rev2_3D
      real, allocatable, dimension(:) :: ibuf_revN_3D
      real, allocatable, dimension(:) :: buf_rev4_3D
      real, allocatable, dimension(:) :: buf_snd2_3D
      real, allocatable, dimension(:) :: ibuf_sndN_3D
      real, allocatable, dimension(:) :: buf_snd4_3D
      integer(4) :: nmax
      real, dimension(-1:Lm+2+padd_X,-1:Mm+2+padd_E,1:nmax) :: A
      integer(4) :: Istr
      integer(4) :: Iend
      integer(4) :: Jstr
      integer(4) :: Jend

                                            
                             
                                            

                                                             
                     
                        

                                                            
                                           
                                                               
                                    
                                      
                                                          

                       

                                    

                        
                              

                       
                          

                                                
                                             
                          

                        

                                 

                                                
                        

                                                              
                      
                              

                                       
                                                                     
                                          
                         

                             

                             

                            

                            

                                             

                            

                                     

                                                               

                         

                                                      
                                                      
                                                                  
                               

                               

                              

                              

                               

                              

                              

                                

                             

                                                                  
                                                                       
                                                                 
                                 
                                
                                                                       
                                                                 
                                                                 
                                                                 
                                                        
                                                                       
                                                                 
                                                                 
            
                                              
                                                   
                     
                              
                               
                                 
                                                  
                                                                       
                                                                 
                                                                 
                        
                      
                           
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                                                    
                           
                                      
                             
                                                 
                                                                       
                                                                 
                     
                                                      
                                                                       
                                                                 
              
                                          
                                                                       
                                                                 
       
                                                           
      real(8) :: Cu_Nbq_X
      real(8) :: Cu_Nbq_Y
      real(8) :: Cu_Nbq_Z
                                             
                                                                       
                                                                 
              
                                                  
                                                                       
                                                                 
              
                                  
                                           
                                                  
                                                                   
                                                               
                                                      
                                                                       
                                                                 
                      
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                        
                                
                                                                       
                                                                 
                                                        

                                                                       
                                                                 
                  
                                                                       
                                                                 
                                                                 
                                                                 
                  

                          

              
                             

                  
                               

                 
                                

                       
                                   

!$AGRIF_DO_NOT_TREAT
      INTEGER*4 :: ocean_grid_comm
      common /cpl_comm/ ocean_grid_comm
!$AGRIF_END_DO_NOT_TREAT
                       
                                                     
                                           
      integer(4) :: sub_X
      integer(4) :: size_X
      integer(4) :: sub_E
      integer(4) :: size_E
                                                                       
                                                                 
                                                                 
                         
                                                                       
                                                                 
                                                                 
                                           
                                                                       
                                                                 
                                                                 
                                                                 
                                              
                                                              
      integer(4) :: size_Z
      integer(4) :: sub_X_3D
      integer(4) :: size_X_3D
      integer(4) :: sub_E_3D
      integer(4) :: size_E_3D
                                                                       
                                                                 
                                                                 
                                                 
                                                                       
                                                                 
                                                                 
                                         
                                                                       
                                                                 
                                                                 
                                                                 
                                                                 
                                                                 
                        
                               
      integer(4) :: ipts
      integer(4) :: jpts
                        

                     
                                                 
                                                                       
                                                                 
                                              
      integer(4) :: i
      integer(4) :: j
      integer(4) :: k
      integer(4) :: isize
      integer(4) :: jsize
      integer(4) :: ksize
      integer(4), dimension(1:8) :: req
      integer(4), dimension(1:MPI_STATUS_SIZE,1:8) :: status
      integer(4) :: ierr
                                 
      integer(4) :: iter
      integer(4) :: mdii
      integer(4) :: mdjj
                                                 
      integer(4) :: imin
      integer(4) :: imax
      integer(4) :: ishft
      integer(4) :: jmin
      integer(4) :: jmax
      integer(4) :: jshft
      if (ii.eq.0 .and. Istr.eq.1) then
        imin=Istr-1
      else
        imin=Istr
      endif
      if (ii.eq.NP_XI-1 .and. Iend.eq.Lmmpi) then
        imax=Iend+1
      else
        imax=Iend
      endif
      ishft=imax-imin+1
      if (jj.eq.0 .and. Jstr.eq.1) then
        jmin=Jstr-1
      else
        jmin=Jstr
      endif
      if (jj.eq.NP_ETA-1 .and. Jend.eq.Mmmpi) then
        jmax=Jend+1
      else
        jmax=Jend
      endif
      jshft=jmax-jmin+1
      size_Z=Npts*Npts*(N+1)
      sub_X=(Lm+NSUB_X-1)/NSUB_X
      size_X=(N+1)*Npts*(sub_X+2*Npts)
      sub_E=(Mm+NSUB_E-1)/NSUB_E
      size_E=(N+1)*Npts*(sub_E+2*Npts)
      if(.not. allocated(buf_snd4_3D)) then
      Allocate(buf_snd4_3D(size_Z),  ibuf_sndN_3D(size_X),
     &         buf_snd2_3D(size_Z),
     &         buf_rev4_3D(size_Z),  ibuf_revN_3D(size_X),
     &         buf_rev2_3D(size_Z),
     &        jbuf_sndW_3D(size_E),                     
     &                                             jbuf_sndE_3D(size_E),
     &        jbuf_revW_3D(size_E),                     
     &                                             jbuf_revE_3D(size_E),
     &         buf_snd1_3D(size_Z),  ibuf_sndS_3D(size_X),
     &         buf_snd3_3D(size_Z),
     &         buf_rev1_3D(size_Z),  ibuf_revS_3D(size_X),
     &         buf_rev3_3D(size_Z))
      endif
      ksize=Npts*Npts*nmax
      isize=Npts*ishft*nmax
      jsize=Npts*jshft*nmax
      do iter=0,1
        mdii=mod(ii+iter,2)
        mdjj=mod(jj+iter,2)
        if (mdii.eq.0) then
          if ((WEST_INTER2)) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do j=jmin,jmax
                do ipts=1,Npts
                  jbuf_sndW_3D(k+nmax*(j-jmin+(ipts-1)*jshft))=
     &                  A(ipts,j,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(jbuf_revW_3D,jbuf_sndW_3D)
            call MPI_Irecv (jbuf_revW_3D, jsize, MPI_DOUBLE_PRECISION,
     &                         p_W, 2, MPI_COMM_WORLD, req(1), ierr)
            call MPI_Send  (jbuf_sndW_3D, jsize, MPI_DOUBLE_PRECISION,
     &                         p_W, 1, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        else
          if (EAST_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do j=jmin,jmax
                do ipts=1,Npts
                  jbuf_sndE_3D(k+nmax*(j-jmin+(ipts-1)*jshft))=
     &                                       A(Lmmpi-Npts+ipts,j,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(jbuf_revE_3D,jbuf_sndE_3D)
            call MPI_Irecv (jbuf_revE_3D, jsize, MPI_DOUBLE_PRECISION,
     &                        p_E, 1, MPI_COMM_WORLD, req(2), ierr)
            call MPI_Send  (jbuf_sndE_3D, jsize, MPI_DOUBLE_PRECISION,
     &                        p_E, 2, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        endif
        if (mdjj.eq.0) then
          if (SOUTH_INTER2) then
!$acc parallel loop if(compute_on_device)
!$acc& default(present) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do i=imin,imax
                  ibuf_sndS_3D(k+nmax*(i-imin+(jpts-1)*ishft))=
     &                          A(i,jpts,k)
                enddo
              enddo
            enddo
!$acc update host(ibuf_sndS_3D(1:isize)) if(.not.use_host_device)
!$acc host_data if(compute_on_device.and.use_host_device)
!$acc&   use_device(ibuf_revS_3D,ibuf_sndS_3D)
            call MPI_Irecv (ibuf_revS_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_S, 4, MPI_COMM_WORLD, req(3), ierr)
            call MPI_Send  (ibuf_sndS_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_S, 3, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        else
          if (NORTH_INTER2) then
!$acc parallel loop if(compute_on_device)
!$acc& default(present) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do i=imin,imax
                  ibuf_sndN_3D(k+nmax*(i-imin+(jpts-1)*ishft))=
     &                                         A(i,Mmmpi-Npts+jpts,k)
                enddo
              enddo
            enddo
!$acc update host(ibuf_sndN_3D(1:isize)) if(.not.use_host_device)
!$acc host_data if(compute_on_device.and.use_host_device)
!$acc&   use_device(ibuf_revN_3D,ibuf_sndN_3D)
            call MPI_Irecv (ibuf_revN_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_N, 3, MPI_COMM_WORLD, req(4), ierr)
            call MPI_Send  (ibuf_sndN_3D, isize, MPI_DOUBLE_PRECISION,
     &                         p_N, 4, MPI_COMM_WORLD,         ierr)
!$acc end host_data
          endif
        endif
        if (mdii.eq.0) then
          if (CORNER_SW) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd1_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                 A(ipts,jpts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev1_3D,buf_snd1_3D)
            call MPI_Irecv (buf_rev1_3D, ksize, MPI_DOUBLE_PRECISION, 
     &                                                             p_SW,
     &                                6, MPI_COMM_WORLD, req(5),ierr)
            call MPI_Send  (buf_snd1_3D, ksize, MPI_DOUBLE_PRECISION, 
     &                                                             p_SW,
     &                                5, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        else
          if (CORNER_NE) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd2_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                            A(Lmmpi+ipts-Npts,Mmmpi+jpts-Npts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev2_3D,buf_snd2_3D)
            call MPI_Irecv (buf_rev2_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NE, 5, MPI_COMM_WORLD, req(6),ierr)
            call MPI_Send  (buf_snd2_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NE, 6, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        endif
        if (mdii.eq.1) then
          if (CORNER_SE) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd3_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                                A(Lmmpi+ipts-Npts,jpts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev3_3D,buf_snd3_3D)
            call MPI_Irecv (buf_rev3_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_SE, 8, MPI_COMM_WORLD, req(7),ierr)
            call MPI_Send  (buf_snd3_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_SE, 7, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        else
          if (CORNER_NW) then
!$acc parallel loop if(compute_on_device) collapse(3)
            do k=1,nmax
              do jpts=1,Npts
                do ipts=1,Npts
                  buf_snd4_3D(k+nmax*(ipts-1+Npts*(jpts-1)))=
     &                                A(ipts,Mmmpi+jpts-Npts,k)
                enddo
              enddo
            enddo
!$acc host_data if(compute_on_device)
!$acc&   use_device(buf_rev4_3D,buf_snd4_3D)
            call MPI_Irecv (buf_rev4_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NW, 7, MPI_COMM_WORLD, req(8),ierr)
            call MPI_Send  (buf_snd4_3D, ksize, MPI_DOUBLE_PRECISION,
     &                  p_NW, 8, MPI_COMM_WORLD,        ierr)
!$acc end host_data
          endif
        endif
      enddo
      if (WEST_INTER2) then
        call MPI_Wait (req(1),status(1,1),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
             A(ipts-Npts,j,k)=
     &          jbuf_revW_3D(k+nmax*(j-jmin+(ipts-1)*jshft))
            enddo
          enddo
        enddo
      endif
      if (WEST_INTER .and. .not. WEST_INTER2) then
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(ipts-Npts,j,k)=A(ipts,j,k)
            enddo
          enddo
        enddo
      endif
      if (EAST_INTER2) then
        call MPI_Wait (req(2),status(1,2),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(Lmmpi+ipts,j,k)=
     &                         jbuf_revE_3D(k+nmax*(j-jmin+(ipts-1)
     &                         *jshft))
            enddo
          enddo
        enddo
      endif
      if (EAST_INTER .and. .not. EAST_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do j=jmin,jmax
            do ipts=1,Npts
              A(Lmmpi+ipts,j,k)=A(Lmmpi+ipts-Npts,j,k)
            enddo
          enddo
        enddo
      endif
      if (SOUTH_INTER2) then
        call MPI_Wait (req(3),status(1,3),ierr)
!$acc update device(ibuf_revS_3D(1:isize)) if(.not.use_host_device)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do i=imin,imax
              A(i,jpts-Npts,k)=ibuf_revS_3D(k+nmax*(i-imin+(jpts-1)
     &                        *ishft))
            enddo
          enddo
        enddo
      endif
      if (SOUTH_INTER .and. .not. SOUTH_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do i=imin,imax
            do jpts=1,Npts
              A(i,jpts-Npts,k)=A(i,jpts,k)
            enddo
          enddo
        enddo
      endif
      if (NORTH_INTER2) then
        call MPI_Wait (req(4),status(1,4),ierr)
!$acc update device(ibuf_revN_3D(1:isize)) if(.not.use_host_device)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do i=imin,imax
              A(i,Mmmpi+jpts,k)=
     &          ibuf_revN_3D(k+nmax*(i-imin+(jpts-1)*ishft))
            enddo
          enddo
        enddo
      endif
      if (NORTH_INTER .and. .not. NORTH_INTER2) then
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do i=imin,imax
            do jpts=1,Npts
              A(i,Mmmpi+jpts,k)=A(i,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
      endif
      if (CORNER_SW) then
        call MPI_Wait (req(5),status(1,5),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,jpts-Npts,k)=
     &                           buf_rev1_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_SW  .and.
     &   SOUTH_INTER .and.  WEST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,jpts-Npts,k)=
     &                           A(ipts,jpts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_NE) then
        call MPI_Wait (req(6),status(1,6),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,Mmmpi+jpts,k)=
     &                           buf_rev2_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_NE  .and.
     &   NORTH_INTER .and.  EAST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,Mmmpi+jpts,k)=
     &              A(Lmmpi+ipts-Npts,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_SE) then
        call MPI_Wait (req(7),status(1,7),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(Lmmpi+ipts,jpts-Npts,k)=
     &                           buf_rev3_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_SE .and.
     &   SOUTH_INTER .and.  EAST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
               A(Lmmpi+ipts,jpts-Npts,k)=
     &                           A(Lmmpi+ipts-Npts,jpts,k)
            enddo
          enddo
        enddo
       endif
      if (CORNER_NW) then
        call MPI_Wait (req(8),status(1,8),ierr)
!$acc parallel loop if(compute_on_device) collapse(3)
        do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,Mmmpi+jpts,k)=
     &                           buf_rev4_3D(k+nmax*(ipts-1+Npts*(jpts-
     &                                                              1)))
            enddo
          enddo
        enddo
      endif
      if (.not. CORNER_NW  .and.
     &   NORTH_INTER .and.  WEST_INTER ) then
!$acc parallel loop if(compute_on_device) collapse(3)
       do k=1,nmax
          do jpts=1,Npts
            do ipts=1,Npts
              A(ipts-Npts,Mmmpi+jpts,k)=A(ipts,Mmmpi+jpts-Npts,k)
            enddo
          enddo
        enddo
       endif
      return
       
      

      end subroutine Sub_Loop_MessPass3D_3pts_tile

