簡體   English   中英

將 OpenMP 與 clang 結合使用

[英]Using OpenMP with 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

當我使用 gcc 時,一切正常: gcc test.c -o test -fopenmp

我也嘗試運行export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5.1.1:$LD_LIBRARY_PATH但它沒有幫助。 `

有什么建議?

一些補充意見:

1) 您需要使用 -fopenmp=libomp 在 clang 中啟用 OpenMP。 -fopenmp 只是鏈接 libgomp 但忽略所有編譯指示。 很奇怪,我知道——很快就會在后備箱里換掉。

2) 3.7 是第一個支持 OpenMP 的版本。 3.6 沒有。

3) clang 只能與 libomp 一起工作。 不要將 libgomp(頭文件或庫)放在 libomp 的方式中! clang 使用 Intel API,libgomp 不支持。 -fopenmp=libomp 應該鏈接正確的庫。

更新

構建最新的 LLVM/Clang 主干 (clang-3.8),安裝 libiomp5,並指定 gomp omp 頭文件的位置。 請注意,用於 libiomp5 的 Ubuntu 包不太正確,因此您需要在 /usr/lib 中添加一個符號鏈接,從 /usr/lib/libiomp5.so 到 /usr/lib/libiomp5.so.5。

./clang++ -I/usr/lib/gcc/x86_64-linux-gnu/4.9/include -fopenmp=libiomp5 -o test test.cpp

我在 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;
    }
}

在 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) +++

$ 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) +++

您可以立即看到問題:clang++ 從不調用 GOMP_parallel,因此您總是得到一個線程。 這是 clang 的瘋狂行為。 您是否嘗試過構建和使用“特殊” OpenMP 版本的 clang?

我讓它在 Linux Mint 17.2 上運行。 (本質上是 Ubuntu 14.04):

軟件包:libiomp-dev clang-3.8

編譯標志: -fopenmp

鏈接器標志: -fopenmp=libiomp5

現在它編譯並使用多個線程。

這是修改后的FindOpenMP.cmake

OMP_NUM_THREADS 環境變量可能就是你想要的。 您也可以以編程方式設置它。

https://gcc.gnu.org/onlinedocs/libgomp/Environment-Variables.html

和叮當一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM