TBB,Thread Building Blocks,線程構建模塊,是Intel公司開發的並行編程開發的工具。其提供C++模版庫,用戶不必關注線程,而專注任務本身。抽象層僅需很少的接口代碼,性能上毫不遜色。且能夠靈活地適合不同的多核平臺,適合於跨平臺的移植(如Linux, Windows, Mac)
1)在官網下載tbb源碼,如下載tar.gz格式
tar -zxvf tbbxx.tar.gz
2)進入解壓縮目錄下,執行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
然後重新編譯 make即可。
3)添加tbb變量
cd build
chmod +x *.sh
sh generate_tbbvars.sh
sh tbbvars.sh
4)將.so庫移至/usr/lib,將頭文件移至 /usr/local/include
cd linux_intel64_gcc_你的版本_release
cp *.so /usr/lib
cp *.so.2 /usr/lib
/sbin/ldconfig
//回到解壓縮目錄下
/usr/local/include
//將頭文件移至 /usr/local/include
cd include
cp *.* /usr/local/include
5)接下來檢驗tbb是否安裝成功
cd ../examples/pipeline/square
make
如果是下面的提示,則表示tbb環境配置成功
g++ -O2 -DNDEBUG -o square square.cpp gen_input.cpp -ltbb -lrt
./square 0 input.txt output.txt
serial run time = 0.249843
parallel run time = 0.0304956
elapsed time : 0.399366 seconds