linux下安裝和使用tbb

說明

線程構(gòu)建模塊(TBB)使您可以輕松編寫并行C ++程序,這些程序可充分利用多核性能,可移植且可組合,并具有面向未來的可擴展性。

特點

廣泛用于任務并行的C ++模板庫。

并行算法和數(shù)據(jù)結(jié)構(gòu),可擴展的內(nèi)存分配和任務調(diào)度。

豐富的通用并行功能集,C ++;?Windows *,Linux *,OS X *和其他操作系統(tǒng)

下載

選擇一個穩(wěn)定版本即可,我用的是tbb-2018_U3

https://github.com/01org/tbb/releases

解壓

tar -zxvf tbb-2018_U3.tar.gz

編譯

cd tbb-2018_U3

make

我的編譯器未報錯

若編譯期間報出以下錯誤:

/tmp/ccxNhOc9.s: Assembler messages:

/tmp/ccxNhOc9.s:615: Error: no such instruction: `xtest'

/tmp/ccxNhOc9.s:643: Error: no such instruction: `xabort $255'

/tmp/ccxNhOc9.s:652: Error: no such instruction: `xabort $255'

/tmp/ccxNhOc9.s:658: Error: no such instruction: `xend'

/tmp/ccxNhOc9.s:825: Error: no such instruction: `xbegin .L56'

/tmp/ccxNhOc9.s:988: Error: no such instruction: `xbegin .L71'

/tmp/ccxNhOc9.s:1216: Error: no such instruction: `xabort $255'

make[1]: *** [x86_rtm_rw_mutex.o] Error 1

make[1]: *** Waiting for unfinished jobs....

make[1]: Leaving directory `/user_data/.tmp/linux_intel64_gcc_cc4.9.1_libc2.12_kernel2.6.32_debug'

make: *** [tbb] Error 2

??? 則表明當前的gcc編譯器不支持-mrtm。則可行的方案是注釋以下幾行。打開tbb目錄build/linux.gcc.inc文件,注釋以下幾行。

# gcc 4.8 and later support RTM intrinsics, but require command line switch to enable them

ifneq (,$(shell gcc -dumpversion | egrep? "^4\.[8-9]"))

? ? RTM_KEY = -mrtm

#endif

添加tbb變量

cd build

chmod +x*.sh

sh generate_tbbvars.sh

sh tbbvars.sh

配置頭文件及庫文件

cd linux_intel64_gcc_你的版本_release

cp *.so /usr/lib64

cp *.so.2 /usr/lib64

ldconfig

//回到解壓縮目錄下

cp -r include/* /usr//include

測試

//進入解壓目錄下的examples目錄

make

經(jīng)過一段時間運行如下說明測試通過

serial run time = 0.21682

parallel run time = 0.0694263

elapsed time : 0.444736 seconds

make[1]: Leaving directory `/home/heweiwei/test/tbb-2018_U3/examples/pipeline/square'

------------------------ test_all/fibonacci/all ------------------------

make -C test_all/fibonacci? -f Makefile all CXX="g++"? ?

make[1]: Entering directory `/home/heweiwei/test/tbb-2018_U3/examples/test_all/fibonacci'

g++ -O2 -DNDEBUG? -m64 -L/home/heweiwei/test/tbb-2018_U3/examples/../build/linux_intel64_gcc_cc4.8.5_libc2.17_kernel3.10.0_release -L/home/heweiwei/test/tbb-2018_U3/examples/../build/linux_intel64_gcc_cc4.8.5_libc2.17_kernel3.10.0_debug -o fibonacci Fibonacci.cpp -ltbb -lpthread -lrt -lrt

./fibonacci

TEST PASSED

make[1]: Leaving directory `/home/heweiwei/test/tbb-2018_U3/examples/test_all/fibonacci'

測試代碼

#include <tbb/tbb.h>

#include<iostream>

using namespace std;

using namespace tbb;

int main()

{
???????? int i = 0;

? ? ? ? concurrent_queue<int> s_tbb_queue;

? ? ? ? for (i = 10; i < 15 ; i ++)

? ? ? ? {

? ? ? ? ? ? ? ? s_tbb_queue.push(i);

? ? ? ? }

? ? ? ? concurrent_queue<int>::iterator iter;

? ? ? ? for(iter = s_tbb_queue.unsafe_begin() ; iter != s_tbb_queue.unsafe_end() ; iter++)

? ? ? ? {

? ? ? ? ? ? ? ? cout<<"value="<<*iter<<endl;

? ? ? ? }

? ? ? ? cout<<"queue_size="<<s_tbb_queue.unsafe_size()<<endl;

? ? ? ? int tmp = 0;

? ? ? ? bool res = 0;

? ? ? ? for (i = 0; i < 8 ; i++)

? ? ? ? {

? ? ? ? ? ? ? ? res = s_tbb_queue.try_pop(tmp);

? ? ? ? ? ? ? ? if(res)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? cout<<"pop_value="<<tmp<<endl;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? cout<<"queue is empty"<<endl;

? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? cout<<"queue_size="<<s_tbb_queue.unsafe_size()<<endl;

? ? ? ? return 0;

}

運行

[heweiwei@heweiwei tbb_test]$ ./a.out

value=10

value=11

value=12

value=13

value=14

queue_size=5

pop_value=10

pop_value=11

pop_value=12

pop_value=13

pop_value=14

queue is empty

queue is empty

queue is empty

queue_size=0

參考

https://blog.csdn.net/u010793236/article/details/74010571

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容