`
pleasetojava
  • 浏览: 704321 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

intel线程库tbb的使用

阅读更多

首先下载:

http://www.threadingbuildingblocks.org/uploads/77/111/2.1/tbb21_20080605oss_win.zip

当前是2.1版本

解压到c盘,打开vs2005,设置vc++的项目目录

include:

C:\tbb21oss_win\include

执行文件:

C:\tbb21oss_win\ia32\vc8\bin

库文件:

C:\tbb21oss_win\ia32\vc8\lib

最后设置 我的电脑--环境变量设置

添加下面到path部分的最前面,记得加上一个“;”:C:\tbb21oss_win\ia32\vc8\bin

然后添加一个变量:

TBB21_INSTALL_DIR 为C:\tbb21oss_win

开启vs2005的命令行工具,进入到C:\tbb21oss_win\ia32\vc8\bin路径,执行tbbvars.bat文件

可以开始第一个程序了,记得带上tbb.lib对应的是release编译模式

  1. /*
  2. Copyright2005-2008IntelCorporation.AllRightsReserved.
  3. ThisfileispartofThreadingBuildingBlocks.
  4. ThreadingBuildingBlocksisfreesoftware;youcanredistributeit
  5. and/ormodifyitunderthetermsoftheGNUGeneralPublicLicense
  6. version2aspublishedbytheFreeSoftwareFoundation.
  7. ThreadingBuildingBlocksisdistributedinthehopethatitwillbe
  8. useful,butWITHOUTANYWARRANTY;withouteventheimpliedwarranty
  9. ofMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
  10. GNUGeneralPublicLicenseformoredetails.
  11. YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
  12. alongwithThreadingBuildingBlocks;ifnot,writetotheFreeSoftware
  13. Foundation,Inc.,51FranklinSt,FifthFloor,Boston,MA02110-1301USA
  14. Asaspecialexception,youmayusethisfileaspartofafreesoftware
  15. librarywithoutrestriction.Specifically,ifotherfilesinstantiate
  16. templatesorusemacrosorinlinefunctionsfromthisfile,oryoucompile
  17. thisfileandlinkitwithotherfilestoproduceanexecutable,this
  18. filedoesnotbyitselfcausetheresultingexecutabletobecoveredby
  19. theGNUGeneralPublicLicense.Thisexceptiondoesnothowever
  20. invalidateanyotherreasonswhytheexecutablefilemightbecoveredby
  21. theGNUGeneralPublicLicense.
  22. */
  23. //
  24. //Exampleprogramthatreadsafileoftextandchangesthefirstletter
  25. //ofeachwordtouppercase.
  26. //
  27. #include"tbb/pipeline.h"
  28. #include"tbb/tick_count.h"
  29. #include"tbb/task_scheduler_init.h"
  30. #include<cstring>
  31. #include<cstdlib>
  32. #include<cstdio>
  33. #include<cctype>
  34. usingnamespacestd;
  35. //!Bufferthatholdsblockofcharactersandlastcharacterofpreviousbuffer.
  36. classMyBuffer{
  37. staticconstsize_tbuffer_size=10000;
  38. char*my_end;
  39. //!storage[0]holdsthelastcharacterofthepreviousbuffer.
  40. charstorage[1+buffer_size];
  41. public:
  42. //!Pointertofirstcharacterinthebuffer
  43. char*begin(){returnstorage+1;}
  44. constchar*begin()const{returnstorage+1;}
  45. //!Pointertoonepastlastcharacterinthebuffer
  46. char*end()const{returnmy_end;}
  47. //!Setendofbuffer.
  48. voidset_end(char*new_ptr){my_end=new_ptr;}
  49. //!Numberofbytesabuffercanhold
  50. size_tmax_size()const{returnbuffer_size;}
  51. //!Numberofbytesappendedtobuffer.
  52. size_tsize()const{returnmy_end-begin();}
  53. };
  54. classMyInputFilter:publictbb::filter{
  55. public:
  56. staticconstsize_tn_buffer=8;
  57. MyInputFilter(FILE*input_file_);
  58. private:
  59. FILE*input_file;
  60. size_tnext_buffer;
  61. charlast_char_of_previous_buffer;
  62. MyBufferbuffer[n_buffer];
  63. /*override*/void*operator()(void*);
  64. };
  65. MyInputFilter::MyInputFilter(FILE*input_file_):
  66. filter(/*is_serial=*/true),
  67. next_buffer(0),
  68. input_file(input_file_),
  69. last_char_of_previous_buffer('')
  70. {
  71. }
  72. void*MyInputFilter::operator()(void*){
  73. MyBuffer&b=buffer[next_buffer];
  74. next_buffer=(next_buffer+1)%n_buffer;
  75. size_tn=fread(b.begin(),1,b.max_size(),input_file);
  76. if(!n){
  77. //endoffile
  78. returnNULL;
  79. }else{
  80. b.begin()[-1]=last_char_of_previous_buffer;
  81. last_char_of_previous_buffer=b.begin()[n-1];
  82. b.set_end(b.begin()+n);
  83. return&b;
  84. }
  85. }
  86. //!Filterthatchangesthefirstletterofeachwordfromlowercasetouppercase.
  87. classMyTransformFilter:publictbb::filter{
  88. public:
  89. MyTransformFilter();
  90. /*override*/void*operator()(void*item);
  91. };
  92. MyTransformFilter::MyTransformFilter():
  93. tbb::filter(/*ordered=*/false)
  94. {}
  95. /*override*/void*MyTransformFilter::operator()(void*item){
  96. MyBuffer&b=*static_cast<MyBuffer*>(item);
  97. intprev_char_is_space=b.begin()[-1]=='';
  98. for(char*s=b.begin();s!=b.end();++s){
  99. if(prev_char_is_space&&islower((unsignedchar)*s))
  100. *s=toupper(*s);
  101. prev_char_is_space=isspace((unsignedchar)*s);
  102. }
  103. return&b;
  104. }
  105. //!Filterthatwriteseachbuffertoafile.
  106. classMyOutputFilter:publictbb::filter{
  107. FILE*my_output_file;
  108. public:
  109. MyOutputFilter(FILE*output_file);
  110. /*override*/void*operator()(void*item);
  111. };
  112. MyOutputFilter::MyOutputFilter(FILE*output_file):
  113. tbb::filter(/*is_serial=*/true),
  114. my_output_file(output_file)
  115. {
  116. }
  117. void*MyOutputFilter::operator()(void*item){
  118. MyBuffer&b=*static_cast<MyBuffer*>(item);
  119. fwrite(b.begin(),1,b.size(),my_output_file);
  120. returnNULL;
  121. }
  122. staticintNThread=tbb::task_scheduler_init::automatic;
  123. staticconstchar*InputFileName="input.txt";
  124. staticconstchar*OutputFileName="output.txt";
  125. staticboolis_number_of_threads_set=false;
  126. voidUsage()
  127. {
  128. fprintf(stderr,"Usage:\ttext_filter[input-file[output-file[nthread]]]\n");
  129. }
  130. intParseCommandLine(intargc,char*argv[]){
  131. //Parsecommandline
  132. if(argc>4){
  133. Usage();
  134. return0;
  135. }
  136. if(argc>=2)InputFileName=argv[1];
  137. if(argc>=3)OutputFileName=argv[2];
  138. if(argc>=4){
  139. NThread=strtol(argv[3],0,0);
  140. if(NThread<1){
  141. fprintf(stderr,"nthreadsetto%d,butmustbeatleast1\n",NThread);
  142. return0;
  143. }
  144. is_number_of_threads_set=true;//Numberofthreadsissetexplicitly
  145. }
  146. return1;
  147. }
  148. intrun_pipeline(intnthreads)
  149. {
  150. FILE*input_file=fopen(InputFileName,"r");
  151. if(!input_file){
  152. perror(InputFileName);
  153. Usage();
  154. return0;
  155. }
  156. FILE*output_file=fopen(OutputFileName,"w");
  157. if(!output_file){
  158. perror(OutputFileName);
  159. return0;
  160. }
  161. //Createthepipeline
  162. tbb::pipelinepipeline;
  163. //Createfile-readingwritingstageandaddittothepipeline
  164. MyInputFilterinput_filter(input_file);
  165. pipeline.add_filter(input_filter);
  166. //Createcapitalizationstageandaddittothepipeline
  167. MyTransformFiltertransform_filter;
  168. pipeline.add_filter(transform_filter);
  169. //Createfile-writingstageandaddittothepipeline
  170. MyOutputFilteroutput_filter(output_file);
  171. pipeline.add_filter(output_filter);
  172. //Runthepipeline
  173. tbb::tick_countt0=tbb::tick_count::now();
  174. pipeline.run(MyInputFilter::n_buffer);
  175. tbb::tick_countt1=tbb::tick_count::now();
  176. //Removefiltersfrompipelinebeforetheyareimplicitlydestroyed.
  177. pipeline.clear();
  178. fclose(output_file);
  179. fclose(input_file);
  180. if(is_number_of_threads_set){
  181. printf("threads=%dtime=%g\n",nthreads,(t1-t0).seconds());
  182. }else{
  183. if(nthreads==1){
  184. printf("serialruntime=%g\n",(t1-t0).seconds());
  185. }else{
  186. printf("parallelruntime=%g\n",(t1-t0).seconds());
  187. }
  188. }
  189. return1;
  190. }
  191. intmain(intargc,char*argv[]){
  192. if(!ParseCommandLine(argc,argv))
  193. return1;
  194. if(is_number_of_threads_set){
  195. //Starttaskscheduler
  196. tbb::task_scheduler_initinit(NThread);
  197. if(!run_pipeline(NThread))
  198. return1;
  199. }else{//Numberofthreadswasn'tsetexplicitly.Runserialandparallelversion
  200. {//serialrun
  201. tbb::task_scheduler_initinit_serial(1);
  202. if(!run_pipeline(1))
  203. return1;
  204. }
  205. {//parallelrun(numberofthreadsisselectedautomatically)
  206. tbb::task_scheduler_initinit_parallel;
  207. if(!run_pipeline(0))
  208. return1;
  209. }
  210. }
  211. return0;
  212. }

第二个程序,对应debug模式,带上tbb_debug.lib:

  1. #include"tbb/task_scheduler_init.h"
  2. #include"tbb/blocked_range.h"
  3. #include"tbb/parallel_for.h"
  4. //链接tbb_debug.lib
  5. //#pragmacomment(lib,"tbb_debug.lib")
  6. usingnamespacetbb;
  7. //对每个Item执行该操作
  8. voidFoo(floatvalue)
  9. {
  10. printf("%.2f\n",value);
  11. }
  12. classApplyFoo
  13. {
  14. float*constmy_a;
  15. public:
  16. voidoperator()(constblocked_range<size_t>&r)const
  17. {
  18. float*a=my_a;
  19. for(size_ti=r.begin();i!=r.end();++i)
  20. Foo(a[i]);
  21. }
  22. ApplyFoo(floata[]):my_a(a){}
  23. };
  24. int_tmain(intargc,_TCHAR*argv[])
  25. {
  26. //创建taskscheduler
  27. //task_scheduler_init支持一个参数,以指定使用的线程数
  28. task_scheduler_initinit;
  29. floata[100];
  30. for(inti=0;i<100;i++)
  31. a[i]=(float)i;
  32. //TBB会把数组分成若干的block
  33. //对block调用ApplyFoo这个functor
  34. parallel_for(blocked_range<size_t>(0,100),ApplyFoo(a));
  35. return0;
  36. }
分享到:
评论

相关推荐

    TBB多线程库

    intel弄的多线程C++开发库,挺漂亮的,值得一用

    intel的tbb库,版本是2018 update5

    TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议是 GPLv2。 TBB 获得过...

    Intel TBB Library

    TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议是 GPLv2。 TBB 获得过...

    tbb, Intel r5与CMake构建系统.zip

    tbb, Intel r5与CMake构建系统 Intel(R) 线程生成块 这是目前基于TBB更新,并且将不时更新以跟踪最新版本的git知识库。 唯一的修改是添加基于的构建系统。因为r5可以很容易地使用git子模块和简单的add_subdirectory ...

    tbb2019_20190522oss_win.zip

    TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议是 GPLv2。 TBB 获得过...

    Threading Building Blocks 线程创建模块开源库

    在多核的平台上开发并行化的程序,必须合理地利用系统的资源 - 如与内核数目相匹配的线程,内存的合理访问...d)线程库的接口适合于跨平台的移植(Linux, Windows, Mac) e)支持的C++编译器 – Microsoft, GNU and Intel

    Pro TBB C++ Thread Blocks Parallel.pdf

    TBB是Intel公司的用于C++多线程和并行计算程序的库文件,该书是2019年新版,内容是英文版的,请注意下载。

    Linux应用程序多核并行化方法研究与实现

    并基于Intel同构多核平台,运用intel并行时库TBB(Intel Thread Building Blocks)进行相应的并行化改造,并在调试和优化阶段运用Intel Thread Checker(线程检测器)检测发现看似正确的程序的潜在错误。实验结果表明,...

    TBB-vs2017-x64

    TBB 2021.7.0版本windows x64运行库

    使用英特尔:registered:线程构建模块和OpenMP库的并行可扩展Burrows-Wheeler转换(BWT)

    本文是基于为实施Burrows-Wheeler转换(BWT)算法提供并行可伸缩代码的示例使用C ++的英特尔:registered:线程构建模块(TBB)和OpenMP库的实用指南。

    《Intel C++ 编译器Windows版》(Intel.C.Plus.Plus.Compiler.v10.0.026)

    标准版编译器和专业版有着相同的效能和特性, 但是不提供多线程库。 特性: -------------------------------------------------------------------------------- 效能: 考虑使用Intel C++编译器专业版来最佳化效能。...

    RcppParallel:使用Rcpp进行并行编程的高级功能

    RcppParallel支持Intel TBB的新接口,并且可以使用TBB_LIB和TBB_INC环境变量配置为使用TBB的外部副本(例如,带有或系统TBB库)。 使用构建RcppParallel的开发版本: 安装 。 例如,在Linux 64位( x86_64 ) ...

    离散控制Matlab代码-Fourier-Transform:使用IntelTBB进行DFT和FFT

    本课程使用线程构建模块更详细地探讨并行性。 您将在一个比CW1更复杂的示例中看到线性加速,即性能与CPU内核数成正比。 该发行版包含一个用于创建和使用傅立叶变换的基本对象框架,以及两个实现: 直接傅立叶变换,...

    用于计算可定制收缩层次顺序的 InertialFlowCutter 算法的 C++ 实现和评估_C++_代码_下载

    InertialFlowCutter 算法的 C++ 实现...要编译代码,您需要最新的 C++14 编译器(例如 g++ 版本 &gt;= 8.2)和英特尔线程构建模块 (TBB) 库。您还需要 readline 库https://tiswww.case.edu/php/chet/readline/rltop.html。

    DFT的matlab源代码-FFT:关于如何并行化通用Cooley-TukeyFFT算法的一些思考,主要是为了对OpenMP,IntelTBB

    FFT算法的一些想法,主要是对不同库(例如OpenMP,Intel TBB,OpenCL和C ++ 11线程)的性能进行基准测试。 摘自Wikipedia:“以JW Cooley和John Tukey命名的Cooley-Tukey算法是最常见的快速傅立叶变换(FFT)算法。...

    浅谈模糊C均值聚类算法的并行化研究

    基于Intel并行库TBB(线程构建模块)和OpenMP运行时库函数,对多核平台下的串行程序进行循环并行化和任务分配的并行化设计。  并行性主要是指同时性或并发性,并行处理是指对一种相对于串行处理的处理方式,它着重...

    嵌入式系统/ARM技术中的浅谈模糊C均值聚类算法的并行化研究

    基于Intel并行库TBB(线程构建模块)和OpenMP运行时库函数,对多核平台下的串行程序进行循环并行化和任务分配的并行化设计。  并行性主要是指同时性或并发性,并行处理是指对一种相对于串行处理的处理方式,它着重...

    模糊C均值聚类算法的并行化研究

    使用Intel Parallel Amplifier高性能工具,针对模糊C均值聚类算法在多核平台的性能问题,找...基于Intel并行库TBB(线程构建模块)和OpenMP运行时库函数,对多核平台下的串行程序进行循环并行化和任务分配的并行化设计。

    awesome-parallel-computing:精选的并行计算资源列表

    英特尔TBB:线程构建基块 Kokkos:用于在 HPC 平台上编写高性能便携式应用程序的 C++ 编程模型 OpenMP:C/C++ 和 Fortran 中的多平台共享内存并行编程 RaftLib:用于启用流和数据流并行计算的 C++ 库 STAPL:C++ 中...

    icnc:C ++的英特尔(R)并行集合

    TBB(英特尔(R)线程构建模块)版本4.1或更高版本( ) cmake( ) C ++编译器:g ++&gt; = 4.4,icc&gt; = 11.0,Microsoft Visual Studio *&gt; = 10(2010) make(Linux *),nmake或Microsoft Visual Studio *...

Global site tag (gtag.js) - Google Analytics