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