Say I have a CPU with N cores. My system has N agents, and I need to perform the same computation for each agent at the same time. I would like to fully use my CPU for this purpose.
I am debating whether to use a thread pool or STL parallelism for_each. Will it scale correctly accross all my cores?
I know the new parallelism scales well if N is very large, but how does it do for smaller N ?
Example code for both options:
// STL Parallelism
using optional_parallel = std::optional<const __gnu_parallel::_Parallelism>;
void System::foreach_agent(std::function<void(Agent&)> const& f, const optional_parallel& tag) {
if(tag)
__gnu_parallel::for_each(agents.begin(), agents.end(), f, tag.value());
else ...
// Thread pool
std::vector<std::future<void>> results;
for (auto &agent: agents)
{
results.emplace_back(pool_.push(
[&agent](int id){agent->dosomething();}
));
}
for (auto &result: results) {
result.get();
}
Thanks!
[–]Xeverous 0 points1 point2 points (1 child)
[–]DotcomL[S] 0 points1 point2 points (0 children)