!===============================================================================
!
! cg_wrapper_cg.F90 - wrapper for cg in BQCDi
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2016 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"

!-------------------------------------------------------------------------------
# define TRACE_NAME cg_wrapper_cg
!-------------------------------------------------------------------------------
subroutine cg_wrapper_cg(x, b, id, iter, res) 

  use module_vol
  use module_action
  use module_field
  use module_cg_arrays
  use module_cg_conf
  use module_cg_procs
  implicit none

  SPINCOL_OVERINDEXED,  intent(inout) :: x
  SPINCOL_OVERINDEXED,  intent(in)    :: b
  integer,              intent(out)   :: iter
  integer,              intent(in)    :: id
  DOUBLE,               intent(in)    :: res

  integer :: cid

!-------------------------------------------------------------------------------

  TRACE_ENTRY

  cid = action%mtilde(id)%cid

  call cg_init_modules()

  call cg_procs%copyin_gauge(cg_conf%u, gauge(2)%u)

  call cg_procs%copyin_spincol(cg_conf%in, b)
  call cg_procs%copyin_spincol(cg_conf%out, x)

  call cg_procs%copyin_clover(cg_conf%c_even, clover(cid)%cfield%i(1,1,EVEN))
  call cg_procs%copyin_clover(cg_conf%c_odd, clover(cid)%cfield%i(1,1,ODD))

  call cg_procs%kernel_cg(cg_conf%out, cg_conf%in, id, iter, res) 

  call cg_procs%copyout_spincol(x, cg_conf%out)

  TRACE_RETURN

end

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