xref: /libCEED/Makefile (revision 68c150f8dda028788705fdc197cd4952635a794a)
1# Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2# All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# This file is part of CEED:  http://github.com/ceed
7
8CONFIG ?= config.mk
9-include $(CONFIG)
10COMMON ?= common.mk
11-include $(COMMON)
12
13ifeq (,$(filter-out undefined default,$(origin CC)))
14  CC = gcc
15endif
16ifeq (,$(filter-out undefined default,$(origin CXX)))
17  CXX = g++
18endif
19ifeq (,$(filter-out undefined default,$(origin FC)))
20  FC = gfortran
21endif
22ifeq (,$(filter-out undefined default,$(origin LINK)))
23  LINK = $(CC)
24endif
25ifeq (,$(filter-out undefined default,$(origin AR)))
26  AR = ar
27endif
28ifeq (,$(filter-out undefined default,$(origin ARFLAGS)))
29  ARFLAGS = crD
30endif
31NVCC ?= $(CUDA_DIR)/bin/nvcc
32NVCC_CXX ?= $(CXX)
33HIPCC ?= $(HIP_DIR)/bin/hipcc
34SED ?= sed
35
36# ASAN must be left empty if you don't want to use it
37ASAN ?=
38
39# These are the values automatically detected here in the makefile. They are
40# augmented with LDFLAGS and LDLIBS from the environment/passed by command line,
41# if any. If the user sets CEED_LDFLAGS or CEED_LDLIBS, they are used *instead
42# of* what we populate here (thus that's advanced usage and not recommended).
43CEED_LDFLAGS ?=
44CEED_LDLIBS ?=
45
46UNDERSCORE ?= 1
47
48# Verbose mode, V or VERBOSE
49V ?= $(VERBOSE)
50
51# MFEM_DIR env variable should point to sibling directory
52ifneq ($(wildcard ../mfem/libmfem.*),)
53  MFEM_DIR ?= ../mfem
54endif
55
56# NEK5K_DIR env variable should point to sibling directory
57ifneq ($(wildcard ../Nek5000/*),)
58  NEK5K_DIR ?= $(abspath ../Nek5000)
59endif
60export NEK5K_DIR
61MPI ?= 1
62
63# CEED_DIR env for NEK5K testing
64export CEED_DIR = $(abspath .)
65
66# XSMM_DIR env variable should point to XSMM master (github.com/hfp/libxsmm)
67XSMM_DIR ?= ../libxsmm
68
69# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
70OCCA_DIR ?= ../occa
71
72# env variable MAGMA_DIR can be used too
73MAGMA_DIR ?= ../magma
74
75# Often /opt/cuda or /usr/local/cuda, but sometimes present on machines that don't support CUDA
76CUDA_DIR  ?=
77CUDA_ARCH ?=
78
79# Often /opt/rocm, but sometimes present on machines that don't support HIP
80HIP_DIR ?=
81HIP_ARCH ?=
82
83# Check for PETSc in ../petsc
84ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
85  PETSC_DIR ?= ../petsc
86endif
87
88# Warning: SANTIZ options still don't run with /gpu/occa
89# export LSAN_OPTIONS=suppressions=.asanignore
90AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
91
92# Note: Intel oneAPI C/C++ compiler is now icx/icpx
93CC_VENDOR := $(subst icc_orig,icc,$(subst oneAPI,icc,$(firstword $(filter gcc clang icc icc_orig oneAPI XL,$(subst -, ,$(shell $(CC) --version))))))
94FC_VENDOR := $(if $(FC),$(firstword $(filter GNU ifort XL,$(shell $(FC) --version 2>&1 || $(FC) -qversion))))
95
96# Default extra flags by vendor
97MARCHFLAG.gcc           := -march=native
98MARCHFLAG.clang         := $(MARCHFLAG.gcc)
99MARCHFLAG.icc           :=
100OMP_SIMD_FLAG.gcc       := -fopenmp-simd
101OMP_SIMD_FLAG.clang     := $(OMP_SIMD_FLAG.gcc)
102OMP_SIMD_FLAG.icc       := -qopenmp-simd
103OPT.gcc                 := -ffp-contract=fast
104OPT.clang               := $(OPT.gcc)
105CFLAGS.gcc              := -fPIC -std=c99 -Wall -Wextra -Wno-unused-parameter -MMD -MP
106CFLAGS.clang            := $(CFLAGS.gcc)
107CFLAGS.icc              := $(CFLAGS.gcc)
108CFLAGS.XL               := -qpic -MMD
109CXXFLAGS.gcc            := -fPIC -std=c++11 -Wall -Wextra -Wno-unused-parameter -MMD -MP
110CXXFLAGS.clang          := $(CXXFLAGS.gcc)
111CXXFLAGS.icc            := $(CXXFLAGS.gcc)
112CXXFLAGS.XL             := -qpic -std=c++11 -MMD
113FFLAGS.GNU              := -fPIC -cpp -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -MMD -MP
114FFLAGS.ifort            := -fPIC -cpp
115FFLAGS.XL               := -qpic -ffree-form -qpreprocess -qextname -MMD
116
117# This check works with compilers that use gcc and clang.  It fails with some
118# compilers; e.g., xlc apparently ignores all options when -E is passed, thus
119# succeeds with any flags.  Users can pass MARCHFLAG=... if desired.
120cc_check_flag = $(shell $(CC) -E -Werror $(1) -x c /dev/null > /dev/null 2>&1 && echo 1)
121MARCHFLAG := $(MARCHFLAG.$(CC_VENDOR))
122MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG),-mcpu=native)
123MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG))
124
125OMP_SIMD_FLAG := $(OMP_SIMD_FLAG.$(CC_VENDOR))
126OMP_SIMD_FLAG := $(if $(call cc_check_flag,$(OMP_SIMD_FLAG)),$(OMP_SIMD_FLAG))
127
128OPT    ?= -O -g $(MARCHFLAG) $(OPT.$(CC_VENDOR)) $(OMP_SIMD_FLAG)
129CFLAGS ?= $(OPT) $(CFLAGS.$(CC_VENDOR))
130CXXFLAGS ?= $(OPT) $(CXXFLAGS.$(CC_VENDOR))
131LIBCXX ?= -lstdc++
132NVCCFLAGS ?= -ccbin $(CXX) -Xcompiler "$(OPT)" -Xcompiler -fPIC
133ifneq ($(CUDA_ARCH),)
134  NVCCFLAGS += -arch=$(CUDA_ARCH)
135endif
136HIPCCFLAGS ?= $(filter-out $(OMP_SIMD_FLAG),$(OPT)) -fPIC -munsafe-fp-atomics
137ifneq ($(HIP_ARCH),)
138  HIPCCFLAGS += --amdgpu-target=$(HIP_ARCH)
139endif
140FFLAGS ?= $(OPT) $(FFLAGS.$(FC_VENDOR))
141
142ifeq ($(COVERAGE), 1)
143  CFLAGS += --coverage
144  CXXFLAGS += --coverage
145  CEED_LDFLAGS += --coverage
146endif
147
148CFLAGS += $(if $(ASAN),$(AFLAGS))
149FFLAGS += $(if $(ASAN),$(AFLAGS))
150CEED_LDFLAGS += $(if $(ASAN),$(AFLAGS))
151CPPFLAGS += -I./include
152CEED_LDLIBS = -lm
153OBJDIR := build
154for_install := $(filter install,$(MAKECMDGOALS))
155LIBDIR := $(if $(for_install),$(OBJDIR),lib)
156
157
158# Installation variables
159prefix ?= /usr/local
160bindir = $(prefix)/bin
161libdir = $(prefix)/lib
162includedir = $(prefix)/include
163pkgconfigdir = $(libdir)/pkgconfig
164INSTALL = install
165INSTALL_PROGRAM = $(INSTALL)
166INSTALL_DATA = $(INSTALL) -m644
167
168# Get number of processors of the machine
169NPROCS := $(shell getconf _NPROCESSORS_ONLN)
170# prepare make options to run in parallel
171MFLAGS := -j $(NPROCS) --warn-undefined-variables \
172                       --no-print-directory --no-keep-going
173
174PYTHON ?= python3
175PROVE ?= prove
176PROVE_OPTS ?= -j $(NPROCS)
177DARWIN := $(filter Darwin,$(shell uname -s))
178SO_EXT := $(if $(DARWIN),dylib,so)
179
180ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
181libceed.so := $(LIBDIR)/libceed.$(SO_EXT)
182libceed.a := $(LIBDIR)/libceed.a
183libceed := $(if $(STATIC),$(libceed.a),$(libceed.so))
184CEED_LIBS = -lceed
185libceed.c := $(filter-out interface/ceed-cuda.c interface/ceed-hip.c interface/ceed-jit-source-root-$(if $(for_install),default,install).c, $(wildcard interface/ceed*.c backends/*.c gallery/*.c))
186gallery.c := $(wildcard gallery/*/ceed*.c)
187libceed.c += $(gallery.c)
188libceeds = $(libceed)
189BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
190BACKENDS_MAKE := $(BACKENDS_BUILTIN)
191TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
192
193# Tests
194tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
195tests.f   := $(if $(FC),$(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90)))
196tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
197ctests    := $(tests)
198tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
199# Examples
200examples.c := $(sort $(wildcard examples/ceed/*.c))
201examples.f := $(if $(FC),$(sort $(wildcard examples/ceed/*.f)))
202examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
203examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
204# MFEM Examples
205mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
206mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
207# Nek5K Examples
208nekexamples  := $(OBJDIR)/nek-bps
209# PETSc Examples
210petscexamples.c := $(wildcard examples/petsc/*.c)
211petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
212# Fluid Dynamics Examples
213fluidsexamples.c := $(sort $(wildcard examples/fluids/*.c))
214fluidsexamples  := $(fluidsexamples.c:examples/fluids/%.c=$(OBJDIR)/fluids-%)
215# Solid Mechanics Examples
216solidsexamples.c := $(sort $(wildcard examples/solids/*.c))
217solidsexamples   := $(solidsexamples.c:examples/solids/%.c=$(OBJDIR)/solids-%)
218
219# Backends/[ref, blocked, memcheck, opt, avx, occa, magma]
220ref.c          := $(sort $(wildcard backends/ref/*.c))
221blocked.c      := $(sort $(wildcard backends/blocked/*.c))
222ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
223opt.c          := $(sort $(wildcard backends/opt/*.c))
224avx.c          := $(sort $(wildcard backends/avx/*.c))
225xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
226cuda.c         := $(sort $(wildcard backends/cuda/*.c))
227cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
228cuda-ref.c     := $(sort $(wildcard backends/cuda-ref/*.c))
229cuda-ref.cpp   := $(sort $(wildcard backends/cuda-ref/*.cpp))
230cuda-ref.cu    := $(sort $(wildcard backends/cuda-ref/kernels/*.cu))
231cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
232cuda-shared.cu := $(sort $(wildcard backends/cuda-shared/kernels/*.cu))
233cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
234cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
235cuda-gen.cu    := $(sort $(wildcard backends/cuda-gen/kernels/*.cu))
236occa.cpp       := $(sort $(shell find backends/occa -type f -name *.cpp))
237magma.c        := $(sort $(wildcard backends/magma/*.c))
238magma.cpp      := $(sort $(wildcard backends/magma/*.cpp))
239magma.cu       := $(sort $(wildcard backends/magma/kernels/cuda/*.cu))
240magma.hip      := $(sort $(wildcard backends/magma/kernels/hip/*.hip.cpp))
241hip.c          := $(sort $(wildcard backends/hip/*.c))
242hip.cpp        := $(sort $(wildcard backends/hip/*.cpp))
243hip-ref.c      := $(sort $(wildcard backends/hip-ref/*.c))
244hip-ref.cpp    := $(sort $(wildcard backends/hip-ref/*.cpp))
245hip-ref.hip    := $(sort $(wildcard backends/hip-ref/kernels/*.hip.cpp))
246hip-shared.c   := $(sort $(wildcard backends/hip-shared/*.c))
247hip-gen.c      := $(sort $(wildcard backends/hip-gen/*.c))
248hip-gen.cpp    := $(sort $(wildcard backends/hip-gen/*.cpp))
249
250# Quiet, color output
251quiet ?= $($(1))
252
253# Cancel built-in and old-fashioned implicit rules which we don't use
254.SUFFIXES:
255
256.SECONDEXPANSION: # to expand $$(@D)/.DIR
257
258%/.DIR :
259	@mkdir -p $(@D)
260	@touch $@
261
262.PRECIOUS: %/.DIR
263
264lib: $(libceed) $(ceed.pc)
265# run 'lib' target in parallel
266par:;@$(MAKE) $(MFLAGS) V=$(V) lib
267backend_status = $(if $(filter $1,$(BACKENDS_MAKE)), [backends: $1], [not found])
268info:
269	$(info ------------------------------------)
270	$(info CC            = $(CC))
271	$(info CXX           = $(CXX))
272	$(info FC            = $(FC))
273	$(info CPPFLAGS      = $(CPPFLAGS))
274	$(info CFLAGS        = $(value CFLAGS))
275	$(info CXXFLAGS      = $(value CXXFLAGS))
276	$(info FFLAGS        = $(value FFLAGS))
277	$(info NVCCFLAGS     = $(value NVCCFLAGS))
278	$(info HIPCCFLAGS    = $(value HIPCCFLAGS))
279	$(info CEED_LDFLAGS  = $(value CEED_LDFLAGS))
280	$(info CEED_LDLIBS   = $(value CEED_LDLIBS))
281	$(info AR            = $(AR))
282	$(info ARFLAGS       = $(ARFLAGS))
283	$(info OPT           = $(OPT))
284	$(info AFLAGS        = $(AFLAGS))
285	$(info ASAN          = $(or $(ASAN),(empty)))
286	$(info VERBOSE       = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
287	$(info ------------------------------------)
288	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,$(MEMCHK_BACKENDS)))
289	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,$(AVX_BACKENDS)))
290	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,$(XSMM_BACKENDS)))
291	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,$(OCCA_BACKENDS)))
292	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,$(MAGMA_BACKENDS)))
293	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
294	$(info HIP_DIR       = $(HIP_DIR)$(call backend_status,$(HIP_BACKENDS)))
295	$(info ------------------------------------)
296	$(info MFEM_DIR      = $(MFEM_DIR))
297	$(info NEK5K_DIR     = $(NEK5K_DIR))
298	$(info PETSC_DIR     = $(PETSC_DIR))
299	$(info ------------------------------------)
300	$(info prefix        = $(prefix))
301	$(info includedir    = $(value includedir))
302	$(info libdir        = $(value libdir))
303	$(info pkgconfigdir  = $(value pkgconfigdir))
304	$(info ------------------------------------)
305	@true
306info-backends:
307	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
308	@true
309info-backends-all:
310	$(info make: 'lib' with backends: $(filter-out $(TEST_BACKENDS),$(BACKENDS)))
311	@true
312
313$(libceed.so) : CEED_LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed.so)))
314
315# Standard Backends
316libceed.c += $(ref.c)
317libceed.c += $(blocked.c)
318libceed.c += $(opt.c)
319
320# Memcheck Backend
321MEMCHK_STATUS = Disabled
322MEMCHK := $(shell echo "$(HASH)include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
323MEMCHK_BACKENDS = /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
324ifeq ($(MEMCHK),1)
325  MEMCHK_STATUS = Enabled
326  libceed.c += $(ceedmemcheck.c)
327  BACKENDS_MAKE += $(MEMCHK_BACKENDS)
328endif
329
330# AVX Backed
331AVX_STATUS = Disabled
332AVX_FLAG := $(if $(filter clang,$(CC_VENDOR)),+avx,-mavx)
333AVX := $(filter $(AVX_FLAG),$(shell $(CC) $(CFLAGS) -v -E -x c /dev/null 2>&1))
334AVX_BACKENDS = /cpu/self/avx/serial /cpu/self/avx/blocked
335ifneq ($(AVX),)
336  AVX_STATUS = Enabled
337  libceed.c += $(avx.c)
338  BACKENDS_MAKE += $(AVX_BACKENDS)
339endif
340
341# Collect list of libraries and paths for use in linking and pkg-config
342PKG_LIBS =
343# Stubs that will not be RPATH'd
344PKG_STUBS_LIBS =
345
346# libXSMM Backends
347XSMM_BACKENDS = /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
348ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
349  PKG_LIBS += -L$(abspath $(XSMM_DIR))/lib -lxsmm -ldl
350  MKL ?=
351  ifeq (,$(MKL)$(MKLROOT))
352    BLAS_LIB = -lblas
353  else
354    ifneq ($(MKLROOT),)
355      # Some installs put everything inside an intel64 subdirectory, others not
356      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
357      MKL_LINK = -L$(MKL_LIBDIR)
358      PKG_LIB_DIRS += $(MKL_LIBDIR)
359    endif
360    BLAS_LIB = $(MKL_LINK) -Wl,--push-state,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl -Wl,--pop-state
361  endif
362  PKG_LIBS += $(BLAS_LIB)
363  libceed.c += $(xsmm.c)
364  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
365  BACKENDS_MAKE += $(XSMM_BACKENDS)
366endif
367
368# OCCA Backends
369OCCA_BACKENDS = /cpu/self/occa
370ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
371  OCCA_MODES := $(shell $(OCCA_DIR)/bin/occa modes)
372  OCCA_BACKENDS += $(if $(filter OpenMP,$(OCCA_MODES)),/cpu/openmp/occa)
373# OCCA_BACKENDS += $(if $(filter OpenCL,$(OCCA_MODES)),/gpu/opencl/occa)
374  OCCA_BACKENDS += $(if $(filter HIP,$(OCCA_MODES)),/gpu/hip/occa)
375  OCCA_BACKENDS += $(if $(filter CUDA,$(OCCA_MODES)),/gpu/cuda/occa)
376
377  $(libceeds) : CPPFLAGS += -I$(OCCA_DIR)/include
378  PKG_LIBS += -L$(abspath $(OCCA_DIR))/lib -locca
379  LIBCEED_CONTAINS_CXX = 1
380  libceed.cpp += $(occa.cpp)
381  BACKENDS_MAKE += $(OCCA_BACKENDS)
382endif
383
384# CUDA Backends
385ifneq ($(CUDA_DIR),)
386	CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64 lib/x86_64-linux-gnu,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
387	CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
388endif
389CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
390CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/shared /gpu/cuda/gen
391ifneq ($(CUDA_LIB_DIR),)
392  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
393  PKG_LIBS += -L$(abspath $(CUDA_LIB_DIR)) -lcudart -lnvrtc -lcuda -lcublas
394  PKG_STUBS_LIBS += -L$(CUDA_LIB_DIR_STUBS)
395  LIBCEED_CONTAINS_CXX = 1
396  libceed.c     += interface/ceed-cuda.c
397  libceed.c     += $(cuda.c) $(cuda-ref.c) $(cuda-shared.c) $(cuda-gen.c)
398  libceed.cpp   += $(cuda.cpp) $(cuda-ref.cpp) $(cuda-gen.cpp)
399  libceed.cu    += $(cuda-ref.cu) $(cuda-shared.cu) $(cuda-gen.cu)
400  BACKENDS_MAKE += $(CUDA_BACKENDS)
401endif
402
403# HIP Backends
404HIP_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(HIP_DIR)/$d/libamdhip64.${SO_EXT}))
405HIP_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(HIP_LIB_DIR))))
406HIP_BACKENDS = /gpu/hip/ref /gpu/hip/shared /gpu/hip/gen
407ifneq ($(HIP_LIB_DIR),)
408  $(libceeds) : HIPCCFLAGS += -I./include
409  ifneq ($(CXX), $(HIPCC))
410    CPPFLAGS += $(subst =,,$(shell $(HIP_DIR)/bin/hipconfig -C))
411  endif
412  $(libceeds) : CPPFLAGS += -I$(HIP_DIR)/include
413  PKG_LIBS += -L$(abspath $(HIP_LIB_DIR)) -lamdhip64 -lhipblas
414  LIBCEED_CONTAINS_CXX = 1
415  libceed.c     += interface/ceed-hip.c
416  libceed.c     += $(hip.c) $(hip-ref.c) $(hip-shared.c) $(hip-gen.c)
417  libceed.cpp   += $(hip.cpp) $(hip-ref.cpp) $(hip-gen.cpp)
418  libceed.hip   += $(hip-ref.hip)
419  BACKENDS_MAKE += $(HIP_BACKENDS)
420endif
421
422# MAGMA Backend
423ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
424  MAGMA_ARCH=$(shell nm -g $(MAGMA_DIR)/lib/libmagma.* | grep -c "hipblas")
425  ifeq ($(MAGMA_ARCH), 0) #CUDA MAGMA
426    ifneq ($(CUDA_LIB_DIR),)
427      cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
428      omp_link = -fopenmp
429      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
430      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
431      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
432      PKG_LIBS += $(magma_link)
433      libceed.c   += $(magma.c)
434      libceed.cpp += $(magma.cpp)
435      libceed.cu  += $(magma.cu)
436      $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
437      $(magma.cpp:%.cpp=$(OBJDIR)/%.o) $(magma.cpp:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
438      $(magma.cu:%.cu=$(OBJDIR)/%.o) : CPPFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(CUDA_DIR)/include
439      MAGMA_BACKENDS = /gpu/cuda/magma /gpu/cuda/magma/det
440    endif
441  else  # HIP MAGMA
442    ifneq ($(HIP_LIB_DIR),)
443      hip_link = -Wl,-rpath,$(HIP_LIB_DIR) -L$(HIP_LIB_DIR) -lhipblas -lhipsparse -lamdhip64
444      omp_link = -fopenmp
445      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(hip_link) $(omp_link)
446      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
447      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
448      PKG_LIBS += $(magma_link)
449      libceed.c   += $(magma.c)
450      libceed.cpp += $(magma.cpp)
451      libceed.hip += $(magma.hip)
452      ifneq ($(CXX), $(HIPCC))
453        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
454        $(magma.cpp:%.cpp=$(OBJDIR)/%.o) $(magma.cpp:%=%.tidy) : CPPFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
455      else
456        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
457        $(magma.cpp:%.cpp=$(OBJDIR)/%.o) $(magma.cpp:%=%.tidy) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
458      endif
459      $(magma.hip:%.hip.cpp=$(OBJDIR)/%.o) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(HIP_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
460      MAGMA_BACKENDS = /gpu/hip/magma /gpu/hip/magma/det
461    endif
462  endif
463  LIBCEED_CONTAINS_CXX = 1
464  BACKENDS_MAKE += $(MAGMA_BACKENDS)
465endif
466
467BACKENDS ?= $(BACKENDS_MAKE)
468export BACKENDS
469
470_pkg_ldflags = $(filter -L%,$(PKG_LIBS))
471_pkg_ldlibs = $(filter-out -L%,$(PKG_LIBS))
472$(libceeds) : CEED_LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%) $(PKG_STUBS_LIBS)
473$(libceeds) : CEED_LDLIBS += $(_pkg_ldlibs)
474ifeq ($(STATIC),1)
475$(examples) $(tests) : CEED_LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%) $(PKG_STUBS_LIBS)
476$(examples) $(tests) : CEED_LDLIBS += $(_pkg_ldlibs)
477endif
478
479pkgconfig-libs-private = $(PKG_LIBS)
480ifeq ($(LIBCEED_CONTAINS_CXX),1)
481  $(libceeds) : LINK = $(CXX)
482  ifeq ($(STATIC),1)
483    $(examples) $(tests) : CEED_LDLIBS += $(LIBCXX)
484	  pkgconfig-libs-private += $(LIBCXX)
485  endif
486endif
487
488# File names *-weak.c contain weak symbol definitions, which must be listed last
489# when creating shared or static libraries.
490weak_last = $(filter-out %-weak.o,$(1)) $(filter %-weak.o,$(1))
491
492libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cpp:%.cpp=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) $(libceed.hip:%.hip.cpp=$(OBJDIR)/%.o)
493$(filter %fortran.o,$(libceed.o)) : CPPFLAGS += $(if $(filter 1,$(UNDERSCORE)),-DUNDERSCORE)
494$(libceed.o): | info-backends
495$(libceed.so) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
496	$(call quiet,LINK) $(LDFLAGS) $(CEED_LDFLAGS) -shared -o $@ $^ $(CEED_LDLIBS) $(LDLIBS)
497
498$(libceed.a) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
499	$(call quiet,AR) $(ARFLAGS) $@ $^
500
501$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
502	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
503
504$(OBJDIR)/%.o : $(CURDIR)/%.cpp | $$(@D)/.DIR
505	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
506
507$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
508	$(call quiet,NVCC) $(filter-out -Wno-unused-function, $(CPPFLAGS)) $(NVCCFLAGS) -c -o $@ $(abspath $<)
509
510$(OBJDIR)/%.o : $(CURDIR)/%.hip.cpp | $$(@D)/.DIR
511	$(call quiet,HIPCC) $(HIPCCFLAGS) -c -o $@ $(abspath $<)
512
513$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
514	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
515
516$(OBJDIR)/% : tests/%.f90 | $$(@D)/.DIR
517	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
518
519$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
520	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
521
522$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
523	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
524
525$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
526	+$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
527	  MFEM_DIR="$(abspath $(MFEM_DIR))" CXX=$(CXX) $*
528	cp examples/mfem/$* $@
529
530# Note: Multiple Nek files cannot be built in parallel. The '+' here enables
531#       this single Nek bps file to be built in parallel with other examples,
532#       such as when calling `make prove-all -j2`.
533$(OBJDIR)/nek-bps : examples/nek/bps/bps.usr examples/nek/nek-examples.sh $(libceed) | $$(@D)/.DIR
534	+$(MAKE) -C examples MPI=$(MPI) CEED_DIR=`pwd` NEK5K_DIR="$(abspath $(NEK5K_DIR))" nek
535	mv examples/nek/build/bps $(OBJDIR)/bps
536	cp examples/nek/nek-examples.sh $(OBJDIR)/nek-bps
537
538# Several executables have common utilities, but we can't build the utilities
539# from separate submake invocations because they'll compete with each
540# other/corrupt output. So we put it in this utility library, but we don't want
541# to manually list source dependencies up at this level, so we'll just always
542# call recursive make to check that this utility is up to date.
543examples/petsc/libutils.a.PHONY: $(libceed) $(ceed.pc)
544	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` AR=$(AR) ARFLAGS=$(ARFLAGS) \
545	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $(basename $(@F))
546
547$(OBJDIR)/petsc-% : examples/petsc/%.c examples/petsc/libutils.a.PHONY $(libceed) $(ceed.pc) | $$(@D)/.DIR
548	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` \
549	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
550	cp examples/petsc/$* $@
551
552$(OBJDIR)/fluids-% : examples/fluids/%.c examples/fluids/src/*.c examples/fluids/*.h examples/fluids/problems/*.c examples/fluids/qfunctions/*.h $(libceed) $(ceed.pc) | $$(@D)/.DIR
553	+$(call quiet,MAKE) -C examples/fluids CEED_DIR=`pwd` \
554	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
555	cp examples/fluids/$* $@
556
557$(OBJDIR)/solids-% : examples/solids/%.c examples/solids/%.h \
558    examples/solids/problems/*.c examples/solids/src/*.c \
559    examples/solids/include/*.h examples/solids/problems/*.h examples/solids/qfunctions/*.h \
560    $(libceed) $(ceed.pc) | $$(@D)/.DIR
561	+$(call quiet,MAKE) -C examples/solids CEED_DIR=`pwd` \
562	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
563	cp examples/solids/$* $@
564
565$(examples) : $(libceed)
566$(tests) : $(libceed)
567$(tests) $(examples) : override LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
568
569run-% : $(OBJDIR)/%
570	@tests/tap.sh $(<:$(OBJDIR)/%=%)
571
572external_examples := \
573	$(if $(MFEM_DIR),$(mfemexamples)) \
574	$(if $(PETSC_DIR),$(petscexamples)) \
575	$(if $(NEK5K_DIR),$(nekexamples)) \
576	$(if $(PETSC_DIR),$(fluidsexamples)) \
577	$(if $(PETSC_DIR),$(solidsexamples))
578
579allexamples = $(examples) $(external_examples)
580
581# The test and prove targets can be controlled via pattern searches.  The
582# default is to run tests and those examples that have no external dependencies.
583# Examples of finer grained control:
584#
585#   make test search='petsc mfem'      # PETSc and MFEM examples
586#   make prove search='t3'             # t3xx series tests
587#   make junit search='ex petsc'       # core ex* and PETSc tests
588search ?= t ex
589realsearch = $(search:%=%%)
590matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
591
592# Test core libCEED
593test : $(matched:$(OBJDIR)/%=run-%)
594
595# Run test target in parallel
596tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
597# CPU C tests only for backend %
598ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
599
600prove : $(matched)
601	$(info Testing backends: $(BACKENDS))
602	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
603# Run prove target in parallel
604prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
605
606prove-all :
607	+$(MAKE) prove realsearch=%
608
609junit-% : $(OBJDIR)/%
610	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
611
612junit : $(matched:$(OBJDIR)/%=junit-%)
613
614all: $(alltests)
615
616examples : $(allexamples)
617ceedexamples : $(examples)
618nekexamples : $(nekexamples)
619mfemexamples : $(mfemexamples)
620petscexamples : $(petscexamples)
621
622# Benchmarks
623allbenchmarks = petsc-bps
624bench_targets = $(addprefix bench-,$(allbenchmarks))
625.PHONY: $(bench_targets) benchmarks
626$(bench_targets): bench-%: $(OBJDIR)/%
627	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS_MAKE)" -r $(*).sh
628benchmarks: $(bench_targets)
629
630$(ceed.pc) : pkgconfig-prefix = $(abspath .)
631$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
632.INTERMEDIATE : $(OBJDIR)/ceed.pc
633%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
634	@$(SED) \
635	    -e "s:%prefix%:$(pkgconfig-prefix):" \
636	    -e "s:%libs_private%:$(pkgconfig-libs-private):" $< > $@
637
638$(OBJDIR)/interface/ceed-jit-source-root-default.o : CPPFLAGS += -DCEED_JIT_SOUCE_ROOT_DEFAULT="\"$(abspath ./include)/\""
639$(OBJDIR)/interface/ceed-jit-source-root-install.o : CPPFLAGS += -DCEED_JIT_SOUCE_ROOT_DEFAULT="\"$(abspath $(includedir))/\""
640
641install : $(libceed) $(OBJDIR)/ceed.pc
642	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
643	  "$(includedir)/ceed/" "$(includedir)/ceed/jit-source/"\
644	  "$(includedir)/ceed/jit-source/cuda/" "$(includedir)/ceed/jit-source/hip/"\
645	  "$(includedir)/ceed/jit-source/gallery/" "$(libdir)" "$(pkgconfigdir)")
646	$(INSTALL_DATA) include/ceed/ceed.h "$(DESTDIR)$(includedir)/ceed/"
647	$(INSTALL_DATA) include/ceed/types.h "$(DESTDIR)$(includedir)/ceed/"
648	$(INSTALL_DATA) include/ceed/ceed-f32.h "$(DESTDIR)$(includedir)/ceed/"
649	$(INSTALL_DATA) include/ceed/ceed-f64.h "$(DESTDIR)$(includedir)/ceed/"
650	$(INSTALL_DATA) include/ceed/fortran.h "$(DESTDIR)$(includedir)/ceed/"
651	$(INSTALL_DATA) include/ceed/backend.h "$(DESTDIR)$(includedir)/ceed/"
652	$(INSTALL_DATA) include/ceed/cuda.h "$(DESTDIR)$(includedir)/ceed/"
653	$(INSTALL_DATA) include/ceed/hip.h "$(DESTDIR)$(includedir)/ceed/"
654	$(INSTALL_DATA) include/ceed/hash.h "$(DESTDIR)$(includedir)/ceed/"
655	$(INSTALL_DATA) include/ceed/khash.h "$(DESTDIR)$(includedir)/ceed/"
656	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
657	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
658	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
659	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
660	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/cuda/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/cuda/"
661	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/hip/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/hip/"
662	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/gallery/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/gallery/"
663
664.PHONY : all cln clean doxygen doc lib install par print test tst prove prv prove-all junit examples style style-c style-py tidy iwyu info info-backends info-backends-all
665
666cln clean :
667	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
668	$(call quiet,MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
669	$(call quiet,MAKE) -C python/tests clean
670	$(RM) benchmarks/*output.txt
671
672distclean : clean
673	$(RM) -r doc/html doc/sphinx/build $(CONFIG)
674
675DOXYGEN ?= doxygen
676doxygen :
677	$(DOXYGEN) -q Doxyfile
678
679doc-html doc-latexpdf doc-epub doc-livehtml : doc-% : doxygen
680	make -C doc/sphinx $*
681
682doc : doc-html
683
684style-c :
685	@astyle --options=.astylerc \
686          $(filter-out include/ceedf.h $(wildcard tests/t*-f.h), \
687            $(wildcard include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
688              examples/*/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp gallery/*/*.[ch]))
689
690AUTOPEP8 = autopep8
691style-py : AUTOPEP8_ARGS = --in-place --aggressive
692style-py :
693	@$(AUTOPEP8) $(AUTOPEP8_ARGS) $(wildcard *.py python**/*.py python/tests/*.py examples**/*.py doc/sphinx/source**/*.py benchmarks/*.py)
694
695style : style-c style-py
696
697CLANG_TIDY ?= clang-tidy
698
699%.c.tidy : %.c
700	$(CLANG_TIDY) $(TIDY_OPTS) $^ -- $(CPPFLAGS) --std=c99 -I$(CUDA_DIR)/include -I$(HIP_DIR)/include -DCEED_JIT_SOUCE_ROOT_DEFAULT="\"$(abspath ./include)/\""
701
702%.cpp.tidy : %.cpp
703	$(CLANG_TIDY) $(TIDY_OPTS) $^ -- $(CPPFLAGS) --std=c++11 -I$(CUDA_DIR)/include -I$(OCCA_DIR)/include -I$(HIP_DIR)/include
704
705tidy_c   : $(libceed.c:%=%.tidy)
706tidy_cpp : $(libceed.cpp:%=%.tidy)
707
708tidy : tidy_c tidy_cpp
709
710ifneq ($(wildcard ../iwyu/*),)
711  IWYU_DIR ?= ../iwyu
712  IWYU_CC  ?= $(IWYU_DIR)/build/bin/include-what-you-use
713endif
714
715iwyu : CC=$(IWYU_CC)
716iwyu : lib
717
718print :
719	@echo $(VAR)=$($(VAR))
720
721print-% :
722	$(info [ variable name]: $*)
723	$(info [        origin]: $(origin $*))
724	$(info [        flavor]: $(flavor $*))
725	$(info [         value]: $(value $*))
726	$(info [expanded value]: $($*))
727	$(info )
728	@true
729
730# "make configure" detects any variables passed on the command line or
731# previously set in config.mk, caching them in config.mk as simple
732# (:=) variables.  Variables set in config.mk or on the command line
733# take precedence over the defaults provided in the file.  Typical
734# usage:
735#
736#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
737#   make
738#   make prove
739#
740# The values in the file can be updated by passing them on the command
741# line, e.g.,
742#
743#   make configure CC=/path/to/other/clang
744
745# All variables to consider for caching
746CONFIG_VARS = CC CXX FC NVCC NVCC_CXX HIPCC \
747	OPT CFLAGS CPPFLAGS CXXFLAGS FFLAGS NVCCFLAGS HIPCCFLAGS \
748	AR ARFLAGS LDFLAGS LDLIBS LIBCXX SED \
749	MAGMA_DIR OCCA_DIR XSMM_DIR CUDA_DIR CUDA_ARCH MFEM_DIR PETSC_DIR NEK5K_DIR HIP_DIR HIP_ARCH
750
751# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
752# was set on the command line or in config.mk (where it will appear as
753# a simple variable).
754needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
755
756configure :
757	$(file > $(CONFIG))
758	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
759	@echo "Configuration cached in $(CONFIG):"
760	@cat $(CONFIG)
761
762wheel : export MARCHFLAG = -march=generic
763wheel : export WHEEL_PLAT = manylinux2010_x86_64
764wheel :
765	docker run -it --user $(shell id -u):$(shell id -g) --rm -v $(PWD):/io -w /io \
766		-e MARCHFLAG -e WHEEL_PLAT \
767		quay.io/pypa/$(WHEEL_PLAT) python/make-wheels.sh
768
769.PHONY : configure wheel
770
771# Include *.d deps when not -B = --always-make: useful if the paths are wonky in a container
772-include $(if $(filter B,$(MAKEFLAGS)),,$(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d))
773