使用 Makefile 使用 MKL、OpenBLAS 和其他库进行编译
compiling with MKL, OpenBLAS, and other libraries by using Makefile
我试过在github(https://github.com/gerowam/influence)上发表过一个概率模型的编译,但是对于不熟悉C语言和编译的人来说,真的很难知道如何安装相关库并在 ubuntu 20.04.
上使用“Makefile”进行编译
下面是我根据github网页上编译过程的简短介绍安装所需的库所做的:
sudo apt-get update
sudo apt-get install git
sudo apt-get install make
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install libmkl-dev
sudo apt-get install libopenblas-dev
sudo apt-get install libhdf5-dev
sudo apt-get install liblapack-dev
sudo git clone http://www.github.com/gerowam/influence
当我运行“制作”时,我得到了以下内容:
swhan@swhan-VirtualBox:~/influence/dtm-dim-rdim/rdim$ make
g++ -w -Wunsafe-loop-optimizations -DSPARSE -DMKL -O3 -DMKL_ILP64 -DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF -I ../lib/include -I ../lib -I ../lib/math -I ../gslwrap/include -I ../gslwrap/include/gslwrap -I ../local/include -I ../lib/util/gflags-1.1/src/gflags -I/include -I /include -I /include -m64 -fopenmp -frounding-math -msse4.2 -mfpmath=sse -march=native -fno-trapping-math -fno-signaling-nans -fno-signed-zeros -fgcse-after-reload -funsafe-loop-optimizations -fgcse-sm -fgcse-las -fipa-pta -funswitch-loops -c -o ss-lm.o ss-lm.c
In file included from ss-lm.h:29,
from ss-lm.c:15:
data.h:41:10: fatal error: mkl_lapacke.h: No such file or directory
41 | #include "mkl_lapacke.h"
| ^~~~~~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: ss-lm.o] Error 1
所以我查看了 Makefile,它看起来像这样:
CFLAGS = -DSPARSE -DMKL -O3 -DMKL_ILP64 \
-DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF \
-I ${SPARSE_GSL_INC} -I ${LIB} -I ${LIB}/math -I ${GSLWRAP_LIB}/include \
-I ${GSLWRAP_LIB}/include/gslwrap -I ${LOCAL_DIR}/include \
-I ${LIB}/util/gflags-1.1/src/gflags -I${LOCAL}/include \
-I ${OPENBLAS_ROOT}/include \
-I ${MKLROOT}/include \
-m64 -fopenmp -frounding-math -msse4.2 -mfpmath=sse -march=native \
-fno-trapping-math -fno-signaling-nans -fno-signed-zeros \
-fgcse-after-reload -funsafe-loop-optimizations \
-fgcse-sm -fgcse-las -fipa-pta -funswitch-loops
#-I ../opt-local/openblas/include \
#-fprofile-use -fprofile-correction
#-fprofile-correction -fprofile-generate -fprofile-arcs -fprofile-values
但是在 Makefile 中,不存在“MKLROOT”和“OPENBLAS_ROOT”的宏,但是当我尝试为两者设置宏时,出现了问题。具体来说,好像MKL库是安装在/usr/include/mkl的路径,但是
-I ${MKLROOT}/include
以上内容让我对如何为 MKL 设置宏感到困惑。另外,如果我设置如下:
MKLROOT=/usr
...
-I ${MKLROOT}/include/mkl
另一个问题弹出。也就是说,
LDFLAGS = -Wl,-Map=ld_linkmap,-cref,-rpath -Wl,${LIB}/lib -L${LIB}/lib \
-L${LOCAL_DIR}/lib -L${LOCAL_DIR}/lib/stl \
-L../opt-local/openblas/lib -L/usr/lib/x86_64-linux-gnu/hdf5/serial \
-lhdf5 -lhdf5_hl -lgsl -lopenblas -lm \
-Wl,--no-as-needed -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_core -lmkl_gnu_thread -lgomp -ldl -lpthread
在这种情况下,与“intel64”相关的内容似乎位于 /usr/include/mkl/intel64 但我不知道如何执行上述命令。以下是我得到的错误信息:
/usr/bin/ld: main.o: in function `main':
main.c:(.text.startup+0x156): undefined reference to `MKL_Set_Interface_Layer'
/usr/bin/ld: main.c:(.text.startup+0x15d): undefined reference to `MKL_Set_Threading_Layer'
collect2: error: ld returned 1 exit status
make: *** [Makefile:55: main] Error 1
由于您的 MKL 库安装在 /usr/include/mkl 位置,您可以将 MKLROOT 环境变量设置为 export MKLROOT=/usr/include/mkl
另一种方法是,安装 oneAPI Base & HPC Toolkits 从那里你可以获得英特尔 MKL 库包并初始化 oneAPI 环境,它会自动
设置 MKLROOT。有关 oneAPI Base Toolkit 和 oneAPI HPC Toolkit 的更多详细信息,请参阅 https://software.intel.com/content/www/us/en/develop/articles/installation-guide-for-intel-oneapi-toolkits.html
参考下面link关于Intel oneMKL
https://software.intel.com/content/www/us/en/develop/documentation/onemkl-linux-developer-guide/top/getting-started/setting-environment-variables/scripts-to-set-environment-variables.html
我试过在github(https://github.com/gerowam/influence)上发表过一个概率模型的编译,但是对于不熟悉C语言和编译的人来说,真的很难知道如何安装相关库并在 ubuntu 20.04.
上使用“Makefile”进行编译下面是我根据github网页上编译过程的简短介绍安装所需的库所做的:
sudo apt-get update
sudo apt-get install git
sudo apt-get install make
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install libmkl-dev
sudo apt-get install libopenblas-dev
sudo apt-get install libhdf5-dev
sudo apt-get install liblapack-dev
sudo git clone http://www.github.com/gerowam/influence
当我运行“制作”时,我得到了以下内容:
swhan@swhan-VirtualBox:~/influence/dtm-dim-rdim/rdim$ make
g++ -w -Wunsafe-loop-optimizations -DSPARSE -DMKL -O3 -DMKL_ILP64 -DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF -I ../lib/include -I ../lib -I ../lib/math -I ../gslwrap/include -I ../gslwrap/include/gslwrap -I ../local/include -I ../lib/util/gflags-1.1/src/gflags -I/include -I /include -I /include -m64 -fopenmp -frounding-math -msse4.2 -mfpmath=sse -march=native -fno-trapping-math -fno-signaling-nans -fno-signed-zeros -fgcse-after-reload -funsafe-loop-optimizations -fgcse-sm -fgcse-las -fipa-pta -funswitch-loops -c -o ss-lm.o ss-lm.c
In file included from ss-lm.h:29,
from ss-lm.c:15:
data.h:41:10: fatal error: mkl_lapacke.h: No such file or directory
41 | #include "mkl_lapacke.h"
| ^~~~~~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: ss-lm.o] Error 1
所以我查看了 Makefile,它看起来像这样:
CFLAGS = -DSPARSE -DMKL -O3 -DMKL_ILP64 \
-DNDEBUG -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF \
-I ${SPARSE_GSL_INC} -I ${LIB} -I ${LIB}/math -I ${GSLWRAP_LIB}/include \
-I ${GSLWRAP_LIB}/include/gslwrap -I ${LOCAL_DIR}/include \
-I ${LIB}/util/gflags-1.1/src/gflags -I${LOCAL}/include \
-I ${OPENBLAS_ROOT}/include \
-I ${MKLROOT}/include \
-m64 -fopenmp -frounding-math -msse4.2 -mfpmath=sse -march=native \
-fno-trapping-math -fno-signaling-nans -fno-signed-zeros \
-fgcse-after-reload -funsafe-loop-optimizations \
-fgcse-sm -fgcse-las -fipa-pta -funswitch-loops
#-I ../opt-local/openblas/include \
#-fprofile-use -fprofile-correction
#-fprofile-correction -fprofile-generate -fprofile-arcs -fprofile-values
但是在 Makefile 中,不存在“MKLROOT”和“OPENBLAS_ROOT”的宏,但是当我尝试为两者设置宏时,出现了问题。具体来说,好像MKL库是安装在/usr/include/mkl的路径,但是
-I ${MKLROOT}/include
以上内容让我对如何为 MKL 设置宏感到困惑。另外,如果我设置如下:
MKLROOT=/usr
...
-I ${MKLROOT}/include/mkl
另一个问题弹出。也就是说,
LDFLAGS = -Wl,-Map=ld_linkmap,-cref,-rpath -Wl,${LIB}/lib -L${LIB}/lib \
-L${LOCAL_DIR}/lib -L${LOCAL_DIR}/lib/stl \
-L../opt-local/openblas/lib -L/usr/lib/x86_64-linux-gnu/hdf5/serial \
-lhdf5 -lhdf5_hl -lgsl -lopenblas -lm \
-Wl,--no-as-needed -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_core -lmkl_gnu_thread -lgomp -ldl -lpthread
在这种情况下,与“intel64”相关的内容似乎位于 /usr/include/mkl/intel64 但我不知道如何执行上述命令。以下是我得到的错误信息:
/usr/bin/ld: main.o: in function `main':
main.c:(.text.startup+0x156): undefined reference to `MKL_Set_Interface_Layer'
/usr/bin/ld: main.c:(.text.startup+0x15d): undefined reference to `MKL_Set_Threading_Layer'
collect2: error: ld returned 1 exit status
make: *** [Makefile:55: main] Error 1
由于您的 MKL 库安装在 /usr/include/mkl 位置,您可以将 MKLROOT 环境变量设置为 export MKLROOT=/usr/include/mkl
另一种方法是,安装 oneAPI Base & HPC Toolkits 从那里你可以获得英特尔 MKL 库包并初始化 oneAPI 环境,它会自动
设置 MKLROOT。有关 oneAPI Base Toolkit 和 oneAPI HPC Toolkit 的更多详细信息,请参阅 https://software.intel.com/content/www/us/en/develop/articles/installation-guide-for-intel-oneapi-toolkits.html
参考下面link关于Intel oneMKL https://software.intel.com/content/www/us/en/develop/documentation/onemkl-linux-developer-guide/top/getting-started/setting-environment-variables/scripts-to-set-environment-variables.html