在 MKL 库中设置 openmp

Set openmp in MKL Library

我正在尝试编译一个最小的 C++ 代码

#include <iostream>
#include <mkl.h>
#include <omp.h>

int main(int argc, char *argv[])
{
omp_set_num_threads(4);

return 0;
}

在 MacOSX Sierra 10.12.5 中使用 MKL 库(icc 版本 17.0.4),使用命令

icc main.cpp -o main.o -DMKL_ILP64 -I/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/include \
  -L/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/lib -Wl,-rpath,/opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/lib -lmkl_intel_ilp64 \
  -lmkl_intel_thread -lmkl_core -liomp5 -lpthread -lm -ldl

但是当我 运行 程序

时收到以下错误
dyld: Library not loaded: @rpath/libiomp5.dylib
  Referenced from: /Users/user/C++/MKL1/./main.o
Reason: image not found
/bin/sh: line 1:  8898 Abort trap: 6           ./main.o
make: *** [run] Error 134

如何使用 MKL 库正确设置 openmpi?我尝试按照 https://software.intel.com/en-us/articles/dyld-library-not-loadedlibiomp5dylib?page=1#comment-1905809

中的说明进行操作

通过添加

 source /opt/intel/compilers_and_libraries_2017.4.181/mac/mkl/bin/mklvars.sh intel64

但是我在 运行 期间仍然遇到同样的错误。

我在 https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/645194

上找到了答案

基本上要加一个 -Wl,-rpath,/opt/intel/compilers_and_libraries_2017.4.181/mac/compiler/lib

编译期间。现在带有 icc 的 openmp 可以完美运行

我已经尝试了几种方法来解决这个问题。这实际上花了一整天。但这是结果,

Project Files

我创建了一个非常简单的项目,只有一个文件,名为,

test.c

1。最简单的解决方案

解决这个问题很简单,

icc -qopenmp -qopenmp-link=static test.c

此方法只是告诉编译器 link 到静态 OpenMP 运行 时间库。

有趣的是,Intel C++ Compiler Docs v.15 say that this is deprecated while Intel C++ Compiler Docs v.17 的文档甚至没有提到这个论点,但它确实有效。可能他们忘了把它拿出来。

2。另一种方法(我不推荐,英特尔的人也不推荐)

要编译文件,您可以使用普通的“-qopenmp”标志

icc -qopenmp test.c

这将创建 "a.out" 文件。

这种修复错误的方法使用了名为

的命令行实用程序
install_name_tool

使用这个方法我们可以改变@rpath/libiomp5.dylib

的路径
install_name_tool -change @rpath/libiomp5.dylib /opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib/libiomp5.dylib a.out

注意:代替compilers_and_libraries_2018.1.126应该是你的编译器版本。

3。最佳(正确)方式之一

你可以直接添加

export DYLD_LIBRARY_PATH="/opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib"

给你的~/.bash_profile

然后用正常的方式编译,

icc -qopenmp test.c

一切正常。


快速提示:您可以使用 otool 命令行实用程序来检查您的文件 links 到的库。

otool -L a.out