xref: /libCEED/Makefile (revision cdd1636dd97a12c0f4af39414e304c73e4c594d7) !
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
17CONFIG ?= config.mk
18-include $(CONFIG)
19
20ifeq (,$(filter-out undefined default,$(origin CC)))
21  CC = gcc
22endif
23ifeq (,$(filter-out undefined default,$(origin CXX)))
24  CXX = g++
25endif
26ifeq (,$(filter-out undefined default,$(origin FC)))
27  FC = gfortran
28endif
29ifeq (,$(filter-out undefined default,$(origin LINK)))
30  LINK = $(CC)
31endif
32NVCC ?= $(CUDA_DIR)/bin/nvcc
33NVCC_CXX ?= $(CXX)
34HIPCC ?= $(HIP_DIR)/bin/hipcc
35
36# ASAN must be left empty if you don't want to use it
37ASAN ?=
38
39LDFLAGS ?=
40UNDERSCORE ?= 1
41
42# MFEM_DIR env variable should point to sibling directory
43ifneq ($(wildcard ../mfem/libmfem.*),)
44  MFEM_DIR ?= ../mfem
45endif
46
47# NEK5K_DIR env variable should point to sibling directory
48ifneq ($(wildcard ../Nek5000/*),)
49  NEK5K_DIR ?= $(abspath ../Nek5000)
50endif
51export NEK5K_DIR
52MPI ?= 1
53
54# CEED_DIR env for NEK5K testing
55export CEED_DIR = $(abspath .)
56
57# XSMM_DIR env variable should point to XSMM master (github.com/hfp/libxsmm)
58XSMM_DIR ?= ../libxsmm
59
60# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
61OCCA_DIR ?= ../occa
62
63# env variable MAGMA_DIR can be used too
64#TODO: FIX THIS with hip detection...
65#MAGMA_DIR ?= ../magma
66# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
67#CUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
68               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
69#TODO: FIX THIS to allow for systems with CUDA and HIP
70CUDA_DIR ?= .
71HIP_DIR ?= /opt/rocm/hip
72
73# Check for PETSc in ../petsc
74ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
75  PETSC_DIR ?= ../petsc
76endif
77
78# Warning: SANTIZ options still don't run with /gpu/occa
79# export LSAN_OPTIONS=suppressions=.asanignore
80AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
81
82CC_VENDOR := $(patsubst gcc%,gcc,$(firstword $(filter gcc% clang icc XL,$(shell $(CC) --version))))
83FC_VENDOR := $(firstword $(filter GNU ifort XL,$(shell $(FC) --version 2>&1 || $(FC) -qversion)))
84
85# Default extra flags by vendor
86MARCHFLAG.gcc           := -march=native
87MARCHFLAG.clang         := $(MARCHFLAG.gcc)
88MARCHFLAG.icc           :=
89OMP_SIMD_FLAG.gcc       := -fopenmp-simd
90OMP_SIMD_FLAG.clang     := $(OMP_SIMD_FLAG.gcc)
91OMP_SIMD_FLAG.icc       := -qopenmp-simd
92OPT.gcc                 := -ffp-contract=fast
93OPT.clang               := $(OPT.gcc)
94CFLAGS.gcc              := -fPIC -std=c99 -Wall -Wextra -Wno-unused-parameter -MMD -MP
95CFLAGS.clang            := $(CFLAGS.gcc)
96CFLAGS.icc              := $(CFLAGS.gcc)
97CFLAGS.XL               := -qpic -MMD
98CXXFLAGS.gcc            := -fPIC -std=c++11 -Wall -Wextra -Wno-unused-parameter -MMD -MP
99CXXFLAGS.clang          := $(CXXFLAGS.gcc)
100CXXFLAGS.icc            := $(CXXFLAGS.gcc)
101CXXFLAGS.XL             := -qpic -MMD
102FFLAGS.GNU              := -fPIC -cpp -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -MMD -MP
103FFLAGS.ifort            := -fPIC -cpp
104FFLAGS.XL               := -qpic -ffree-form -qpreprocess -qextname -MMD
105
106# This check works with compilers that use gcc and clang.  It fails with some
107# compilers; e.g., xlc apparently ignores all options when -E is passed, thus
108# succeeds with any flags.  Users can pass MARCHFLAG=... if desired.
109cc_check_flag = $(shell $(CC) -E -Werror $(1) -x c /dev/null > /dev/null 2>&1 && echo 1)
110MARCHFLAG := $(MARCHFLAG.$(CC_VENDOR))
111MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG),-mcpu=native)
112MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG))
113
114OMP_SIMD_FLAG := $(OMP_SIMD_FLAG.$(CC_VENDOR))
115OMP_SIMD_FLAG := $(if $(call cc_check_flag,$(OMP_SIMD_FLAG)),$(OMP_SIMD_FLAG))
116
117OPT    ?= -O -g $(MARCHFLAG) $(OPT.$(CC_VENDOR)) $(OMP_SIMD_FLAG)
118CFLAGS ?= $(OPT) $(CFLAGS.$(CC_VENDOR))
119CXXFLAGS ?= $(OPT) $(CXXFLAGS.$(CC_VENDOR))
120NVCCFLAGS ?= -ccbin $(CXX) -Xcompiler "$(OPT)" -Xcompiler -fPIC
121HIPCCFLAGS ?= -g -ffp-contract=fast $(OMP_SIMD_FLAG) -Wextra -Wno-unused-parameter -fno-gpu-rdc -fPIC
122FFLAGS ?= $(OPT) $(FFLAGS.$(FC_VENDOR))
123
124ifeq ($(COVERAGE), 1)
125  CFLAGS += --coverage
126  LDFLAGS += --coverage
127endif
128
129CFLAGS += $(if $(ASAN),$(AFLAGS))
130FFLAGS += $(if $(ASAN),$(AFLAGS))
131LDFLAGS += $(if $(ASAN),$(AFLAGS))
132CPPFLAGS += -I./include
133LDLIBS = -lm
134OBJDIR := build
135LIBDIR := lib
136
137# Installation variables
138prefix ?= /usr/local
139bindir = $(prefix)/bin
140libdir = $(prefix)/lib
141okldir = $(libdir)/okl
142includedir = $(prefix)/include
143pkgconfigdir = $(libdir)/pkgconfig
144INSTALL = install
145INSTALL_PROGRAM = $(INSTALL)
146INSTALL_DATA = $(INSTALL) -m644
147
148# Get number of processors of the machine
149NPROCS := $(shell getconf _NPROCESSORS_ONLN)
150# prepare make options to run in parallel
151MFLAGS := -j $(NPROCS) --warn-undefined-variables \
152                       --no-print-directory --no-keep-going
153
154PYTHON ?= python3
155PROVE ?= prove
156PROVE_OPTS ?= -j $(NPROCS)
157DARWIN := $(filter Darwin,$(shell uname -s))
158SO_EXT := $(if $(DARWIN),dylib,so)
159
160ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
161libceed := $(LIBDIR)/libceed.$(SO_EXT)
162CEED_LIBS = -lceed
163libceed.c := $(wildcard interface/ceed*.c)
164gallery.c := $(wildcard gallery/*/ceed*.c)
165libceed.c += $(gallery.c)
166libceed_test := $(LIBDIR)/libceed_test.$(SO_EXT)
167libceeds = $(libceed) $(libceed_test)
168BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
169BACKENDS := $(BACKENDS_BUILTIN)
170
171# Tests
172tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
173tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90))
174tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
175ctests    := $(tests)
176tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
177# Examples
178examples.c := $(sort $(wildcard examples/ceed/*.c))
179examples.f := $(sort $(wildcard examples/ceed/*.f))
180examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
181examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
182# MFEM Examples
183mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
184mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
185# Nek5K Examples
186nekexamples  := $(OBJDIR)/nek-bps
187# PETSc Examples
188petscexamples.c := $(wildcard examples/petsc/*.c)
189petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
190# Fluid Dynamics Examples
191fluidsexamples.c := $(sort $(wildcard examples/fluids/*.c))
192fluidsexamples  := $(fluidsexamples.c:examples/fluids/%.c=$(OBJDIR)/fluids-%)
193# Solid Mechanics Examples
194solidsexamples.c := $(sort $(wildcard examples/solids/*.c))
195solidsexamples   := $(solidsexamples.c:examples/solids/%.c=$(OBJDIR)/solids-%)
196
197# Backends/[ref, blocked, template, memcheck, opt, avx, occa, magma]
198ref.c          := $(sort $(wildcard backends/ref/*.c))
199blocked.c      := $(sort $(wildcard backends/blocked/*.c))
200template.c     := $(sort $(wildcard backends/template/*.c))
201ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
202opt.c          := $(sort $(wildcard backends/opt/*.c))
203avx.c          := $(sort $(wildcard backends/avx/*.c))
204xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
205cuda.c         := $(sort $(wildcard backends/cuda/*.c))
206cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
207cuda.cu        := $(sort $(wildcard backends/cuda/*.cu))
208cuda-reg.c     := $(sort $(wildcard backends/cuda-reg/*.c))
209cuda-reg.cu    := $(sort $(wildcard backends/cuda-reg/*.cu))
210cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
211cuda-shared.cu := $(sort $(wildcard backends/cuda-shared/*.cu))
212cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
213cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
214cuda-gen.cu    := $(sort $(wildcard backends/cuda-gen/*.cu))
215occa.c         := $(sort $(wildcard backends/occa/*.c))
216magma.c        := $(sort $(wildcard backends/magma/*.c))
217magma.cu       := $(sort $(wildcard backends/magma/*.cu))
218hip.hip        := $(sort $(wildcard backends/hip/*.hip))
219hip.cpp        := $(sort $(wildcard backends/hip/*.cpp))
220hip.c          := $(sort $(wildcard backends/hip/*.c))
221
222# Output using the 216-color rules mode
223rule_file = $(notdir $(1))
224rule_path = $(patsubst %/,%,$(dir $(1)))
225last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
226ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
227emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
228color_out = @if [ -t 1 ]; then \
229				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
230					$(1) $(call ansicolor,$(2)) \
231					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
232				printf "  %10s %s\n" $(1) $(2); fi
233# if TERM=dumb, use it, otherwise switch to the term one
234output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
235
236# if V is set to non-nil, turn the verbose mode
237quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
238
239# Cancel built-in and old-fashioned implicit rules which we don't use
240.SUFFIXES:
241
242.SECONDEXPANSION: # to expand $$(@D)/.DIR
243
244%/.DIR :
245	@mkdir -p $(@D)
246	@touch $@
247
248.PRECIOUS: %/.DIR
249
250lib: $(libceed) $(ceed.pc)
251# run 'lib' target in parallel
252par:;@$(MAKE) $(MFLAGS) V=$(V) lib
253backend_status = $(if $(filter $1,$(BACKENDS)), [backends: $1], [not found])
254info:
255	$(info ------------------------------------)
256	$(info CC            = $(CC))
257	$(info CXX           = $(CXX))
258	$(info FC            = $(FC))
259	$(info CPPFLAGS      = $(CPPFLAGS))
260	$(info CFLAGS        = $(value CFLAGS))
261	$(info CXXFLAGS      = $(value CXXFLAGS))
262	$(info FFLAGS        = $(value FFLAGS))
263	$(info NVCCFLAGS     = $(value NVCCFLAGS))
264	$(info LDFLAGS       = $(value LDFLAGS))
265	$(info LDLIBS        = $(LDLIBS))
266	$(info OPT           = $(OPT))
267	$(info AFLAGS        = $(AFLAGS))
268	$(info ASAN          = $(or $(ASAN),(empty)))
269	$(info V             = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
270	$(info ------------------------------------)
271	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,/cpu/self/memcheck/serial /cpu/sef/memcheck/blocked))
272	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,/cpu/self/avx/serial /cpu/self/avx/blocked))
273	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,/cpu/self/xsmm/serial /cpu/self/xsmm/blocked))
274	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,/cpu/occa /gpu/occa /omp/occa))
275	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,/gpu/magma /gpu/magma/det))
276	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
277	$(info HIP_DIR      = $(HIP_DIR)$(call backend_status,$(HIP_BACKENDS)))
278	$(info ------------------------------------)
279	$(info MFEM_DIR      = $(MFEM_DIR))
280	$(info NEK5K_DIR     = $(NEK5K_DIR))
281	$(info PETSC_DIR     = $(PETSC_DIR))
282	$(info ------------------------------------)
283	$(info prefix        = $(prefix))
284	$(info includedir    = $(value includedir))
285	$(info libdir        = $(value libdir))
286	$(info okldir        = $(value okldir))
287	$(info pkgconfigdir  = $(value pkgconfigdir))
288	$(info ------------------------------------)
289	@true
290info-backends:
291	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
292.PHONY: lib all par info info-backends
293
294$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
295$(libceed_test) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed_test)))
296
297# Standard Backends
298libceed.c += $(ref.c)
299libceed.c += $(blocked.c)
300libceed.c += $(opt.c)
301
302# Testing Backends
303test_backends.c := $(template.c)
304TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
305
306# Memcheck Backend
307MEMCHK_STATUS = Disabled
308MEMCHK := $(shell echo "\#include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
309ifeq ($(MEMCHK),1)
310  MEMCHK_STATUS = Enabled
311  libceed.c += $(ceedmemcheck.c)
312  BACKENDS += /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
313endif
314
315# AVX Backed
316AVX_STATUS = Disabled
317AVX_FLAG := $(if $(filter clang,$(CC_VENDOR)),+avx,-mavx)
318AVX := $(filter $(AVX_FLAG),$(shell $(CC) $(OPT) -v -E -x c /dev/null 2>&1))
319ifneq ($(AVX),)
320  AVX_STATUS = Enabled
321  libceed.c += $(avx.c)
322  BACKENDS += /cpu/self/avx/serial /cpu/self/avx/blocked
323endif
324
325# libXSMM Backends
326ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
327  $(libceeds) : LDFLAGS += -L$(XSMM_DIR)/lib -Wl,-rpath,$(abspath $(XSMM_DIR)/lib)
328  $(libceeds) : LDLIBS += -lxsmm -ldl
329  MKL ?=
330  ifeq (,$(MKL)$(MKLROOT))
331    BLAS_LIB = -lblas
332  else
333    ifneq ($(MKLROOT),)
334      # Some installs put everything inside an intel64 subdirectory, others not
335      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
336      MKL_LINK = -L$(MKL_LIBDIR) -Wl,-rpath,$(MKL_LIBDIR)
337    endif
338    BLAS_LIB = $(MKL_LINK) -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl
339  endif
340  $(libceeds) : LDLIBS += $(BLAS_LIB)
341  libceed.c += $(xsmm.c)
342  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
343  BACKENDS += /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
344endif
345
346# OCCA Backends
347ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
348  $(libceeds) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
349  $(libceeds) : LDLIBS += -locca
350  libceed.c += $(occa.c)
351  $(occa.c:%.c=$(OBJDIR)/%.o) $(occa.c:%=%.tidy) : CPPFLAGS += -I$(OCCA_DIR)/include
352  BACKENDS += /cpu/occa /gpu/occa /omp/occa
353endif
354
355# CUDA Backends
356CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
357CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
358CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
359CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/reg /gpu/cuda/shared /gpu/cuda/gen
360ifneq ($(CUDA_LIB_DIR),)
361  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
362  $(libceeds) : LDFLAGS += -L$(CUDA_LIB_DIR) -Wl,-rpath,$(abspath $(CUDA_LIB_DIR))
363  $(libceeds) : LDLIBS += -lcudart -lnvrtc -lcuda
364  $(libceeds) : LINK = $(CXX)
365  libceed.c   += $(cuda.c) $(cuda-reg.c) $(cuda-shared.c) $(cuda-gen.c)
366  libceed.cpp += $(cuda.cpp) $(cuda-gen.cpp)
367  libceed.cu  += $(cuda.cu) $(cuda-reg.cu) $(cuda-shared.cu) $(cuda-gen.cu)
368  BACKENDS += $(CUDA_BACKENDS)
369endif
370
371
372# HIP Backends
373ROCM_DIR = $(HIP_DIR)/..
374HIP_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(HIP_DIR)/$d/libhiprtc.${SO_EXT}))
375HIP_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(HIP_LIB_DIR))))
376HIP_BACKENDS = /gpu/hip/ref
377ifneq ($(HIP_LIB_DIR),)
378  $(libceeds) : HIPCCFLAGS += -I$(HIP_DIR)/include -I./include -I$(ROCM_DIR)/include
379  ifneq ($(CXX), $(HIPCC))
380    CPPFLAGS += $(subst =,,$(shell $(HIP_DIR)/bin/hipconfig -C))
381  endif
382  $(libceeds) : LDFLAGS += -L$(HIP_LIB_DIR) -Wl,-rpath,$(abspath $(HIP_LIB_DIR))
383  $(libceeds) : LDLIBS += -lhip_hcc -lhiprtc
384  $(libceeds) : LINK = $(CXX)
385  libceed.hip += $(hip.hip)
386  libceed.cpp += $(hip.cpp)
387  libceed.c   += $(hip.c)
388  $(hip.c:%.c=$(OBJDIR)/%.o) $(hip.c:%=%.tidy) : CPPFLAGS += -I$(HIP_DIR)/include -I$(ROCM_DIR)/include
389  BACKENDS += $(HIP_BACKENDS)
390endif
391
392# MAGMA Backend
393ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
394  ifneq ($(CUDA_LIB_DIR),)
395  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
396  omp_link = -fopenmp
397  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
398  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
399  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
400  $(libceeds)           : LDLIBS += $(magma_link)
401  $(tests) $(examples) : LDLIBS += $(magma_link)
402  libceed.c  += $(magma.c)
403  libceed.cu += $(magma.cu)
404  $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
405  $(magma.cu:%.cu=$(OBJDIR)/%.o) : CPPFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
406  BACKENDS += /gpu/magma /gpu/magma/det
407  endif
408endif
409
410export BACKENDS
411
412libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cpp:%.cpp=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) $(libceed.hip:%.hip=$(OBJDIR)/%.o)
413$(filter %fortran.o,$(libceed.o)) : CPPFLAGS += $(if $(filter 1,$(UNDERSCORE)),-DUNDERSCORE)
414$(libceed.o): | info-backends
415$(libceed) : $(libceed.o) | $$(@D)/.DIR
416	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
417
418$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
419	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
420
421$(OBJDIR)/%.o : $(CURDIR)/%.cpp | $$(@D)/.DIR
422	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
423
424$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
425	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
426
427$(OBJDIR)/%.o : $(CURDIR)/%.hip | $$(@D)/.DIR
428	$(call quiet,HIPCC) $(HIPCCFLAGS) -c -o $@ $(abspath $<)
429
430$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
431	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
432
433$(OBJDIR)/% : tests/%.f90 | $$(@D)/.DIR
434	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
435
436$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
437	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
438
439$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
440	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
441
442$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
443	+$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
444	  MFEM_DIR="$(abspath $(MFEM_DIR))" CXX=$(CXX) $*
445	mv examples/mfem/$* $@
446
447# Note: Multiple Nek files cannot be built in parallel. The '+' here enables
448#       this single Nek bps file to be built in parallel with other examples,
449#       such as when calling `make prove-all -j2`.
450$(OBJDIR)/nek-bps : examples/nek/bps/bps.usr examples/nek/nek-examples.sh $(libceed) | $$(@D)/.DIR
451	+$(MAKE) -C examples MPI=$(MPI) CEED_DIR=`pwd` NEK5K_DIR="$(abspath $(NEK5K_DIR))" nek
452	mv examples/nek/build/bps $(OBJDIR)/bps
453	cp examples/nek/nek-examples.sh $(OBJDIR)/nek-bps
454
455$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
456	+$(MAKE) -C examples/petsc CEED_DIR=`pwd` \
457	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
458	mv examples/petsc/$* $@
459
460$(OBJDIR)/fluids-% : examples/fluids/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
461	+$(MAKE) -C examples/fluids CEED_DIR=`pwd` \
462	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
463	mv examples/fluids/$* $@
464
465$(OBJDIR)/solids-% : examples/solids/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
466	+$(MAKE) -C examples/solids CEED_DIR=`pwd` \
467	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
468	mv examples/solids/$* $@
469
470libceed_test.o = $(test_backends.c:%.c=$(OBJDIR)/%.o)
471$(libceed_test) : $(libceed.o) $(libceed_test.o) | $$(@D)/.DIR
472	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
473
474$(examples) : $(libceed)
475$(tests) : $(libceed_test)
476$(tests) : CEED_LIBS = -lceed_test
477$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
478
479run-t% : BACKENDS += $(TEST_BACKENDS)
480run-% : $(OBJDIR)/%
481	@tests/tap.sh $(<:$(OBJDIR)/%=%)
482
483external_examples := \
484	$(if $(MFEM_DIR),$(mfemexamples)) \
485	$(if $(PETSC_DIR),$(petscexamples)) \
486	$(if $(NEK5K_DIR),$(nekexamples)) \
487	$(if $(PETSC_DIR),$(fluidsexamples)) \
488	$(if $(PETSC_DIR),$(solidsexamples))
489
490allexamples = $(examples) $(external_examples)
491
492# The test and prove targets can be controlled via pattern searches.  The
493# default is to run tests and those examples that have no external dependencies.
494# Examples of finer grained control:
495#
496#   make test search='petsc mfem'      # PETSc and MFEM examples
497#   make prove search='t3'             # t3xx series tests
498#   make junit search='ex petsc'       # core ex* and PETSc tests
499search ?= t ex
500realsearch = $(search:%=%%)
501matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
502
503# Test core libCEED
504test : $(matched:$(OBJDIR)/%=run-%)
505
506# Run test target in parallel
507tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
508# CPU C tests only for backend %
509ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
510
511prove : BACKENDS += $(TEST_BACKENDS)
512prove : $(matched)
513	$(info Testing backends: $(BACKENDS))
514	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
515# Run prove target in parallel
516prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
517
518prove-all :
519	+$(MAKE) prove realsearch=%
520
521junit-t% : BACKENDS += $(TEST_BACKENDS)
522junit-% : $(OBJDIR)/%
523	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
524
525junit : $(matched:$(OBJDIR)/%=junit-%)
526
527all: $(alltests)
528
529examples : $(allexamples)
530ceedexamples : $(examples)
531nekexamples : $(nekexamples)
532mfemexamples : $(mfemexamples)
533petscexamples : $(petscexamples)
534
535# Benchmarks
536allbenchmarks = petsc-bps
537bench_targets = $(addprefix bench-,$(allbenchmarks))
538.PHONY: $(bench_targets) benchmarks
539$(bench_targets): bench-%: $(OBJDIR)/%
540	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS)" -r $(*).sh
541benchmarks: $(bench_targets)
542
543$(ceed.pc) : pkgconfig-prefix = $(abspath .)
544$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
545.INTERMEDIATE : $(OBJDIR)/ceed.pc
546%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
547	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
548
549OCCA        := $(OCCA_DIR)/bin/occa
550OKL_KERNELS := $(wildcard backends/occa/*.okl)
551
552okl-cache :
553	$(OCCA) cache ceed $(OKL_KERNELS)
554
555okl-clear:
556	$(OCCA) clear -y -l ceed
557
558install : $(libceed) $(OBJDIR)/ceed.pc
559	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
560	  "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)"))
561	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
562	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
563	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
564	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
565	$(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/")
566
567.PHONY : cln clean doxygen doc lib install all print test tst prove prv prove-all junit examples style style-c style-py tidy okl-cache okl-clear info info-backends
568
569cln clean :
570	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
571	$(MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
572	$(MAKE) -C tests/python clean
573	$(RM) benchmarks/*output.txt
574
575distclean : clean
576	$(RM) -r doc/html doc/sphinx/build $(CONFIG)
577
578DOXYGEN ?= doxygen
579doxygen :
580	$(DOXYGEN) Doxyfile
581
582doc-html doc-latexpdf doc-epub : doc-% : doxygen
583	make -C doc/sphinx $*
584
585doc : doc-html
586
587style-c :
588	@astyle --options=.astylerc \
589          $(filter-out include/ceedf.h tests/t320-basis-f.h, \
590            $(wildcard include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
591              examples/*/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp gallery/*/*.[ch]))
592
593AUTOPEP8 = autopep8
594style-py : AUTOPEP8_ARGS = --in-place --aggressive
595style-py :
596	@$(AUTOPEP8) $(AUTOPEP8_ARGS) $(wildcard *.py python**/*.py tests/python**/*.py examples**/*.py doc/sphinx/source**/*.py benchmarks/*.py)
597
598style : style-c style-py
599
600CLANG_TIDY ?= clang-tidy
601%.c.tidy : %.c
602	$(CLANG_TIDY) $(TIDY_OPTS) $^ -- $(CPPFLAGS) --std=c99 -I$(CUDA_DIR)/include
603
604tidy : $(libceed.c:%=%.tidy)
605
606print :
607	@echo $(VAR)=$($(VAR))
608
609print-% :
610	$(info [ variable name]: $*)
611	$(info [        origin]: $(origin $*))
612	$(info [        flavor]: $(flavor $*))
613	$(info [         value]: $(value $*))
614	$(info [expanded value]: $($*))
615	$(info )
616	@true
617
618# "make configure" detects any variables passed on the command line or
619# previously set in config.mk, caching them in config.mk as simple
620# (:=) variables.  Variables set in config.mk or on the command line
621# take precedence over the defaults provided in the file.  Typical
622# usage:
623#
624#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
625#   make
626#   make prove
627#
628# The values in the file can be updated by passing them on the command
629# line, e.g.,
630#
631#   make configure CC=/path/to/other/clang
632
633# All variables to consider for caching
634CONFIG_VARS = CC CXX FC NVCC NVCC_CXX HIPCC \
635	OPT CFLAGS CPPFLAGS CXXFLAGS FFLAGS NVCCFLAGS HIPCCFLAGS \
636	LDFLAGS LDLIBS \
637	MAGMA_DIR XSMM_DIR CUDA_DIR MFEM_DIR PETSC_DIR NEK5K_DIR HIP_DIR
638
639# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
640# was set on the command line or in config.mk (where it will appear as
641# a simple variable).
642needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
643
644configure :
645	$(file > $(CONFIG))
646	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
647	@echo "Configuration cached in $(CONFIG):"
648	@cat $(CONFIG)
649
650wheel : export MARCHFLAG = -march=generic
651wheel : export WHEEL_PLAT = manylinux2010_x86_64
652wheel :
653	docker run -it --user $(shell id -u):$(shell id -g) --rm -v $(PWD):/io -w /io \
654		-e MARCHFLAG -e WHEEL_PLAT \
655		quay.io/pypa/$(WHEEL_PLAT) python/make-wheels.sh
656
657.PHONY : configure wheel
658
659# Include *.d deps when not -B = --always-make: useful if the paths are wonky in a container
660-include $(if $(filter B,$(MAKEFLAGS)),,$(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d))
661