Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Functions in parallel_for body

kio_marv
Beginner
468 Views

Hi,

I have a liitle question about having function declarations inside a parallel_for body.

Here's the code:

[cpp]#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/partitioner.h"

#include <iostream>
#include <vector>

class Do{
private:
  std::vector< std::vector<unsigned int> >* lv;
private:
  Count(const std::vector<unsigned int>& v) {
    int c(0);
    for(int i=0;i<v.size();++i) ++c;
  }
public:
  Do(std::vector< std::vector<unsigned int> >* lv_):lv(lv_){}
  void operator()(const tbb::blocked_range<unsigned int>& r) const {
    for(unsigned int i=r.begin();i<r.end();++i) {
//      unsigned int place(i);
      for(unsigned int k(0); k<10; ++k){
        (*lv).push_back(k+i);
      }
      int cnt(0);
      Count((*lv));
    }
  }

};

int main(int argc, char* argv[]) {
  std::vector< std::vector<unsigned int> > gv;
  gv.reserve(2);
 
  tbb::parallel_for(tbb::blocked_range<unsigned int>(0,2),Do(&gv),tbb::auto_partitioner());
  for(unsigned int i(0);i<2;++i) {
    std::cout << "Line " << i << ": ";
    for(unsigned int j(0);j<gv.size();++j){
      std::cout << gv << " ";
    }
    std::cout << "\n";
  }
  return 0;
}[/cpp]

When compiling this code, I get the error message In member function ‘void Do::operator()(const tbb::blocked_range<unsigned int>&) const’:
error: passing ‘const Do’ as ‘this’ argument of ‘int Do::Count(const std::vector<unsigned int, std::allocator<unsigned int> >&)’ discards qualifiers.

My question is why that happens. Why can't I call Count from the parallel_for loop?.

Thanks in advance.

0 Kudos
1 Solution
RafSchietekat
Valued Contributor III
468 Views
Why can't I call Count from the parallel_for loop?
Making Count() const (like the operator()) will get rid of the error message, but it will still be your responsibility to do something useful of course. For the purpose of counting, you would probably be better off with parallel_reduce(): parallel_for() has no well-defined way to return state to the caller, and thread-safe manipulation of a shared object passed by reference through the Body instance is typically slower than recursive reduction. Locking down the Body instance in parallel_for() makes that clear at build time, to help prevent simple mistakes, e.g., not realising that parallel_for() may unpredictably make copies of the Body or reuse the same instance.

View solution in original post

0 Kudos
2 Replies
RafSchietekat
Valued Contributor III
469 Views
Why can't I call Count from the parallel_for loop?
Making Count() const (like the operator()) will get rid of the error message, but it will still be your responsibility to do something useful of course. For the purpose of counting, you would probably be better off with parallel_reduce(): parallel_for() has no well-defined way to return state to the caller, and thread-safe manipulation of a shared object passed by reference through the Body instance is typically slower than recursive reduction. Locking down the Body instance in parallel_for() makes that clear at build time, to help prevent simple mistakes, e.g., not realising that parallel_for() may unpredictably make copies of the Body or reuse the same instance.
0 Kudos
kio_marv
Beginner
468 Views
Ok, thanks. The Count function is just an example, I don't plan to use a counter in there :)
0 Kudos
Reply