!===============================================================================
!
! global_eo_sum_vec_mpi.F90 - reduction operations in MPI
!
!-------------------------------------------------------------------------------
!
! Copyright (C) Yoshifumi Nakamura
!
! 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 global_eo_sum_vec(n, sum)
  use module_function_decl
  implicit none
  include 'mpif.h'
  integer, intent(in) :: n
  REAL,    intent(inout) :: sum(n)
  REAL                 :: tmpe(n),tmpo(n),sume(n),sumo(n)
  integer  ierror

  TIMING_START(timing_bin_global_sum_vec)

  if (mod(my_pe(),2) == 0)then
     tmpe = sum
     tmpo = 0
  else
     tmpe = 0
     tmpo = sum
  endif

  call mpi_allreduce(tmpe, sume, n, BQCD_REAL, MPI_SUM, MPI_COMM_WORLD, ierror)
  call mpi_allreduce(tmpo, sumo, n, BQCD_REAL, MPI_SUM, MPI_COMM_WORLD, ierror)

  if (mod(my_pe(),2) == 0)then
     sum = sume
  else
     sum = sumo
  endif

  TIMING_STOP(timing_bin_global_sum_vec)
end

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