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 17ifeq (,$(filter-out undefined default,$(origin CC))) 18 CC = gcc 19endif 20ifeq (,$(filter-out undefined default,$(origin FC))) 21 FC = gfortran 22endif 23NVCC = $(CUDA_DIR)/bin/nvcc 24 25# ASAN must be left empty if you don't want to use it 26ASAN ?= 27 28LDFLAGS ?= 29UNDERSCORE ?= 1 30 31# MFEM_DIR env variable should point to sibling directory 32ifneq ($(wildcard ../mfem/libmfem.*),) 33 MFEM_DIR?=../mfem 34endif 35 36# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa) 37OCCA_DIR ?= ../occa 38 39# env variable MAGMA_DIR can be used too 40MAGMA_DIR ?= ../magma 41# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda 42CUDA_DIR ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \ 43 $(shell which nvcc 2> /dev/null))))),/usr/local/cuda) 44 45# Warning: SANTIZ options still don't run with /gpu/occa 46# export LSAN_OPTIONS=suppressions=.asanignore 47AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer 48 49OPT = -O -g 50CFLAGS = -std=c99 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP 51NVCCFLAGS = $(OPT) 52# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately: 53ifneq ($(filter %xlf %xlf_r,$(FC)),) 54 FFLAGS = $(OPT) -qpreprocess -qextname -qpic -MMD 55else # gfortran/Intel-style options 56 FFLAGS = -cpp $(OPT) -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP 57endif 58 59ifeq ($(UNDERSCORE), 1) 60 CFLAGS += -DUNDERSCORE 61endif 62 63ifeq ($(COVERAGE), 1) 64 CFLAGS += --coverage 65 LDFLAGS += --coverage 66endif 67 68CFLAGS += $(if $(ASAN),$(AFLAGS)) 69FFLAGS += $(if $(ASAN),$(AFLAGS)) 70LDFLAGS += $(if $(ASAN),$(AFLAGS)) 71CPPFLAGS = -I./include 72LDLIBS = -lm 73OBJDIR := build 74LIBDIR := lib 75 76# Installation variables 77prefix ?= /usr/local 78bindir = $(prefix)/bin 79libdir = $(prefix)/lib 80okldir = $(libdir)/okl 81includedir = $(prefix)/include 82pkgconfigdir = $(libdir)/pkgconfig 83INSTALL = install 84INSTALL_PROGRAM = $(INSTALL) 85INSTALL_DATA = $(INSTALL) -m644 86 87# Get number of processors of the machine 88NPROCS := $(shell getconf _NPROCESSORS_ONLN) 89# prepare make options to run in parallel 90MFLAGS := -j $(NPROCS) --warn-undefined-variables \ 91 --no-print-directory --no-keep-going 92 93PROVE ?= prove 94PROVE_OPTS ?= -j $(NPROCS) 95DARWIN := $(filter Darwin,$(shell uname -s)) 96SO_EXT := $(if $(DARWIN),dylib,so) 97 98ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc 99libceed := $(LIBDIR)/libceed.$(SO_EXT) 100libceed.c := $(wildcard interface/ceed*.c) 101BACKENDS := /cpu/self/ref /cpu/self/tmpl /cpu/self/opt 102 103# Tests 104tests.c := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c)) 105tests.f := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f)) 106tests := $(tests.c:tests/%.c=$(OBJDIR)/%) 107ctests := $(tests) 108tests += $(tests.f:tests/%.f=$(OBJDIR)/%) 109#examples 110examples.c := $(sort $(wildcard examples/ceed/*.c)) 111examples.f := $(sort $(wildcard examples/ceed/*.f)) 112examples := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%) 113examples += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%) 114#mfemexamples 115mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp)) 116mfemexamples := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%) 117petscexamples.c := $(sort $(wildcard examples/petsc/*.c)) 118petscexamples := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%) 119 120# backends/[ref & occa & magma] 121ref.c := $(sort $(wildcard backends/ref/*.c)) 122template.c := $(sort $(wildcard backends/template/*.c)) 123optimized.c:= $(sort $(wildcard backends/optimized/*.c)) 124occa.c := $(sort $(wildcard backends/occa/*.c)) 125magma_preprocessor := python backends/magma/gccm.py 126magma_pre_src := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c)) 127magma_dsrc := $(wildcard backends/magma/magma_d*.c) 128magma_tmp.c := $(magma_pre_src:%.c=%_tmp.c) 129magma_tmp.cu := $(magma_pre_src:%.c=%_cuda.cu) 130magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c) 131magma_allsrc.cu:= $(magma_tmp.cu) 132 133# Output using the 216-color rules mode 134rule_file = $(notdir $(1)) 135rule_path = $(patsubst %/,%,$(dir $(1))) 136last_path = $(notdir $(patsubst %/,%,$(dir $(1)))) 137ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17) 138emacs_out = @printf " %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2)) 139color_out = @if [ -t 1 ]; then \ 140 printf " %10s \033[38;5;%d;1m%s\033[m/%s\n" \ 141 $(1) $(call ansicolor,$(2)) \ 142 $(call rule_path,$(2)) $(call rule_file,$(2)); else \ 143 printf " %10s %s\n" $(1) $(2); fi 144# if TERM=dumb, use it, otherwise switch to the term one 145output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2)) 146 147# if V is set to non-nil, turn the verbose mode 148quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1))) 149 150.SUFFIXES: 151.SUFFIXES: .c .o .d 152.SECONDEXPANSION: # to expand $$(@D)/.DIR 153 154.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu) 155 156%/.DIR : 157 @mkdir -p $(@D) 158 @touch $@ 159 160.PRECIOUS: %/.DIR 161 162this: $(libceed) $(ceed.pc) 163# run 'this' target in parallel 164all:;@$(MAKE) $(MFLAGS) V=$(V) this 165info: 166 $(info ------------------------------------) 167 $(info CC = $(CC)) 168 $(info FC = $(FC)) 169 $(info CPPFLAGS = $(CPPFLAGS)) 170 $(info CFLAGS = $(value CFLAGS)) 171 $(info FFLAGS = $(value FFLAGS)) 172 $(info NVCCFLAGS = $(value NVCCFLAGS)) 173 $(info LDFLAGS = $(value LDFLAGS)) 174 $(info LDLIBS = $(LDLIBS)) 175 $(info OPT = $(OPT)) 176 $(info AFLAGS = $(AFLAGS)) 177 $(info ASAN = $(or $(ASAN),(empty))) 178 $(info V = $(or $(V),(empty)) [verbose=$(if $(V),on,off)]) 179 $(info ------------------------------------) 180 $(info CUDA_DIR = $(CUDA_DIR)) 181 $(info OCCA_DIR = $(OCCA_DIR)) 182 $(info MAGMA_DIR = $(MAGMA_DIR)) 183 $(info ------------------------------------) 184 $(info MFEM_DIR = $(MFEM_DIR)) 185 $(info PETSC_DIR = $(PETSC_DIR)) 186 $(info ------------------------------------) 187 $(info prefix = $(prefix)) 188 $(info includedir = $(value includedir)) 189 $(info libdir = $(value libdir)) 190 $(info okldir = $(value okldir)) 191 $(info pkgconfigdir = $(value pkgconfigdir)) 192 $(info ------------------------------------) 193 @true 194.PHONY: info 195 196$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed))) 197 198libceed.c += $(ref.c) 199libceed.c += $(template.c) 200libceed.c += $(optimized.c) 201ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),) 202 OCCA_ON = YES 203 $(info libCEED: OCCA backend enabled [OCCA_DIR=$(OCCA_DIR)]) 204 $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib) 205 $(libceed) : LDLIBS += -locca 206 libceed.c += $(occa.c) 207 $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include 208 BACKENDS += /cpu/occa /gpu/occa /omp/occa 209endif 210ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),) 211 CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT})) 212 CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR)))) 213 ifneq ($(CUDA_LIB_DIR),) 214 $(info libCEED: MAGMA backend enabled [MAGMA_DIR=$(MAGMA_DIR)]) 215 cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart 216 omp_link = -fopenmp 217 magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link) 218 magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma 219 magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static)) 220 magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) 221 $(libceed) : LDLIBS += $(magma_link) 222 $(tests) $(examples) : LDLIBS += $(magma_link) 223 $(libceed) : $(magma_allsrc.o) 224 libceed.c += $(magma_allsrc.c) 225 libceed.cu += $(magma_allsrc.cu) 226 $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include 227 $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) : NVCCFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include 228 BACKENDS += /gpu/magma 229 endif 230endif 231 232export BACKENDS 233 234# generate magma_tmp.c and magma_cuda.cu from magma.c 235$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR 236 $(magma_preprocessor) $< 237 238$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR 239 $(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) 240 241$(OBJDIR)/%.o : %.c | $$(@D)/.DIR 242 $(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<) 243 244$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR 245 $(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<) 246 247$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR 248 $(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS) 249 250$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR 251 $(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS) 252 253$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR 254 $(call quiet,LINK.c) -o $@ $(abspath $<) -lceed $(LDLIBS) 255 256$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR 257 $(call quiet,LINK.F) -o $@ $(abspath $<) -lceed $(LDLIBS) 258 259$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR 260 $(MAKE) -C examples/mfem CEED_DIR=`pwd` $* 261 mv examples/mfem/$* $@ 262 263$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR 264 $(MAKE) -C examples/petsc CEED_DIR=`pwd` $* 265 mv examples/petsc/$* $@ 266 267$(tests) $(examples) : $(libceed) 268$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR) 269 270run-% : $(OBJDIR)/% 271 @tests/tap.sh $(<:build/%=%) 272# Test core libCEED 273test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%) 274 275# run test target in parallel 276tst : ;@$(MAKE) $(MFLAGS) V=$(V) test 277# CPU C tests only for backend % 278ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;) 279 280prove : $(tests) $(examples) 281 $(info Testing backends: $(BACKENDS)) 282 $(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%) 283# run prove target in parallel 284prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove 285 286alltests := $(tests) $(examples) $(if $(MFEM_DIR),$(mfemexamples)) $(if $(PETSC_DIR),$(petscexamples)) 287prove-all : $(alltests) 288 $(info Testing backends: $(BACKENDS)) 289 $(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(alltests:$(OBJDIR)/%=%) 290 291examples : $(examples) 292 293$(ceed.pc) : pkgconfig-prefix = $(abspath .) 294$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix) 295.INTERMEDIATE : $(OBJDIR)/ceed.pc 296%/ceed.pc : ceed.pc.template | $$(@D)/.DIR 297 @sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@ 298 299OCCA := $(OCCA_DIR)/bin/occa 300OKL_KERNELS := $(wildcard backends/occa/*.okl) 301 302okl-cache : 303 $(OCCA) cache ceed $(OKL_KERNELS) 304 305okl-clear: 306 $(OCCA) clear -y -l ceed 307 308install : $(libceed) $(OBJDIR)/ceed.pc 309 $(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\ 310 "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)")) 311 $(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/" 312 $(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/" 313 $(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/" 314 $(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/" 315 $(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/") 316 317.PHONY : all cln clean print test tst prove prv examples style install doc okl-cache okl-clear 318 319cln clean : 320 $(RM) *.o *.d $(libceed) 321 $(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig 322 $(MAKE) -C examples/ceed clean 323 $(MAKE) -C examples/mfem clean 324 $(MAKE) -C examples/petsc clean 325 (cd examples/nek5000 && bash make-nek-examples.sh clean) 326 $(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o 327 328distclean : clean 329 $(RM) -r doc/html 330 331doc : 332 doxygen Doxyfile 333 334style : 335 astyle --style=google --indent=spaces=2 --max-code-length=80 \ 336 --keep-one-line-statements --keep-one-line-blocks --lineend=linux \ 337 --suffix=none --preserve-date --formatted \ 338 --exclude=include/ceedf.h \ 339 include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \ 340 examples/*/*.[ch] examples/*/*.[ch]pp 341 342print : 343 @echo $(VAR)=$($(VAR)) 344 345print-% : 346 $(info [ variable name]: $*) 347 $(info [ origin]: $(origin $*)) 348 $(info [ value]: $(value $*)) 349 $(info [expanded value]: $($*)) 350 $(info ) 351 @true 352 353-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d) 354