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