xref: /libCEED/Makefile (revision de24ff68e16fd37a5bf984ef4439dae785c57f1b)
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
66LIBDIR := lib
67
68# Installation variables
69prefix ?= /usr/local
70bindir = $(prefix)/bin
71libdir = $(prefix)/lib
72okldir = $(prefix)/lib/okl
73includedir = $(prefix)/include
74pkgconfigdir = $(libdir)/pkgconfig
75INSTALL = install
76INSTALL_PROGRAM = $(INSTALL)
77INSTALL_DATA = $(INSTALL) -m644
78
79# Get number of processors of the machine
80NPROCS := $(shell getconf _NPROCESSORS_ONLN)
81# prepare make options to run in parallel
82MFLAGS := -j $(NPROCS) --warn-undefined-variables \
83                       --no-print-directory --no-keep-going
84
85PROVE ?= prove
86PROVE_OPTS ?= -j $(NPROCS)
87DARWIN := $(filter Darwin,$(shell uname -s))
88SO_EXT := $(if $(DARWIN),dylib,so)
89
90ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
91libceed := $(LIBDIR)/libceed.$(SO_EXT)
92libceed.c := $(wildcard ceed*.c)
93BACKENDS := /cpu/self/ref /cpu/self/tmpl /cpu/self/opt
94
95# Tests
96tests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
97tests.f   := $(sort $(wildcard tests/t[0-9][0-9]-*.f))
98tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
99ctests    := $(tests)
100tests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
101#examples
102examples.c := $(sort $(wildcard examples/ceed/*.c))
103examples.f := $(sort $(wildcard examples/ceed/*.f))
104examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
105examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
106# backends/[ref & occa  & magma]
107ref.c      := $(sort $(wildcard backends/ref/*.c))
108template.c := $(sort $(wildcard backends/template/*.c))
109optimized.c:= $(sort $(wildcard backends/optimized/*.c))
110occa.c     := $(sort $(wildcard backends/occa/*.c))
111magma_preprocessor := python backends/magma/gccm.py
112magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
113magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
114magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
115magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
116magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
117magma_allsrc.cu:= $(magma_tmp.cu)
118
119# Output using the 216-color rules mode
120rule_file = $(notdir $(1))
121rule_path = $(patsubst %/,%,$(dir $(1)))
122last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
123ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
124emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
125color_out = @if [ -t 1 ]; then \
126				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
127					$(1) $(call ansicolor,$(2)) \
128					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
129				printf "  %10s %s\n" $(1) $(2); fi
130# if TERM=dumb, use it, otherwise switch to the term one
131output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
132
133# if V is set to non-nil, turn the verbose mode
134quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
135
136.SUFFIXES:
137.SUFFIXES: .c .o .d
138.SECONDEXPANSION: # to expand $$(@D)/.DIR
139
140.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
141
142%/.DIR :
143	@mkdir -p $(@D)
144	@touch $@
145
146.PRECIOUS: %/.DIR
147
148this: $(libceed) $(ceed.pc)
149# run 'this' target in parallel
150all:;@$(MAKE) $(MFLAGS) V=$(V) this
151
152$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
153
154libceed.c += $(ref.c)
155libceed.c += $(template.c)
156libceed.c += $(optimized.c)
157ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
158  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
159  $(libceed) : LDLIBS += -locca
160  libceed.c += $(occa.c)
161  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
162  BACKENDS += /cpu/occa /gpu/occa /omp/occa /ocl/occa
163endif
164ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
165  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
166  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
167  ifneq ($(CUDA_LIB_DIR),)
168  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
169  omp_link = -fopenmp
170  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
171  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
172  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
173  magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o)
174  $(libceed)           : LDLIBS += $(magma_link)
175  $(tests) $(examples) : LDLIBS += $(magma_link)
176  $(libceed) : $(magma_allsrc.o)
177  libceed.c  += $(magma_allsrc.c)
178  libceed.cu += $(magma_allsrc.cu)
179  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
180  $(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
181  BACKENDS += /gpu/magma
182  endif
183endif
184
185export BACKENDS
186
187# generate magma_tmp.c and magma_cuda.cu from magma.c
188$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR
189	$(magma_preprocessor) $<
190
191$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
192	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
193
194$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
195	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
196
197$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
198	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
199
200$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
201	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
202
203$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
204	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
205
206$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
207	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
208
209$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
210	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
211
212$(tests) $(examples) : $(libceed)
213$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
214
215run-% : $(OBJDIR)/%
216	@tests/tap.sh $(<:build/%=%)
217
218test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
219# run test target in parallel
220tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
221# CPU C tests only for backend %
222ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
223
224prove : $(tests) $(examples)
225	$(info Testing backends: $(BACKENDS))
226	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
227# run prove target in parallel
228prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
229
230examples : $(examples)
231
232$(ceed.pc) : pkgconfig-prefix = $(abspath .)
233$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
234.INTERMEDIATE : $(OBJDIR)/ceed.pc
235%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
236	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
237
238# The occa executable is not linked with RPATH by default, so we need it to find its libocca.so
239OCCA               := $(if $(DARWIN),DYLD_LIBRARY_PATH,LD_LIBRARY_PATH)=$(OCCA_DIR)/lib $(OCCA_DIR)/bin/occa
240OKL_KERNELS        := $(wildcard backends/occa/*.okl)
241
242okl-cache :
243	$(OCCA) cache ceed $(OKL_KERNELS)
244
245okl-clear:
246	$(OCCA) clear -y -l ceed
247
248install : $(libceed) $(OBJDIR)/ceed.pc
249	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(okldir)" "$(DESTDIR)$(pkgconfigdir)"
250	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
251	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
252	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
253	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
254	$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/"
255
256.PHONY : all cln clean print test tst prove prv examples style install doc okl-cache okl-clear
257
258cln clean :
259	$(RM) *.o *.d $(libceed)
260	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
261	$(MAKE) -C examples/ceed clean
262	$(MAKE) -C examples/mfem clean
263	$(MAKE) -C examples/petsc clean
264	(cd examples/nek5000 && bash make-nek-examples.sh clean)
265	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
266
267distclean : clean
268	rm -rf doc/html
269
270doc :
271	doxygen Doxyfile
272
273style :
274	astyle --style=google --indent=spaces=2 --max-code-length=80 \
275            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
276            --suffix=none --preserve-date --formatted \
277            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp
278
279print :
280	@echo $(VAR)=$($(VAR))
281
282print-% :
283	$(info [ variable name]: $*)
284	$(info [        origin]: $(origin $*))
285	$(info [         value]: $(value $*))
286	$(info [expanded value]: $($*))
287	$(info )
288	@true
289
290-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
291