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