!===============================================================================
!
! cg_copy_clover.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2012-2015 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"
# include "cg_clover.i"
# include "simd.h"

FOR _cc in _c1 _c2
FOR rkind in 4 8
SET _rr = _r %% rkind
SET _rrs = _rr %% s
SET SIMD_WIDTH = SIMD_WIDTH %% $(toupper _rr)
!-------------------------------------------------------------------------------
# undef TRACE_NAME
# define TRACE_NAME cg_copyin%%_cc%%_rr
!-------------------------------------------------------------------------------
subroutine cg_copyin%%_cc%%_rr(out, in)

  use typedef_cg_clover
  use module_vol
  use module_switches
  implicit none
  type(type_clover_c1_r8) :: in(2, volh)
  type(type_clover%%_cc%%_rr) :: out(2, volh)

  integer :: i, j

  if (.not. switches%clover) return

  TRACE_ENTRY

  !$omp parallel do
  do i = 1, volh
  IF [[ _cc = _c1 ]]
  DO K = 1, 2
  DO I = 1, 6
  DO J = I, 6
     Clover_c1(out, K, i, I, J) = Clover_c1(in, K, i, I, J)
  ENDDO
  ENDDO
  ENDDO
  ENDIF
  IF [[ _cc = _c2 ]]
  DO K = 1, 2
  DO I = 1, 6
     ReClover_c2(out, K, i, I, I) = Clover_c1(in, K, i, I, I)
  DO J = I+1, 6
     ReClover_c2(out, K, i, I, J) = real(Clover_c1(in, K, i, I, J))
     ImClover_c2(out, K, i, I, J) = aimag(Clover_c1(in, K, i, I, J))
  ENDDO
  ENDDO
  ENDDO
  ENDIF
  enddo

  TRACE_RETURN
end

!-------------------------------------------------------------------------------
# undef TRACE_NAME
# define TRACE_NAME cg_copyin%%_cc%%_rrs
!-------------------------------------------------------------------------------
subroutine cg_copyin%%_cc%%_rrs(out, in)

  use typedef_cg_clover
  use module_cg_vol
  use module_vol
  use module_switches
  implicit none
  type(type_clover_c1_r8) :: in(2, volh)
  type(type_clover%%_cc%%_rrs) :: out(2, volh%%_rrs)

  integer :: i, j, w

  if (.not. switches%clover) return

  TRACE_ENTRY

  !$omp parallel do private(i, w)
  do j = 0, volh%%_rrs - 1
     i = j * SIMD_WIDTH
     do w = 1, SIMD_WIDTH
     IF [[ _cc = _c1 ]]
     DO K = 1, 2
     DO I = 1, 6
        Clover_c1(out, K, j+1, I, I)%r(w) = Clover_c1(in, K, i+w, I, I)
     DO J = I+1, 6
        Clover_c1(out, K, j+1, I, J)%r(w) = real(Clover_c1(in, K, i+w, I, J))
        Clover_c1(out, K, j+1, I, J)%i(w) = aimag(Clover_c1(in, K, i+w, I, J))
     ENDDO
     ENDDO
     ENDDO
     ENDIF
     IF [[ _cc = _c2 ]]
     DO K = 1, 2
     DO I = 1, 6
        ReClover_c2(out, K, j+1, I, I)(w) = Clover_c1(in, K, i+w, I, I)
     DO J = I+1, 6
        ReClover_c2(out, K, j+1, I, J)(w) = real(Clover_c1(in, K, i+w, I, J))
        ImClover_c2(out, K, j+1, I, J)(w) = aimag(Clover_c1(in, K, i+w, I, J))
     ENDDO
     ENDDO
     ENDDO
     ENDIF
     enddo
  enddo

  TRACE_RETURN
end

ENDSET
ENDSET
ENDSET
ENDFOR
ENDFOR
!===============================================================================
