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