tbb基础之parallel_for用法详解

要讲解parallel_for,我们首先讲一个例子,该例子是对数组的每一个元素进行遍历,常规的串行算法代码如下:

  1. template< typename T> void Visit( T var)
  2. {
  3. printf( "%0.2f, ", var);
  4. }
  5. void Sequence_Visit( const float* fArray, int nSize)
  6. {
  7. for ( int i= 0; i<nSize; i++)
  8. {
  9. Visit< float>(fArray[i]);
  10. if ( i% 5== 0 && i> 0)
  11. {
  12. printf( "\n");
  13. }
  14. }
  15. }
          上面这段代码很熟悉,常规编程中我们都是这样写的,这里不再做分析。而采用TBB中的parallel_for重写后的代码如下:

  1. class ApplyFoo
  2. {
  3. float* const my_a;
  4. public:
  5. void operator()(const blocked_range<size_t>& range) const
  6. {
  7. float* a = my_a;
  8. for ( size_t i = range.begin(); i!=range.end(); ++i)
  9. {
  10. Foo(a[i]);
  11. if ( i% 5== 0 && i> 0)
  12. {
  13. printf( "\n");
  14. }
  15. }
  16. }
  17. ApplyFoo( float a[]):my_a(a)
  18. {
  19. }
  20. void Foo( float var) const
  21. {
  22. printf( "%0.2f, ", var);
  23. }
  24. };
  25. void ParallelApplyFoo( float a[], size_t n )
  26. {
  27. parallel_for( blocked_range< size_t>( 0, n, 100), ApplyFoo(a), auto_partitioner());
  28. }
          这段代码中,我们首先定义了一个ApplyFoo类,并重载了operator(),而在ParallelApplyFoo函数中调用parallel_for来并行对数组元素进行操作,这里 出现了两个新词:blocked_range和parallel_for。下面逐个分析:

           1、blocked_range

            blocked_range是一个模板类,表述了一维迭代(iterator),我们可以通过其头文件blocked_range.h查看其定义:

  1. template< typename Value>
  2. class blocked_range {
  3. public:
  4. //! Type of a value
  5. typedef Value const_iterator;
  6. typedef std:: size_t size_type;
  7. //! Construct range with default-constructed values for begin and end.
  8. blocked_range() : my_end(), my_begin() {}
  9. //! Construct range over half-open interval [begin,end), with the given grainsize.
  10. blocked_range( Value begin_, Value end_, size_type grainsize_= 1 ) :
  11. my_end(end_), my_begin(begin_), my_grainsize(grainsize_)
  12. {
  13. __TBB_ASSERT( my_grainsize> 0, "grainsize must be positive" );
  14. }
  15. //! Beginning of range.
  16. const_iterator begin() const { return my_begin;}
  17. //! One past last value in range.
  18. const_iterator end() const { return my_end;}
  19. //! Size of the range
  20. size_type size() const {
  21. __TBB_ASSERT( !(end()<begin()), "size() unspecified if end()<begin()" );
  22. return size_type(my_end-my_begin);
  23. }
  24. //! The grain size for this range.
  25. size_type grainsize() const { return my_grainsize;}
  26. //------------------------------------------------------------------------
  27. // Methods that implement Range concept
  28. //------------------------------------------------------------------------
  29. //! True if range is empty.
  30. bool empty() const { return !(my_begin<my_end);}
  31. //! True if range is divisible.
  32. /** Unspecified if end()<begin(). */
  33. bool is_divisible() const { return my_grainsize<size();}
  34. //! Split range.
  35. /** The new Range *this has the second half, the old range r has the first half.
  36. Unspecified if end()<begin() or !is_divisible(). */
  37. blocked_range( blocked_range& r, split ) :
  38. my_end(r.my_end),
  39. my_begin(do_split(r)),
  40. my_grainsize(r.my_grainsize)
  41. {}
  42. private:
  43. /** NOTE: my_end MUST be declared before my_begin, otherwise the forking constructor will break. */
  44. Value my_end;
  45. Value my_begin;
  46. size_type my_grainsize;
  47. //! Auxiliary function used by forking constructor.
  48. static Value do_split( blocked_range& r ) {
  49. __TBB_ASSERT( r.is_divisible(), "cannot split blocked_range that is not divisible" );
  50. Value middle = r.my_begin + (r.my_end-r.my_begin)/ 2u;
  51. r.my_end = middle;
  52. return middle;
  53. }
  54. template< typename RowValue, typename ColValue>
  55. friend class blocked_range2d;
  56. template< typename RowValue, typename ColValue, typename PageValue>
  57. friend class blocked_range3d;
  58. };
      从上面blocked_range的定义中,可以看到该类有3个构造函数,而我们上面的实例代码采用的构造函数为:

  1. blocked_range( Value begin_, Value end_, size_type grainsize_= 1 ) :
  2. my_end(end_), my_begin(begin_), my_grainsize(grainsize_)
  3. {
  4. __TBB_ASSERT( my_grainsize> 0, "grainsize must be positive" );
  5. }

      第一个参数表示起始,第二个参数表示结束,它们的类型为const_iterator,表示的区间为[begin,end)这样一个半开区间。第三个参数,grainsize,表示的是一个“合适的大小”块,这个块会在一个循环中进行处理,如果数组比这个grainsize还大,parallel_for会把它分割为独立的block,然后分别进行调度(有可能由多个线程进行处理)。

     这样我们知道,grainsize其实决定了TBB什么时候对数据进行划分,如果我们把grainsize指定得太小,那就可能会导致产生过多得block,从而使得不同block间的overhead增加(比如多个线程间切换的代价),有可能会使性能下降。相反,如果grainsize设得太大,以致于这个数组几乎没有被划分,那又会导致不能发挥parallel_for期望达到的并行效果,也没有达到理想得性能。所以我们在决定grainsize时需要小心,最好是能够经过调整测试后得到的值,当然你也可以如本例中一样不指定,让TBB帮你来决定合适的值(一般不是最优的)。一个调整grainsize的经验性步骤:

1)首先把grainsize设得比预想的要大一些,通常设为10000 
2)在单处理机机器上运行,得到性能数据 
3)把grainsize减半,看性能降低多少,如果降低在5%-10%之间,那这个grainsize就已经是一个不错的设定。

    另外,在该类定义的最后可以看到还定义了:

  1. template< typename RowValue, typename ColValue>
  2. friend class blocked_range2d;
  3. template< typename RowValue, typename ColValue, typename PageValue>
  4. friend class blocked_range3d;
      特别是blocked_range2d对于处理矩阵和图像数据非常有用。

      2、Parallel_for

      parallel_for是本文的核心,因此首先我们看下其定义(在parallel_for.h文件中,这里只摘录了部分代码):

  1. template< typename Range, typename Body>
  2. void parallel_for( const Range& range, const Body& body ) {
  3. internal::start_for<Range,Body,__TBB_DEFAULT_PARTITIONER>::run(range,body,__TBB_DEFAULT_PARTITIONER());
  4. }
  5. //! Parallel iteration over range with simple partitioner.
  6. template< typename Range, typename Body>
  7. void parallel_for( const Range& range, const Body& body, const simple_partitioner& partitioner ) {
  8. internal::start_for<Range,Body,simple_partitioner>::run(range,body,partitioner);
  9. }
  10. //! Parallel iteration over range with auto_partitioner.
  11. template< typename Range, typename Body>
  12. void parallel_for( const Range& range, const Body& body, const auto_partitioner& partitioner ) {
  13. internal::start_for<Range,Body,auto_partitioner>::run(range,body,partitioner);
  14. }
  15. //! Parallel iteration over range with affinity_partitioner.
  16. template< typename Range, typename Body>
  17. void parallel_for( const Range& range, const Body& body, affinity_partitioner& partitioner ) {
  18. internal::start_for<Range,Body,affinity_partitioner>::run(range,body,partitioner);
  19. }
     本文用的是第二种调用方法,其参数:

1)range:指定划分block的范围。
2)body:指定对block应用的操作,Body可以看成是一个操作子functor,它的operator(...)会以blocked_range为参数进行调用,当然如果我们传过来的是一个函数指针也是可以的,只要它能以blocked_range为参数进行调用。 
3)partitioner:指定划分器,可选的两种simple_partitioner和auto_partitioner。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值