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