#  Clover inverter with spectroscopy
#  MIMD version 7

#  Generic make-include file for clover_invert2 codes
#  For BOTH flavor-diagonal and mixed flavor {heavy-light} codes
#  This template file defines rules and macros common to all architectures
#  It is intended to be an include file for other Makefiles.
#  Don't use it by itself!
#
#  Select the inverter algorithm in the Makefile
#
#	"su3_clov" flavor-diagonal mesons and baryons with
#          bi-conjugate gradient inverter

MYINCLUDEDIR = .
INCLUDEDIR = ../include
#Where the complex and su3 libraries are
LIBDIR = ../libraries

GLOBAL_HEADERS = ${INCLUDEDIR} ../libraries/include

INLINE_HEADERS = \
  ../sse/include/ \
  ../sse2/include/ \
  ../libraries/include/

MY_HEADERS = \
  lattice.h \
  params.h \
  defines.h \
  cl_inv_includes.h

HEADERS = ${GLOBAL_HEADERS} ${MY_HEADERS} ${INLINE_HEADERS} ${SCIDAC_HEADERS}

# All required object files
# Basic set from current directory
MY_OBJECTS =         \
  clover_info.o      \
  control.o          \
  gauge_info.o       \
  ksprop_info.o      \
  make_prop.o        \
  setup.o            \
  spectrum_cl.o

# Basic set from generic directory
G_OBJECTS =          \
  ape_smear.o        \
  check_unitarity.o  \
  d_plaq4.o          \
  gaugefix2.o        \
  io_detect.o        \
  io_helpers.o       \
  io_lat4.o          \
  io_lat_utils.o     \
  io_source_cmplx_fm.o \
  make_lattice.o     \
  nersc_cksum.o       \
  path_product.o     \
  project_su3_hit.o  \
  remap_stdio_from_args.o \
  rephase.o          \
  reunitarize2.o

ifeq ($(strip ${HAVEFFTW}),true)
  G_OBJECTS += remap_fftw_fourier.o
else
  G_OBJECTS += restrict_fourier.o
endif

ifeq ($(strip ${HAVEQIO}),true)
  G_OBJECTS += io_scidac.o io_scidac_types.o
endif

# Set from generic_ks directory
G_KS_OBJECTS =      \
  io_helpers_ks.o   \
  io_prop_ks.o      \
  io_prop_ks_fm.o   \
  ks_source.o       \
  ks_invert.o       \
  ks_utilities.o    \
  mat_invert.o      \
  quark_stuff.o

ifeq ($(strip ${HAVEQIO}),true)
  G_KS_OBJECTS += io_scidac_ks.o
endif

# Basic set from generic_wilson directory
G_WILSON_OBJECTS =   \
  baryon_cont.o      \
  canopy2weyl_rot.o  \
  dirac_utilities.o  \
  discretize_wf.o    \
  dslash_w_space.o   \
  dslash_w3.o        \
  gammas.o           \
  gauss_smear_w.o    \
  io_helpers_w.o     \
  io_prop_w.o        \
  io_prop_w_fm.o     \
  io_source_w_fm.o   \
  staggered2naive.o  \
  w_baryon.o         \
  w_baryon_hl.o      \
  w_meson_mom.o      \
  w_meson_open_mom.o \
  w_source.o         \
  wilson_invert.o

ifeq ($(strip ${HAVEQIO}),true)
  G_WILSON_OBJECTS += io_scidac_w.o
endif

# Basic set from generic_clover directory
G_CLOVER_OBJECTS =   \
  f_mu_nu.o        \
  make_clov2.o

# Objects depending on architecture and software package 

# Selection of layout
ifeq ($(strip ${QCDOC}),true)
  LAYOUT = layout_qcdoc.o # Required
else
  ifeq ($(strip ${HAVEQDP}),true)
    LAYOUT = layout_qdp.o # Required
  else
    LAYOUT = layout_hyper_prime.o # Choices possible here
  endif
endif

# Selection of the Wilson fermion links algorithm
FLINKS_WILSON = ${FLINKS_WILSONQOP}

# Selection of the clover conjugate gradient algorithm
# The following inverters are not supported in QOP
# so we use the plain MILC version:
CONGRAD_CL = \
      cl_solver_utilities.o \
      d_cgilu_cl.o     \
      d_hopilu_cl.o    \
      d_mrilu_cl.o
# The bicgilu inverter is supported, so select it
ifeq ($(strip ${HAVEQOP}),true)
# Interface to access QOP
  CONGRAD_CL += d_bicgilu_cl_qop.o \
     d_bicgilu_cl_qop_D.o d_bicgilu_cl_qop_F.o
  CONGRAD_CL += ${WILSONINVERTQOP}
else
  ifeq ($(strip ${HAVEQDP}),true)
# We don't have a working standalone QDP version of the bicgilu inverter
# so we use the plain MILC code version
    CONGRAD_CL += d_bicgilu_cl.o
  else
# Plain MILC inverter
    CONGRAD_CL += d_bicgilu_cl.o
  endif
endif

CL_OBJECTS = ${FLINKS_WILSON} ${CONGRAD_CL}

# Selection of a non-QDP FN Dslash 
# Choices are dslash_fn.o dslash_fn2.o dslash_fn_dblstore.o
DSLASH_FN = dslash_fn_dblstore.o

# Selection of the fermion links algorithm
ifeq ($(strip ${HAVEQOP}),true)
# Interface to access QOP
  FLINKS_FN = ${FLINKS_FNQOP}
else
  ifeq ($(strip ${HAVEQDP}),true)
    FLINKS_FN = fermion_links_asqtad_qdp.o fermion_links_helpers.o
    FLINKS_FN += map_milc_to_qdp_D.o map_milc_to_qdp_F.o
  else
    FLINKS_FN = fermion_links_fn.o fermion_links_helpers.o
  endif
endif

# Selection of the conjugate gradient algorithm
ifeq ($(strip ${HAVEQOP}),true)
# Interface to access QOP
  CONGRAD_FN = d_congrad5_fn_qop_two_src.o d_congrad5_fn_qop.o \
     d_congrad5_fn_qop_D.o d_congrad5_fn_qop_F.o d_congrad_opt.o
#  CONGRAD_FN = d_congrad5_two_src.o d_congrad5_fn.o d_congrad_opt.o
  CONGRAD_FN += ${ASQINVERTQOP}
else
  ifeq ($(strip ${HAVEQDP}),true)
#    Here you may use the fn_qdp set or the fn_1sum_qdp set
    CONGRAD_FN = d_congrad5_two_src.o d_congrad5_fn_qdp.o \
       d_congrad5_fn_qdp_D.o d_congrad5_fn_qdp_F.o d_congrad_opt.o
    DSLASH_FN += dslash_fn_qdp_D.o dslash_fn_qdp_F.o
  else
    # Choose a non_QDP FN conjugate gradient algorithm
    CONGRAD_FN = d_congrad5_two_src.o d_congrad5_fn.o d_congrad_opt.o
  endif
endif

INV_FN_OBJECTS = ${CONGRAD_FN} ${DSLASH_FN}

# The only choice for inverters and Dslash for EO actions
INV_EO_OBJECTS = d_congrad5_eo.o d_congrad_opt.o dslash_eo.o
MULTI_INV_EO = ks_multicg.o ks_multicg_offset.o

# For EO actions we don't precompute the fat links for Dslash at the moment
FN_OBJECTS = ${FLINKS_FN} ${INV_FN_OBJECTS}
EO_OBJECTS = fermion_links_eo.o ${INV_EO_OBJECTS}

# EDRAM allocation for QCDOC
ifeq ($(strip ${QCDOC}),true)
  QCDOC_OBJECTS = qcdoc_alloc.o
endif

ADD_OBJECTS = ${MACHINE_DEP_IO} ${COMMPKG} ${QCDOC_OBJECTS} ${QDP_OBJECTS} \
   ${SCIDAC_OBJECTS}

# Generic QOP objects
ifeq ($(strip ${HAVEQOP}),true)
  ADD_OBJECTS += ${GENERICQOP}
endif

OBJECTS = ${MY_OBJECTS} ${G_OBJECTS} ${G_KS_OBJECTS} ${G_WILSON_OBJECTS} \
  ${G_CLOVER_OBJECTS} ${LAYOUT} ${CL_OBJECTS} ${ADD_OBJECTS} ${EXTRA_OBJECTS}

#Libraries for complex numbers and su3 functions
P = $(strip ${PRECISION})
QCDLIB = ${LIBDIR}/su3.${P}.a ${LIBDIR}/complex.${P}.a 

LIBRARIES = ${QCDLIB} ${SCIDAC_LIBRARIES}

# Default rule for C compilation
.c.o: ; ${CC} -c ${CFLAGS} $<

# To force a full remake when changing targets
LASTMAKE = .lastmake.${MAKEFILE}.${P}.${MYTARGET}

${LASTMAKE}: ${ALL_MAKES}
	-/bin/rm -f .lastmake.*
	${MAKE} -f ${MAKEFILE} clean
	touch ${LASTMAKE}

# All make files used in this compilation
ALL_MAKES = \
  Make_template \
  ${MAKEFILE} \
  ../Make_template_qop \
  ../Make_template_scidac \
  ../generic/Make_template \
  ../generic_ks/Make_template \
  ../generic_wilson/Make_template \
  ../generic_clover/Make_template \

# To force a remake when changing any make file, header, or target
${OBJECTS} : ${HEADERS} ${LASTMAKE} ${ALL_MAKES}

fermion_links_fn.o: quark_action.h 
gauge_stuff.o: gauge_action.h 
gauge_info.o:  quark_action.h
quark_stuff.o: quark_action.h 

# Directories where we store the improved action headers
QUARKIMP = ../generic_ks/imp_actions

quark_action.h: ${QUARKIMP}/${QUARK}
	/bin/cp ${QUARKIMP}/${QUARK} quark_action.h


ifeq ($(strip ${QCDOC}),true)
  DQCDOC = -DQCDOC
endif

ifeq ($(strip ${HAVEQMP}),true)
  DHAVE_QMP = -DHAVE_QMP
endif

ifeq ($(strip ${HAVEQIO}),true)
  DHAVE_QIO = -DHAVE_QIO
endif

ifeq ($(strip ${HAVEQDP}),true)
  DHAVE_QDP = -DHAVE_QDP
endif

ifeq ($(strip ${HAVEQOP}),true)
  DHAVE_QOP = -DHAVE_QOP
endif

DARCH = ${DQCDOC} ${DHAVE_QMP} ${DHAVE_QIO} ${DHAVE_QDP} ${DHAVE_QOP}

##### Targets:

# Common compiler macros

# Optional DEFINES for timing:
# -DIOTIME for I/O timing
# -DPRTIME for propagator timing
# -DCGTIME for cg timing and iterations

su3_clov::
	${MAKE} -f ${MAKEFILE} target "MYTARGET= $@" \
	"QUARK = asqtad_action.h" \
	"DEFINES= -DFN -DIOTIME -DPRTIME -DCL_CG=BICG" \
	"EXTRA_OBJECTS= ${FN_OBJECTS}"

# Hacks to allow more than one test case
su3_clov2: su3_clov
	   cp su3_clov su3_clov2

su3_clov3: su3_clov
	   cp su3_clov su3_clov3

# Single-processor utilities
tie_open_meson:	tie_open_meson.c ${MAKEFILE}
	gcc tie_open_meson.c -o $@

dump_open_meson: dump_open_meson.c ${MAKEFILE}
	gcc dump_open_meson.c -o $@

clean:
	-/bin/rm -f *.o

# Rules for generic routines
include ../generic/Make_template

# Rules for generic_ks routines
include ../generic_ks/Make_template

# Rules for generic_wilson routines
include ../generic_wilson/Make_template

# Rules for generic_clover routines
include ../generic_clover/Make_template


libmake:
	@echo Building libraries with ${MAKELIBRARIES}
	cd ${LIBDIR} ;\
	${MAKE} -f ${MAKELIBRARIES} "APP_CC=${CC}" "PRECISION=${PRECISION}" all

localmake: ${OBJECTS} ${LIBRARIES}
	${LD} -o ${MYTARGET} ${LDFLAGS} \
	${OBJECTS} ${QCDLIB} ${ILIB} -lm
	touch localmake

target: libmake localmake 
