!===============================================================================
!
! xbound_sc22_mpi.F90 - boundary exchange with MPI
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2012-2013 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/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine xbound_sc22_field(a)

  use module_function_decl
  use module_vol
  use module_input
  implicit none

  SC22_FIELD :: a

  if (num_pes() == 1) return

  TIMING_START(timing_bin_xbound_sc)

  if (input%tuning_xbound_sc_i == 0) then
     call xbound_sc22(a, 1)
     call xbound_sc22(a, 2)
     call xbound_sc22(a, 3)
     call xbound_sc22(a, 4)
  else
     call xbound_sc22_field_i(a)
  endif

  TIMING_STOP(timing_bin_xbound_sc)

end

!-------------------------------------------------------------------------------
subroutine xbound_sc22_field_i(a)  ! "i"mmediate MPI calls

  use module_xbound_sc2
  use module_function_decl
  use module_vol
  implicit none

  integer :: mu, fb, i, n_request
  SC22_FIELD :: a

  if (num_pes() == 1) return

  n_request = 0

  do mu = 1, DIM
  do fb = FWD, BWD
     i = i_bound(mu, fb)
     if (i /= 0) then
        n_request = n_request + 1
        call mpi_irecv( &
         a(1,1, b(i)%i_target,1), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
        n_request = n_request + 1
        call mpi_irecv( &
         a(1,1, b(i)%i_target,2), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
     endif
  enddo
  enddo

  do mu = 1, DIM
  do fb = FWD, BWD
     i = i_bound(mu, fb)
     if (i /= 0) then
        n_request = n_request + 1
        call mpi_isend( &
         a(1,1, b(i)%i_source,1), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
        n_request = n_request + 1
        call mpi_isend( &
         a(1,1, b(i)%i_source,2), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
     endif
  enddo
  enddo

  call mpi_waitall(n_request, request, status, ierror)
end

!-------------------------------------------------------------------------------
subroutine xbound_sc22(a, direction)

  use module_xbound_sc2
  use module_function_decl
  use module_vol
  implicit none

  integer :: i, direction, mu, fb
  SC22_FIELD :: a

  if (num_pes() == 1) return

#ifdef TIMING
  if (direction == 1) TIMING_START(timing_bin_xbound_sc_x)
  if (direction == 2) TIMING_START(timing_bin_xbound_sc_y)
  if (direction == 3) TIMING_START(timing_bin_xbound_sc_z)
  if (direction == 4) TIMING_START(timing_bin_xbound_sc_t)
#endif

  mu = direction
  do fb = FWD, BWD
     i = i_bound(mu, fb)
     if (i /= 0) then
        call mpi_sendrecv( &
         a(1,1, b(i)%i_source,1), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         a(1,1, b(i)%i_target,1), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, status, ierror)
        call mpi_sendrecv( &
         a(1,1, b(i)%i_source,2), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         a(1,1, b(i)%i_target,2), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, status, ierror)
     endif
  enddo

#ifdef TIMING
  if (direction == 1) TIMING_STOP(timing_bin_xbound_sc_x)
  if (direction == 2) TIMING_STOP(timing_bin_xbound_sc_y)
  if (direction == 3) TIMING_STOP(timing_bin_xbound_sc_z)
  if (direction == 4) TIMING_STOP(timing_bin_xbound_sc_t)
#endif

end

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