Parallelizing an Index-Based for-Loop
Learn how to build a parallel_for() algorithm by combining std::for_each() with std::views::iota(), and how to execute algorithms on GPUs.
Even though we recommend using algorithms, sometimes a raw, index-based for
-loop is required for a specific task. The standard library algorithms provide an equivalent of a range-based for
-loop by including the algorithm std::for_each()
in the library.
However, there is no algorithm equivalent of an index-based for
-loop. In other words, we cannot easily parallelize code like this by simply adding a parallel policy to it:
auto v = std::vector<std::string>{"A", "B", "C"};for (auto i = 0u; i < v.size(); ++i) {v[i] += std::to_string(i+1);}// v is now { "A1", "B2", "C3" }
But let’s see how we can build one by combining algorithms. As we will have already concluded, implementing parallel algorithms is complicated. But in this case, we will build a parallel_for()
algorithm using std::for_each()
as a building block, thus leaving the complex parallelism to std::for_each()
.
Combining std::for_each()
with std::views::iota()
An index-based for
-loop based on a standard ...