!===============================================================================
!
! blockbicggr.F90 - solves "matrix_mult * X = B"
!
!-------------------------------------------------------------------------------
!
! 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 bloclbicggr(x, b, id, iter, res, nblock)
  use module_function_decl
  use module_check_solution
  use module_action
  use module_cg
  use module_vol
  use module_counter
  implicit none

  real(8), dimension(2*4*3*volh_tot,nblock), intent(inout) :: x
  real(8), dimension(2*4*3*volh_tot,nblock), intent(in) :: b
  integer,              intent(out)   :: iter
  integer,              intent(in)    :: id
  DOUBLE,               intent(in)    :: res

  real(8), allocatable ::r0(:,:),r(:,:),p(:,:),aap(:,:)
  integer       :: i, niter, ierr
  character(72) :: msg
  integer, save :: nitert=0

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

  DEBUG2S("Start: cg")
  TIMING_START(timing_bin_cg)

  allocate(r(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(p(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)
  allocate(aap(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot), STAT = ierr)

  niter = 0

  do ib = 1, nblock
     call wdagw(r0(1,ib), x(1,ib), id) 
     niter = niter +1
     nitert= nitert+1

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


!!  if (stop_condition(rtrold, res, bb)) goto 9999

  do ib = 1, nblock
     !! precon F=MR

     !$omp parallel do
     do i = 1, size_sc_field
        f(i,ib) = r(i,ib)
     enddo
     
     call wdagw(w(1,ib), f(1,ib), id) 
     niter = niter +1
     nitert= nitert+1

     !$omp parallel do
     do i = 1, size_sc_field
        v(i,ib) = w(i,ib)
     enddo
  enddo

  do niter = 1, cg_para%maxiter

     do ib = 1, nblock
        wr=wr+sc_cdotc(w(1,ib),r(1,ib))
        ww=ww+sc_norm2(w(1,ib))
        do jb = 1, nblock
           rv(ib,jb)=sc_cdotc(r(1,ib),v(1,jb))
           rr(ib,jb)=sc_cdotc(r(1,ib),r(1,jb))
        enddo
     enddo
     !! globalsum
     xi=1
     alpha=1

     do ib = 1, nblock
        !$omp parallel do
        do i = 1, size_sc_field
           s(i,ib) = p(i,ib) - zeta * v(i,ib)
        enddo
     enddo

     do ib = 1, nblock
        do i = 1, size_sc_field
           tmp=0
           do jb = 1, nblock
              tmp = tmp + s(i,jb) * alpha(jb,ib)
           enddo
           u(i,ib) = tmp
        enddo
     enddo





     call wdagw(aap, p, id) ; nitert= nitert+1
     paap = ZERO
     !$omp parallel do reduction(+: paap) private(dtmp1, dtmp2)
     do i = 1, size_sc_field
        dtmp1 = p(i)
        dtmp2 = aap(i)
        paap = paap + dtmp1 * dtmp2
     enddo
     call global_sum_vec(1, paap)
     ak = rtrold / paap 

     rtr = 0.0_8
     !$omp parallel do reduction(+: rtr) private(dtmp)
     do i = 1, size_sc_field
        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)


if(my_pe()==0 .and. mod(niter,100)==0  )write(0,*)"cg res:",nitert, niter, rtr
#ifdef DEBUG2
if(my_pe()==0 .and. mod(niter,100)==0  )write(0,*)"cg res:",niter, rtr
#endif

     if (stop_condition(rtr, res, bb)) goto 9999
     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
if(my_pe()==0) write(0,*)"no conv=============="
if(my_pe()==0) write(0,*)"p(1)=",p(1)
     call wdagw_r8(aap, p, id)
if(my_pe()==0) write(0,*)"wdagw_r8 aap(1)=",aap(1)
if(my_pe()==0) write(0,*)"wdagw_r8 aap(2)=",aap(2)
if(my_pe()==0) write(0,*)"wdagw_r8 aap(3)=",aap(3)
if(my_pe()==0) write(0,*)"wdagw_r8 aap(4)=",aap(4)
     call wdagw(aap, p, id)
if(my_pe()==0) write(0,*)"wdagw    aap(1)=",aap(1)
if(my_pe()==0) write(0,*)"wdagw    aap(2)=",aap(2)
if(my_pe()==0) write(0,*)"wdagw    aap(3)=",aap(3)
if(my_pe()==0) write(0,*)"wdagw    aap(4)=",aap(4)


     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
  if(my_pe()==0 )write(0,*)"CG done! res:",nitert, niter, rtr

  TIMING_STOP(timing_bin_cg)

  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"
        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), iter
     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
  deallocate(aap, STAT = ierr)
  if (ierr /= 0) then
     call stderr2_int("deallocation failed", 1, ierr)
     call die("cg: deallocation failed aap")
  endif

  DEBUG2S("End: cg")
end

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