0

In 2019, is there a platform-agnostic (Intel, AMD, Windows, *nix, etc.) way to simply parallelize a for loop, with minimal additional code to use all cores available. Here, it is assume the code within the for loop is written in a thread-safe manner. Let us also assume the latest and greatest language versions are supported.

Let's say this simple code is what I have:

std::vector<SomeObject> objects;
for (const auto& object: objects) {
// do some thread-safe work with object
// how to parallelize this?
}
dev_nut
  • 2,476
  • 4
  • 29
  • 49
  • 2
    While the duplicate is correct, most of the answers are terribly outdated. See [`std::for_each`](https://en.cppreference.com/w/cpp/algorithm/for_each) which accepts an execution policy allowing parallelization. – François Andrieux Mar 05 '19 at 15:48
  • @FrançoisAndrieux If there's a newer, better answer to that question, there's no reason not to add it there. – Caleb Mar 05 '19 at 15:50
  • Hmmm, why didn't the question police bot not suggest that question to me when asking this question? That bot needs to be improved. – dev_nut Mar 05 '19 at 16:05
  • 1
    What about [std::for_each](https://en.cppreference.com/w/cpp/algorithm/for_each) with execution policy of [std::execution::parallel_policy](https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) ? – Jesper Juhl Mar 05 '19 at 16:12
  • @Caleb that was asked when C++17 didn't exist. This question seems like a duplicate, but is an unsolved problem. Specially with multi core PCs becoming prevalent. – dev_nut Mar 05 '19 at 16:38
  • @dev_nut There's [at least one C++17 answer](https://stackoverflow.com/a/45773308/643383) there, and it seems in line with François Andrieux's comment. Won't that work for your problem? – Caleb Mar 05 '19 at 21:41
  • @FrançoisAndrieux No worries, I missed whatever you wrote. Cheers. – Caleb Mar 05 '19 at 21:44

1 Answers1

0

I think OpenMP is what you are looking for.

And if you need to compute in multiple nodes, try MPI.

someone
  • 122
  • 2
  • 9