Intel Thread Building Blocks (TBB) 入门篇

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/kezunhai/article/details/44678845


一、什么是TBB

       TBB(Thread Building Blocks)是英特尔发布的一个库,全称为 Threading Building Blocks。TBB 获得过 17 届 Jolt Productivity Awards,是一套 C++ 模板库,和直接利用 OS API 写程序的 raw thread 比,在并行编程方面提供了适当的抽象,当然还包括更多其他内容,比如 task 概念,常用算法的成熟实现,自动负载均衡特 性还有不绑定 CPU 数量的灵活的可扩展性等等。STL 之父,Alexander Stepanov 对此评价不错,他说“Threading Building Blocks… could become a basis for the concurrency dimension of the C++ standard library”。其他 TBB 的早期用户,包括 Autodesk,Sun,Red Hat, Turbo Linux 等亦然。现在 O’Reilly 已经出版了一本 Intel Threading Building Blocks: Outfitting C++ for Multi-core Processor Parallelism。

二、为什么要TBB

        在多核的平台上开发并行化的程序,必须合理地利用系统的资源 - 如与内核数目相匹配的线程,内存的合理访问次序,最大化重用缓存。有时候用户使用(系统)低级的应用接口创建、管理线程,很难保证是否程序处于最佳状态。 

       而 Intel Thread Building Blocks (TBB) 很好地解决了上述问题: 
  1)TBB提供C++模版库,用户不必关注线程,而专注任务本身。 
  2)抽象层仅需很少的接口代码,性能上毫不逊色。 
  3)灵活地适合不同的多核平台。 
  4)线程库的接口适合于跨平台的移植(Linux, Windows, Mac) 
  5)支持的C++编译器 – Microsoft, GNU and Intel  

三、TBB库包含的内容

        TBB包含了 Algorithms、Containers、Memory Allocation、Synchronization、Timing、Task Scheduling这六个模块。TBB的结构:


1、通用的并行算法 
      1) 循环的并行: 
       ① parallel_for
       parallel_for是在一个值域执行并行迭代操作的模板函数(如对数组求和),
           
parallel_for(range, body, partitioner)提供了并行迭代的泛型形式。它表示在区域的每个值,并行执行body。partitioner选项指定了分割策略。Range类型必须符合Range概念模型。body必须符合下表的要求:
                
例子:

  
  
  1. #include <iostream>
  2. #include <vector>
  3. #include <tbb/tbb.h>
  4. #include <tbb/blocked_range.h>
  5. #include <tbb/parallel_for.h>
  6. using namespace std;
  7. using namespace tbb;
  8. typedef vector< int>::iterator IntVecIt;
  9. struct body
  10. {
  11. void operator()(const blocked_range<IntVecIt>&r)const
  12. {
  13. for( auto i = r.begin(); i!=r.end(); i++)
  14. cout<<*i<< ' ';
  15. }
  16. };
  17. int main()
  18. {
  19. vector< int> vec;
  20. for( int i= 0; i< 10; i++)
  21. vec.push_back(i);
  22. parallel_for(blocked_range< IntVecIt>(vec.begin(), vec.end())
  23. , body());
  24. return 0;
  25. }
       ②parallel_reduce
       parallel_reduce模板在一个区域迭代,将由各个任务计算得到的部分结果合并,得到最终结果。parallel_reduce对区域(range)类型的要求与parallel_for一样。body类型需要分割构造函数以及一个join方法。body的分割构造函数拷贝运行循环体需要的只读数据,并分配并归操作中初始化并归变量的标志元素。join方法会组合并归操作中各任务的结果。
parallel_reduce使用分割构造函数来为每个线程生成一个或多个body的拷贝。当它拷贝body的时候,也许body的operator()或者join()正在并发运行。要确保这种并发运行下的安全。典型应用中,这种安全要求不会消耗你太多的精力。 

  
  
  1. #include <iostream>
  2. #include <tbb/parallel_reduce.h>
  3. #include <tbb/blocked_range.h>
  4. #include <vector>
  5. using namespace std;
  6. using namespace tbb;
  7. int main()
  8. {
  9. vector< int> vec;
  10. for( int i= 0; i< 100; i++)
  11. vec.push_back(i);
  12. int result = parallel_reduce(blocked_range< vector< int>::iterator>(vec.begin(), vec.end()),
  13. 0,[]( const blocked_range< vector< int>::iterator>& r, int init)-> int{
  14. for( auto a = r.begin(); a!=r.end(); a++)
  15. init+=*a;
  16. return init;
  17. },
  18. []( int x, int y)-> int{
  19. return x+y;
  20. }
  21. );
  22. cout<< "result:"<<result<< endl;
  23. return 0;
  24. }
       ③parallel_scan 

        并行计算前束(prefix)的函数模板。即输入一个数组,生成一个数组,其中每个元素的值都是原数组中在此元素之前的元素的某个运算符的结果的累积。比如求和:
      输入:[2, 8,  9, -4,   1, 3, -2,  7]
     生成:[0, 2, 10, 19,  15, 16, 19, 17]


  例子:


  
  
  1. #include <tbb/parallel_scan.h>
  2. #include <tbb/blocked_range.h>
  3. #include <iostream>
  4. using namespace tbb;
  5. using namespace std;
  6. template< typename T>
  7. class Body
  8. {
  9. T _sum;
  10. T* const _y;
  11. const T* const _x;
  12. public:
  13. Body(T y[], const T x[]):_sum( 0), _x(x), _y(y){}
  14. T get_sum() const
  15. {
  16. return _sum;
  17. }
  18. template< typename Tag>
  19. void operator()(const blocked_range<int>& r, Tag)
  20. {
  21. T temp = _sum;
  22. for( int i = r.begin(); i< r.end(); i++)
  23. {
  24. temp+=_x[i];
  25. if(Tag::is_final_scan())
  26. _y[i] = temp;
  27. }
  28. _sum = temp;
  29. }
  30. Body(Body&b, split):_x(b._x), _y(b._y), _sum( 0){}
  31. void reverse_join(Body& a)
  32. {
  33. _sum+=a._sum;
  34. }
  35. void assign(Body& b)
  36. {
  37. _sum = b._sum;
  38. }
  39. };
  40. int main()
  41. {
  42. int x[ 10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  43. int y[ 10];
  44. Body< int> body(y,x);
  45. parallel_scan(blocked_range< int>( 0, 10), body);
  46. cout<< "sum:"<<body.get_sum()<< endl;
  47. return 0;
  48. }
       ④parallel_do 
    并行处理工作项的模板函数。


       如果所有来自输入流的元素不能随机访问,那么parallel_do中的并行就不具备可扩展性。为达到可扩展性,可按如下方式之一处理:

      为了提高速度,B::operator()的粒度至少要约10万个时钟周期。否则,parallel_do的内在开销就会影响有效工作。算法可以传递一个task_group_context对象,这样它的任务可以在此组内执行。默认情况下,算法在它自己的有界组中执行。
      例子:

  
  
  1. #include <tbb/parallel_do.h>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. using namespace tbb;
  6. struct t_test
  7. {
  8. string msg;
  9. int ref;
  10. void operator()()const
  11. {
  12. cout<<msg<< endl;
  13. }
  14. };
  15. template < typename T>
  16. struct body_test
  17. {
  18. void operator()(T* t, parallel_do_feeder<T*>& feeder) const
  19. {
  20. (*t)();
  21. if(t->ref == 0)
  22. {
  23. t->msg = "added msg";
  24. feeder.add(t);
  25. t->ref++;
  26. }
  27. }
  28. };
  29. int main()
  30. {
  31. t_test *pt = new t_test;
  32. pt->ref = 0;
  33. pt->msg = "original msg";
  34. vector<t_test*> vec;
  35. vec.push_back(pt);
  36. parallel_do(vec.begin(), vec.end(), body_test<t_test>());
  37. delete pt;
  38. return 0;
  39. }
       2)流的并行算法 
      ① pipeline
       其定义:

  
  
  1. class pipeline
  2. {
  3. public:
  4. pipeline();
  5. ~pipeline();
  6. void add_filter( filter& f );
  7. void run( size_t max_number_of_live_tokens
  8. [,task_group_context& group] );
  9. void clear();
  10. };
       可按以下步骤使用pipeline类:
        1、从filter继承类f,f的构造函数传递给基类filter的构造函数一个参数,来指定它的模式
        2、重载虚方法filter::operator()来实现过滤器对元素处理,并返回一个将被下一个过滤器处理的元素指针。如果流里没有其他的要处理的元素,返回空值。最后一个过滤器的返回值将被忽略。
       3、生成pipeline类的实例
       4、生成过滤器f的实例,并将它们按先后顺序加给pipeline。一个过滤器的实例一次只能加给一个pipeline。同一时间,一个过滤器禁止成为多个pipeline的成员。
       5、调用pipeline::run方法。参数max_number_of_live_tokens指定了能并发运行的阶段数量上限。较高的值会以更多的内存消耗为代价来增加并发性。 
        ② 过滤器基类 filter 

  
  
  1. class filter
  2. {
  3. public:
  4. enum mode
  5. {
  6. parallel = implementation-defined,
  7. serial_in_order = implementation-defined,
  8. serial_out_of_order =implementation-defined
  9. };
  10. bool is_serial() const;
  11. bool is_ordered() const;
  12. virtual void* operator()( void* item ) = 0;
  13. virtual void finalize( void* item ) {}
  14. virtual ~filter();
  15. protected:
  16. filter( mode );
  17. };
     
        由于parallel过滤器支持并行加速,所以推荐使用。如果必须使用serial过滤器,那么serial_out_of_order类型的过滤器是优先考虑的,因为他在处理顺序上的约束较少。
       ③ 线程绑定过滤器thread_bound_filter

  
  
  1. classthread_bound_filter: public filter
  2. {
  3. protected:
  4. thread_bound_filter(mode filter_mode);
  5. public:
  6. enum result_type
  7. {
  8. success,
  9. item_not_available,
  10. end_of_stream
  11. };
  12. result_type try_process_item();
  13. result_type process_item();
  14. };
       管道中过滤器的抽象基类,线程必须显式为其提供服务。当一个过滤器必须由某个指定线程执行的时候会派上用场。服务于thread_bound_filter的线程不能是调用pipeline::run()的线程。例如:

  
  
  1. #include<iostream>
  2. #include <tbb/pipeline.h>
  3. #include<tbb/compat/thread>
  4. #include<tbb/task_scheduler_init.h>
  5. using namespacestd;
  6. using namespacetbb;
  7. char input[] = "abcdefg\n";
  8. classinputfilter: public filter
  9. {
  10. char *_ptr;
  11. public:
  12. void *operator()(void *)
  13. {
  14. if(*_ptr)
  15. {
  16. cout<< "input:"<<*_ptr<< endl;
  17. return _ptr++;
  18. }
  19. else return 0;
  20. }
  21. inputfilter():filter(serial_in_order),_ptr(input){}
  22. };
  23. classoutputfilter: public thread_bound_filter
  24. {
  25. public:
  26. void *operator()(void *item)
  27. {
  28. cout<<*( char*)item;
  29. return 0;
  30. }
  31. outputfilter():thread_bound_filter(serial_in_order){}
  32. };
  33. voidrun_pipeline(pipeline *p)
  34. {
  35. p->run( 8);
  36. }
  37. int main()
  38. {
  39. inputfilter inf;
  40. outputfilter ouf;
  41. pipeline p;
  42. p.add_filter(inf);
  43. p.add_filter(ouf);
  44. //由于主线程服务于继承自thread_bound_filter的outputfilter,所以pipeline要运行在另一个单独的线程
  45. thread t(run_pipeline, &p);
  46. while(ouf.process_item()!=thread_bound_filter::end_of_stream)
  47. continue;
  48. t.join();
  49. return 0;
  50. }
           ⑤ parallel_pipeline
         函数parallel_pipeline提供了一种强类型的面向lambda的方式来建立并运行管道。 parallel_while – 用于非结构化的流或堆 
      pipeline - 对流水线的每一阶段并行,有效使用缓存 
        3)并行排序 
        parallel_sort – 并行快速排序,调用了parallel_for 

2)任务调度者 
管理线程池,及隐藏本地线程复杂度 
并行算法的实现由任务调度者的接口完成 
任务调度者的设计考虑到本地线程的并行所引起的性能问题 

3)并行容器 
concurrent_hash_map 
concurrent_vector 
concurrent_queue 

4)同步原语 
atomic 
mutex 
spin_mutex – 适合于较小的敏感区域 
queuing_mutex – 线程按次序等待(获得)一个锁 
spin_rw_mutex 
queuing_rw_mutex 
说明:使用read-writer mutex允许对多线程开放”读”操作 


5)高性能的内存申请 
使用TBB的allocator 代替 C语言的 malloc/realloc/free 调用 
使用TBB的allocator 代替 C++语言的 new/delete 操作 

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值