From dc39ca5b4017442fe5e792cc5b1b52f9bd2d1504 Mon Sep 17 00:00:00 2001 From: milfeld Date: Thu, 7 Jan 2021 13:28:11 -0600 Subject: [PATCH 1/2] Rewrote Makefile for clarity and easier adoption to other systems. See comments in Makefile. --- Makefile | 152 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index 6094ef5..fcd0367 100755 --- a/Makefile +++ b/Makefile @@ -1,97 +1,121 @@ - -CFLAGS = $(shell gsl-config --cflags) -std=c++0x -##CFLAGS += -ggdb -gdwarf-2 -Wall -I include -O -CFLAGS +=-O3 -I include -##CFLAGS += -D _DEBUG # comment this line if you don't need compile with debug; if you don't need tracing function, define TRACING_DISABLED -MPCFLAG = -I /cm/shared/mpi/openmpi/2.1/intel/17.0/include -LIBS = $(shell gsl-config --libs) - -##CC = icpc -##MPCC = mpiicpc +# +# Update 2020-01-06: +# o Use VPATH for finding cpp file in different directories -- this simplifies rules +# o Abort if gsl-config isn't available +# o Fixed (INTEL) compiler search 0=found | 1=notfound ; make conditional simple (ifeq 0|1) +# o Also use conditional for GCC +# o For objects, use basename to get base file name +# o Clean up directory prefix (shorten variable names and group) +# o Simplified obj and bin rule logic and readability. +# o put rules in canonical order +# -- now the makefile looks a bit cleaner Kent milfeld@tacc.utexas.edu +# +# TODO: use function to create VPATH +# TODO: Check this out on OSX +# TODO: Fix MPI after learning purpose +# +VPATH=src/boundary_conditions:src/classes:src/io:src/math:src/parser:src/reactions:src/system_setup:src/trajectory_functions BDIR = bin ODIR = obj SDIR = src EDIR = EXEs -EPDIR = EXE_PAR -OS := $(shell uname) -INTEL := $(shell which icpc) +EPDIR = EXE_PAR + +.PHONY: any -ifndef INTEL - CC = g++ - MPCC = mpicxx +# REQUIREMENTS: gls and directories + +hasGSL = $(shell type gsl-config >/dev/null 2>&1; echo $$?) +ifeq ($(hasGSL),1) + $(error " GSL must be installed, and gsl-config must be in path.") else - CC = icpc - MPCC = mpiicpc + $(shell mkdir -p bin) + $(shell mkdir -p obj) endif -##ifeq ($(OS),Linux) -## _OBJS = $(shell find $(SDIR) -name "*.cpp" | sed -r 's/(\.cc|.cpp)/.o/') -##else - _OBJS = $(shell find $(SDIR) -name "*.cpp" | sed -E 's/(\.cc|.cpp)/.o/' | sed -E 's/src/./') -##endif +# EXECUTABLE SETUP for serial, MPI, OpenMP (omp) +# +ifeq (serial,$(MAKECMDGOALS)) + _EXEC = nerdss +endif -_EXECUTABLES = nerdss \ -## template \ -## test_angles \ -## test_loops \ -## rd_gen_reweightPBC \ - rd_boundfraconly \ +ifeq (mpi,$(MAKECMDGOALS)) + _EXEC = nerdss_mpi + DEFS = -DMPI +endif -_PAREXECUTABLES = nerdss_mpi \ +ifeq (omp,$(MAKECMDGOALS)) + _EXEC = nerdss_omp + DEFS = -DOMP + PLANG = -fopenmp +endif +ifeq (clean,$(MAKECMDGOALS)) + MAKECMDGOALS = dummy +endif + EXEC = $(patsubst %,$(BDIR)/%,$(_EXEC)) -OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS)) -EXECUTABLES = $(patsubst %,$(BDIR)/%,$(_EXECUTABLES)) +OS := $(shell uname) +INTEL = $(shell type icpc >/dev/null 2>&1; echo $$?) -PAREXECUTABLES = $(patsubst %,$(BDIR)/%,$(_PAREXECUTABLES)) +CFLAGS = $(shell gsl-config --cflags) -std=c++0x +LIBS = $(shell gsl-config --libs) +#---------------COMPILER SETUP -_SOURCES = ${_EXECUTABLES:=.cpp} -SOURCES = $(patsubst %,$(EDIR)/%,$(_SOURCES)) +ifeq ($(GCC),0) # Found/Use Intel icpc compiler + CC = g++ + MPCC = mpicxx + CFLAGS += -O3 -I include +# MPCFLAG = -I /cm/shared/mpi/openmpi/2.1/intel/17.0/include +endif -_PARSOURCES = ${_PAREXECUTABLES:=.cpp} -PARSOURCES = $(patsubst %,$(EPDIR)/%,$(_PARSOURCES)) +ifeq ($(INTEL),0) # Found/Use Intel icpc compiler + CC = icpc + MPCC = mpicxx + CFLAGS += -O3 -I include +endif +#---------------OBJECT FILES -all: dirs $(EXECUTABLES) -#$(PAREXECUTABLES) +ifeq ($(OS),Linux) + _OBJS = $(shell find $(SDIR) -name "*.cpp" | xargs -n 1 basename | sed -r 's/(\.cc|.cpp)/.o/') +else + _OBJS = $(shell find $(SDIR) -name "*.cpp" | xargs -n 1 basename | sed -E 's/(\.cc|.cpp)/.o/') +endif -$(ODIR)/%.o: $(SDIR)/%.cpp - @echo "Compiling $<" - $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ - @echo "------------" + OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS)) -$(EXECUTABLES): $(OBJS) - @echo "Compiling $(EDIR)/$(@F).cpp" - $(CC) $(CFLAGS) $(CFLAGS2) -o $@ $(EDIR)/$(@F).cpp $(OBJS) $(LIBS) - @echo "------------" -$(PAREXECUTABLES): $(OBJS) - @echo "Compiling $(EPDIR)/$(@F).cpp" - $(MPCC) $(CFLAGS) $(CFLAGS2) -o $@ $(EPDIR)/$(@F).cpp $(OBJS) $(LIBS) - @echo "------------" +#---------------RULES +syntax: + @echo "------------------------------------" + @printf '\033[31m%s\033[0m\n' " USAGE: make serial|mpi|omp" + @echo "------------------------------------" + exit 0 -dirs: - mkdir -p bin - mkdir -p obj - mkdir -p obj/classes - mkdir -p obj/math - mkdir -p obj/parser - mkdir -p obj/shared - mkdir -p obj/reactions - mkdir -p obj/system_setup - mkdir -p obj/boundary_conditions - mkdir -p obj/trajectory_functions - mkdir -p obj/io +# Rules: for $(MAKECMDGOALS) serial, mpi, or omp build +# $(EXEC) bin/nerdss, bin/nerdss_mpi or /binnerdss_omp +$(MAKECMDGOALS):$(EXEC) + @echo "Finished making (re-)building $(MAKECMDGOALS) version, $(EXEC)." +$(EXEC): $(OBJS) + @echo "Compiling $(EDIR)/$(@F).cpp" + $(CC) $(CFLAGS) $(CFLAGS2) -o $@ $(EDIR)/$(@F).cpp $(OBJS) $(LIBS) $(PLANG) + @echo "------------" + +obj/%.o: %.cpp + @echo "Compiling $<" + $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ $(PLANG) $(DEFS) + @echo "------------" clean: rm -rf $(ODIR) bin - +# Reference: https://www.gnu.org/software/make/manual/html_node/Quick-Reference.html From 995c467892cd4018394a594b7e40debc987590fe Mon Sep 17 00:00:00 2001 From: milfeld Date: Thu, 7 Jan 2021 16:47:30 -0600 Subject: [PATCH 2/2] Now works on OSX. --- Makefile | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) mode change 100755 => 100644 Makefile diff --git a/Makefile b/Makefile old mode 100755 new mode 100644 index fcd0367..06da859 --- a/Makefile +++ b/Makefile @@ -11,8 +11,9 @@ # -- now the makefile looks a bit cleaner Kent milfeld@tacc.utexas.edu # # TODO: use function to create VPATH -# TODO: Check this out on OSX # TODO: Fix MPI after learning purpose +# TODO: Make rules for *.hpp's +# # VPATH=src/boundary_conditions:src/classes:src/io:src/math:src/parser:src/reactions:src/system_setup:src/trajectory_functions @@ -28,10 +29,10 @@ EPDIR = EXE_PAR hasGSL = $(shell type gsl-config >/dev/null 2>&1; echo $$?) ifeq ($(hasGSL),1) - $(error " GSL must be installed, and gsl-config must be in path.") +$(error " GSL must be installed, and gsl-config must be in path.") else - $(shell mkdir -p bin) - $(shell mkdir -p obj) +$(shell mkdir -p bin) +$(shell mkdir -p obj) endif # EXECUTABLE SETUP for serial, MPI, OpenMP (omp) @@ -59,7 +60,8 @@ endif OS := $(shell uname) -INTEL = $(shell type icpc >/dev/null 2>&1; echo $$?) +INTEL = $(shell type icpc >/dev/null 2>&1; echo $$?) +GCC = $(shell type g++ >/dev/null 2>&1; echo $$?) CFLAGS = $(shell gsl-config --cflags) -std=c++0x LIBS = $(shell gsl-config --libs) @@ -107,15 +109,16 @@ $(MAKECMDGOALS):$(EXEC) $(EXEC): $(OBJS) @echo "Compiling $(EDIR)/$(@F).cpp" - $(CC) $(CFLAGS) $(CFLAGS2) -o $@ $(EDIR)/$(@F).cpp $(OBJS) $(LIBS) $(PLANG) + $(CC) $(CFLAGS) $(CFLAGS2) -o $@ $(EDIR)/$(@F).cpp -Iinclude $(OBJS) $(LIBS) $(PLANG) @echo "------------" obj/%.o: %.cpp - @echo "Compiling $<" - $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ $(PLANG) $(DEFS) + @echo "Compiling $< at $(