xref: /libCEED/Makefile (revision 9f0427d99e9674f1e08f64878fc1ceefe3e53022)
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/libmfem.*),)
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 -march=native -ffp-contract=fast
50CFLAGS = -std=c99 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
51NVCCFLAGS = -Xcompiler "$(OPT)" -Xcompiler -fPIC
52# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately:
53ifneq ($(filter %xlf %xlf_r,$(FC)),)
54  FFLAGS = $(OPT) -ffree-form -qpreprocess -qextname -qpic -MMD
55else # gfortran/Intel-style options
56  FFLAGS = -cpp     $(OPT) -ffree-form -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP
57endif
58
59ifeq ($(UNDERSCORE), 1)
60  CFLAGS += -DUNDERSCORE
61endif
62
63ifeq ($(COVERAGE), 1)
64  CFLAGS += --coverage
65  LDFLAGS += --coverage
66endif
67
68CFLAGS += $(if $(ASAN),$(AFLAGS))
69FFLAGS += $(if $(ASAN),$(AFLAGS))
70LDFLAGS += $(if $(ASAN),$(AFLAGS))
71CPPFLAGS = -I./include
72LDLIBS = -lm
73OBJDIR := build
74LIBDIR := lib
75
76# Installation variables
77prefix ?= /usr/local
78bindir = $(prefix)/bin
79libdir = $(prefix)/lib
80okldir = $(libdir)/okl
81includedir = $(prefix)/include
82pkgconfigdir = $(libdir)/pkgconfig
83INSTALL = install
84INSTALL_PROGRAM = $(INSTALL)
85INSTALL_DATA = $(INSTALL) -m644
86
87# Get number of processors of the machine
88NPROCS := $(shell getconf _NPROCESSORS_ONLN)
89# prepare make options to run in parallel
90MFLAGS := -j $(NPROCS) --warn-undefined-variables \
91                       --no-print-directory --no-keep-going
92
93PROVE ?= prove
94PROVE_OPTS ?= -j $(NPROCS)
95DARWIN := $(filter Darwin,$(shell uname -s))
96SO_EXT := $(if $(DARWIN),dylib,so)
97
98ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
99libceed := $(LIBDIR)/libceed.$(SO_EXT)
100libceed.c := $(wildcard interface/ceed*.c)
101BACKENDS_BUILTIN := /cpu/self/ref /cpu/self/tmpl /cpu/self/blocked
102BACKENDS := $(BACKENDS_BUILTIN)
103
104# Tests
105tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
106tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f))
107tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
108ctests    := $(tests)
109tests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
110#examples
111examples.c := $(sort $(wildcard examples/ceed/*.c))
112examples.f := $(sort $(wildcard examples/ceed/*.f))
113examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
114examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
115#mfemexamples
116mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
117mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
118petscexamples.c := $(sort $(wildcard examples/petsc/*.c))
119petscexamples  := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
120
121# backends/[ref, template, blocked, avx, occa, magma]
122ref.c      := $(sort $(wildcard backends/ref/*.c))
123template.c := $(sort $(wildcard backends/template/*.c))
124cuda.c     := $(sort $(wildcard backends/cuda/*.c))
125cuda.cu    := $(sort $(wildcard backends/cuda/*.cu))
126blocked.c  := $(sort $(wildcard backends/blocked/*.c))
127avx.c      := $(sort $(wildcard backends/avx/*.c))
128occa.c     := $(sort $(wildcard backends/occa/*.c))
129magma_preprocessor := python backends/magma/gccm.py
130magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
131magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
132magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
133magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
134magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
135magma_allsrc.cu:= $(magma_tmp.cu)
136
137# Output using the 216-color rules mode
138rule_file = $(notdir $(1))
139rule_path = $(patsubst %/,%,$(dir $(1)))
140last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
141ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
142emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
143color_out = @if [ -t 1 ]; then \
144				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
145					$(1) $(call ansicolor,$(2)) \
146					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
147				printf "  %10s %s\n" $(1) $(2); fi
148# if TERM=dumb, use it, otherwise switch to the term one
149output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
150
151# if V is set to non-nil, turn the verbose mode
152quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
153
154# Cancel built-in and old-fashioned implicit rules which we don't use
155.SUFFIXES:
156
157.SECONDEXPANSION: # to expand $$(@D)/.DIR
158
159.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
160
161%/.DIR :
162	@mkdir -p $(@D)
163	@touch $@
164
165.PRECIOUS: %/.DIR
166
167lib: $(libceed) $(ceed.pc)
168# run 'lib' target in parallel
169all:;@$(MAKE) $(MFLAGS) V=$(V) lib
170backend_status = $(if $(filter $1,$(BACKENDS)), [backends: $1], [not found])
171info:
172	$(info ------------------------------------)
173	$(info CC         = $(CC))
174	$(info FC         = $(FC))
175	$(info CPPFLAGS   = $(CPPFLAGS))
176	$(info CFLAGS     = $(value CFLAGS))
177	$(info FFLAGS     = $(value FFLAGS))
178	$(info NVCCFLAGS  = $(value NVCCFLAGS))
179	$(info LDFLAGS    = $(value LDFLAGS))
180	$(info LDLIBS     = $(LDLIBS))
181	$(info OPT        = $(OPT))
182	$(info AFLAGS     = $(AFLAGS))
183	$(info ASAN       = $(or $(ASAN),(empty)))
184	$(info V          = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
185	$(info ------------------------------------)
186	$(info AVX_STATUS = $(AVX_STATUS)$(call backend_status,/cpu/self/avx))
187	$(info OCCA_DIR   = $(OCCA_DIR)$(call backend_status,/cpu/occa /gpu/occa /omp/occa))
188	$(info MAGMA_DIR  = $(MAGMA_DIR)$(call backend_status,/gpu/magma))
189	$(info CUDA_DIR   = $(CUDA_DIR)$(call backend_status,/gpu/magma))
190	$(info ------------------------------------)
191	$(info MFEM_DIR   = $(MFEM_DIR))
192	$(info PETSC_DIR  = $(PETSC_DIR))
193	$(info ------------------------------------)
194	$(info prefix       = $(prefix))
195	$(info includedir   = $(value includedir))
196	$(info libdir       = $(value libdir))
197	$(info okldir       = $(value okldir))
198	$(info pkgconfigdir = $(value pkgconfigdir))
199	$(info ------------------------------------)
200	@true
201info-backends:
202	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
203.PHONY: lib all info info-backends
204
205$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
206
207# Standard Backends
208libceed.c += $(ref.c)
209libceed.c += $(template.c)
210libceed.c += $(blocked.c)
211
212# AVX Backed
213AVX_STATUS = Disabled
214AVX := $(shell $(CC) $(CFLAGS) -v -E - < /dev/null 2>&1 | grep -c avx)
215ifeq ($(AVX),1)
216  AVX_STATUS = Enabled
217  libceed.c += $(avx.c)
218  BACKENDS += /cpu/self/avx
219endif
220
221# OCCA Backends
222ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
223  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
224  $(libceed) : LDLIBS += -locca
225  libceed.c += $(occa.c)
226  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
227  BACKENDS += /cpu/occa /gpu/occa /omp/occa
228endif
229
230# Cuda Backend
231CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
232CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
233ifneq ($(CUDA_LIB_DIR),)
234  $(libceed) : CFLAGS += -I$(CUDA_DIR)/include
235  $(libceed) : LDFLAGS += -L$(CUDA_LIB_DIR) -Wl,-rpath,$(abspath $(CUDA_LIB_DIR))
236  $(libceed) : LDLIBS += -lcudart -lnvrtc -lcuda
237  libceed.c  += $(cuda.c)
238  libceed.cu += $(cuda.cu)
239  BACKENDS += /gpu/cuda
240endif
241
242# MAGMA Backend
243ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
244  ifneq ($(CUDA_LIB_DIR),)
245  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
246  omp_link = -fopenmp
247  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
248  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
249  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
250  $(libceed)           : LDLIBS += $(magma_link)
251  $(tests) $(examples) : LDLIBS += $(magma_link)
252  libceed.c  += $(magma_allsrc.c)
253  libceed.cu += $(magma_allsrc.cu)
254  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
255  $(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
256  BACKENDS += /gpu/magma
257  endif
258endif
259
260export BACKENDS
261
262# generate magma_tmp.c and magma_cuda.cu from magma.c
263%_tmp.c %_cuda.cu : %.c
264	$(magma_preprocessor) $<
265
266libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o)
267$(libceed.o): | info-backends
268$(libceed) : $(libceed.o) | $$(@D)/.DIR
269	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
270
271$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
272	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
273
274$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
275	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
276
277$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
278	$(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS)
279
280$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
281	$(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS)
282
283$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
284	$(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS)
285
286$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
287	$(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS)
288
289$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
290	$(MAKE) -C examples/mfem CEED_DIR=`pwd` $*
291	mv examples/mfem/$* $@
292
293$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
294	$(MAKE) -C examples/petsc CEED_DIR=`pwd` $*
295	mv examples/petsc/$* $@
296
297$(tests) $(examples) : $(libceed)
298$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
299
300run-% : $(OBJDIR)/%
301	@tests/tap.sh $(<:build/%=%)
302# Test core libCEED
303test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
304
305# run test target in parallel
306tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
307# CPU C tests only for backend %
308ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
309
310prove : $(tests) $(examples)
311	$(info Testing backends: $(BACKENDS))
312	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
313# run prove target in parallel
314prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
315
316alltests := $(tests) $(examples) $(if $(MFEM_DIR),$(mfemexamples)) $(if $(PETSC_DIR),$(petscexamples))
317prove-all : $(alltests)
318	$(info Testing backends: $(BACKENDS))
319	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(alltests:$(OBJDIR)/%=%)
320
321examples : $(examples)
322
323$(ceed.pc) : pkgconfig-prefix = $(abspath .)
324$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
325.INTERMEDIATE : $(OBJDIR)/ceed.pc
326%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
327	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
328
329OCCA        := $(OCCA_DIR)/bin/occa
330OKL_KERNELS := $(wildcard backends/occa/*.okl)
331
332okl-cache :
333	$(OCCA) cache ceed $(OKL_KERNELS)
334
335okl-clear:
336	$(OCCA) clear -y -l ceed
337
338install : $(libceed) $(OBJDIR)/ceed.pc
339	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
340	  "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)"))
341	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
342	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
343	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
344	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
345	$(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/")
346
347.PHONY : cln clean print test tst prove prv examples style install doc okl-cache okl-clear
348
349cln clean :
350	$(RM) -r $(OBJDIR) $(LIBDIR)
351	$(MAKE) -C examples/ceed clean
352	$(MAKE) -C examples/mfem clean
353	$(MAKE) -C examples/petsc clean
354	(cd examples/nek5000 && bash make-nek-examples.sh clean)
355	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
356
357distclean : clean
358	$(RM) -r doc/html
359
360doc :
361	doxygen Doxyfile
362
363style :
364	astyle --style=google --indent=spaces=2 --max-code-length=80 \
365            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
366            --suffix=none --preserve-date --formatted \
367            --exclude=include/ceedf.h --exclude=tests/t310-basis-f.h \
368            include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
369            examples/*/*.[ch] examples/*/*.[ch]pp -i
370
371print :
372	@echo $(VAR)=$($(VAR))
373
374print-% :
375	$(info [ variable name]: $*)
376	$(info [        origin]: $(origin $*))
377	$(info [         value]: $(value $*))
378	$(info [expanded value]: $($*))
379	$(info )
380	@true
381
382-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
383