!===============================================================================
!
! cg_comm.F90 - communication
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2012-2015 Hinnerk Stueben
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
# define LANGUAGE_FORTRAN
# include "defs.h"
# include "cg_defs.h"

!-------------------------------------------------------------------------------
subroutine T(cg_comm_fld_isend)(a, mu, fwd_bwd)

  use module_cg_comm_requests
  use T(module_cg_comm)
  use T(module_vol)
  implicit none
  spincol_FLD, intent(in) :: a
  integer, intent(in) :: mu, fwd_bwd

  include 'mpif.h'
  integer :: ierror

  if (cg_comm(mu, fwd_bwd)%send%rank == MPI_PROC_NULL) return

  call mpi_isend( &
      Spincol(a,1,1,1,cg_comm(mu, fwd_bwd)%send%ibuf), &
                      cg_comm(mu, fwd_bwd)%send%ibuf), &
                      cg_comm(mu, fwd_bwd)%send%count, &
                      cg_comm(mu, fwd_bwd)%send%datatype, &
                      cg_comm(mu, fwd_bwd)%send%rank, &
                      0, MPI_COMM_WORLD, request(i_request()), ierror)  
end

!-------------------------------------------------------------------------------
subroutine T(cg_comm_fld_irecv)(a, mu, fwd_bwd)

  use module_cg_comm_requests
  use T(module_cg_comm)
  use T(module_vol)
  implicit none
  spincol_FLD, intent(in) :: a
  integer, intent(in) :: mu, fwd_bwd

  include 'mpif.h'
  integer :: ierror

  if (cg_comm(mu, fwd_bwd)%recv%rank == MPI_PROC_NULL) return

  call mpi_irecv( &
      Spincol(a,1,1,1,cg_comm(mu, fwd_bwd)%recv%ibuf), &
                      cg_comm(mu, fwd_bwd)%recv%count, &
                      cg_comm(mu, fwd_bwd)%recv%datatype, &
                      cg_comm(mu, fwd_bwd)%recv%rank, &
                      0, MPI_COMM_WORLD, request(i_request()), ierror)  
end

!-------------------------------------------------------------------------------
subroutine T(cg_comm_fld_sendrecv)(a, mu, fwd_bwd)

  use T(module_cg_comm)
  use T(module_vol)
  implicit none
  spincol_FLD, intent(in) :: a
  integer, intent(in) :: mu, fwd_bwd

  include 'mpif.h'
  integer :: ierror

  call mpi_sendrecv( &
      Spincol(a,1,1,1,cg_comm(mu, fwd_bwd)%send%ibuf), &
                      cg_comm(mu, fwd_bwd)%send%count, &
                      cg_comm(mu, fwd_bwd)%send%datatype, &
                      cg_comm(mu, fwd_bwd)%send%rank, &
                      0, 
      Spincol(a,1,1,1,cg_comm(mu, fwd_bwd)%recv%ibuf), &
                      cg_comm(mu, fwd_bwd)%recv%count, &
                      cg_comm(mu, fwd_bwd)%recv%datatype, &
                      cg_comm(mu, fwd_bwd)%recv%rank, &
                      0, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierror)  
end

!===============================================================================
