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