## How to use parallel_reduce for vector

Beginner
1,093 Views

Hi,

I need to analyze N nodes that will generate M new nodes using parallel_reduce :

``````struct MyReduction {
MyClassPtr* _this = nullptr;
const std::vector<Node*>& bestNodes;

std::vector<Node*> value;

MyReduction(MyClassPtr* _t, const std::vector<Node*>& n) : _this(_t), bestNodes(n) {}
MyReduction(MyReduction& s, tbb::split) : _this(s._this), bestNodes(s.bestNodes) {
value = std::vector<Node*>();
}

void operator()( const tbb::blocked_range<size_t>& r ) {

auto tmp = value;
const auto tab = _this->_analyseNodes(bestNodes, r.begin(), r.end(), lThreadId);
tmp.insert(tmp.end(), tab.cbegin(), tab.cend());

value = tmp;
}

void join(MyReduction& rhs) {
value.insert(value.end(), rhs.value.begin(), rhs.value.end());
}
};
``````

And the call :

``````MyReduction result(this, bestNodes);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, bestNodes.size()), result);
``````

The result after the reduction, i.e variable "result.value" is not correct and I think there is a data race inside the operator() because I never have the same size of this vector.

If I add a mutex inside the operator() function, it works...

``````void operator()( const tbb::blocked_range<size_t>& r ) {

static std::mutex mutex;
mutex.lock();
auto tmp = value;
const auto tab = _this->_analyseNodes(bestNodes, r.begin(), r.end(), lThreadId);
tmp.insert(tmp.end(), tab.cbegin(), tab.cend());
value = tmp;

mutex.unlock();
}``````

What is wrong with my reduction ?

Note that if I use the lambda version of parallel_reduce, it works perfectly but I want to use the imperative form to avoid temporaries and copies.

Thanks

1 Solution
Moderator
1,037 Views

Hi,

Thanks for reaching out to us.

In order to remove data races in the code, one way of synchronization methods is musing mutexes as these are used to simplify writing race-free code.

https://oneapi-src.github.io/oneTBB/main/tbb_userguide/Mutual_Exclusion.html

>>What is wrong with my reduction?

Thanks & Regards,

Noorjahan.

3 Replies
Moderator
1,038 Views

Hi,

Thanks for reaching out to us.

In order to remove data races in the code, one way of synchronization methods is musing mutexes as these are used to simplify writing race-free code.

https://oneapi-src.github.io/oneTBB/main/tbb_userguide/Mutual_Exclusion.html

>>What is wrong with my reduction?

Thanks & Regards,

Noorjahan.

Moderator
1,018 Views

Hi,

We haven't heard back from you. Could you please provide an update on your issue?

Thanks & Regards,

Noorjahan.

Moderator
995 Views

Hi,

We have not heard back from you, so we will close this inquiry now. If you need further assistance, please post a new question.

Thanks & Regards,

Noorjahan.