1

I want to traverse my std::map in parallel with parallel_for.
As the tutorial says, we cannot randomly access it, so I chose parallel_for_each.

std::map<std::string, std::string> map_; // student name, student age
tbb::parallel_for_each(map_.begin(), map_.end(), [&](const auto& item) { 
 processOne worker;
 worker(item.first, map_.size());
});

But it seems that parallel_for_each will not chunk the map into subtasks, so it is quite slow.

Is it possible to do something like this?

tbb::parallel_for(tbb::blocked_range<int>(0, map_.size(), 10000), [&](tbb::blocked_range<int> r) {
 ...
});
paleonix
3,3435 gold badges20 silver badges42 bronze badges
asked Jan 17, 2024 at 8:40
3
  • consider to use a std::vector<std::pair<Key,Value>> instead of the map Commented Jan 17, 2024 at 8:59
  • I tried but it is twice as slow as parellel_for_each, weird. Commented Jan 17, 2024 at 10:49
  • You can get better results with tbb::concurrent_hash_map if it suits you ... or with array where you actually keep boost::intrusive::set elements. That std::map just isn't designed to be very efficient with lot of data, even when single threaded. Commented Jan 17, 2024 at 10:54

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.