xref: /libCEED/Makefile (revision 2158b1b06851b8d004f561315dfbcf1c968c8a5b)
1# Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2# Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3# All Rights reserved. See files LICENSE and NOTICE for details.
4#
5# This file is part of CEED, a collection of benchmarks, miniapps, software
6# libraries and APIs for efficient high-order finite element and spectral
7# element discretizations for exascale applications. For more information and
8# source code availability see http://github.com/ceed.
9#
10# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11# a collaborative effort of two U.S. Department of Energy organizations (Office
12# of Science and the National Nuclear Security Administration) responsible for
13# the planning and preparation of a capable exascale ecosystem, including
14# software, applications, hardware, advanced system engineering and early
15# testbed platforms, in support of the nation's exascale computing imperative.
16
17ifeq (,$(filter-out undefined default,$(origin CC)))
18  CC = gcc
19endif
20ifeq (,$(filter-out undefined default,$(origin FC)))
21  FC = gfortran
22endif
23NVCC = $(CUDA_DIR)/bin/nvcc
24
25# ASAN must be left empty if you don't want to use it
26ASAN ?=
27NDEBUG ?= 1
28
29LDFLAGS ?=
30UNDERSCORE ?= 1
31
32# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
33OCCA_DIR ?= ../occa
34
35# env variable MAGMA_DIR can be used too
36MAGMA_DIR ?= ../magma
37# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
38CUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
39               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
40
41# Warning: SANTIZ options still don't run with /gpu/occa
42# export LSAN_OPTIONS=suppressions=.asanignore
43AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
44
45CFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
46FFLAGS = -cpp     -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP
47# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately:
48ifneq ($(filter %xlf %xlf_r,$(FC)),)
49  FFLAGS = -qpreprocess -qextname -qpic -MMD
50endif
51
52CFLAGS += $(if $(NDEBUG),-O2 -DNDEBUG=1,-g)
53
54ifeq ($(UNDERSCORE), 1)
55  CFLAGS += -DUNDERSCORE
56endif
57
58FFLAGS += $(if $(NDEBUG),-O2 -DNDEBUG=1,-g)
59
60CFLAGS += $(if $(ASAN),$(AFLAGS))
61FFLAGS += $(if $(ASAN),$(AFLAGS))
62LDFLAGS += $(if $(ASAN),$(AFLAGS))
63CPPFLAGS = -I./include
64LDLIBS = -lm
65OBJDIR := build
66MFEM_EX_DIR := examples/mfem
67LIBDIR := lib
68
69# Installation variables
70prefix ?= /usr/local
71bindir = $(prefix)/bin
72libdir = $(prefix)/lib
73okldir = $(prefix)/lib/okl
74includedir = $(prefix)/include
75pkgconfigdir = $(libdir)/pkgconfig
76INSTALL = install
77INSTALL_PROGRAM = $(INSTALL)
78INSTALL_DATA = $(INSTALL) -m644
79
80# Get number of processors of the machine
81NPROCS := $(shell getconf _NPROCESSORS_ONLN)
82# prepare make options to run in parallel
83MFLAGS := -j $(NPROCS) --warn-undefined-variables \
84                       --no-print-directory --no-keep-going
85
86PROVE ?= prove
87PROVE_OPTS ?= -j $(NPROCS)
88DARWIN := $(filter Darwin,$(shell uname -s))
89SO_EXT := $(if $(DARWIN),dylib,so)
90
91ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
92libceed := $(LIBDIR)/libceed.$(SO_EXT)
93libceed.c := $(wildcard ceed*.c)
94BACKENDS := /cpu/self/ref /cpu/self/tmpl /cpu/self/opt
95
96# Tests
97tests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
98tests.f   := $(sort $(wildcard tests/t[0-9][0-9]-*.f))
99tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
100ctests    := $(tests)
101tests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
102#examples
103examples.c := $(sort $(wildcard examples/ceed/*.c))
104examples.f := $(sort $(wildcard examples/ceed/*.f))
105examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
106examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
107#mfemexamples
108mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
109mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(MFEM_EX_DIR)/%)
110# backends/[ref & occa  & magma]
111ref.c      := $(sort $(wildcard backends/ref/*.c))
112template.c := $(sort $(wildcard backends/template/*.c))
113optimized.c:= $(sort $(wildcard backends/optimized/*.c))
114occa.c     := $(sort $(wildcard backends/occa/*.c))
115magma_preprocessor := python backends/magma/gccm.py
116magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
117magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
118magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
119magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
120magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
121magma_allsrc.cu:= $(magma_tmp.cu)
122
123# Output using the 216-color rules mode
124rule_file = $(notdir $(1))
125rule_path = $(patsubst %/,%,$(dir $(1)))
126last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
127ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
128emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
129color_out = @if [ -t 1 ]; then \
130				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
131					$(1) $(call ansicolor,$(2)) \
132					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
133				printf "  %10s %s\n" $(1) $(2); fi
134# if TERM=dumb, use it, otherwise switch to the term one
135output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
136
137# if V is set to non-nil, turn the verbose mode
138quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
139
140.SUFFIXES:
141.SUFFIXES: .c .o .d
142.SECONDEXPANSION: # to expand $$(@D)/.DIR
143
144.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
145
146%/.DIR :
147	@mkdir -p $(@D)
148	@touch $@
149
150.PRECIOUS: %/.DIR
151
152this: $(libceed) $(ceed.pc)
153# run 'this' target in parallel
154all:;@$(MAKE) $(MFLAGS) V=$(V) this
155
156$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
157
158libceed.c += $(ref.c)
159libceed.c += $(template.c)
160libceed.c += $(optimized.c)
161ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
162  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
163  $(libceed) : LDLIBS += -locca
164  libceed.c += $(occa.c)
165  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
166  BACKENDS += /cpu/occa /gpu/occa /omp/occa /ocl/occa
167endif
168ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
169  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
170  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
171  ifneq ($(CUDA_LIB_DIR),)
172  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
173  omp_link = -fopenmp
174  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
175  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
176  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
177  magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o)
178  $(libceed)           : LDLIBS += $(magma_link)
179  $(tests) $(examples) : LDLIBS += $(magma_link)
180  $(libceed) : $(magma_allsrc.o)
181  libceed.c  += $(magma_allsrc.c)
182  libceed.cu += $(magma_allsrc.cu)
183  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
184  $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) : NVCCFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
185  BACKENDS += /gpu/magma
186  endif
187endif
188
189export BACKENDS
190
191# generate magma_tmp.c and magma_cuda.cu from magma.c
192$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR
193	$(magma_preprocessor) $<
194
195$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
196	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
197
198$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
199	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
200
201$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
202	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
203
204$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
205	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
206
207$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
208	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
209
210$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
211	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
212
213$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
214	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
215
216$(tests) $(examples) : $(libceed)
217$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
218
219# Build MFEM BPs
220libceed-mfem :
221	make && cd examples && make mfem && cd ../
222
223run-% : $(OBJDIR)/%
224	@tests/tap.sh $(<:build/%=%)
225# Test core libCEED
226test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
227
228# run test target in parallel
229tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
230# CPU C tests only for backend %
231ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
232
233prove : $(tests) $(examples)
234	$(info Testing backends: $(BACKENDS))
235	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
236# run prove target in parallel
237prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
238
239prove-mfem : $(tests) $(examples)
240	make libceed-mfem
241	$(info Testing backends: $(BACKENDS))
242	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
243	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(mfemexamples:$(MFEM_EX_DIR)/%=%)
244
245examples : $(examples)
246
247$(ceed.pc) : pkgconfig-prefix = $(abspath .)
248$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
249.INTERMEDIATE : $(OBJDIR)/ceed.pc
250%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
251	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
252
253# The occa executable is not linked with RPATH by default, so we need it to find its libocca.so
254OCCA               := $(if $(DARWIN),DYLD_LIBRARY_PATH,LD_LIBRARY_PATH)=$(OCCA_DIR)/lib $(OCCA_DIR)/bin/occa
255OKL_KERNELS        := $(wildcard backends/occa/*.okl)
256
257okl-cache :
258	$(OCCA) cache ceed $(OKL_KERNELS)
259
260okl-clear:
261	$(OCCA) clear -y -l ceed
262
263install : $(libceed) $(OBJDIR)/ceed.pc
264	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(okldir)" "$(DESTDIR)$(pkgconfigdir)"
265	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
266	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
267	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
268	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
269	$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/"
270
271.PHONY : all cln clean print test tst prove prv examples style install doc okl-cache okl-clear
272
273cln clean :
274	$(RM) *.o *.d $(libceed)
275	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
276	$(MAKE) -C examples/ceed clean
277	$(MAKE) -C examples/mfem clean
278	$(MAKE) -C examples/petsc clean
279	(cd examples/nek5000 && bash make-nek-examples.sh clean)
280	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
281
282distclean : clean
283	rm -rf doc/html
284
285doc :
286	doxygen Doxyfile
287
288style :
289	astyle --style=google --indent=spaces=2 --max-code-length=80 \
290            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
291            --suffix=none --preserve-date --formatted \
292            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp
293
294print :
295	@echo $(VAR)=$($(VAR))
296
297print-% :
298	$(info [ variable name]: $*)
299	$(info [        origin]: $(origin $*))
300	$(info [         value]: $(value $*))
301	$(info [expanded value]: $($*))
302	$(info )
303	@true
304
305-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
306