xref: /libCEED/Makefile (revision 04743b7b5fe0104955757737fc8974a8dd7b80ea)
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
48# Allow users to customize LDFLAGS (libceed appends directly to this variable)
49override LDFLAGS := $(LDFLAGS)
50
51UNDERSCORE ?= 1
52
53# Verbose mode, V or VERBOSE
54V ?= $(VERBOSE)
55
56# MFEM_DIR env variable should point to sibling directory
57ifneq ($(wildcard ../mfem/libmfem.*),)
58  MFEM_DIR ?= ../mfem
59endif
60
61# NEK5K_DIR env variable should point to sibling directory
62ifneq ($(wildcard ../Nek5000/*),)
63  NEK5K_DIR ?= $(abspath ../Nek5000)
64endif
65export NEK5K_DIR
66MPI ?= 1
67
68# CEED_DIR env for NEK5K testing
69export CEED_DIR = $(abspath .)
70
71# XSMM_DIR env variable should point to XSMM master (github.com/hfp/libxsmm)
72XSMM_DIR ?= ../libxsmm
73
74# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
75OCCA_DIR ?= ../occa
76
77# env variable MAGMA_DIR can be used too
78MAGMA_DIR ?= ../magma
79
80# Often /opt/cuda or /usr/local/cuda, but sometimes present on machines that don't support CUDA
81CUDA_DIR  ?=
82CUDA_ARCH ?=
83
84# Often /opt/rocm, but sometimes present on machines that don't support HIP
85HIP_DIR ?=
86HIP_ARCH ?=
87
88# Check for PETSc in ../petsc
89ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
90  PETSC_DIR ?= ../petsc
91endif
92
93# Warning: SANTIZ options still don't run with /gpu/occa
94# export LSAN_OPTIONS=suppressions=.asanignore
95AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
96
97# Note: Intel oneAPI C/C++ compiler is now icx/icpx
98CC_VENDOR := $(subst oneAPI,icc,$(firstword $(filter gcc clang icc oneAPI XL,$(subst -, ,$(shell $(CC) --version)))))
99FC_VENDOR := $(if $(FC),$(firstword $(filter GNU ifort XL,$(shell $(FC) --version 2>&1 || $(FC) -qversion))))
100
101# Default extra flags by vendor
102MARCHFLAG.gcc           := -march=native
103MARCHFLAG.clang         := $(MARCHFLAG.gcc)
104MARCHFLAG.icc           :=
105OMP_SIMD_FLAG.gcc       := -fopenmp-simd
106OMP_SIMD_FLAG.clang     := $(OMP_SIMD_FLAG.gcc)
107OMP_SIMD_FLAG.icc       := -qopenmp-simd
108OPT.gcc                 := -ffp-contract=fast
109OPT.clang               := $(OPT.gcc)
110CFLAGS.gcc              := -fPIC -std=c99 -Wall -Wextra -Wno-unused-parameter -MMD -MP
111CFLAGS.clang            := $(CFLAGS.gcc)
112CFLAGS.icc              := $(CFLAGS.gcc)
113CFLAGS.XL               := -qpic -MMD
114CXXFLAGS.gcc            := -fPIC -std=c++11 -Wall -Wextra -Wno-unused-parameter -MMD -MP
115CXXFLAGS.clang          := $(CXXFLAGS.gcc)
116CXXFLAGS.icc            := $(CXXFLAGS.gcc)
117CXXFLAGS.XL             := -qpic -std=c++11 -MMD
118FFLAGS.GNU              := -fPIC -cpp -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -MMD -MP
119FFLAGS.ifort            := -fPIC -cpp
120FFLAGS.XL               := -qpic -ffree-form -qpreprocess -qextname -MMD
121
122# This check works with compilers that use gcc and clang.  It fails with some
123# compilers; e.g., xlc apparently ignores all options when -E is passed, thus
124# succeeds with any flags.  Users can pass MARCHFLAG=... if desired.
125cc_check_flag = $(shell $(CC) -E -Werror $(1) -x c /dev/null > /dev/null 2>&1 && echo 1)
126MARCHFLAG := $(MARCHFLAG.$(CC_VENDOR))
127MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG),-mcpu=native)
128MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG))
129
130OMP_SIMD_FLAG := $(OMP_SIMD_FLAG.$(CC_VENDOR))
131OMP_SIMD_FLAG := $(if $(call cc_check_flag,$(OMP_SIMD_FLAG)),$(OMP_SIMD_FLAG))
132
133OPT    ?= -O -g $(MARCHFLAG) $(OPT.$(CC_VENDOR)) $(OMP_SIMD_FLAG)
134CFLAGS ?= $(OPT) $(CFLAGS.$(CC_VENDOR))
135CXXFLAGS ?= $(OPT) $(CXXFLAGS.$(CC_VENDOR))
136LIBCXX ?= -lstdc++
137NVCCFLAGS ?= -ccbin $(CXX) -Xcompiler "$(OPT)" -Xcompiler -fPIC
138ifneq ($(CUDA_ARCH),)
139  NVCCFLAGS += -arch=$(CUDA_ARCH)
140endif
141HIPCCFLAGS ?= $(filter-out $(OMP_SIMD_FLAG),$(OPT)) -fPIC -munsafe-fp-atomics
142ifneq ($(HIP_ARCH),)
143  HIPCCFLAGS += --amdgpu-target=$(HIP_ARCH)
144endif
145FFLAGS ?= $(OPT) $(FFLAGS.$(FC_VENDOR))
146
147ifeq ($(COVERAGE), 1)
148  CFLAGS += --coverage
149  CXXFLAGS += --coverage
150  override LDFLAGS += --coverage
151endif
152
153CFLAGS += $(if $(ASAN),$(AFLAGS))
154FFLAGS += $(if $(ASAN),$(AFLAGS))
155override LDFLAGS += $(if $(ASAN),$(AFLAGS))
156CPPFLAGS += -I./include
157LDLIBS = -lm
158OBJDIR := build
159LIBDIR := lib
160
161# Installation variables
162prefix ?= /usr/local
163bindir = $(prefix)/bin
164libdir = $(prefix)/lib
165includedir = $(prefix)/include
166pkgconfigdir = $(libdir)/pkgconfig
167INSTALL = install
168INSTALL_PROGRAM = $(INSTALL)
169INSTALL_DATA = $(INSTALL) -m644
170
171# Get number of processors of the machine
172NPROCS := $(shell getconf _NPROCESSORS_ONLN)
173# prepare make options to run in parallel
174MFLAGS := -j $(NPROCS) --warn-undefined-variables \
175                       --no-print-directory --no-keep-going
176
177PYTHON ?= python3
178PROVE ?= prove
179PROVE_OPTS ?= -j $(NPROCS)
180DARWIN := $(filter Darwin,$(shell uname -s))
181SO_EXT := $(if $(DARWIN),dylib,so)
182
183ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
184libceed.so := $(LIBDIR)/libceed.$(SO_EXT)
185libceed.a := $(LIBDIR)/libceed.a
186libceed := $(if $(STATIC),$(libceed.a),$(libceed.so))
187CEED_LIBS = -lceed
188libceed.c := $(filter-out interface/ceed-cuda.c interface/ceed-hip.c, $(wildcard interface/ceed*.c backends/*.c gallery/*.c))
189gallery.c := $(wildcard gallery/*/ceed*.c)
190libceed.c += $(gallery.c)
191libceeds = $(libceed)
192BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
193BACKENDS_MAKE := $(BACKENDS_BUILTIN)
194TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
195
196# Tests
197tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
198tests.f   := $(if $(FC),$(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90)))
199tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
200ctests    := $(tests)
201tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
202# Examples
203examples.c := $(sort $(wildcard examples/ceed/*.c))
204examples.f := $(if $(FC),$(sort $(wildcard examples/ceed/*.f)))
205examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
206examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
207# MFEM Examples
208mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
209mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
210# Nek5K Examples
211nekexamples  := $(OBJDIR)/nek-bps
212# PETSc Examples
213petscexamples.c := $(wildcard examples/petsc/*.c)
214petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
215# Fluid Dynamics Examples
216fluidsexamples.c := $(sort $(wildcard examples/fluids/*.c))
217fluidsexamples  := $(fluidsexamples.c:examples/fluids/%.c=$(OBJDIR)/fluids-%)
218# Solid Mechanics Examples
219solidsexamples.c := $(sort $(wildcard examples/solids/*.c))
220solidsexamples   := $(solidsexamples.c:examples/solids/%.c=$(OBJDIR)/solids-%)
221
222# Backends/[ref, blocked, memcheck, opt, avx, occa, magma]
223ref.c          := $(sort $(wildcard backends/ref/*.c))
224blocked.c      := $(sort $(wildcard backends/blocked/*.c))
225ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
226opt.c          := $(sort $(wildcard backends/opt/*.c))
227avx.c          := $(sort $(wildcard backends/avx/*.c))
228xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
229cuda.c         := $(sort $(wildcard backends/cuda/*.c))
230cuda-ref.c     := $(sort $(wildcard backends/cuda-ref/*.c))
231cuda-ref.cpp   := $(sort $(wildcard backends/cuda-ref/*.cpp))
232cuda-ref.cu    := $(sort $(wildcard backends/cuda-ref/kernels/*.cu))
233cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
234cuda-shared.cu := $(sort $(wildcard backends/cuda-shared/kernels/*.cu))
235cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
236cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
237cuda-gen.cu    := $(sort $(wildcard backends/cuda-gen/kernels/*.cu))
238occa.cpp       := $(sort $(shell find backends/occa -type f -name *.cpp))
239magma.c        := $(sort $(wildcard backends/magma/*.c))
240magma.cu       := $(sort $(wildcard backends/magma/kernels/cuda/*.cu))
241magma.hip      := $(sort $(wildcard backends/magma/kernels/hip/*.hip.cpp))
242hip.c          := $(sort $(wildcard backends/hip/*.c))
243hip.cpp        := $(sort $(wildcard backends/hip/*.cpp))
244hip-ref.c      := $(sort $(wildcard backends/hip-ref/*.c))
245hip-ref.cpp    := $(sort $(wildcard backends/hip-ref/*.cpp))
246hip-ref.hip    := $(sort $(wildcard backends/hip-ref/kernels/*.hip.cpp))
247hip-shared.c   := $(sort $(wildcard backends/hip-shared/*.c))
248hip-gen.c      := $(sort $(wildcard backends/hip-gen/*.c))
249hip-gen.cpp    := $(sort $(wildcard backends/hip-gen/*.cpp))
250
251# Quiet, color output
252quiet ?= $($(1))
253
254# Cancel built-in and old-fashioned implicit rules which we don't use
255.SUFFIXES:
256
257.SECONDEXPANSION: # to expand $$(@D)/.DIR
258
259%/.DIR :
260	@mkdir -p $(@D)
261	@touch $@
262
263.PRECIOUS: %/.DIR
264
265lib: $(libceed) $(ceed.pc)
266# run 'lib' target in parallel
267par:;@$(MAKE) $(MFLAGS) V=$(V) lib
268backend_status = $(if $(filter $1,$(BACKENDS_MAKE)), [backends: $1], [not found])
269info:
270	$(info ------------------------------------)
271	$(info CC            = $(CC))
272	$(info CXX           = $(CXX))
273	$(info FC            = $(FC))
274	$(info CPPFLAGS      = $(CPPFLAGS))
275	$(info CFLAGS        = $(value CFLAGS))
276	$(info CXXFLAGS      = $(value CXXFLAGS))
277	$(info FFLAGS        = $(value FFLAGS))
278	$(info NVCCFLAGS     = $(value NVCCFLAGS))
279	$(info HIPCCFLAGS    = $(value HIPCCFLAGS))
280	$(info LDFLAGS       = $(value LDFLAGS))
281	$(info LDLIBS        = $(LDLIBS))
282	$(info AR            = $(AR))
283	$(info ARFLAGS       = $(ARFLAGS))
284	$(info OPT           = $(OPT))
285	$(info AFLAGS        = $(AFLAGS))
286	$(info ASAN          = $(or $(ASAN),(empty)))
287	$(info VERBOSE       = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
288	$(info ------------------------------------)
289	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,$(MEMCHK_BACKENDS)))
290	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,$(AVX_BACKENDS)))
291	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,$(XSMM_BACKENDS)))
292	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,$(OCCA_BACKENDS)))
293	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,$(MAGMA_BACKENDS)))
294	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
295	$(info HIP_DIR       = $(HIP_DIR)$(call backend_status,$(HIP_BACKENDS)))
296	$(info ------------------------------------)
297	$(info MFEM_DIR      = $(MFEM_DIR))
298	$(info NEK5K_DIR     = $(NEK5K_DIR))
299	$(info PETSC_DIR     = $(PETSC_DIR))
300	$(info ------------------------------------)
301	$(info prefix        = $(prefix))
302	$(info includedir    = $(value includedir))
303	$(info libdir        = $(value libdir))
304	$(info pkgconfigdir  = $(value pkgconfigdir))
305	$(info ------------------------------------)
306	@true
307info-backends:
308	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
309	@true
310info-backends-all:
311	$(info make: 'lib' with backends: $(filter-out $(TEST_BACKENDS),$(BACKENDS)))
312	@true
313
314$(libceed.so) : override LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed.so)))
315
316# Standard Backends
317libceed.c += $(ref.c)
318libceed.c += $(blocked.c)
319libceed.c += $(opt.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) $(CFLAGS) -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# Stubs that will not be RPATH'd
345PKG_STUBS_LIBS =
346
347# libXSMM Backends
348XSMM_BACKENDS = /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
349ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
350  PKG_LIBS += -L$(abspath $(XSMM_DIR))/lib -lxsmm -ldl
351  MKL ?=
352  ifeq (,$(MKL)$(MKLROOT))
353    BLAS_LIB = -lblas
354  else
355    ifneq ($(MKLROOT),)
356      # Some installs put everything inside an intel64 subdirectory, others not
357      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
358      MKL_LINK = -L$(MKL_LIBDIR)
359      PKG_LIB_DIRS += $(MKL_LIBDIR)
360    endif
361    BLAS_LIB = $(MKL_LINK) -Wl,--push-state,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl -Wl,--pop-state
362  endif
363  PKG_LIBS += $(BLAS_LIB)
364  libceed.c += $(xsmm.c)
365  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
366  BACKENDS_MAKE += $(XSMM_BACKENDS)
367endif
368
369# OCCA Backends
370OCCA_BACKENDS = /cpu/self/occa
371ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
372  OCCA_MODES := $(shell $(OCCA_DIR)/bin/occa modes)
373  OCCA_BACKENDS += $(if $(filter OpenMP,$(OCCA_MODES)),/cpu/openmp/occa)
374# OCCA_BACKENDS += $(if $(filter OpenCL,$(OCCA_MODES)),/gpu/opencl/occa)
375  OCCA_BACKENDS += $(if $(filter HIP,$(OCCA_MODES)),/gpu/hip/occa)
376  OCCA_BACKENDS += $(if $(filter CUDA,$(OCCA_MODES)),/gpu/cuda/occa)
377
378  $(libceeds) : CPPFLAGS += -I$(OCCA_DIR)/include
379  PKG_LIBS += -L$(abspath $(OCCA_DIR))/lib -locca
380  LIBCEED_CONTAINS_CXX = 1
381  libceed.cpp += $(occa.cpp)
382  BACKENDS_MAKE += $(OCCA_BACKENDS)
383endif
384
385# CUDA Backends
386CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
387CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
388CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
389CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/shared /gpu/cuda/gen
390ifneq ($(CUDA_LIB_DIR),)
391  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
392  PKG_LIBS += -L$(abspath $(CUDA_LIB_DIR)) -lcudart -lnvrtc -lcuda -lcublas
393  PKG_STUBS_LIBS += -L$(CUDA_LIB_DIR_STUBS)
394  LIBCEED_CONTAINS_CXX = 1
395  libceed.c     += interface/ceed-cuda.c
396  libceed.c     += $(cuda.c) $(cuda-ref.c) $(cuda-shared.c) $(cuda-gen.c)
397  libceed.cpp   += $(cuda-ref.cpp) $(cuda-gen.cpp)
398  libceed.cu    += $(cuda-ref.cu) $(cuda-shared.cu) $(cuda-gen.cu)
399  BACKENDS_MAKE += $(CUDA_BACKENDS)
400endif
401
402# HIP Backends
403HIP_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(HIP_DIR)/$d/libamdhip64.${SO_EXT}))
404HIP_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(HIP_LIB_DIR))))
405HIP_BACKENDS = /gpu/hip/ref /gpu/hip/shared /gpu/hip/gen
406ifneq ($(HIP_LIB_DIR),)
407  $(libceeds) : HIPCCFLAGS += -I./include
408  ifneq ($(CXX), $(HIPCC))
409    CPPFLAGS += $(subst =,,$(shell $(HIP_DIR)/bin/hipconfig -C))
410  endif
411  $(libceeds) : CPPFLAGS += -I$(HIP_DIR)/include
412  PKG_LIBS += -L$(abspath $(HIP_LIB_DIR)) -lamdhip64 -lhipblas
413  LIBCEED_CONTAINS_CXX = 1
414  libceed.c     += interface/ceed-hip.c
415  libceed.c     += $(hip.c) $(hip-ref.c) $(hip-shared.c) $(hip-gen.c)
416  libceed.cpp   += $(hip.cpp) $(hip-ref.cpp) $(hip-gen.cpp)
417  libceed.hip   += $(hip-ref.hip)
418  BACKENDS_MAKE += $(HIP_BACKENDS)
419endif
420
421# MAGMA Backend
422ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
423  MAGMA_ARCH=$(shell nm -g $(MAGMA_DIR)/lib/libmagma.* | grep -c "hipblas")
424  ifeq ($(MAGMA_ARCH), 0) #CUDA MAGMA
425    ifneq ($(CUDA_LIB_DIR),)
426      cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
427      omp_link = -fopenmp
428      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
429      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
430      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
431      PKG_LIBS += $(magma_link)
432      libceed.c  += $(magma.c)
433      libceed.cu += $(magma.cu)
434      $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
435      $(magma.cu:%.cu=$(OBJDIR)/%.o) : CPPFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(CUDA_DIR)/include
436      MAGMA_BACKENDS = /gpu/cuda/magma /gpu/cuda/magma/det
437    endif
438  else  # HIP MAGMA
439    ifneq ($(HIP_LIB_DIR),)
440      hip_link = -Wl,-rpath,$(HIP_LIB_DIR) -L$(HIP_LIB_DIR) -lhipblas -lhipsparse -lamdhip64
441      omp_link = -fopenmp
442      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(hip_link) $(omp_link)
443      magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
444      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
445      PKG_LIBS += $(magma_link)
446      libceed.c  += $(magma.c)
447      libceed.hip += $(magma.hip)
448      ifneq ($(CXX), $(HIPCC))
449        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
450      else
451        $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
452      endif
453      $(magma.hip:%.hip.cpp=$(OBJDIR)/%.o) : HIPCCFLAGS += -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(HIP_DIR)/include -DHAVE_HIP -DADD_
454      MAGMA_BACKENDS = /gpu/hip/magma /gpu/hip/magma/det
455    endif
456  endif
457  LIBCEED_CONTAINS_CXX = 1
458  BACKENDS_MAKE += $(MAGMA_BACKENDS)
459endif
460
461BACKENDS ?= $(BACKENDS_MAKE)
462export BACKENDS
463
464_pkg_ldflags = $(filter -L%,$(PKG_LIBS))
465_pkg_ldlibs = $(filter-out -L%,$(PKG_LIBS))
466$(libceeds) : override LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%) $(PKG_STUBS_LIBS)
467$(libceeds) : LDLIBS += $(_pkg_ldlibs)
468ifeq ($(STATIC),1)
469$(examples) $(tests) : override LDFLAGS += $(_pkg_ldflags) $(_pkg_ldflags:-L%=-Wl,-rpath,%) $(PKG_STUBS_LIBS)
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# Several executables have common utilities, but we can't build the utilities
533# from separate submake invocations because they'll compete with each
534# other/corrupt output. So we put it in this utility library, but we don't want
535# to manually list source dependencies up at this level, so we'll just always
536# call recursive make to check that this utility is up to date.
537examples/petsc/libutils.a.PHONY: $(libceed) $(ceed.pc)
538	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` AR=$(AR) ARFLAGS=$(ARFLAGS) \
539	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $(basename $(@F))
540
541$(OBJDIR)/petsc-% : examples/petsc/%.c examples/petsc/libutils.a.PHONY $(libceed) $(ceed.pc) | $$(@D)/.DIR
542	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` \
543	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
544	cp examples/petsc/$* $@
545
546$(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
547	+$(call quiet,MAKE) -C examples/fluids CEED_DIR=`pwd` \
548	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
549	cp examples/fluids/$* $@
550
551$(OBJDIR)/solids-% : examples/solids/%.c examples/solids/%.h \
552    examples/solids/problems/*.c examples/solids/src/*.c \
553    examples/solids/include/*.h examples/solids/problems/*.h examples/solids/qfunctions/*.h \
554    $(libceed) $(ceed.pc) | $$(@D)/.DIR
555	+$(call quiet,MAKE) -C examples/solids CEED_DIR=`pwd` \
556	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
557	cp examples/solids/$* $@
558
559$(examples) : $(libceed)
560$(tests) : $(libceed)
561$(tests) $(examples) : override LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
562
563run-% : $(OBJDIR)/%
564	@tests/tap.sh $(<:$(OBJDIR)/%=%)
565
566external_examples := \
567	$(if $(MFEM_DIR),$(mfemexamples)) \
568	$(if $(PETSC_DIR),$(petscexamples)) \
569	$(if $(NEK5K_DIR),$(nekexamples)) \
570	$(if $(PETSC_DIR),$(fluidsexamples)) \
571	$(if $(PETSC_DIR),$(solidsexamples))
572
573allexamples = $(examples) $(external_examples)
574
575# The test and prove targets can be controlled via pattern searches.  The
576# default is to run tests and those examples that have no external dependencies.
577# Examples of finer grained control:
578#
579#   make test search='petsc mfem'      # PETSc and MFEM examples
580#   make prove search='t3'             # t3xx series tests
581#   make junit search='ex petsc'       # core ex* and PETSc tests
582search ?= t ex
583realsearch = $(search:%=%%)
584matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
585
586# Test core libCEED
587test : $(matched:$(OBJDIR)/%=run-%)
588
589# Run test target in parallel
590tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
591# CPU C tests only for backend %
592ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
593
594prove : $(matched)
595	$(info Testing backends: $(BACKENDS))
596	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
597# Run prove target in parallel
598prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
599
600prove-all :
601	+$(MAKE) prove realsearch=%
602
603junit-% : $(OBJDIR)/%
604	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
605
606junit : $(matched:$(OBJDIR)/%=junit-%)
607
608all: $(alltests)
609
610examples : $(allexamples)
611ceedexamples : $(examples)
612nekexamples : $(nekexamples)
613mfemexamples : $(mfemexamples)
614petscexamples : $(petscexamples)
615
616# Benchmarks
617allbenchmarks = petsc-bps
618bench_targets = $(addprefix bench-,$(allbenchmarks))
619.PHONY: $(bench_targets) benchmarks
620$(bench_targets): bench-%: $(OBJDIR)/%
621	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS_MAKE)" -r $(*).sh
622benchmarks: $(bench_targets)
623
624$(ceed.pc) : pkgconfig-prefix = $(abspath .)
625$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
626.INTERMEDIATE : $(OBJDIR)/ceed.pc
627%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
628	@$(SED) \
629	    -e "s:%prefix%:$(pkgconfig-prefix):" \
630	    -e "s:%libs_private%:$(pkgconfig-libs-private):" $< > $@
631
632install : $(libceed) $(OBJDIR)/ceed.pc
633	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
634	  "$(includedir)/ceed/" "$(libdir)" "$(pkgconfigdir)")
635	$(INSTALL_DATA) include/ceed/ceed.h "$(DESTDIR)$(includedir)/ceed/"
636	$(INSTALL_DATA) include/ceed/ceed-f32.h "$(DESTDIR)$(includedir)/ceed/"
637	$(INSTALL_DATA) include/ceed/ceed-f64.h "$(DESTDIR)$(includedir)/ceed/"
638	$(INSTALL_DATA) include/ceed/fortran.h "$(DESTDIR)$(includedir)/ceed/"
639	$(INSTALL_DATA) include/ceed/backend.h "$(DESTDIR)$(includedir)/ceed/"
640	$(INSTALL_DATA) include/ceed/cuda.h "$(DESTDIR)$(includedir)/ceed/"
641	$(INSTALL_DATA) include/ceed/hip.h "$(DESTDIR)$(includedir)/ceed/"
642	$(INSTALL_DATA) include/ceed/hash.h "$(DESTDIR)$(includedir)/ceed/"
643	$(INSTALL_DATA) include/ceed/khash.h "$(DESTDIR)$(includedir)/ceed/"
644	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
645	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
646	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
647	$(INSTALL_DATA) include/ceedf.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 iwyu info info-backends info-backends-all
650
651cln clean :
652	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
653	$(call quiet,MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
654	$(call quiet,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) -q 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
695ifneq ($(wildcard ../iwyu/*),)
696  IWYU_DIR ?= ../iwyu
697  IWYU_CC  ?= $(IWYU_DIR)/build/bin/include-what-you-use
698endif
699
700iwyu : CC=$(IWYU_CC)
701iwyu : lib
702
703print :
704	@echo $(VAR)=$($(VAR))
705
706print-% :
707	$(info [ variable name]: $*)
708	$(info [        origin]: $(origin $*))
709	$(info [        flavor]: $(flavor $*))
710	$(info [         value]: $(value $*))
711	$(info [expanded value]: $($*))
712	$(info )
713	@true
714
715# "make configure" detects any variables passed on the command line or
716# previously set in config.mk, caching them in config.mk as simple
717# (:=) variables.  Variables set in config.mk or on the command line
718# take precedence over the defaults provided in the file.  Typical
719# usage:
720#
721#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
722#   make
723#   make prove
724#
725# The values in the file can be updated by passing them on the command
726# line, e.g.,
727#
728#   make configure CC=/path/to/other/clang
729
730# All variables to consider for caching
731CONFIG_VARS = CC CXX FC NVCC NVCC_CXX HIPCC \
732	OPT CFLAGS CPPFLAGS CXXFLAGS FFLAGS NVCCFLAGS HIPCCFLAGS \
733	AR ARFLAGS LDFLAGS LDLIBS LIBCXX SED \
734	MAGMA_DIR OCCA_DIR XSMM_DIR CUDA_DIR CUDA_ARCH MFEM_DIR PETSC_DIR NEK5K_DIR HIP_DIR HIP_ARCH
735
736# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
737# was set on the command line or in config.mk (where it will appear as
738# a simple variable).
739needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
740
741configure :
742	$(file > $(CONFIG))
743	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
744	@echo "Configuration cached in $(CONFIG):"
745	@cat $(CONFIG)
746
747wheel : export MARCHFLAG = -march=generic
748wheel : export WHEEL_PLAT = manylinux2010_x86_64
749wheel :
750	docker run -it --user $(shell id -u):$(shell id -g) --rm -v $(PWD):/io -w /io \
751		-e MARCHFLAG -e WHEEL_PLAT \
752		quay.io/pypa/$(WHEEL_PLAT) python/make-wheels.sh
753
754.PHONY : configure wheel
755
756# Include *.d deps when not -B = --always-make: useful if the paths are wonky in a container
757-include $(if $(filter B,$(MAKEFLAGS)),,$(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d))
758