将OpenMP与clang一起使用

kuh*_*har 18 c c++ openmp clang

我在使用clang(3.6和3.8 ToT)编译OpenMP代码时遇到问题.

我关注了这篇博文http://blog.llvm.org/2015/05/openmp-support_22.html,但问题是编译的程序只在一个线程上执行.我正在使用ubuntu 15.04 x64,我安装了libgomp和libiopmp,并使用以下命令编译我的代码:

clang test.c -o test -fopenmp -L/usr/lib/gcc/x86_64-linux-gnu/5.1.1
Run Code Online (Sandbox Code Playgroud)

当我使用gcc代替时,一切正常: gcc test.c -o test -fopenmp

我也试过跑,export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5.1.1:$LD_LIBRARY_PATH但没有帮助.`

有什么建议?

小智 19

一些额外的评论:

1)您需要使用-fopenmp = libomp在clang中启用OpenMP.-fopenmp只链接libgomp但忽略了所有的pragma.很奇怪,我知道 - 并且很快就会在后备箱里改变.

2)3.7是第一个支持OpenMP的版本.3.6没有.

3)clang只能使用libomp.不要把libgomp(头文件或库)放在libomp的方式!clang使用Intel API,libgomp不支持.-fopenmp = libomp应链接正确的库.

此致,

安德烈·博汉科

软件工程师Intel

  • 你能澄清一下在当前的OSX版本下,正确的方法是编译符合C++ - 11的程序,请使用OpenMP调用`demo.cpp`吗?我的意思是完整的命令行吗? (2认同)
  • @Andrey Bokhanko我正在尝试使用clang 4.0和CMake 3.7使用`SET(CMAKE_CXX_FLAGS"$ {CMAKE_CXX_FLAGS} -Wall -std = c ++ 11 -fopenmp = libomp")find_package(OpenMP REQUIRED)`但仍然"无法找到OpenMP的" (2认同)

Tim*_*Tim 9

更新

构建最新的LLVM/Clang主干(clang-3.8),安装libiomp5,并指定gomp omp头文件的位置.请注意,libiomp5的Ubuntu包不太正确,因此您需要在/usr/lib/libiomp5.so中将/ usr/lib中的符号链接添加到/usr/lib/libiomp5.so.5.

./clang++ -I/usr/lib/gcc/x86_64-linux-gnu/4.9/include -fopenmp=libiomp5 -o test test.cpp
Run Code Online (Sandbox Code Playgroud)

我在Linux Mint 17.2(基本上是Ubuntu可信任)上使用g ++ - 5.1和clang ++ - 3.6,我看到了与以下代码相同的结果.

#include <iostream>
#include <omp.h>
int main() {
    #pragma omp parallel num_threads(4)
    {
        #pragma omp critical
        std::cout << "tid = " << omp_get_thread_num() << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

在ltrace下运行它会显示问题:

克++

$ g++ -fopenmp -o test test.cpp
$ ./test
tid = 0
tid = 3
tid = 2
tid = 1
$ ltrace ./test
__libc_start_main(0x400af6, 1, 0x7ffc937b8198, 0x400bc0 <unfinished ...>
_ZNSt8ios_base4InitC1Ev(0x6021b1, 0xffff, 0x7ffc937b81a8, 5)   = 0
__cxa_atexit(0x4009f0, 0x6021b1, 0x602090, 0x7ffc937b7f70)     = 0
GOMP_parallel(0x400b6d, 0, 4, 0 <unfinished ...>
GOMP_critical_start(0, 128, 0, 0)                              = 0
tid = 3
tid = 2
omp_get_thread_num(0x7f9fe13894a8, 1, 0, 0x493e0)              = 0
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(0x6020a0, 0x400c44, 0, 0x493e0) = 0x6020a0
_ZNSolsEi(0x6020a0, 0, 0x7f9fe1a03988, 0x203d2064)             = 0x6020a0
_ZNSolsEPFRSoS_E(0x6020a0, 0x400920, 0x7f9fe1a03988, 0 <unfinished ...>
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(0x6020a0, 0x400920, 0x7f9fe1a03988, 0) = 0x6020a0
<... _ZNSolsEPFRSoS_E resumed> )                               = 0x6020a0
GOMP_critical_end(0x7f9fe0d2d400, 0x7f9fe0d2e9e0, 0, -1)       = 0
tid = 1
tid = 0
<... GOMP_parallel resumed> )                                  = 0
_ZNSt8ios_base4InitD1Ev(0x6021b1, 0, 224, 0x7f9fe0d2df50)      = 0x7f9fe1a08940
+++ exited (status 0) +++
Run Code Online (Sandbox Code Playgroud)

$ clang++ -fopenmp -o test test.cpp
$ ./test
tid = 0
$ ltrace ./test
__libc_start_main(0x4009a0, 1, 0x7ffde4782538, 0x400a00 <unfinished ...>
_ZNSt8ios_base4InitC1Ev(0x6013f4, 0x7ffde4782538, 0x7ffde4782548, 5) = 0
__cxa_atexit(0x400830, 0x6013f4, 0x6012c8, 0x7ffde4782310)     = 0
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(0x6012e0, 0x400a84, 0x7ffde4782548, 6) = 0x6012e0
omp_get_thread_num(0x7f3e4698c006, 0x7f3e4698c000, 0x7f3e46764988, 1024) = 0
_ZNSolsEi(0x6012e0, 0, 0x7f3e46764988, 1024)                   = 0x6012e0
_ZNSolsEPFRSoS_E(0x6012e0, 0x4007a0, 0x7f3e46764988, 0 <unfinished ...>
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(0x6012e0, 0x4007a0, 0x7f3e46764988, 0) = 0x6012e0
tid = 0
<... _ZNSolsEPFRSoS_E resumed> )                               = 0x6012e0
_ZNSt8ios_base4InitD1Ev(0x6013f4, 0, 224, 0x7f3e45886f50)      = 0x7f3e46769940
+++ exited (status 0) +++
Run Code Online (Sandbox Code Playgroud)

你可以立即看到问题:clang ++从不调用GOMP_parallel,所以你总是得到一个线程.这是铿锵声的疯狂行为.您是否尝试过构建和使用"特殊" OpenMP版本的clang?


vei*_*eio 6

我在Linux Mint 17.2上工作了.(基本上是Ubuntu 14.04):

包:libiomp-dev clang-3.8

编译标志: -fopenmp

链接器标志: -fopenmp=libiomp5

现在它编译并使用多个线程.

这是修改后的FindOpenMP.cmake