!===============================================================================
!
! cg_d35.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2016 Hinnerk Stueben
!
! 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/>.
!
!-------------------------------------------------------------------------------
# define LANGUAGE_FORTRAN
# include "defs.h"
# include "cg_defs.h"

!-------------------------------------------------------------------------------
#ifdef DAGGER
# define NAME cg_d35_dag
# define NAME2 cg_d2_dag
#else
# define NAME cg_d35
# define NAME2 cg_d2
#endif

# define TRACE_NAME T(NAME)
!-------------------------------------------------------------------------------
subroutine T(NAME)(e, o, out, in, u, a)

!// out is of type "e" = EVEN or ODD
!// in is of type "o" = ODD or EVEN

  use typedef_simd
  use T(module_nn)
  use T(module_vol)
  use T(module_threads)
  use module_function_decl
  implicit none
 
  integer, intent(in)          :: e, o
  spincol_FIELD, intent(out)   :: out
  spincol_FIELD, intent(inout) :: in   ! 'xbound' modifies 'in'
  g1_FIELD, intent(in)         :: u
  sc2_FIELD, intent(out)       :: a    ! scratch array

#ifdef DAGGER
  integer, parameter :: dag = 1
#else
  integer, parameter :: dag = 0
#endif
  integer :: thread, i1, i2
  logical :: comm_thread, timing_thread

  TRACE_ENTRY
  TIMING_START(timing_bin_cg_d)

  !$omp parallel default(none) &
  !$omp shared(e, o, out, in, u, a, nn, volh_tot) &
  !$omp shared(i_start, i_end) &
  !$omp private(thread, i1, i2, comm_thread, timing_thread)

  thread = my_thread()
  comm_thread = (thread == 0)
  timing_thread = (thread == 1)
  i1 = i_start(thread)
  i2 = i_end(thread)

  if (comm_thread) then
     call T(cg_proj_xbound)(in, a, 4, dag)
  else
     call T(cg_proj)(a, in, 3, dag)
  endif
  !$omp barrier

  if (comm_thread) then
     call cg_proj_wait(4)
     call T(cg_proj_xbound)(in, a, 3, dag)
  else
     call T(cg_proj)(a, in, 2, dag)
     if (timing_thread) TIMING_START(timing_bin_cg_d_x)
#ifdef SIMD
     call T(STRCAT(NAME2, _x1_block))(out, in, u(1,1,1,e,1), u(1,1,1,e,5), nn(1,e,1,FWD), nn(1,e,1,BWD), volh_tot, i1, i2)
#else
     call T(STRCAT(NAME2, _x1_block))(out, in, u(1,1,1,e,1), u(1,1,1,o,1), nn(1,e,1,FWD), nn(1,e,1,BWD), volh_tot, i1, i2)
#endif
     if (timing_thread) TIMING_STOP(timing_bin_cg_d_x)
  endif
  !$omp barrier

  if (comm_thread) then
     call cg_proj_wait(3)
     call T(cg_proj_xbound)(in, a, 2, dag)
  else
     if (timing_thread) TIMING_START(timing_bin_cg_d_t)
     call T(STRCAT(NAME2, _t_block))(out, in, u(1,1,1,e,4), u(1,1,1,o,4), nn(1,e,4,FWD), nn(1,e,4,BWD), volh_tot, i1, i2)
     if (timing_thread) TIMING_STOP(timing_bin_cg_d_t)
  endif
  !$omp barrier

  if (comm_thread) then
     call cg_proj_wait(2)
  else
     if (timing_thread) TIMING_START(timing_bin_cg_d_z)
     call T(STRCAT(NAME2, _z_block))(out, in, u(1,1,1,e,3), u(1,1,1,o,3), nn(1,e,3,FWD), nn(1,e,3,BWD), volh_tot, i1, i2)
     if (timing_thread) TIMING_STOP(timing_bin_cg_d_z)
  endif
  !$omp barrier

  if (.not. comm_thread) then
     if (timing_thread) TIMING_START(timing_bin_cg_d_y)
     call T(STRCAT(NAME2, _y_block))(out, in, u(1,1,1,e,2), u(1,1,1,o,2), nn(1,e,2,FWD), nn(1,e,2,BWD), volh_tot, i1, i2)
     if (timing_thread) TIMING_STOP(timing_bin_cg_d_y)
  endif

  !$omp end parallel

  TIMING_STOP(timing_bin_cg_d)
  TRACE_RETURN
end

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