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