xref: /libCEED/Makefile (revision 4e015973f3c9f2e3adbb5b892ef639de661995f7)
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)
19COMMON ?= common.mk
20-include $(COMMON)
21
22ifeq (,$(filter-out undefined default,$(origin CC)))
23  CC = gcc
24endif
25ifeq (,$(filter-out undefined default,$(origin CXX)))
26  CXX = g++
27endif
28ifeq (,$(filter-out undefined default,$(origin FC)))
29  FC = gfortran
30endif
31ifeq (,$(filter-out undefined default,$(origin LINK)))
32  LINK = $(CC)
33endif
34ifeq (,$(filter-out undefined default,$(origin AR)))
35  AR = ar
36endif
37ifeq (,$(filter-out undefined default,$(origin ARFLAGS)))
38  ARFLAGS = crD
39endif
40NVCC ?= $(CUDA_DIR)/bin/nvcc
41NVCC_CXX ?= $(CXX)
42HIPCC ?= $(HIP_DIR)/bin/hipcc
43SED ?= sed
44
45# ASAN must be left empty if you don't want to use it
46ASAN ?=
47
48LDFLAGS ?=
49UNDERSCORE ?= 1
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 oneAPI,icc,$(patsubst gcc%,gcc,$(firstword $(filter gcc% clang icc oneAPI XL,$(shell $(CC) --version)))))
94FC_VENDOR := $(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
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  LDFLAGS += --coverage
146endif
147
148CFLAGS += $(if $(ASAN),$(AFLAGS))
149FFLAGS += $(if $(ASAN),$(AFLAGS))
150LDFLAGS += $(if $(ASAN),$(AFLAGS))
151CPPFLAGS += -I./include
152LDLIBS = -lm
153OBJDIR := build
154LIBDIR := lib
155
156# Installation variables
157prefix ?= /usr/local
158bindir = $(prefix)/bin
159libdir = $(prefix)/lib
160includedir = $(prefix)/include
161pkgconfigdir = $(libdir)/pkgconfig
162INSTALL = install
163INSTALL_PROGRAM = $(INSTALL)
164INSTALL_DATA = $(INSTALL) -m644
165
166# Get number of processors of the machine
167NPROCS := $(shell getconf _NPROCESSORS_ONLN)
168# prepare make options to run in parallel
169MFLAGS := -j $(NPROCS) --warn-undefined-variables \
170                       --no-print-directory --no-keep-going
171
172PYTHON ?= python3
173PROVE ?= prove
174PROVE_OPTS ?= -j $(NPROCS)
175DARWIN := $(filter Darwin,$(shell uname -s))
176SO_EXT := $(if $(DARWIN),dylib,so)
177
178ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
179libceed.so := $(LIBDIR)/libceed.$(SO_EXT)
180libceed.a := $(LIBDIR)/libceed.a
181libceed := $(if $(STATIC),$(libceed.a),$(libceed.so))
182CEED_LIBS = -lceed
183libceed.c := $(filter-out interface/ceed-cuda.c interface/ceed-hip.c, $(wildcard interface/ceed*.c backends/*.c gallery/*.c))
184gallery.c := $(wildcard gallery/*/ceed*.c)
185libceed.c += $(gallery.c)
186libceed_test.so := $(LIBDIR)/libceed_test.$(SO_EXT)
187libceed_test.a := $(LIBDIR)/libceed_test.a
188libceed_test := $(if $(STATIC),$(libceed_test.a),$(libceed_test.so))
189libceeds = $(libceed) $(libceed_test)
190BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
191BACKENDS_MAKE := $(BACKENDS_BUILTIN)
192TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
193
194# Tests
195tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
196tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90))
197tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
198ctests    := $(tests)
199tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
200# Examples
201examples.c := $(sort $(wildcard examples/ceed/*.c))
202examples.f := $(sort $(wildcard examples/ceed/*.f))
203examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
204examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
205# MFEM Examples
206mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
207mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
208# Nek5K Examples
209nekexamples  := $(OBJDIR)/nek-bps
210# PETSc Examples
211petscexamples.c := $(wildcard examples/petsc/*.c)
212petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
213# Fluid Dynamics Examples
214fluidsexamples.c := $(sort $(wildcard examples/fluids/*.c))
215fluidsexamples  := $(fluidsexamples.c:examples/fluids/%.c=$(OBJDIR)/fluids-%)
216# Solid Mechanics Examples
217solidsexamples.c := $(sort $(wildcard examples/solids/*.c))
218solidsexamples   := $(solidsexamples.c:examples/solids/%.c=$(OBJDIR)/solids-%)
219
220# Backends/[ref, blocked, template, memcheck, opt, avx, occa, magma]
221ref.c          := $(sort $(wildcard backends/ref/*.c))
222blocked.c      := $(sort $(wildcard backends/blocked/*.c))
223template.c     := $(sort $(wildcard backends/template/*.c))
224ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
225opt.c          := $(sort $(wildcard backends/opt/*.c))
226avx.c          := $(sort $(wildcard backends/avx/*.c))
227xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
228cuda.c         := $(sort $(wildcard backends/cuda/*.c))
229cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
230cuda.cu        := $(sort $(wildcard backends/cuda/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.cu       := $(sort $(wildcard backends/magma/kernels/cuda/*.cu))
239magma.hip      := $(sort $(wildcard backends/magma/kernels/hip/*.hip.cpp))
240hip.c          := $(sort $(wildcard backends/hip/*.c))
241hip.cpp        := $(sort $(wildcard backends/hip/*.cpp))
242hip.hip        := $(sort $(wildcard backends/hip/kernels/*.hip.cpp))
243hip-shared.c   := $(sort $(wildcard backends/hip-shared/*.c))
244hip-gen.c      := $(sort $(wildcard backends/hip-gen/*.c))
245hip-gen.cpp    := $(sort $(wildcard backends/hip-gen/*.cpp))
246
247# Quiet, color output
248quiet ?= $($(1))
249
250# Cancel built-in and old-fashioned implicit rules which we don't use
251.SUFFIXES:
252
253.SECONDEXPANSION: # to expand $$(@D)/.DIR
254
255%/.DIR :
256	@mkdir -p $(@D)
257	@touch $@
258
259.PRECIOUS: %/.DIR
260
261lib: $(libceed) $(ceed.pc)
262# run 'lib' target in parallel
263par:;@$(MAKE) $(MFLAGS) V=$(V) lib
264backend_status = $(if $(filter $1,$(BACKENDS_MAKE)), [backends: $1], [not found])
265info:
266	$(info ------------------------------------)
267	$(info CC            = $(CC))
268	$(info CXX           = $(CXX))
269	$(info FC            = $(FC))
270	$(info CPPFLAGS      = $(CPPFLAGS))
271	$(info CFLAGS        = $(value CFLAGS))
272	$(info CXXFLAGS      = $(value CXXFLAGS))
273	$(info FFLAGS        = $(value FFLAGS))
274	$(info NVCCFLAGS     = $(value NVCCFLAGS))
275	$(info HIPCCFLAGS    = $(value HIPCCFLAGS))
276	$(info LDFLAGS       = $(value LDFLAGS))
277	$(info LDLIBS        = $(LDLIBS))
278	$(info AR            = $(AR))
279	$(info ARFLAGS       = $(ARFLAGS))
280	$(info OPT           = $(OPT))
281	$(info AFLAGS        = $(AFLAGS))
282	$(info ASAN          = $(or $(ASAN),(empty)))
283	$(info V             = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
284	$(info ------------------------------------)
285	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,$(MEMCHK_BACKENDS)))
286	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,$(AVX_BACKENDS)))
287	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,$(XSMM_BACKENDS)))
288	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,$(OCCA_BACKENDS)))
289	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,$(MAGMA_BACKENDS)))
290	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
291	$(info HIP_DIR       = $(HIP_DIR)$(call backend_status,$(HIP_BACKENDS)))
292	$(info ------------------------------------)
293	$(info MFEM_DIR      = $(MFEM_DIR))
294	$(info NEK5K_DIR     = $(NEK5K_DIR))
295	$(info PETSC_DIR     = $(PETSC_DIR))
296	$(info ------------------------------------)
297	$(info prefix        = $(prefix))
298	$(info includedir    = $(value includedir))
299	$(info libdir        = $(value libdir))
300	$(info pkgconfigdir  = $(value pkgconfigdir))
301	$(info ------------------------------------)
302	@true
303info-backends:
304	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
305	@true
306info-backends-all:
307	$(info make: 'lib' with backends: $(filter-out $(TEST_BACKENDS),$(BACKENDS)))
308	@true
309
310$(libceed.so) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed.so)))
311$(libceed_test) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed_test)))
312
313# Standard Backends
314libceed.c += $(ref.c)
315libceed.c += $(blocked.c)
316libceed.c += $(opt.c)
317
318# Testing Backends
319test_backends.c := $(template.c)
320
321# Memcheck Backend
322MEMCHK_STATUS = Disabled
323MEMCHK := $(shell echo "$(HASH)include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
324MEMCHK_BACKENDS = /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
325ifeq ($(MEMCHK),1)
326  MEMCHK_STATUS = Enabled
327  libceed.c += $(ceedmemcheck.c)
328  BACKENDS_MAKE += $(MEMCHK_BACKENDS)
329endif
330
331# AVX Backed
332AVX_STATUS = Disabled
333AVX_FLAG := $(if $(filter clang,$(CC_VENDOR)),+avx,-mavx)
334AVX := $(filter $(AVX_FLAG),$(shell $(CC) $(OPT) -v -E -x c /dev/null 2>&1))
335AVX_BACKENDS = /cpu/self/avx/serial /cpu/self/avx/blocked
336ifneq ($(AVX),)
337  AVX_STATUS = Enabled
338  libceed.c += $(avx.c)
339  BACKENDS_MAKE += $(AVX_BACKENDS)
340endif
341
342# Collect list of libraries and paths for use in linking and pkg-config
343PKG_LIBS =
344
345# libXSMM Backends
346XSMM_BACKENDS = /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
347ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
348  PKG_LIBS += -L$(abspath $(XSMM_DIR))/lib -lxsmm -ldl
349  MKL ?=
350  ifeq (,$(MKL)$(MKLROOT))
351    BLAS_LIB = -lblas
352  else
353    ifneq ($(MKLROOT),)
354      # Some installs put everything inside an intel64 subdirectory, others not
355      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
356      MKL_LINK = -L$(MKL_LIBDIR)
357      PKG_LIB_DIRS += $(MKL_LIBDIR)
358    endif
359    BLAS_LIB = $(MKL_LINK) -Wl,--push-state,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl -Wl,--pop-state
360  endif
361  PKG_LIBS += $(BLAS_LIB)
362  libceed.c += $(xsmm.c)
363  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
364  BACKENDS_MAKE += $(XSMM_BACKENDS)
365endif
366
367# OCCA Backends
368OCCA_BACKENDS = /cpu/self/occa
369ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
370  OCCA_MODES := $(shell $(OCCA_DIR)/bin/occa modes)
371  OCCA_BACKENDS += $(if $(filter OpenMP,$(OCCA_MODES)),/cpu/openmp/occa)
372# OCCA_BACKENDS += $(if $(filter OpenCL,$(OCCA_MODES)),/gpu/opencl/occa)
373  OCCA_BACKENDS += $(if $(filter HIP,$(OCCA_MODES)),/gpu/hip/occa)
374  OCCA_BACKENDS += $(if $(filter CUDA,$(OCCA_MODES)),/gpu/cuda/occa)
375
376  $(libceeds) : CPPFLAGS += -I$(OCCA_DIR)/include
377  PKG_LIBS += -L$(abspath $(OCCA_DIR))/lib -locca
378  LIBCEED_CONTAINS_CXX = 1
379  libceed.cpp += $(occa.cpp)
380  BACKENDS_MAKE += $(OCCA_BACKENDS)
381endif
382
383# CUDA Backends
384CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
385CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
386CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
387CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/shared /gpu/cuda/gen
388ifneq ($(CUDA_LIB_DIR),)
389  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
390  PKG_LIBS += -L$(abspath $(CUDA_LIB_DIR)) -lcudart -lnvrtc -lcuda -lcublas
391  LIBCEED_CONTAINS_CXX = 1
392  libceed.c     += interface/ceed-cuda.c
393  libceed.c     += $(cuda.c) $(cuda-shared.c) $(cuda-gen.c)
394  libceed.cpp   += $(cuda.cpp) $(cuda-gen.cpp)
395  libceed.cu    += $(cuda.cu) $(cuda-shared.cu) $(cuda-gen.cu)
396  BACKENDS_MAKE += $(CUDA_BACKENDS)
397endif
398
399# HIP Backends
400HIP_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(HIP_DIR)/$d/libamdhip64.${SO_EXT}))
401HIP_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(HIP_LIB_DIR))))
402HIP_BACKENDS = /gpu/hip/ref /gpu/hip/shared /gpu/hip/gen
403ifneq ($(HIP_LIB_DIR),)
404  $(libceeds) : HIPCCFLAGS += -I./include
405  ifneq ($(CXX), $(HIPCC))
406    CPPFLAGS += $(subst =,,$(shell $(HIP_DIR)/bin/hipconfig -C))
407  endif
408  $(libceeds) : CPPFLAGS += -I$(HIP_DIR)/include
409  PKG_LIBS += -L$(abspath $(HIP_LIB_DIR)) -lamdhip64 -lhipblas
410  LIBCEED_CONTAINS_CXX = 1
411  libceed.c     += interface/ceed-hip.c
412  libceed.c     += $(hip.c) $(hip-shared.c) $(hip-gen.c)
413  libceed.cpp   += $(hip.cpp) $(hip-gen.cpp)
414  libceed.hip   += $(hip.hip)
415  BACKENDS_MAKE += $(HIP_BACKENDS)
416endif
417
418# MAGMA Backend
419ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
420  MAGMA_ARCH=$(shell nm -g $(MAGMA_DIR)/lib/libmagma.* | grep -c "hipblas")
421  ifeq ($(MAGMA_ARCH), 0) #CUDA MAGMA
422    ifneq ($(CUDA_LIB_DIR),)
423      cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
424      omp_link = -fopenmp
425      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
426      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
427      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
428      PKG_LIBS += $(magma_link)
429      libceed.c  += $(magma.c)
430      libceed.cu += $(magma.cu)
431      $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
432      $(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
433      MAGMA_BACKENDS = /gpu/cuda/magma /gpu/cuda/magma/det
434    endif
435  else  # HIP MAGMA
436    ifneq ($(HIP_LIB_DIR),)
437      hip_link = -Wl,-rpath,$(HIP_LIB_DIR) -L$(HIP_LIB_DIR) -lhipblas -lhipsparse -lamdhip64
438      omp_link = -fopenmp
439      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(hip_link) $(omp_link)
440      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
441      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
442      PKG_LIBS += $(magma_link)
443      libceed.c  += $(magma.c)
444      libceed.hip += $(magma.hip)
445      ifneq ($(CXX), $(HIPCC))
446        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
447      else
448        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
449      endif
450      $(magma.hip:%.hip.cpp=$(OBJDIR)/%.o) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
451      MAGMA_BACKENDS = /gpu/hip/magma /gpu/hip/magma/det
452    endif
453  endif
454  LIBCEED_CONTAINS_CXX = 1
455  BACKENDS_MAKE += $(MAGMA_BACKENDS)
456endif
457
458BACKENDS_ENV ?= BACKENDS
459ifneq ($(BACKENDS_ENV),)
460  BACKENDS = $(BACKENDS_MAKE)
461  export BACKENDS
462endif
463
464_pkg_ldflags = $(filter -L%,$(PKG_LIBS))
465_pkg_ldlibs = $(filter-out -L%,$(PKG_LIBS))
466$(libceeds) : LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%)
467$(libceeds) : LDLIBS += $(_pkg_ldlibs)
468ifeq ($(STATIC),1)
469$(examples) $(tests) : LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%)
470$(examples) $(tests) : LDLIBS += $(_pkg_ldlibs)
471endif
472
473pkgconfig-libs-private = $(PKG_LIBS)
474ifeq ($(LIBCEED_CONTAINS_CXX),1)
475  $(libceeds) : LINK = $(CXX)
476  ifeq ($(STATIC),1)
477    $(examples) $(tests) : LDLIBS += $(LIBCXX)
478	  pkgconfig-libs-private += $(LIBCXX)
479  endif
480endif
481
482# File names *-weak.c contain weak symbol definitions, which must be listed last
483# when creating shared or static libraries.
484weak_last = $(filter-out %-weak.o,$(1)) $(filter %-weak.o,$(1))
485
486libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cpp:%.cpp=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) $(libceed.hip:%.hip.cpp=$(OBJDIR)/%.o)
487$(filter %fortran.o,$(libceed.o)) : CPPFLAGS += $(if $(filter 1,$(UNDERSCORE)),-DUNDERSCORE)
488$(libceed.o): | info-backends
489$(libceed.so) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
490	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
491
492$(libceed.a) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
493	$(call quiet,AR) $(ARFLAGS) $@ $^
494
495$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
496	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
497
498$(OBJDIR)/%.o : $(CURDIR)/%.cpp | $$(@D)/.DIR
499	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
500
501$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
502	$(call quiet,NVCC) $(filter-out -Wno-unused-function, $(CPPFLAGS)) $(NVCCFLAGS) -c -o $@ $(abspath $<)
503
504$(OBJDIR)/%.o : $(CURDIR)/%.hip.cpp | $$(@D)/.DIR
505	$(call quiet,HIPCC) $(HIPCCFLAGS) -c -o $@ $(abspath $<)
506
507$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
508	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
509
510$(OBJDIR)/% : tests/%.f90 | $$(@D)/.DIR
511	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
512
513$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
514	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
515
516$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
517	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
518
519$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
520	+$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
521	  MFEM_DIR="$(abspath $(MFEM_DIR))" CXX=$(CXX) $*
522	cp examples/mfem/$* $@
523
524# Note: Multiple Nek files cannot be built in parallel. The '+' here enables
525#       this single Nek bps file to be built in parallel with other examples,
526#       such as when calling `make prove-all -j2`.
527$(OBJDIR)/nek-bps : examples/nek/bps/bps.usr examples/nek/nek-examples.sh $(libceed) | $$(@D)/.DIR
528	+$(MAKE) -C examples MPI=$(MPI) CEED_DIR=`pwd` NEK5K_DIR="$(abspath $(NEK5K_DIR))" nek
529	mv examples/nek/build/bps $(OBJDIR)/bps
530	cp examples/nek/nek-examples.sh $(OBJDIR)/nek-bps
531
532$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
533	+$(MAKE) -C examples/petsc CEED_DIR=`pwd` \
534	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
535	cp examples/petsc/$* $@
536
537$(OBJDIR)/fluids-% : examples/fluids/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
538	+$(MAKE) -C examples/fluids CEED_DIR=`pwd` \
539	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
540	cp examples/fluids/$* $@
541
542$(OBJDIR)/solids-% : examples/solids/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
543	+$(MAKE) -C examples/solids CEED_DIR=`pwd` \
544	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
545	cp examples/solids/$* $@
546
547libceed_test.o = $(test_backends.c:%.c=$(OBJDIR)/%.o)
548$(libceed_test.so) : $(call weak_last,$(libceed.o) $(libceed_test.o)) | $$(@D)/.DIR
549	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
550
551$(libceed_test.a) : $(call weak_last,$(libceed.o) $(libceed_test.o)) | $$(@D)/.DIR
552	$(call quiet,AR) $(ARFLAGS) $@ $^
553
554$(examples) : $(libceed)
555$(tests) : $(libceed_test)
556$(tests) : CEED_LIBS = -lceed_test
557$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
558
559run-t% : BACKENDS_MAKE += $(TEST_BACKENDS)
560run-% : $(OBJDIR)/%
561	@tests/tap.sh $(<:$(OBJDIR)/%=%)
562
563external_examples := \
564	$(if $(MFEM_DIR),$(mfemexamples)) \
565	$(if $(PETSC_DIR),$(petscexamples)) \
566	$(if $(NEK5K_DIR),$(nekexamples)) \
567	$(if $(PETSC_DIR),$(fluidsexamples)) \
568	$(if $(PETSC_DIR),$(solidsexamples))
569
570allexamples = $(examples) $(external_examples)
571
572# The test and prove targets can be controlled via pattern searches.  The
573# default is to run tests and those examples that have no external dependencies.
574# Examples of finer grained control:
575#
576#   make test search='petsc mfem'      # PETSc and MFEM examples
577#   make prove search='t3'             # t3xx series tests
578#   make junit search='ex petsc'       # core ex* and PETSc tests
579search ?= t ex
580realsearch = $(search:%=%%)
581matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
582
583# Test core libCEED
584test : $(matched:$(OBJDIR)/%=run-%)
585
586# Run test target in parallel
587tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
588# CPU C tests only for backend %
589ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
590
591prove : BACKENDS_MAKE += $(TEST_BACKENDS)
592prove : $(matched)
593	$(info Testing backends: $(BACKENDS))
594	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
595# Run prove target in parallel
596prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
597
598prove-all :
599	+$(MAKE) prove realsearch=%
600
601junit-t% : BACKENDS_MAKE += $(TEST_BACKENDS)
602junit-% : $(OBJDIR)/%
603	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
604
605junit : $(matched:$(OBJDIR)/%=junit-%)
606
607all: $(alltests)
608
609examples : $(allexamples)
610ceedexamples : $(examples)
611nekexamples : $(nekexamples)
612mfemexamples : $(mfemexamples)
613petscexamples : $(petscexamples)
614
615# Benchmarks
616allbenchmarks = petsc-bps
617bench_targets = $(addprefix bench-,$(allbenchmarks))
618.PHONY: $(bench_targets) benchmarks
619$(bench_targets): bench-%: $(OBJDIR)/%
620	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS_MAKE)" -r $(*).sh
621benchmarks: $(bench_targets)
622
623$(ceed.pc) : pkgconfig-prefix = $(abspath .)
624$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
625.INTERMEDIATE : $(OBJDIR)/ceed.pc
626%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
627	@$(SED) \
628	    -e "s:%prefix%:$(pkgconfig-prefix):" \
629	    -e "s:%libs_private%:$(pkgconfig-libs-private):" $< > $@
630
631install : $(libceed) $(OBJDIR)/ceed.pc
632	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
633	  "$(includedir)/ceed/" "$(libdir)" "$(pkgconfigdir)")
634	$(INSTALL_DATA) include/ceed/ceed.h "$(DESTDIR)$(includedir)/ceed/"
635	$(INSTALL_DATA) include/ceed/fortran.h "$(DESTDIR)$(includedir)/ceed/"
636	$(INSTALL_DATA) include/ceed/backend.h "$(DESTDIR)$(includedir)/ceed/"
637	$(INSTALL_DATA) include/ceed/cuda.h "$(DESTDIR)$(includedir)/ceed/"
638	$(INSTALL_DATA) include/ceed/hip.h "$(DESTDIR)$(includedir)/ceed/"
639	$(INSTALL_DATA) include/ceed/hash.h "$(DESTDIR)$(includedir)/ceed/"
640	$(INSTALL_DATA) include/ceed/khash.h "$(DESTDIR)$(includedir)/ceed/"
641	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
642	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
643	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
644	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
645	$(INSTALL_DATA) include/ceed-backend.h "$(DESTDIR)$(includedir)/"
646	$(INSTALL_DATA) include/ceed-hash.h "$(DESTDIR)$(includedir)/"
647	$(INSTALL_DATA) include/ceed-khash.h "$(DESTDIR)$(includedir)/"
648
649.PHONY : all cln clean doxygen doc lib install par print test tst prove prv prove-all junit examples style style-c style-py tidy info info-backends info-backends-all
650
651cln clean :
652	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
653	$(MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
654	$(MAKE) -C python/tests clean
655	$(RM) benchmarks/*output.txt
656
657distclean : clean
658	$(RM) -r doc/html doc/sphinx/build $(CONFIG)
659
660DOXYGEN ?= doxygen
661doxygen :
662	$(DOXYGEN) Doxyfile
663
664doc-html doc-latexpdf doc-epub doc-livehtml : doc-% : doxygen
665	make -C doc/sphinx $*
666
667doc : doc-html
668
669style-c :
670	@astyle --options=.astylerc \
671          $(filter-out include/ceedf.h $(wildcard tests/t*-f.h), \
672            $(wildcard include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
673              examples/*/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp gallery/*/*.[ch]))
674
675AUTOPEP8 = autopep8
676style-py : AUTOPEP8_ARGS = --in-place --aggressive
677style-py :
678	@$(AUTOPEP8) $(AUTOPEP8_ARGS) $(wildcard *.py python**/*.py python/tests/*.py examples**/*.py doc/sphinx/source**/*.py benchmarks/*.py)
679
680style : style-c style-py
681
682CLANG_TIDY ?= clang-tidy
683
684%.c.tidy : %.c
685	$(CLANG_TIDY) $(TIDY_OPTS) $^ -- $(CPPFLAGS) --std=c99 -I$(CUDA_DIR)/include -I$(HIP_DIR)/include
686
687%.cpp.tidy : %.cpp
688	$(CLANG_TIDY) $(TIDY_OPTS) $^ -- $(CPPFLAGS) --std=c++11 -I$(CUDA_DIR)/include -I$(OCCA_DIR)/include -I$(HIP_DIR)/include
689
690tidy_c   : $(libceed.c:%=%.tidy)
691tidy_cpp : $(libceed.cpp:%=%.tidy)
692
693tidy : tidy_c tidy_cpp
694
695print :
696	@echo $(VAR)=$($(VAR))
697
698print-% :
699	$(info [ variable name]: $*)
700	$(info [        origin]: $(origin $*))
701	$(info [        flavor]: $(flavor $*))
702	$(info [         value]: $(value $*))
703	$(info [expanded value]: $($*))
704	$(info )
705	@true
706
707# "make configure" detects any variables passed on the command line or
708# previously set in config.mk, caching them in config.mk as simple
709# (:=) variables.  Variables set in config.mk or on the command line
710# take precedence over the defaults provided in the file.  Typical
711# usage:
712#
713#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
714#   make
715#   make prove
716#
717# The values in the file can be updated by passing them on the command
718# line, e.g.,
719#
720#   make configure CC=/path/to/other/clang
721
722# All variables to consider for caching
723CONFIG_VARS = CC CXX FC NVCC NVCC_CXX HIPCC \
724	OPT CFLAGS CPPFLAGS CXXFLAGS FFLAGS NVCCFLAGS HIPCCFLAGS \
725	AR ARFLAGS LDFLAGS LDLIBS LIBCXX SED \
726	MAGMA_DIR OCCA_DIR XSMM_DIR CUDA_DIR CUDA_ARCH MFEM_DIR PETSC_DIR NEK5K_DIR HIP_DIR HIP_ARCH
727
728# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
729# was set on the command line or in config.mk (where it will appear as
730# a simple variable).
731needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
732
733configure :
734	$(file > $(CONFIG))
735	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
736	@echo "Configuration cached in $(CONFIG):"
737	@cat $(CONFIG)
738
739wheel : export MARCHFLAG = -march=generic
740wheel : export WHEEL_PLAT = manylinux2010_x86_64
741wheel :
742	docker run -it --user $(shell id -u):$(shell id -g) --rm -v $(PWD):/io -w /io \
743		-e MARCHFLAG -e WHEEL_PLAT \
744		quay.io/pypa/$(WHEEL_PLAT) python/make-wheels.sh
745
746.PHONY : configure wheel
747
748# Include *.d deps when not -B = --always-make: useful if the paths are wonky in a container
749-include $(if $(filter B,$(MAKEFLAGS)),,$(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d))
750