!===============================================================================
!
! module_cg_comm_requests.F90 - MPI 'requests'
!
!-------------------------------------------------------------------------------
!
! 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"

!-------------------------------------------------------------------------------
module module_cg_comm_requests

  implicit none
  
  integer, parameter :: max_request = DIM * 2 * 2
  integer, save      :: request(max_request)
  integer, save      :: n_request = 0

contains

  integer function i_request()
    n_request = n_request + 1
    if (n_request <= max_request) then
       i_request = n_request
    else
       call die("module_cg_comm_requests: too many requests")
    endif
  end function i_request

  subroutine reset_requests()
    n_request = 0
  end subroutine reset_requests

end

!-------------------------------------------------------------------------------
subroutine cg_comm_waitall()

  use module_cg_comm_requests
  implicit none
  include 'mpif.h'
  integer :: ierror
  
  if (n_request == 0) return

  call mpi_waitall(n_request, request, MPI_STATUS_IGNORE, ierror)
  call reset_requests()

end

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