!===============================================================================
!
! cg_mix.F90 - mixed precision cg
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2008 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 cg_mix(x, b, id, iter, iter4, res) 
  use module_function_decl
  use module_check_solution
  use module_field
  use module_action
  use module_mre
  use      module_cg
  use      module_counter
  use      module_vol
  implicit none

  type(type_mre), save :: solutions

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

  REAL, allocatable ::r(:),p(:)
  REAL          :: rtr, interval, rtr0, bb, res_inner
  integer       :: i, niter, ierr
  character(72) :: msg

!
! needed for checking solution
!
  REAL          :: norm(3)
  integer, save :: headline_check_written = 0
  character(*), parameter :: key_chk = "%cgmixChk"
  character(*), parameter :: fmt_chk_h = "(1x, 2a,  a6, 5a20)"
  character(*), parameter :: fmt_chk_b = "(1x, a10, i6, 3e20.10, 2i20)"

!-------------------------------------------------------------------------------
  DEBUG2S("Start: cg_mix")
  TIMING_START(timing_bin_cg_mix)

  allocate(r(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  if (ierr /= 0) then
     call stderr2_int("allocation failed", 1, ierr)
     call die("cg_mix: allocation failed r")
  endif
  allocate(p(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  if (ierr /= 0) then
     call stderr2_int("allocation failed", 1, ierr)
     call die("cg_mix: allocation failed p")
  endif

  iter4 = 0

  bb = ZERO
  !$omp parallel do reduction(+: bb)
  do i = 1, size_sc_field
     bb = bb + b(i)**2
  enddo
  call global_sum_vec(1, bb)

  DEBUG2R("norm(b)=   ",  sqrt(bb)*HALF/action%mtilde(id)%kappa)
  DEBUG2R("rsd    =   ",  res**2*bb/(TWO*action%mtilde(id)%kappa)**2)


  call wdagw_r8(r, x, id)
  niter = 1

  rtr = ZERO
  !$omp parallel do reduction(+: rtr)
  do i = 1, size_sc_field
     r(i) = b(i) - r(i)
     rtr = rtr + r(i)**2
  enddo
  call global_sum_vec(1, rtr)

  if (stop_condition(rtr, res, bb)) goto 9999
  rtr0=rtr

  do niter = 1, cg_para%maxiter
     call mre_get2(solutions, p, r, id)
     res_inner=res_cg_inner(res, rtr0, bb, niter)

     call cg_inner(p, r, id, iter4, res_cg_inner(res, rtr0, bb, niter))
     call mre_put(solutions, p, 0)

     !$omp parallel do
     do i = 1, size_sc_field
        x(i) = x(i) + p(i)
     enddo

     call wdagw_r8(r, x, id)
     rtr = ZERO
     !$omp parallel do reduction(+: rtr)
     do i = 1, size_sc_field
        r(i) = b(i) - r(i)
        rtr = rtr + r(i)**2
     enddo
     call global_sum_vec(1, rtr)

     if (stop_condition(rtr, res, bb)) goto 9999
  enddo

  niter = niter - 1

  if (cg_para%log /= 2) then
     write(msg, *) "cg(): no convergence; rtr = ", rtr 
     call die(msg)
  endif

9999 continue

  call mre_put(solutions, p, 1)
  call update_cg_stat(cg_stat, niter, 0)

  iter = niter

  TIMING_STOP(timing_bin_cg_mix)

  if (cg_para%check /= 0) then  ! check solution

     call check_solution(id, x, b, norm)

     if (headline_check_written == 0) then
        if (my_pe() == 0) then
           write(UREC, fmt_chk_h) &
               "T", key_chk, "traj", "rtr", "Rb", "Rx", "iterations", "iter4"
        endif

        headline_check_written = 1
     endif

     if (my_pe() == 0) then
        write(UREC, fmt_chk_b) &
           key_chk, counter%traj, rtr, norm(1)/norm(2), norm(1)/norm(3), &
           niter, iter4
     endif
  endif

  deallocate(r, STAT = ierr)
  if (ierr /= 0) then
     call stderr2_int("deallocation failed", 1, ierr)
     call die("cg: deallocation failed r")
  endif
  deallocate(p, STAT = ierr)
  if (ierr /= 0) then
     call stderr2_int("deallocation failed", 1, ierr)
     call die("cg: deallocation failed p")
  endif

  DEBUG2S("End: cg_mix")
end

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