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

  type(type_mre_r16), 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(8), allocatable ::r(:),p(:)
  real(16), allocatable ::r16(:),p16(:),x16(:)
  real(16)          :: rtr, interval, rtr0, bb
  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_r16")
  TIMING_START(timing_bin_cg_mix)

  allocate(r(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(p(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(r16(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(p16(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(x16(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)

  iter4 = 0

  bb = 0
  !$omp parallel do reduction(+: bb)
  do i = 1, size_sc_field
     bb = bb + (real(b(i),kind=16))**2
  enddo
  call global_sum_vec_r16(1, bb)

  x16=x
  call wdagw_r16(r16, x16, id)
  niter = 1

  rtr = 0
  !$omp parallel do reduction(+: rtr)
  do i = 1, size_sc_field
     r16(i) = b(i) - r16(i)
     rtr = rtr + r16(i)**2
  enddo
  call global_sum_vec_r16(1, rtr)

  if (stop_condition_r16(rtr, real(res,kind=16), bb)) goto 9999
  rtr0=rtr

  do niter = 1, cg_para%maxiter
!!     call mre_get2_r16(solutions, p16, r16, id)
     p=p16
     r=r16
!!     p=r
     call cg_inner(p, r, id, iter4, dble(res_cg_inner(res, dble(rtr0), dble(bb), niter)))
     p16=p
!!     call mre_put(solutions, p16, 0)

     !$omp parallel do
     do i = 1, size_sc_field
        x16(i) = x16(i) + p16(i)
     enddo

     call wdagw_r16(r16, x16, id)
     rtr = 0
     !$omp parallel do reduction(+: rtr)
     do i = 1, size_sc_field
        r16(i) = b(i) - r16(i)
        rtr = rtr + r16(i)**2
     enddo
     call global_sum_vec_r16(1, rtr)

     if (stop_condition_r16(rtr, real(res,kind=16), 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)

  x=x16
  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)
  deallocate(p, STAT = ierr)
  deallocate(r16, STAT = ierr)
  deallocate(p16, STAT = ierr)
  deallocate(x16, STAT = ierr)

  DEBUG2S("End: cg_mix_r16")
stop
end

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