!===============================================================================
!
! cg_hh.F90 - cg for DD
!
!-------------------------------------------------------------------------------
!
! 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(x, b, id, iter, res) 
  use module_cg
  use module_dd
  use module_function_decl
  implicit none
  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
  REAL          :: ak, bk
  DOUBLE        :: rtr, rtrold, paap, bb, dtmp,dtmp1,dtmp2
  integer       :: i, niter, sc_size ,i8,i4

  character(72) :: msg

!
! needed for checking solution
!
  DOUBLE        :: norm(3)
  integer, save :: headline_check_written = 0
  character(*), parameter :: key_chke = "%cgChkEven"
  character(*), parameter :: key_chko = "%cgChkOdd "
  character(*), parameter :: fmt_chk_h = "(1x, 2a,  a6, 4a20)"
  character(*), parameter :: fmt_chk_b = "(1x, a11, i6, 3e20.10, i20)"

!-------------------------------------------------------------------------------
  DEBUG2S("Start: cg_hh")

  sc_size = 24*ld%lat%volh*gd%lat%volh*2

  bb = 0
  !$omp parallel do reduction(+: bb) private(dtmp)
  do i = 1, sc_size
     dtmp = b(i)
     bb = bb + dtmp**2
  enddo
  call global_sum_vec(1, bb)

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

!  call get_cg_stat2(dd_stat, i8,i4)
!write(0,*)"cg_hh_outer: result d8", niter, i8
!write(0,*)"cg_hh_outer: result d4", niter, i4

  rtrold = 0
  !$omp parallel do reduction(+: rtrold) private(dtmp)
  do i = 1, sc_size
     r(i) = b(i) - r(i)
     p(i) = r(i)
     dtmp = r(i)
     rtrold = rtrold + dtmp**2
  enddo
  call global_sum_vec(1, rtrold)

  bk=0
  if (stop_condition(rtrold, res, bb)) goto 9999
  do niter = 1, cg_para%maxiter
     call unprec_hdagh(aap, p, id)

!  call get_cg_stat2(dd_stat, i8,i4)
!write(0,*)"cg_hh_outer: result d8", niter, i8
!write(0,*)"cg_hh_outer: result d4", niter, i4

     paap = ZERO
     !$omp parallel do reduction(+: paap) private(dtmp1, dtmp2)
     do i = 1, sc_size
        dtmp1 = p(i)
        dtmp2 = aap(i)
        paap = paap + dtmp1 * dtmp2
     enddo
     call global_sum_vec(1, paap)
     if (paap /= 0) ak = rtrold / paap 

     rtr = 0
     !$omp parallel do reduction(+: rtr) private(dtmp)
     do i = 1, sc_size
        x(i) = x(i) + ak * p(i)
        r(i) = r(i) - ak * aap(i)
        dtmp = r(i)
        rtr = rtr + dtmp**2
     enddo
     call global_sum_vec(1, rtr)

#ifdef DEBUG2
if(my_pe()==0 .and. mod(niter,100)==0  )write(0,*)niter,"rtr",rtr
#endif
!if(my_pe()==0 .and. mod(niter,1)==0  )write(0,*)niter,"rtr",rtr

     if (stop_condition(rtr, res, bb)) goto 9999
     if (rtrold /= 0) bk = rtr / rtrold
     rtrold = rtr

     call sc_xpby(p, r, bk)  ! p = r + bk * p
  enddo
  niter = niter - 1

  if (cg_para%log /= 2) then
     write(msg, *) "cg_hh(): no convergence; rtr = ", rtr, RKIND
     call die(msg)
  endif
9999 continue
  iter = niter
#if RKIND == 8
  call update_cg_stat(hh_stat, iter, 0)
#endif
  DEBUG2S("End: cg_hh")
end
!===============================================================================
