################################################################################
#
# 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 = check1 check2 check3 check4 check5 check6 check7 check8

ARCHIVE = archive iodat marchive sarchive

DFT = dft4d dft_com dft_shuf dft_wspace fft small_dft

FLAGS = action_parms dfl_parms dft4d_parms dft_parms flags hmc_parms \
        lat_parms rat_parms smd_parms wflow_parms

FORCES = force0

LATTICE = bcnds ftidx geogen geometry uidx

LINALG = liealg salg_dble valg_dble

MDFLDS = fcom mdflds

MSFCTS = farchive fldops latavg pt2pt

RANDOM = ranlux ranlux_common ranlxd ranlxs gauss

SFLDS = sflds

SU3FCTS = chexp cm3x3 random_su3 su3prod su3ren

TCHARGE = ftcom ftensor

UFLDS = bstap udcom uflds uinit plaq_sum

UTILS = array endian error mutils qsum utils wspace

VFLDS = vinit

WFLOW = wflow

MODULES = $(ARCHIVE) $(DFT) $(FORCES) $(LATTICE) $(LINALG) $(FLAGS) \
          $(MDFLDS) $(MSFCTS) $(RANDOM) $(TCHARGE) $(SFLDS) $(SU3FCTS) \
          $(UFLDS) $(UTILS) $(VFLDS) $(WFLOW)


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

LOGOPTION =


# search path for modules

MDIR = ../../modules

VPATH = .:$(MDIR)/archive:$(MDIR)/dft:$(MDIR)/forces:$(MDIR)/linalg:\
          $(MDIR)/lattice:$(MDIR)/flags:$(MDIR)/mdflds:$(MDIR)/msfcts:\
          $(MDIR)/random:$(MDIR)/sflds:$(MDIR)/su3fcts:$(MDIR)/tcharge:\
          $(MDIR)/uflds:$(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

############################## 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

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