################################################################################
#
# Makefile to compile and link C programs with MPI subroutines. Version valid
# for Linux machines with GNU make.
#
# "make" compiles and links the specified main programs and modules, using the
# specified libraries (if any), and produces the executables.
#
# "make clean" removes all files generated by "make".
#
# Dependencies on included files are automatically taken care of.
#
################################################################################

all: rmxeq mkdep mkxeq
.PHONY: all


# main programs and modules to be compiled

MAIN = cvt1 ms1 ms2 ms3 ms4 qcd1 qcd2 xms1 xms2 ym1 ym2

ARCHIVE = archive iodat marchive sarchive

BLOCK = block blk_grid map_u2blk map_sw2blk map_s2blk

DFL = dfl_geometry dfl_modes dfl_sap_gcr dfl_subspace ltl_gcr

DFT = dft4d dft_com dft_shuf dft_wspace fft small_dft

DIRAC = Dw_dble Dw Dw_bnd

FLAGS = flags action_parms dfl_parms dft4d_parms dft_parms force_parms \
        hmc_parms lat_parms mdint_parms parms rat_parms rw_parms sap_parms \
        smd_parms solver_parms wflow_parms

FORCES = force0 force1 force2 force3 force4 force5 \
         frcfcts genfrc tmcg tmcgm xtensor

LATTICE = bcnds blk_geometry ftidx geogen geometry uidx

LINALG = cmatrix cmatrix_dble liealg salg salg_dble valg valg_dble

LINSOLV = cgne fgcr fgcr4vd gcr4v mscg

LITTLE = Aw Aw_blk Aw_com1 Aw_com2 Aw_dble Aw_gen Aw_hop Aw_ops ltl_modes

MDFLDS = mdflds fcom

MSFCTS = farchive fiodat fldops latavg pt2pt

RANDOM = ranlux ranlux_common ranlxs ranlxd gauss

RATFCTS = elliptic zolotarev ratfcts

SAP = blk_solv sap_com sap_gcr sap

SFLDS = scom sdcom sflds Pbnd Pbnd_dble unorm

SU3FCTS = chexp su3prod su3ren cm3x3 random_su3

SW_TERM = pauli pauli_dble swalg swexp swflds sw_term

TCHARGE = ftcom ftensor tcharge ym_action

UFLDS = bstap plaq_sum udcom uflds uinit

UPDATE = chrono counters mdsteps mdint msize hmc rwtm rwtmeo rwrat \
         sanity smd update wsize

UTILS = array endian error futils mutils qsum utils wspace

VFLDS = vflds vinit vcom vdcom

WFLOW = wflow

MODULES = $(ARCHIVE) $(BLOCK) $(DFL) $(DFT) $(DIRAC) $(FLAGS) $(FORCES) \
          $(LATTICE) $(LINALG) $(LINSOLV) $(LITTLE) $(MDFLDS) $(MSFCTS) \
          $(RANDOM) $(RATFCTS) $(SAP) $(SFLDS) $(SU3FCTS) $(SW_TERM) \
          $(TCHARGE) $(UFLDS) $(UPDATE) $(UTILS) $(VFLDS) $(WFLOW)


# Logging option (-mpilog or -mpitrace or -mpianim)

LOGOPTION =


# search path for modules

MDIR = ../modules

VPATH = .:$(MDIR)/archive:$(MDIR)/block:$(MDIR)/dirac:$(MDIR)/dfl:\
          $(MDIR)/dft:$(MDIR)/flags:$(MDIR)/forces:$(MDIR)/lattice:\
          $(MDIR)/linalg:$(MDIR)/linsolv:$(MDIR)/little:$(MDIR)/mdflds:\
          $(MDIR)/msfcts:$(MDIR)/random:$(MDIR)/ratfcts:$(MDIR)/sap:\
          $(MDIR)/sflds:$(MDIR)/su3fcts:$(MDIR)/sw_term:$(MDIR)/tcharge:\
          $(MDIR)/uflds:$(MDIR)/update:$(MDIR)/utils:$(MDIR)/vflds:\
          $(MDIR)/wflow


# additional include directories

INCPATH = $(MPI_INCLUDE) ../include


# additional libraries

LIBS = m

LIBPATH = $(MPI_HOME)/lib


# scheduling and optimization options

CFLAGS = -std=c89 -pedantic -fstrict-aliasing -fopenmp -Wall \
         -Wno-long-long -Wno-format-overflow -Wstrict-prototypes -Werror \
         -O -mfpmath=sse -mno-avx -DAVX -DFMA3 -DPM

LFLAGS = -fopenmp

# These CFLAGS options are recommended if the gcc compiler is used on a
# x86-64 machine supporting AVX and FMA3 instructions. See the README in
# the top directory for alternative choices of the optimization options.
#
# The available debugging flags are
#
# -DCGNE_DBG -DFGCR_DBG -DFGCR4VD_DBG -DGCR4V -DFORCE_DBG -DMSCG_DBG
# -DDFL_MODES_DBG -DMDINT_DBG -DRWRAT_DBG -DIGNORE_ERRORS
#
# Add these (or some of these) to CFLAGS if debugging output is desired.

############################## do not change ###################################

SHELL=/bin/bash
CC=mpicc
CLINKER=$(CC)

PGMS= $(MAIN) $(MODULES)

-include $(addsuffix .d,$(PGMS))


# rule to make dependencies

$(addsuffix .d,$(PGMS)): %.d: %.c Makefile
	@ $(GCC) -ansi $< -MM $(addprefix -I,$(INCPATH)) -o $@


# rule to compile source programs

$(addsuffix .o,$(PGMS)): %.o: %.c Makefile
	$(CC) $< -c $(CFLAGS) $(LOGOPTION) $(addprefix -I,$(INCPATH))


# rule to link object files

$(MAIN): %: %.o $(addsuffix .o,$(MODULES)) Makefile
	$(CLINKER) $< $(addsuffix .o,$(MODULES)) $(LFLAGS) $(LOGOPTION) \
        $(addprefix -L,$(LIBPATH)) $(addprefix -l,$(LIBS)) -o $@


# produce executables

mkxeq: $(MAIN)


# remove old executables

rmxeq:
	@ -rm -f $(MAIN); \
        echo "delete old executables"


# make dependencies

mkdep:  $(addsuffix .d,$(PGMS))
	@ echo "generate tables of dependencies"


# clean directory

clean:
	@ -rm -rf *.d *.o *.alog *.clog *.slog $(MAIN)
.PHONY: clean

################################################################################
