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