!===============================================================================
!
! cg_bg.F90 - cg on BlueGene
!  
!-------------------------------------------------------------------------------
!
! 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/>.
!
!-------------------------------------------------------------------------------
! I tested cg.F90 and cg_bg.F90 2 times
! with input.001 both on 4x4x4x4 and 8x4x4x4 on bgp 1 core 
! and did not see gain
!                                             s    Mflop/s
!cg.F90
!001_4444.0: CG                    143     6.52     595.41
!001_4444.1: CG                    143     6.52     595.41
!001_8444.0: CG                    143    13.96     570.76
!001_8444.1: CG                    143    13.96     570.76
!
!cg_bg.F90
!001_4444_cgbg.0: CG               143     6.52     595.40
!001_4444_cgbg.1: CG               143     6.52     595.41
!001_8444_cgbg.0: CG               143    13.95     570.76
!001_8444_cgbg.1: CG               143    13.96     570.76
!
! 12.Feb.2010, Y.N.
!
!-------------------------------------------------------------------------------
subroutine cg(x, b, id, iter, res)
  use module_function_decl
  use module_check_solution
  use module_action
  use module_cg
  use module_vol
  use module_counter
  implicit none

  complex(8), dimension(4*3*volh_tot), intent(inout) :: x
  complex(8), dimension(4*3*volh_tot), intent(in) :: b
  integer, intent(out) :: iter
  integer, intent(in) :: id
  real(8), intent(in) :: res
  complex(8), dimension(4*3*volh_tot) :: r, p, aap

  real(8) :: ak, bk
  real(8) :: rtr, rtrold, paap, bb, dtmp, dtmp1, dtmp2
  integer :: i, niter, ierr
  character(72) :: msg
  complex(8) :: ctmp
!
! needed for checking solution
!
  real(8) :: norm(3)
  integer, save :: headline_check_written = 0
  character(*), parameter :: key_chk = "%cgChk"
  character(*), parameter :: fmt_chk_h = "(1x, 2a, a6, 4a20)"
  character(*), parameter :: fmt_chk_b = "(1x, a7, i6, 3e20.10, i20)"

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

  call alignx(16, x(1))
  call alignx(16, b(1))
 
  call timing_start(11)

  call alignx(16, ctmp)
  ctmp = 0
  do i = 1, size_sc_field/2
     ctmp =fpmadd(ctmp,b(i),b(i))
  enddo
  bb=dreal(ctmp)+dimag(ctmp)
  call global_sum_vec(1, bb)
 
  call wdagw(r, x, id); niter = 1

  call alignx(16, r(1))
  call alignx(16, b(1))
  call alignx(16, p(1))
  r(1:4*3*volh)=b(1:4*3*volh)-r(1:4*3*volh)
  p(1:4*3*volh)=r(1:4*3*volh)
  call alignx(16, ctmp)
  ctmp = 0
  call alignx(16, r(1))
  do i = 1, size_sc_field/2
     ctmp =fpmadd(ctmp,r(i),r(i))
  enddo
  rtrold=dreal(ctmp)+dimag(ctmp)
  call global_sum_vec(1, rtrold)
  if (stop_condition(rtrold, res, bb)) goto 9999

 
  bk=0.0_8
  do niter = 1, cg_para%maxiter
     call wdagw(aap, p, id)
     call alignx(16, ctmp)
     ctmp = 0
     call alignx(16, p(1))
     call alignx(16, aap(1))
     do i = 1, size_sc_field/2
        ctmp =fpmadd(ctmp,aap(i),p(i))
     enddo
     paap=dreal(ctmp)+dimag(ctmp)
     call global_sum_vec(1, paap)
     ak = rtrold / paap

     call alignx(16, p(1))
     call alignx(16, aap(1))
     call alignx(16, x(1))
     call alignx(16, r(1))
     do i = 1, size_sc_field/2
        x(i)=fxcpmadd( x(i),  p(i),ak)
        r(i)=fxcpnmsub(r(i),aap(i),ak)
     enddo

     call alignx(16, ctmp)
     ctmp = 0
     call alignx(16, r(1))
     do i = 1, size_sc_field/2
        ctmp =fpmadd(ctmp,r(i),r(i))
     enddo
     rtr=dreal(ctmp)+dimag(ctmp)
     call global_sum_vec(1, rtr)

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

     do i = 1, size_sc_field/2
        p(i)=fxcpmadd( r(i),  p(i),bk)
     enddo
  enddo

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

9999 continue

  call update_cg_stat(cg_stat, niter, 0)
  cg_iterations_total = cg_stat%niter_tot_run
  iter = niter

  call timing_stop(11)

  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(6, fmt_chk_h) &
               "T", key_chk, "traj", "rtr", "Rb", "Rx", "iterations"
        endif
        headline_check_written = 1
     endif

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

end

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