2

Is it possible to pass a class member function (with parameters) to parallel_for? Something along the lines:

void classT::A(const tbb::blocked_range<std::size_t>& r,b) {}

void classT::B(e,f,g) { 
   tbb::parallel_for( blocked_range<size_t>(0,n), <need to call A with parameter b, 
                                                                       along with r> ) 
}
mskb
  • 341
  • 3
  • 12

1 Answers1

1

Look at this example.

They create a class and suply the class with a all the parameters needed. In that class is also a operator() which does an oppertion on the data. The parallel_for is then called with a instance of that class.

parallel_for(blocked_range<int>(0, nElements, 100), ArraySummer( p_A, p_B, p_SUM_TBB ) ); 
//The class is arraysummer

How you could do it :

class ClassTACaller
{
   int* m_parameter;
   ClassT* m_Tinstance

public:

   ClassTACaller(ClassT* tinstance, int* param):m_parameter(param), m_Tinstance(tinstance){}
   void operator() ( const blocked_range<int>& r ) const 
   {
      m_Tinstance->A(r, param);
   }
};

parallel_for(blocked_range<int>(0, nElements, 100), ClassTACaller(&classTinstance, &x));
Vincent
  • 648
  • 3
  • 9
  • thanks. I did come across such examples in my search. But I was wondering if there is a way to package my tasks in the class's member functions itself (like the way people do with boost::thread, and boost::bind) instead of building separate classes for every task. Is this possible? – mskb Nov 01 '14 at 13:37
  • If your compiler supports c++11, you can use `std::bind`, it schould work. `std::bind` does the same thing as I did, but it uses templates. – Vincent Nov 01 '14 at 13:46
  • Unfortunately, doesn't seem to. I tried; here is what it looks like: _parallel_for(tbb::blocked_range(0,n), std::bind( &classT::A, this, b, std::placeholders::_1 ));_ and there are several errors, the first of which is: – mskb Nov 01 '14 at 13:49
  • /usr/local/include/tbb/parallel_for.h:102:37: error: no matching function for call to object of type 'const std::__1::__bind > &, const tbb::blocked_range &), classT *, tbb::concurrent_vector > &, std::__1::placeholders::__ph<1> &>' void run_body( Range &r ) { my_body( r ); } – mskb Nov 01 '14 at 13:51
  • Just for clarification: the concurrent_vector is the "b" parameter. – mskb Nov 01 '14 at 14:10