!===============================================================================
!
! cg_hh_mix.F90 - mixed precision cg
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2009 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_hh_mix(x, b, id, iter, res) 
  use module_cg
  use module_dd
  use module_vol
  use module_mre
  use module_function_decl
  implicit none
  type(type_mre), save :: solutions
  REAL,dimension(24*ld%lat%volh*gd%lat%volh*2),intent(inout) :: x
  REAL,dimension(24*ld%lat%volh*gd%lat%volh*2),intent(in)    :: b
  integer,              intent(out)   :: iter
  integer,              intent(in)    :: id
  DOUBLE,               intent(in)    :: res
  REAL,dimension(24*ld%lat%volh*gd%lat%volh*2) :: r,p,aap
  DOUBLE        :: rtr, rtr0, bb, res_inner
  integer       :: i, niter, iter4
  character(72) :: msg
!-------------------------------------------------------------------------------
  DEBUG2S("Start: cg_hh_mix")
  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)

  call unprec_hdagh(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_unprec_hdagh_get2(solutions, p, r, id)
     call cg_hh_inner(p, r, id, iter4, res_cg_inner(res, rtr0, bb, niter))
     call mre_put(solutions, p, 0)

     x=x+p
     call unprec_hdagh(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
#ifdef DEBUG2
if(my_pe()==0 )write(0,*)"hhmix:",niter,"rtr",rtr
#endif
  enddo
  niter = niter - 1

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

9999 continue
  call mre_put(solutions, p, 1)
  iter = niter
  call update_cg_stat(hh_stat, iter, iter4)
  DEBUG2S("End: cg_hh_mix")
end

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