xref: /libCEED/Makefile (revision 4d1cd9fced9d3348a69bfa814c29cdf42f409211)
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
106PROVE ?= prove
107PROVE_OPTS ?= -j $(NPROCS)
108DARWIN := $(filter Darwin,$(shell uname -s))
109SO_EXT := $(if $(DARWIN),dylib,so)
110
111ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
112libceed := $(LIBDIR)/libceed.$(SO_EXT)
113libceed.c := $(wildcard interface/ceed*.c)
114BACKENDS_BUILTIN := /cpu/self/ref /cpu/self/tmpl /cpu/self/blocked
115BACKENDS := $(BACKENDS_BUILTIN)
116
117# Tests
118tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
119tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90))
120tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
121ctests    := $(tests)
122tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
123#examples
124examples.c := $(sort $(wildcard examples/ceed/*.c))
125examples.f := $(sort $(wildcard examples/ceed/*.f))
126examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
127examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
128#mfemexamples
129mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
130mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
131#nekexamples
132nekexamples.usr := $(sort $(wildcard examples/nek5000/*.usr))
133nekexamples  := $(nekexamples.usr:examples/nek5000/%.usr=nek-%)
134#petscexamples
135petscexamples.c := $(sort $(wildcard examples/petsc/*.c))
136petscexamples  := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
137
138# backends/[ref, template, blocked, avx, occa, magma]
139ref.c      := $(sort $(wildcard backends/ref/*.c))
140template.c := $(sort $(wildcard backends/template/*.c))
141cuda.c     := $(sort $(wildcard backends/cuda/*.c))
142cuda.cu    := $(sort $(wildcard backends/cuda/*.cu))
143blocked.c  := $(sort $(wildcard backends/blocked/*.c))
144avx.c      := $(sort $(wildcard backends/avx/*.c))
145xsmm.c     := $(sort $(wildcard backends/xsmm/*.c))
146occa.c     := $(sort $(wildcard backends/occa/*.c))
147magma_preprocessor := python backends/magma/gccm.py
148magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
149magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
150magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
151magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
152magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
153magma_allsrc.cu:= $(magma_tmp.cu)
154
155# Output using the 216-color rules mode
156rule_file = $(notdir $(1))
157rule_path = $(patsubst %/,%,$(dir $(1)))
158last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
159ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
160emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
161color_out = @if [ -t 1 ]; then \
162				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
163					$(1) $(call ansicolor,$(2)) \
164					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
165				printf "  %10s %s\n" $(1) $(2); fi
166# if TERM=dumb, use it, otherwise switch to the term one
167output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
168
169# if V is set to non-nil, turn the verbose mode
170quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
171
172# Cancel built-in and old-fashioned implicit rules which we don't use
173.SUFFIXES:
174
175.SECONDEXPANSION: # to expand $$(@D)/.DIR
176
177.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
178
179%/.DIR :
180	@mkdir -p $(@D)
181	@touch $@
182
183.PRECIOUS: %/.DIR
184
185lib: $(libceed) $(ceed.pc)
186# run 'lib' target in parallel
187par:;@$(MAKE) $(MFLAGS) V=$(V) lib
188backend_status = $(if $(filter $1,$(BACKENDS)), [backends: $1], [not found])
189info:
190	$(info ------------------------------------)
191	$(info CC         = $(CC))
192	$(info FC         = $(FC))
193	$(info CPPFLAGS   = $(CPPFLAGS))
194	$(info CFLAGS     = $(value CFLAGS))
195	$(info FFLAGS     = $(value FFLAGS))
196	$(info NVCCFLAGS  = $(value NVCCFLAGS))
197	$(info LDFLAGS    = $(value LDFLAGS))
198	$(info LDLIBS     = $(LDLIBS))
199	$(info OPT        = $(OPT))
200	$(info AFLAGS     = $(AFLAGS))
201	$(info ASAN       = $(or $(ASAN),(empty)))
202	$(info V          = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
203	$(info ------------------------------------)
204	$(info AVX_STATUS = $(AVX_STATUS)$(call backend_status,/cpu/self/avx))
205	$(info XSMM_DIR   = $(XSMM_DIR)$(call backend_status,/cpu/xsmm/serial /cpu/xsmm/blocked))
206	$(info OCCA_DIR   = $(OCCA_DIR)$(call backend_status,/cpu/occa /gpu/occa /omp/occa))
207	$(info MAGMA_DIR  = $(MAGMA_DIR)$(call backend_status,/gpu/magma))
208	$(info CUDA_DIR   = $(CUDA_DIR)$(call backend_status,/gpu/magma))
209	$(info ------------------------------------)
210	$(info MFEM_DIR   = $(MFEM_DIR))
211	$(info NEK5K_DIR  = $(NEK5K_DIR))
212	$(info PETSC_DIR  = $(PETSC_DIR))
213	$(info ------------------------------------)
214	$(info prefix       = $(prefix))
215	$(info includedir   = $(value includedir))
216	$(info libdir       = $(value libdir))
217	$(info okldir       = $(value okldir))
218	$(info pkgconfigdir = $(value pkgconfigdir))
219	$(info ------------------------------------)
220	@true
221info-backends:
222	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
223.PHONY: lib all par info info-backends
224
225$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
226
227# Standard Backends
228libceed.c += $(ref.c)
229libceed.c += $(template.c)
230libceed.c += $(blocked.c)
231
232# AVX Backed
233AVX_STATUS = Disabled
234AVX := $(shell $(CC) $(CFLAGS) -v -E - < /dev/null 2>&1 | grep -c avx)
235ifeq ($(AVX),1)
236  AVX_STATUS = Enabled
237  libceed.c += $(avx.c)
238  BACKENDS += /cpu/self/avx
239endif
240
241# libXSMM Backends
242ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
243  $(libceed) : LDFLAGS += -L$(XSMM_DIR)/lib -Wl,-rpath,$(abspath $(XSMM_DIR)/lib)
244  $(libceed) : LDLIBS += -lxsmm -ldl -lblas
245  libceed.c += $(xsmm.c)
246  $(xsmm.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(XSMM_DIR)/include
247  BACKENDS += /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
248endif
249
250# OCCA Backends
251ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
252  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
253  $(libceed) : LDLIBS += -locca
254  libceed.c += $(occa.c)
255  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
256  BACKENDS += /cpu/occa /gpu/occa /omp/occa
257endif
258
259# Cuda Backend
260CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
261CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
262ifneq ($(CUDA_LIB_DIR),)
263  $(libceed) : CFLAGS += -I$(CUDA_DIR)/include
264  $(libceed) : LDFLAGS += -L$(CUDA_LIB_DIR) -Wl,-rpath,$(abspath $(CUDA_LIB_DIR))
265  $(libceed) : LDLIBS += -lcudart -lnvrtc -lcuda
266  libceed.c  += $(cuda.c)
267  libceed.cu += $(cuda.cu)
268  BACKENDS += /gpu/cuda
269endif
270
271# MAGMA Backend
272ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
273  ifneq ($(CUDA_LIB_DIR),)
274  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
275  omp_link = -fopenmp
276  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
277  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
278  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
279  $(libceed)           : LDLIBS += $(magma_link)
280  $(tests) $(examples) : LDLIBS += $(magma_link)
281  libceed.c  += $(magma_allsrc.c)
282  libceed.cu += $(magma_allsrc.cu)
283  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
284  $(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
285  BACKENDS += /gpu/magma
286  endif
287endif
288
289export BACKENDS
290
291# generate magma_tmp.c and magma_cuda.cu from magma.c
292%_tmp.c %_cuda.cu : %.c
293	$(magma_preprocessor) $<
294
295libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o)
296$(libceed.o): | info-backends
297$(libceed) : $(libceed.o) | $$(@D)/.DIR
298	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
299
300$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
301	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
302
303$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
304	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
305
306$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
307	$(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS)
308
309$(OBJDIR)/% : tests/%.f90 | $$(@D)/.DIR
310	$(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS)
311
312$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
313	$(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS)
314
315$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
316	$(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS)
317
318$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
319	$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
320	  MFEM_DIR="$(abspath $(MFEM_DIR))" $*
321	mv examples/mfem/$* $@
322
323$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
324	$(MAKE) -C examples/petsc CEED_DIR=`pwd` \
325	  PETSC_DIR="$(abspath $(PETSC_DIR))" $*
326	mv examples/petsc/$* $@
327
328$(tests) $(examples) : $(libceed)
329$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
330
331run-% : $(OBJDIR)/%
332	@tests/tap.sh $(<:build/%=%)
333# Test core libCEED
334test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
335
336# run test target in parallel
337tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
338# CPU C tests only for backend %
339ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
340
341prove : $(tests) $(examples)
342	$(info Testing backends: $(BACKENDS))
343	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
344# run prove target in parallel
345prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
346
347allexamples := $(examples) $(if $(MFEM_DIR),$(mfemexamples)) $(if $(PETSC_DIR),$(petscexamples))
348alltests := $(tests) $(allexamples)
349fulltestlist = $(alltests) $(if $(NEK5K_DIR), $(nekexamples))
350prepnektests:
351	(export CC FC && cd examples && make prepnektests)
352prove-all : $(alltests) $(if $(NEK5K_DIR), prepnektests)
353	$(info Testing backends: $(BACKENDS))
354	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(fulltestlist:$(OBJDIR)/%=%)
355
356all: $(alltests)
357
358examples : $(allexamples)
359
360# Benchmarks
361allbenchmarks = petsc-bp1 petsc-bp3
362bench_targets = $(addprefix bench-,$(allbenchmarks))
363.PHONY: $(bench_targets) benchmarks
364$(bench_targets): bench-%: $(OBJDIR)/%
365	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS)" -r $(*).sh
366benchmarks: $(bench_targets)
367
368$(ceed.pc) : pkgconfig-prefix = $(abspath .)
369$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
370.INTERMEDIATE : $(OBJDIR)/ceed.pc
371%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
372	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
373
374OCCA        := $(OCCA_DIR)/bin/occa
375OKL_KERNELS := $(wildcard backends/occa/*.okl)
376
377okl-cache :
378	$(OCCA) cache ceed $(OKL_KERNELS)
379
380okl-clear:
381	$(OCCA) clear -y -l ceed
382
383install : $(libceed) $(OBJDIR)/ceed.pc
384	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
385	  "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)"))
386	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
387	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
388	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
389	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
390	$(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/")
391
392.PHONY : cln clean doc lib install all print test tst prove prv prove-all examples style okl-cache okl-clear info info-backends
393
394cln clean :
395	$(RM) -r $(OBJDIR) $(LIBDIR)
396	$(MAKE) -C examples/ceed clean
397	$(MAKE) -C examples/mfem clean
398	$(MAKE) -C examples/petsc clean
399	(cd examples/nek5000 && bash make-nek-examples.sh clean)
400	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
401	$(RM) -rf benchmarks/*output.txt
402
403distclean : clean
404	$(RM) -r doc/html
405
406doc :
407	doxygen Doxyfile
408
409style :
410	astyle --style=google --indent=spaces=2 --max-code-length=80 \
411            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
412            --suffix=none --preserve-date --formatted \
413            --exclude=include/ceedf.h --exclude=tests/t310-basis-f.h \
414            include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
415            examples/*/*.[ch] examples/*/*.[ch]pp -i
416
417print :
418	@echo $(VAR)=$($(VAR))
419
420print-% :
421	$(info [ variable name]: $*)
422	$(info [        origin]: $(origin $*))
423	$(info [         value]: $(value $*))
424	$(info [expanded value]: $($*))
425	$(info )
426	@true
427
428-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
429