linux下安装和使用tbb

linux下安装和使用tbb,第1张

线程构建模块(TBB)使您可以轻松编写并行C ++程序,这些程序可充分利用多核性能,可移植且可组合,并具有面向未来的可扩展性。 广泛用于任务并行的C ++模板库。 并行算法和数据结构,可扩展的内存分配和任务调度。 丰富的通用并行功能集,C ++ Windows *,Linux *,OS X *和其他 *** 作系统 选择一个稳定版本即可,我用的是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 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 经过一段时间运行如下说明测试通过 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 = 10i <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 = 0i <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

编译

tar -xf tbb41_20120718oss_src.tgz

cd ./tbb41_20120718oss_src.tgz,

按照readme的提示gmake 编译完成之后,build路径下面,会出现:

drwxr-xr-x 2 root root 20480 01-24 12:42 linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_debug

drwxr-xr-x 2 root root 20480 01-24 12:38 linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release

修改环境变量

shell下执行: # vim ~/.bashrc

在最后添加 source /root/Desktop/tbb40_233oss/build/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_debug/tbbvars.sh

那么使用连接库的时候要使用-ltbb_debug

或者 source /root/Desktop/tbb40_233oss/build/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release/tbbvars.sh 对应的链接库就是-ltbb了

1、测试 ltbb

重新打开一个shell,进入/tbb/tbb41_20120718oss/examples/pipeline/square

输入make,出现下面信息,表示成功安装

g++ -O2 -DNDEBUG -o square square.cpp -ltbb -lrt

./square 0 input.txt output.txt

serial run time = 0.160147

parallel run time = 0.0810647

elapsed time : 0.359243 seconds

2、测试ltbb_debug,就source deubg文件里面的tbbvars.sh

输入make debug test,出现下面信息表示成功安装

g++ -O2 -o gen_input gen_input.cpp -lrt

./gen_input >input.txt

g++ -O0 -g -DTBB_USE_DEBUG -o square square.cpp -ltbb_debug -lrt

./square 0 input.txt output.txt

serial run time = 0.169299

parallel run time = 0.0883386

elapsed time : 0.26235 seconds

3、测试自己写的cpp文件

记住在编译的时候加上-ltbb或-ltbb_debug。错误一般都是找不到库或者头文件,可以上网看看怎么设置PATH,LIBRARY_PATH,LD_LIBRARY_PATH。把这些路径设到tbb编译好的路径里面就行了。

例如gcc test.cc -ltbb_debug


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/6229974.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-18
下一篇 2023-03-18

发表评论

登录后才能评论

评论列表(0条)

保存