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