############MAKEFILE FOR MEDYAN####################
#
# As outlined in the install guide, the following
# libraries and compilers are needed:
#
#	GCC 4.7 and above
#	Boost 1.49 and above
#	GSL
#
#   If running unit tests, googletest is needed.
#
# Tweak the following compiler and library options
# for your given system.
##################################################

TESTS	:= $(shell find ./TESTS -name 'test_*')
SOURCES_CPP := $(shell find . -name '*.cpp' ! -path './TESTS/*' ! -path './dist_moduleV2/*')
SOURCES_CPP += ./dist_moduleV2/dist_avx_par.cpp  ./dist_moduleV2/dist_bench.cpp  ./dist_moduleV2/dist_example.cpp  ./dist_moduleV2/dist_mod_vars.cpp
SOURCES_CU  := $(shell find . -name '*.cu'  ! -path './TESTS/*' ! -path './dist_moduleV2/*')
SOURCES = $(SOURCES_CPP) $(SOURCES_CU)
HEADERS := $(shell find . -name '*.h' ! -path './dist_moduleV2/*')
HEADERS += ./dist_moduleV2/umesimd/UMESimd.h

OBJECTS_CPP := $(SOURCES_CPP:.cpp=.o)
OBJECTS_CU  := $(SOURCES_CU:.cu=.o)
OBJECTS = $(OBJECTS_CPP) $(OBJECTS_CU)

# Compiler and linker choice
CXX = g++ -std=c++14
NVCC = nvcc -std=c++11

# Flags used by compiler and linker
DEBUG = -g
FAST_CXX = -Os -ffast-math -fno-finite-math-only -flto -funroll-loops -DNDEBUG -mtune=native -march=native # -Xpreprocessor -fopenmp # -mavx2
#FAST_CXX = -Og -march=native -mtune=native

# Compiler flags
INCLUDES = -I./dist_module -I./dist_module/umesimd/ -I/usr/local/include -I/homes/achansek/xtl_install/include/ -I/homes/achansek/xtensor_install/include/ -I./dist_moduleV2 -I./dist_moduleV2/umesimd\
-I /a/fs-3/export/home/deepthought2/achansek/boost_1_61_0\
-I./ -IStructure -IStructure/Special      \
-IStructure/SurfaceMesh                   \
-IChemistry                               \
-IMechanics/                              \
-IMechanics/Minimizer                     \
-IMechanics/ForceField                    \
-IMechanics/ForceField/Volume             \
-IMechanics/ForceField/VolumeConservation \
-IMechanics/ForceField/Boundary           \
-IMechanics/ForceField/Branching          \
-IMechanics/ForceField/Filament           \
-IMechanics/ForceField/Linker             \
-IMechanics/ForceField/MotorGhost         \
-IMechanics/ForceField/Bubble             \
#-IMechanics/ForceField/Membrane           \

INCLUDES_NVCC = $(INCLUDES) -I/usr/local/include -I/usr/local/cuda/include

CXXFLAGS_CXX = $(FAST_CXX) -Wall -Wno-sign-compare -Wno-maybe-uninitialized \
-Wno-uninitialized -Wno-unknown-warning-option $(INCLUDES)
CXXFLAGS_NVCC = -g -G -x=cu -arch=sm_35 -Xcompiler -lpthread -ldl -lrt \
-lineinfo --compiler-options -O0 -Xptxas -v $(INCLUDES_NVCC)

# Linker flags
LDLIBS = -L/usr/local/lib/ -lboost_system -L./dist_module/umesimd -static-libstdc++ -pthread -L/a/fs-3/export/home/deepthought2/achansek/boost_1_61_0/lib/
LDLIBS_NVCC = $(LDLIBS) -L/usr/local/cuda/lib64 -L/usr/local/cuda/lib -Icudart -lpthread -lnvToolsExt
LDFLAGS_CXX = $(FAST_CXX) 

#################MAIN USER MACROS#################
#
# Please set these macros according to the
# desired simulation type. See the install
# and usage guides for more details.
#
##################################################

#Initialize chemical components of system
CPPFLAGS += -DCHEMISTRY
#Initialize mechanical components of system
CPPFLAGS += -DMECHANICS
CPPFLAGS += -DMOVEBEADSLINESEARCH
#Use dynamic rate changing
CPPFLAGS += -DDYNAMICRATES
#vectorized serial energy minimization
CPPFLAGS += -DSERIAL
#neighbors list calculations
#CPPFLAGS += -DNLORIGINAL
CPPFLAGS += -DNLSTENCILLIST
CPPFLAGS += -DHYBRID_NLSTENCILLIST
CPPFLAGS += -DSIMDBINDINGSEARCH
#Boost memory macros
CPPFLAGS += -DBOOST_MEM_POOL
CPPFLAGS += -DBOOL_POOL_NSIZE=65536
CPPFLAGS += -DNPROCS=8
CPPFLAGS += -DMOVEBEADSLINESEARCH
CPPFLAGS += -DPLOSFEEDBACK
CPPFLAGS += -DCHECKFORCES_INF_NAN
#if compiling the testing suite
#CPPFLAGS += -DTESTING

#Detailed chemistry macros.
#FOR ALL STANDARD FUNCTIONALITY, NONE OF THESE SHOULD BE TURNED OFF!!!

#Track dependents. Needed for Gillespie and NRM algorithm
CPPFLAGS += -DTRACK_DEPENDENTS

#Track zero copy and max copy number.
#For passivating and activating reactions accordingly
CPPFLAGS += -DTRACK_ZERO_COPY_N
CPPFLAGS += -DTRACK_UPPER_COPY_N

#Reaction signaling. Needed for all Filament, Linker,
#Motor, and BranchingPoint reactions in system
CPPFLAGS += -DREACTION_SIGNALING

#Species signaling
CPPFLAGS += -DRSPECIES_SIGNALING

#CUDA specific
CPPFLAGS_NVCC = $(CPPFLAGS) -DCUDAACCL

#Non-CUDA specific
CPPFLAGS_CXX = $(CPPFLAGS)

################MAKEFILE OPTIONS###################
#
#
#   make [all]        - makes everything
#   make Makefile.dep - makes dependencies
#   make clean	      - remove all files
#						generated by make
#
#	Use USING_CUDA=true to make cuda version
#
##################################################

all: medyan

medyan: $(OBJECTS)
ifeq ($(USING_CUDA), true)
	$(NVCC) -o MEDYAN $(OBJECTS) $(LDLIBS_NVCC)
else
	$(CXX) $(LDFLAGS_CXX) -o MEDYAN $(OBJECTS) $(LDLIBS)
endif

%.o: %.cpp
	$(CXX) $(CPPFLAGS_CXX) $(CXXFLAGS_CXX) -c $< -o $@

%.o: %.cu
ifeq ($(USING_CUDA), true)
	$(NVCC) $(CPPFLAGS_NVCC) $(CXXFLAGS_NVCC) -c $< -o $@
else
	$(CXX) $(CPPFLAGS_CXX) $(CXXFLAGS_CXX) -xc++ -c $< -o $@
endif

clean:
	\find . -name '*.o' -delete
	\rm -f MEDYAN

Makefile.dep: $(SOURCES) $(HEADERS)
	@echo "Updating the dependency file: Makefile.dep"
ifeq ($(USING_CUDA), true)
	$(SHELL) -ec '$(NVCC) -MM $(CPPFLAGS_NVCC) $(INCLUDES_NVCC)  $(filter %.cpp %.cu,$^) \
	| sed '\''s/^\(.*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
	[ -s $@ ] || rm -f $@'
else
	$(SHELL) -ec '$(CXX) -MM -xc++ $(CPPFLAGS_CXX) $(INCLUDES)  $(filter %.cpp %.cu,$^) \
	| sed '\''s/^\(.*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
	[ -s $@ ] || rm -f $@'
endif

include Makefile.dep

