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