MagmaDNN 第01记 编译
迪丽瓦拉
2025-05-28 02:28:27
0

代码仓库地址:

Bitbucket

下载源代码:

$ git clone https://bitbucket.org/icl/magmadnn.git

系统需求:

Any MagmaDNN install:

  • c++11 capable compiler (recommended g++ >= 8)
  • BLAS implementation (openblas, atlas, intel-mkl, etc...)
  • make
  • git

MagmaDNN-GPU install:

  • CUDA >=10 (recommended CUDA >=11.0)
  • CuDNN >=7.6 (recommended CuDNN >=8.0)
  • Magma >=2.5.0

同时 Unet 的example 需要使用到 opencv,

sudo apt install libopencv-dev python3-opencv

修改make.device:

# make.device
# add gpu specific compilation flagsifneq ($(findstring Kepler, $(GPU_TARGET)),)
MIN_ARCH ?= 300
#NV_SM += -gencode arch=compute_30,code=sm_30
NV_SM += -gencode arch=compute_35,code=sm_35,code=sm_86
NV_COMP := -gencode arch=compute_35,code=compute_35,code=compute_86
endififneq ($(findstring Maxwell, $(GPU_TARGET)),)
MIN_ARCH ?= 500
NV_SM += -gencode arch=compute_50,code=sm_50
NV_SM += -gencode arch=compute_52,code=sm_52
NV_COMP := -gencode arch=compute_52,code=compute_52
endififneq ($(findstring Pascal, $(GPU_TARGET)),)
MIN_ARCH ?= 600
NV_SM += -gencode arch=compute_60,code=sm_60
NV_SM += -gencode arch=compute_61,code=sm_61
NV_COMP := -gencode arch=compute_61,code=compute_61
endififneq ($(findstring Volta, $(GPU_TARGET)),)
MIN_ARCH ?= 700
NV_SM += -gencode arch=compute_70,code=sm_70
NV_COMP := -gencode arch=compute_70,code=compute_70
endififneq ($(findstring Tesla, $(GPU_TARGET)),)
MIN_ARCH ?= 750
NV_SM += -gencode arch=compute_75,code=sm_75
NV_COMP := -gencode arch=compute_75,code=compute_75
endififneq ($(findstring Ampere, $(GPU_TARGET)),)
MIN_ARCH ?= 800
NV_SM += -gencode arch=compute_80,code=sm_80
NV_COMP := -gencode arch=compute_80,code=compute_80
endififeq ($(NV_COMP),)
$(error GPU_TARGET, is $(GPU_TARGET), must contain one or more of Kepler, Maxwell, Pascal, Volta, Tesla, and Ampere)
endif

修改make.inc文件,大概如下:

# rename this file to make.inc and place it in the same directory as the main makefile
# a file to set makefile rules, targets, and settings
# use it to set make parameters
# uncomment (remove #) to set a parameter# install location
prefix = /usr/local/magmadnn# set to 1 if you want to compile with MKLDNN
#USE_MKLDNN = 0# set to 0 if you don't want to compile with CUDA
TRY_CUDA = 1# set if you want to make the library in debug mode (adds "-O0 -g -DDEBUG" to flags)
#DEBUG = 1# define which compilers to use
CXX = g++
NVCC = nvcc# GPU targets
GPU_TARGET = Ampere
# Kepler Pascal# LINKS to locations of CUDA and MAGMA
CUDADIR ?= /usr/local/cuda
MAGMADIR ?= /usr/local/magma# LINKS to your CPU C BLAS library (i.e. atlas, mkl, openblas)
#BLASDIR ?= /usr/local/openblas
BLASDIR ?= /opt/OpenBLAS
BLASLIB ?= openblas# compiler options
OPTIMIZATION_LEVEL = -O3
WARNINGS = -Wall
CXX_VERSION = -std=c++11# the extension for object files
#o_ext = .o# what type of shared/dynamic library to create
# should only set to dylib if on MAC OS
#LIBSHARED_EXT = .dylib
#LIBSHARED_FLAG = -dynamiclib# other library settings
ARCH = ar
ARCH_FLAGS = cr
RANLIB = ranlib
RANLIB_FLAGS = -no_warning_for_no_symbols# don't mess with these unless you know what you're doing.
TESTING_DIR = testing
LIB_DIR = lib
TARGET_DIRS = src
EXAMPLE_DIR = examples
DOCS_DIR = docs

 修改makefile:

# This makefile can build and install the magmadnn library# include any user-defined compile options (pre-sets)
include make.inc# compilers for c++ and cu
CXX ?= g++
NVCC ?= nvcc# MKLDNN flag
USE_MKLDNN ?= 0# CUDA flag
TRY_CUDA ?= 1# OpenCV flag
HAVE_OPENCV ?= 1# locations of cuda and magma installations
CUDADIR ?= /usr/local/cuda
# Location of cuDNN library
CUDNN_INC_DIR ?=
CUDNN_LIB_DIR ?=
MAGMADIR ?= /usr/local/magma
BLASDIR ?= /usr/local/openblas
BLASINC ?= $(BLASDIR)/include
BLASLIB_PATH ?= $(BLASDIR)/lib
BLASLIB ?= openblas# Distributed memory version
USE_MPI ?= 0# where to install magmadnn (make must have sudo access if prefix is root privileged)
prefix ?= /usr/local/magmadnn# headers needed for library compilation
INC := -I./include -I$(BLASINC)# Add cuDNN include directory
ifneq ($(CUDNN_INC_DIR),)
INC += -I$(CUDNN_INC_DIR)
endif# libs to link with
LIBDIRS := -L$(BLASLIB_PATH)
LIBS = -l$(BLASLIB)# Add cuDNN lib directory
ifneq ($(CUDNN_LIB_DIR),)
LIBDIRS += -L$(CUDNN_LIB_DIR)
endififeq ($(USE_MKLDNN),1)
MKLDNN_MACRO = -DMAGMADNN_HAVE_MKLDNN
LIBS += -lmkldnn
endififeq ($(HAVE_OPENCV),1)
INC += -I/usr/include/opencv4
LIBDIRS += -L/usr/lib
LIBS += -lopencv_core
endifUSE_CUDA = 0
ifeq ($(TRY_CUDA),1)
# use nvcc to determine if we should compile for gpu or not
GPU_TARGET ?= Keplerifneq ($(shell which nvcc),)
include make.device
CUDA_MACRO = -DMAGMADNN_HAVE_CUDA -D_HAS_CUDA_ifeq ($(HAVE_OPENCV),1)
CUDA_MACRO += -DMAGMADNN_HAVE_OPENCV
endifINC += -I$(CUDADIR)/include -I$(MAGMADIR)/include
LIBDIRS += -L$(CUDADIR)/lib64 -L$(MAGMADIR)/lib
LIBS += -lcudart -lcudnn -lmagma
USE_CUDA=1
endif
endif# individual flags for compilation
OPTIMIZATION_LEVEL ?= -O3
WARNINGS ?= -Wall
FPIC ?= -fPIC
CXX_VERSION ?= -std=c++11
DEBUG ?= 0
PROFILE_FLAGS ?= # this flag dictates whether to use openmp or not for some CPU code
# set it to false by default
USE_OPENMP ?= 0
ifeq ($(USE_OPENMP),1)
OPENMP_FLAGS = -fopenmp -D_USE_OPENMP_
endif# set optimization to Og for debugging
ifeq ($(DEBUG),1)
OPTIMIZATION_LEVEL = -O0
endif# the entire flags for compilation
CXXFLAGS := $(OPTIMIZATION_LEVEL) $(WARNINGS) $(CXX_VERSION) $(CUDA_MACRO) $(MKLDNN_MACRO) $(FPIC) -MMD $(OPENMP_FLAGS) $(PROFILE_FLAGS)
NVCCFLAGS := $(CXX_VERSION) $(OPTIMIZATION_LEVEL) -Xcompiler "$(CXXFLAGS)" $(NV_SM) $(NV_COMP)
LD_FLAGS := $(LIBDIRS) $(LIBS)# include -g for debugging
ifeq ($(DEBUG),1)
CXXFLAGS += -g -DDEBUG
NVCCFLAGS += -g -DDEBUG
endif# make these available to child makefiles
export CXX
export NVCC
export INC
export LD_FLAGS
export prefix
export CUDADIR
export MAGMADIR
export OPTIMIZATION_LEVEL
export WARNINGS
export CXX_VERSION
export CUDA_MACRO
export USE_CUDA
export CXXFLAGS
export NVCCFLAGS
export LIBDIRS
export LIBS
export USE_MPI
export USE_MKLDNN
export HAVE_OPENCV# default extension for object files
o_ext ?= o# where are the source files (files that need to be compiled) found
TARGET_DIRS ?= srcall: $(TARGET_DIRS)# step into source directories and use their makefiles
$(TARGET_DIRS):@echo "==== Building Sources ===="
ifeq ($(USE_CUDA),1)@echo "CUDA installation found. Building GPU/CPU."
else@echo "(X) CUDA installation not found. Building CPU-only."
endif$(MAKE) -C $@@echo# collect all the object files from the source directories (deepest: src/compute/*/*.cpp)
OBJ_FILES = $(wildcard $(TARGET_DIRS)/*.$(o_ext) $(TARGET_DIRS)/*/*.$(o_ext) $(TARGET_DIRS)/*/*/*.$(o_ext))
# collect dependency files (.d)
DEP_FILES = $(wildcard $(TARGET_DIRS)/*.d $(TARGET_DIRS)/*/*.d $(TARGET_DIRS)/*/*/*.d)# MAKE THE LIB FILESLIB_DIR ?= lib# archiver and flags
ARCH ?= ar
ARCH_FLAGS ?= cr
RANLIB ?= ranlib
RANLIB_FLAGS ?= # set EXT to .dylib and FLAG to -dynamiclib for MAC OS
LIBSHARED_EXT ?= .so
LIBSHARED_FLAG ?= -sharedlibstatic := $(LIB_DIR)/libmagmadnn.a
libshared := $(LIB_DIR)/libmagmadnn$(LIBSHARED_EXT)lib: $(TARGET_DIRS) static shared
static: $(libstatic)
shared: $(libshared)$(libstatic): @echo "==== building static lib ===="mkdir -p $(LIB_DIR)$(ARCH) $(ARCH_FLAGS) $@ $(OBJ_FILES)$(RANLIB) $(RANLIB_FLAGS) $@@echo # MacOS specific install information
ostype = ${shell echo $${OSTYPE}}
ifneq ($(findstring darwin, ${ostype}),)$(libshared): LIBSHARED_FLAG += -install_name $(prefix)/lib/$(notdir $(libshared))
endif$(libshared):@echo "==== building shared lib ===="mkdir -p $(LIB_DIR)$(CXX) $(LIBSHARED_FLAG) $(FPIC) -o $@ $(OBJ_FILES) -L./lib $(LD_FLAGS)@echo # make the testers
TESTING_DIR ?= testing
testing:@echo "==== building testing sources ===="# step into the testing directories and call their makefiles$(MAKE) -C $(TESTING_DIR)@echo# make the examples
EXAMPLE_DIR ?= examples
examples:@echo "==== building examples ===="# step into example directory and use its makefile$(MAKE) -C $(EXAMPLE_DIR)@echo# build the library first, then link the lib together.
# install copies the newly made libs into prefix
install: lib@echo "==== installing libs ===="mkdir -p $(prefix)mkdir -p $(prefix)/includemkdir -p $(prefix)/libcp -r ./include/* $(prefix)/includecp $(libstatic) $(prefix)/libcp $(libshared) $(prefix)/lib@echo
# make tutorial smaples
TUTORIAL_DIR ?= docs/tutorials/tutorial_samples
tutorial_samples:@echo "==== tutorial samples ===="# step into tutorial_samples directory and use its makefile$(MAKE) -C $(TUTORIAL_DIR)@echoupdate-includes:@echo "=== updating includes ==="mkdir -p $(prefix)mkdir -p $(prefix)/includecp -r ./include/* $(prefix)/include@echo# build the docs files and the refman.pdf
DOCS_DIR ?= docs
docs:
ifneq ($(shell which doxygen),)doxygen doxygen.config$(MAKE) -C $(DOCS_DIR)/latex
endif# TODO: change to call clean on subdirectory makefiles
clean:rm $(OBJ_FILES) $(DEP_FILES).PHONY: $(TARGET_DIRS) $(libstatic) $(libshared) $(TESTING_DIR) $(EXAMPLE_DIR) $(DOCS_DIR)

make$ sudo make install

make install后在编译testing和examples:

$ make testing$ make examples

使用cmake再试试:

 

相关内容