forked from exoclime/THOR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
396 lines (312 loc) · 13.6 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# for release build and SM=35
# $ make release -j8 SM=35
# for debug build and default SM
# $ make debug -j8
# default build builds release and SM=30
# $ make -j8
# to test localy while forcing g++-5:
# make release -j8 ccbin='-ccbin g++-5'
#
# to show commands echoed
# $ make VERBOSE=1
# SM number can also be set in Makefile.conf
# shell colors
RED := "\033[1;31m"
YELLOW := "\033[1;33m"
GREEN := "\033[1;32m"
BLUE := "\033[1;34m"
MAGENTA := "\033[1;35m"
CYAN := "\033[1;36m"
END := "\033[0m"
#######################################################################
# get local config
ifeq ($(wildcard Makefile.conf),Makefile.conf)
$(info Using local configuration from Makefile.conf )
include Makefile.conf
else
$(info No local configuration )
endif
#######################################################################
# Set up variables for files and compilation
# Builds THOR executable
SM ?= 35 # Streaming Multiprocessor version
COMP ?= nvcc
# objects
obj_cuda := esp.o grid.o esp_initial.o simulation_setup.o thor_driver.o profx_driver.o esp_output.o debug_helpers.o profx_globdiag.o reduction_add.o phy_modules_device.o ultrahot_thermo.o profx_sponge.o cuda_device_memory.o
obj_cpp := storage.o binary_test.o config_file.o cmdargs.o directories.o log_writer.o iteration_timer.o
obj := $(obj_cpp) $(obj_cuda)
#objects for tests
obj_tests_cmdargs := cmdargs_test.o cmdargs.o
obj_tests_config := config_test.o config_file.o
obj_tests_storage := storage_test.o storage.o log_writer.o directories.o
obj_tests_directories := directories_test.o directories.o log_writer.o
obj_tests_gen_init := gen_init.o storage.o grid.o simulation_setup.o directories.o log_writer.o
obj_tests_reduction_add := reduction_add_test.o reduction_add.o directories.o log_writer.o
#######################################################################
# flags
CUDA_PATH := /usr/lib/cuda/
CUDA_LIBS := /usr/lib/x86-64-linux-gnu/
ifeq ($(COMP), nvcc)
# define specific compiler for nvcc. if if fails on newer installations, get it to use g++-5
CC = nvcc
CC_comp_flag = -dc
ccbin :=
# ccbin := -ccbin g++-5
CDB := none
arch := -arch sm_$(SM)
dependencies_flags = --generate-dependencies
# define common flags
cpp_flags := $(ccbin) --compiler-options -Wall -std=c++11 -DDEVICE_SM=$(SM)
cuda_flags := $(ccbin) --compiler-options -Wall -std=c++11 -DDEVICE_SM=$(SM)
cpp_dep_flags := $(ccbin) -std=c++11
cuda_dep_flags := $(ccbin) -std=c++11
link_flags = $(ccbin)
else
# need to compile with clang for compilation database
CC := $(COMP)
CC_comp_flag = -c
CDB = -MJ
arch := --cuda-gpu-arch=sm_$(SM)
dependencies_flags := -MM
# define common flags
cpp_flags := -Wall -std=c++11 -DDEVICE_SM=$(SM)
cuda_flags := -Wall -std=c++11 -DDEVICE_SM=$(SM) --cuda-path=$(CUDA_PATH)
cpp_dep_flags := -std=c++11
cuda_dep_flags := -std=c++11 --cuda-path=$(CUDA_PATH)
link_flags = --cuda-path=$(CUDA_PATH) -L$(CUDA_LIBS) -lcudart_static -ldl -lrt -pthread
endif
# define debug flags
debug_flags := -g -G
# define release flags
release_flags := -O3
# define profiling flags
profiling_flags := -pg -lineinfo --default-stream per-thread
#######################################################################
# define where to find sources
source_dirs := src src/ESP src/utils src/test
vpath %.cu $(source_dirs)
vpath %.cpp $(source_dirs)
# Path where the hdf5 lib was installed,
h5libs := $(shell h5c++ -show -shlib | awk -v ORS=" " '{ for ( n=1; n<=NF; n++ ) if ($$n ~ "^-lh") print $$n }')
h5libdir := $(shell h5c++ -show -shlib | awk -v ORS=" " '{ for ( n=1; n<=NF; n++ ) if ($$n ~ "^-L") print $$n }')
h5include := $(shell h5c++ -show -shlib | awk -v ORS=" " '{ for ( n=1; n<=NF; n++ ) if ($$n ~ "^-I") print $$n }')
$(info h5libs="$(h5libs)")
$(info h5libdir="$(h5libdir)")
$(info h5include="$(h5include)")
includehdf = $(h5include)
includedir = src/headers
#######################################################################
# directory names
OBJDIR = obj
BINDIR = bin
RESDIR = results
TESTDIR = tests
# modules search directory.
# set if not set from command line or config file
# will run the Makefile in that directory, passing it this makefile's
# variables
MODULES_SRC ?= src/physics/managers/empty/
.PHONY: all clean
#######################################################################
# compute targets
$(info goals: $(MAKECMDGOALS))
# set some build values depending on target
# default target value, falls back to release below
OUTPUTDIR := UNDEF
MODE := UNDEF
# profiling target
ifeq "$(findstring prof, $(MAKECMDGOALS))" "prof"
cuda_flags += $(profiling_flags) -DBUILD_LEVEL="\"profiling\""
cpp_flags += $(profiling_flags) -DBUILD_LEVEL="\"profiling\""
MODE := prof
OUTPUTDIR := prof
endif
# debug target
ifeq "$(findstring debug, $(MAKECMDGOALS))" "debug"
cuda_flags += $(debug_flags) -DBUILD_LEVEL="\"debug\""
cpp_flags += $(debug_flags) -DBUILD_LEVEL="\"debug\""
MODE := debug
OUTPUTDIR := debug
endif
# release target
ifeq "$(findstring release, $(MAKECMDGOALS))" "release"
cuda_flags += $(release_flags) -DBUILD_LEVEL="\"release\""
cpp_flags += $(release_flags) -DBUILD_LEVEL="\"release\""
MODE := release
OUTPUTDIR := release
endif
# test target
ifeq "$(findstring tests, $(MAKECMDGOALS))" "tests"
cuda_flags += $(debug_flags) -DBUILD_LEVEL="\"test\""
cpp_flags += $(debug_flags) -DBUILD_LEVEL="\"test\""
MODE := tests
OUTPUTDIR := debug
endif
# by default, build release target
ifeq "$(MODE)" "UNDEF"
cuda_flags += $(release_flags) -DBUILD_LEVEL="\"release\""
cpp_flags += $(release_flags) -DBUILD_LEVEL="\"release\""
MODE := release
OUTPUTDIR := release
endif
debug: symlink
release: symlink
prof: symlink
cdb:
#######################################################################
# main binary
all: symlink
$(info Compiler: $(CC))
$(info Compile mode: $(MODE))
$(info Output objects to: $(OBJDIR)/$(OUTPUTDIR))
$(info Output tests to: $(BINDIR)/$(TESTDIR))
$(info cuda_flags: $(cuda_flags) )
$(info cpp_flags: $(cpp_flags) )
$(info link_flags: $(link_flags) )
$(info arch: $(arch) )
$(info MODULES_SRC: $(MODULES_SRC))
$(info CC compile flag: $(CC_comp_flag))
ifndef VERBOSE
.SILENT:
endif
#######################################################################
# create object directory if missing
$(BINDIR) $(RESDIR) $(OBJDIR):
mkdir $@
$(BINDIR)/${OUTPUTDIR}: $(BINDIR)
mkdir -p $(BINDIR)/$(OUTPUTDIR)
$(OBJDIR)/${OUTPUTDIR}: $(OBJDIR)
mkdir -p $(OBJDIR)/$(OUTPUTDIR)
$(BINDIR)/${TESTDIR}: $(BINDIR)
mkdir -p $(BINDIR)/$(TESTDIR)
#######################################################################
# make dependency targets
# this generates obj/(debug|release)/*.d files containing dependencies targets.
# it uses the compiler to find all the header files a CUDA/C++ file depends on
# those files are then included at the end
# make is run twice, once to create the dependency targets and once to run the compilation
# for CUDA files
$(OBJDIR)/${OUTPUTDIR}/%.d: %.cu | $(OBJDIR)/$(OUTPUTDIR) $(OBJDIR)
@echo $(BLUE)computing dependencies $@ $(END)
set -e; rm -f $@; \
$(CC) $(dependencies_flags) $(arch) $(cuda_dep_flags) $(h5include) -I$(includedir) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
# for C++ files
$(OBJDIR)/${OUTPUTDIR}/%.d: %.cpp | $(OBJDIR)/$(OUTPUTDIR) $(OBJDIR)
@echo $(BLUE)computing dependencies $@ $(END)
set -e; rm -f $@; \
$(CC) $(dependencies_flags) $(arch) $(cpp_dep_flags) $(h5include) -I$(includedir) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
#######################################################################
# build objects
# CUDA files
$(OBJDIR)/${OUTPUTDIR}/%.o: %.cu $(OBJDIR)/$(OUTPUTDIR)/%.d| $(OBJDIR)/$(OUTPUTDIR) $(OBJDIR)
@echo $(YELLOW)creating $@ $(END)
if test $$CDB = "-MJ" ; then \
$(CC) $(CC_comp_flag) $(arch) $(cuda_flags) $(h5include) -I$(includedir) $(CDB) [email protected] -o $@ $<; \
else \
$(CC) $(CC_comp_flag) $(arch) $(cuda_flags) $(h5include) -I$(includedir) -o $@ $<; \
fi
# C++ files
$(OBJDIR)/${OUTPUTDIR}/%.o: %.cpp $(OBJDIR)/$(OUTPUTDIR)/%.d| $(OBJDIR)/$(OUTPUTDIR) $(OBJDIR)
@echo $(YELLOW)creating $@ $(END)
if test $$CDB = "-MJ" ; then \
$(CC) $(CC_comp_flag) $(arch) $(cpp_flags) $(h5include) -I$(includedir) $(CDB) [email protected] -o $@ $<; \
else \
$(CC) $(CC_comp_flag) $(arch) $(cpp_flags) $(h5include) -I$(includedir) -o $@ $<; \
fi
# link *.o objects
$(BINDIR)/${OUTPUTDIR}/esp: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj)) $(MODULES_SRC)/libphy_modules.a | $(BINDIR) $(RESDIR) $(BINDIR)/$(OUTPUTDIR) $(OBJDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) -o $(BINDIR)/$(OUTPUTDIR)/esp $(arch) $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj)) -L$(MODULES_SRC) -lphy_modules $(h5libdir) $(h5libs) $(link_flags)
if test $$CDB = "-MJ" ; then \
rm -f compile_commands.json; \
sed -e '1s/^/[\n/' -e '$$s/,$$/\n]/' $(OBJDIR)/$(OUTPUTDIR)/*.o.json $(MODULES_SRC)/obj/*.o.json > compile_commands.json; \
sed -i.bak s/-xcuda/-xc++/g compile_commands.json; \
rm -f compile_commands.json.bak; \
fi
# phony so that it will always be run
.PHONY: symlink
symlink: $(BINDIR)/$(OUTPUTDIR)/esp
@echo $(BLUE)make link from $(BINDIR)/$(OUTPUTDIR)/esp to $(BINDIR)/esp $(END)
rm -f $(BINDIR)/esp
ln -s $(BINDIR)/$(OUTPUTDIR)/esp -r -t bin
#######################################################################
# include physics modules
export
# always call submakefile for modules
.PHONY: $(MODULES_SRC)/libphy_modules.a
$(MODULES_SRC)/libphy_modules.a:
@echo $(MAGENTA)Creating physics module from subdir $(MODULES_SRC)$(END)
$(MAKE) -f Makefile -C $(MODULES_SRC)
#######################################################################
# Build Tests
#define build_test =
# $(CC) $(arch) $(flags) $(debug_flags) -o $(BINDIR)/$(TESTDIR)/cmdargs_test $(addprefix #$(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_cmdargs))
#endef
tests: ${BINDIR}/${TESTDIR}/cmdargs_test ${BINDIR}/${TESTDIR}/config_test ${BINDIR}/${TESTDIR}/storage_test ${BINDIR}/${TESTDIR}/directories_test ${BINDIR}/${TESTDIR}/gen_init ${BINDIR}/${TESTDIR}/reduction_add_test
$(BINDIR)/$(TESTDIR)/cmdargs_test: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_cmdargs)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/cmdargs_test $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_cmdargs))
$(BINDIR)/$(TESTDIR)/config_test: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_config)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/config_test $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_config))
$(BINDIR)/$(TESTDIR)/directories_test: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_directories)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/directories_test $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_directories))
$(BINDIR)/$(TESTDIR)/storage_test: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_storage)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/storage_test $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_storage)) $(h5libdir) $(h5libs)
$(BINDIR)/$(TESTDIR)/gen_init: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_gen_init)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/gen_init $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_gen_init)) $(h5libdir) $(h5libs)
$(BINDIR)/$(TESTDIR)/reduction_add_test: $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_reduction_add)) | $(BINDIR)/${OUTPUTDIR} $(BINDIR)/$(TESTDIR) $(BINDIR) $(RESDIR)
@echo $(YELLOW)creating $@ $(END)
$(CC) $(arch) $(link_flags) -o $(BINDIR)/$(TESTDIR)/reduction_add_test $(addprefix $(OBJDIR)/$(OUTPUTDIR)/,$(obj_tests_reduction_add)) $(h5libdir) $(h5libs)
#######################################################################
# Cleanup
.phony: clean,ar
clean:
@echo $(CYAN)clean up binaries $(END)
-$(RM) $(BINDIR)/debug/esp
-$(RM) $(BINDIR)/release/esp
-$(RM) $(BINDIR)/prof/esp
@echo $(CYAN)clean up objects files and dependencies $(END)
-$(RM) $(OBJDIR)/debug/*.o $(OBJDIR)/debug/*.d $(OBJDIR)/debug/*.d.* $(OBJDIR)/debug/*.o.json
-$(RM) $(OBJDIR)/release/*.o $(OBJDIR)/release/*.d $(OBJDIR)/release/*.d.* $(OBJDIR)/release/*.o.json
-$(RM) $(OBJDIR)/prof/*.o $(OBJDIR)/prof/*.d $(OBJDIR)/prof/*.d.* $(OBJDIR)/prof/*.o.json
@echo $(CYAN)clean up tests binaries $(END)
-$(RM) $(BINDIR)/tests/cmdargs_test
-$(RM) $(BINDIR)/tests/storage_test
-$(RM) $(BINDIR)/tests/config_test
-$(RM) $(BINDIR)/tests/directories_test
-$(RM) $(BINDIR)/tests/gen_init
-$(RM) $(BINDIR)/tests/test_reduction_add
@echo $(CYAN)clean up symlink $(END)
-$(RM) $(BINDIR)/esp
@echo $(CYAN)clean up modules directory $(END)
$(MAKE) -C $(MODULES_SRC) clean
@echo $(CYAN)clean up directories $(END)
-$(RM) -d $(BINDIR)/debug $(BINDIR)/release $(BINDIR)/prof
-$(RM) -d $(BINDIR)/tests
-$(RM) -d $(OBJDIR)/debug
-$(RM) -d $(OBJDIR)/release
-$(RM) -d $(OBJDIR)/prof
-$(RM) -d $(BINDIR)
-$(RM) -d $(OBJDIR)
#######################################################################
# dependencies includes
ifneq ($(MAKECMDGOALS),clean)
ifeq "${MODE}" "tests"
include $(obj_tests_config:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
include $(obj_tests_cmdargs:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
include $(obj_tests_storage:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
include $(obj_tests_directories:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
include $(obj_tests_gen_init:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
include $(obj_tests_reduce_add:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
else
include $(obj:%.o=$(OBJDIR)/$(OUTPUTDIR)/%.d)
endif
endif