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