!===============================================================================
!
! xbound_u1gen_mpi.F90 - boundary exchange of U(1) generator field with MPI 
!                        in this file 'a_qed' is called 'u'
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 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"

!-------------------------------------------------------------------------------
module module_xbound_u1gen

  use module_xbound
  implicit none
  integer, save            :: n_bound = 0
  type (type_xbound), save :: b(max_bound)
end

!-------------------------------------------------------------------------------
subroutine init_xbound_u1gen()

  use module_xbound_u1gen
  use module_switches
  implicit none

  integer :: x, y, z, t

  if (.not. switches%qed) return

  do t = -1, 1
  do z = -1, 1
  do y = -1, 1
  do x = -1, 1
     call init_xch_bound(n_bound, b, 1, x, y, z, t)
  enddo
  enddo
  enddo
  enddo
end

!-------------------------------------------------------------------------------
subroutine xbound_u1gen_field(u)

  use module_function_decl
  use module_vol
  use module_switches
  implicit none

  U1_GENERATOR_FIELD, intent(inout) :: u
  integer :: mu, eo

  if (.not. switches%qed) return
  if (num_pes() == 1) return

  TIMING_START(timing_bin_xbound_u1gen)

  do mu = 1, DIM
  do eo = EVEN, ODD
     call xbound_u1gen(u, eo, mu)
  enddo
  enddo

  TIMING_STOP(timing_bin_xbound_u1gen)

end

!-------------------------------------------------------------------------------
subroutine xbound_u1gen(u, eo, mu)

  use module_function_decl
  use module_input
  use module_vol
  use module_switches
  implicit none

  U1_GENERATOR_FIELD, intent(inout) :: u
  integer, intent(in) :: eo, mu

  if (.not. switches%qed) return
  if (num_pes() == 1) return

  if (input%tuning_xbound_g_i == 0) then  ! take same mode as in 'xbound_g'
     call xbound_u1gen_sr(u, eo, mu)
  else
     call xbound_u1gen_i(u, eo, mu)
  endif

end

!-------------------------------------------------------------------------------
subroutine xbound_u1gen_sr(u, eo, mu)  ! sendrecv 

  use module_xbound_u1gen
  use module_vol
  implicit none

  U1_GENERATOR_FIELD, intent(inout) :: u
  integer, intent(in) :: eo, mu
  integer :: i

  do i = 1, n_bound
     call mpi_sendrecv( &
         u(b(i)%i_source, eo,mu), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         u(b(i)%i_target, eo,mu), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, status, ierror)
  enddo

end

!-------------------------------------------------------------------------------
subroutine xbound_u1gen_i(u, eo, mu)  ! 'i'mmediate MPI calls

  use module_xbound_u1gen
  use module_vol
  implicit none

  U1_GENERATOR_FIELD, intent(inout) :: u
  integer, intent(in) :: eo, mu
  integer :: i, n_request

  n_request = 0

  do i = 1, n_bound
     n_request = n_request + 1
     call mpi_irecv( &
         u(b(i)%i_target, eo,mu), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)

     n_request = n_request + 1
     call mpi_isend( &
         u(b(i)%i_source, eo,mu), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
  enddo

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

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