OpenMP环境添加(Mac)
1 | $ brew reinstall gcc --without-multilib |
安装错误
1 | # Problem: No available formula with the name "clang-omp" |
安装错误参考,用以上方法发现未解决问题;
fatal: unable to access https://github.com/Homebrew/homebrew-core:
LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core failed!
Error: Could not link:
/usr/local/share/man/man1/brew.1
Please delete these paths and run
brew update
.
Error: Could not link:
/usr/local/share/doc/homebrew
1 | 删除 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core 文件夹 |
fatal: unable to access ‘https://github.com/Homebrew/homebrew-core/': LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Error: Failure while executing: git clone https://github.com/Homebrew/homebrew-core /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core –depth=1
Error: Failure while executing: /usr/local/bin/brew tap homebrew/core
1 | brew doctor |
Error: No available formula with the name “clang-omp”
==> Searching for a previously deleted formula (in the last month)…
Warning: homebrew/core is shallow clone. To get complete history run:
git -C “$(brew –repo homebrew/core)” fetch –unshallow
Error: No previously deleted formula found.
==> Searching for similarly named formulae…
==> Searching local taps…
Error: No similarly named formulae found.
==> Searching taps…
==> Searching taps on GitHub…
Error: No formulae found in taps.
1 | ''' |
On Apple Clang, you need to add several options to use OpenMP’s front end
instead of the standard driver option. This usually looks like
-Xpreprocessor -fopenmp -lomp
You might need to make sure the lib and include directories are discoverable
if /usr/local is not searched:
-L/usr/local/opt/libomp/lib -I/usr/local/opt/libomp/include
For CMake, the following flags will cause the OpenMP::OpenMP_CXX target to
be set up correctly:
-DOpenMP_CXX_FLAGS=”-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include”
-DOpenMP_CXX_LIB_NAMES=”omp”
-DOpenMP_omp_LIBRARY=/usr/local/opt/libomp/lib/libomp.dylib
接下来发现libomp已经在我的lib库里了,定位:/usr/local/opt/libomp;
下面要做的事情就是终端到你写的.c文件下,输入以下命令编译;1
clang -Xclang -fopenmp openmp_test.c -I /usr/local/opt/libom/include -L /usr/local/opt/libomp/lib -lomp
上面这个命令会链接头文件和动态库,我们想办法把这两个路径添加到环境变量里,每次都写路径很麻烦;
Compiler
1 | $ export OMP_NUM_THREADS=4 |
因为我们要用clang来编译,所以上面的这个编译过程应该写为;
1 | export OMP_NUM_THREADS=4 |
最终解决方案,Homebrew安装的GCC存放在 /usr/local/bin/gcc-5 中(GCC5.2.0版)。此时,系统中依然有 /usr/bin/gcc ,然而这个GCC是clang的链接。所以在编译时,需要使用类似 gcc-5 xxx.c -o xxx -fopenmp 进行编译,而不是使用默认的gcc。
所以只要我们用下面的编译方法就可以了:1
2 gcc-6 -fopenmp filename.c -o filename
./filename #执行吧!
真的是绕了一圈,原来是llvm 与 gcc的历史问题;
PS
附1:测试程序
1 |
|
附2: 继续找发现一个在Xcode上安装clang-omp安装的方法,未测试通过,慎用;
1 | $ brew install llvm |