################################################################################
#
# Makefile to compile and link C programs with MPI subroutines
#
# Version valid for Linux machines with MPICH
#
# "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 = time1 time2 time3

RANDOM = ranlxs ranlxd gauss random_su3

START = geometry start sinit utils flipbc

FLAGS = flags parms wspace

MISC = mutils endian sse_fcts su3_fcts su3_prods \
       cmatrix_dble cmatrix

LINALG = linalg linalg_dble valg valg_dble

LINSOLV = fgcr fgcr4vd

SW_TERM = pauli pauli_dble swinit sw_term blk_sw_term

DIRAC = Pbnd Qbnd Qhat scom Pbnd_dble Qbnd_dble Qhat_dble sdcom

UPDATE = ucom shift

BLOCK = block blk_grid blk_umap blk_swmap blk_smap

EVA = jacobi_dble

DFL = vgrid vflds vinit vcom vdcom Zgen Zops Zhat Zhat_dble dfl_subspace \
      dfl dfl_sap_gcr dfl_modes

SAP_GCR = blk_solv msap sap_gcr

HMC = chrono liealg hmcflds

BM = cg_iter cg_iter_dble time_msap time_Zhat

MODULES = $(RANDOM) $(START) $(FLAGS) $(MISC) \
          $(LINALG) $(LINSOLV) $(SW_TERM) $(DIRAC) $(UPDATE) $(BLOCK) \
          $(EVA) $(DFL) $(SAP_GCR) $(HMC) $(BM)


# DD-HMC distribution

DDHMC = ../../DD-HMC-1.2.2

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

LOGOPTION =


# search path for modules

MDIR = $(DDHMC)/modules

MDIR_BM = ../modules

VPATH = .:$(MDIR)/random:$(MDIR)/start:$(MDIR)/flags:$(MDIR)/misc:\
          $(MDIR)/linalg:$(MDIR)/linsolv:$(MDIR)/sw_term:$(MDIR)/dirac:\
          $(MDIR)/update:$(MDIR)/block:$(MDIR)/eva:$(MDIR)/dfl:$(MDIR)/sap_gcr:\
          $(MDIR)/hmc:$(MDIR_BM)/bm:


# additional include directories

INCPATH = $(MPIR_HOME)/include $(DDHMC)/include/ ../include


# additional libraries

LIBS = m

LIBPATH = $(MPIR_HOME)/lib


# scheduling and optimization options

CFLAGS = -std=c89 -pedantic -Wall -Wno-long-long \
         -O3 -m32 -malign-double -msse3 -DSSE3 -DPM  \
         -Wstrict-prototypes -fstrict-aliasing -Werror


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

SHELL=/bin/bash
CC=$(MPIR_HOME)/bin/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)) $(CFLAGS) $(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

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