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

parallel_for using boost bind

dophine
Beginner
250 Views
Hi,
I try to use boost::bind instead of lambda function as the compiler is not 4.7.
got the following error. any way to solve it?
test.cpp: In member function void list::parallel():
test.cpp:27: error: no matching function for call to parallel_for(__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >, size_t, boost::_bi::bind_t, boost::_bi::list2<:_BI::VALUE>, boost::arg<1> > >)
using namespace std;
struct Obj
{
int32_t value;
};
class list
{
std::vector objList;
public:
void process(Obj& obj)
{
//do something
};
void parallel()
{
tbb::parallel_for(objList.begin(), objList.end(), size_t(1), boost::bind(&list::process, this, _1));
};
};
int main(int argc,char *argv[])
{
return 1;
}
0 Kudos
1 Solution
Anton_M_Intel
Employee
250 Views
The first mistake is not related to bind:iterators are passed directly to the parallel_for. It accepts either numeric types or a Range-modelling type (e.g. blocked_range). The second, an iterator is not implicitly convertable to its reference_type which is expected in list::process.
Please try the following:
void process(std::vector::iterator it)
...
tbb::parallel_for(tbb:blocked_range<:VECTOR>::iterator>(objList.begin(), objList.end()),boost::bind(&list::process, this, _1)

View solution in original post

5 Replies
RafSchietekat
Black Belt
250 Views
list::process() expects a reference to Obj, but the result of boost::bind() will get an iterator to Obj from tbb::parallel_for, so you'll have to dereference it first?
dophine
Beginner
250 Views
ok I try to modify it to accept iterator. However, no luck.
test.cpp: In member function void list::parallel():
test.cpp:52: error: no matching function for call to parallel_for(__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >, size_t, boost::_bi::bind_t > > >, boost::_bi::list2<:_BI::VALUE>, boost::arg<1> > >)
void process(std::vector::iterator obj)
{
//do something
};
Anton_M_Intel
Employee
251 Views
The first mistake is not related to bind:iterators are passed directly to the parallel_for. It accepts either numeric types or a Range-modelling type (e.g. blocked_range). The second, an iterator is not implicitly convertable to its reference_type which is expected in list::process.
Please try the following:
void process(std::vector::iterator it)
...
tbb::parallel_for(tbb:blocked_range<:VECTOR>::iterator>(objList.begin(), objList.end()),boost::bind(&list::process, this, _1)
RafSchietekat
Black Belt
250 Views
"The first mistake is not related to bind:iterators are passed directly to the parallel_for. It accepts either numeric types or a Range-modelling type (e.g. blocked_range)."
Ah, I hadn't looked at the implementation yet: there's some code that doesn't work with iterators, but maybe compiler diagnostics in the presence of SFINAE just doesn't drill through to that underlying reason even if there's no remaining specialisation?

So it might work with a Range and the revised process().

(Edited)
dophine
Beginner
250 Views
Thank you.
Just a small modification to make it work.
void process(const tbb::blocked_range<:VECTOR>::iterator>& r) const;
Reply