What happens in case of nested loop with c++ execution policy?

Viewed 86

For example if I have a top loop with std::execution::parallel_policy and one or more nested loop with the same or other execution policy how is it handled? Does it cause any issue?

In my case I have a top loop that start some heavy problem resolution over each element of a collection. The problem resolution is implemented with some heavy computation in the form of a loop. Should I use execution policy only on the top loop, nested loops or both?

E.g.:

#include <iostream>
#include <algorithm>
#include <execution>
#include <vector>

void NestedHeavyComputation(int candidate) {
    //Heavy computation
}

void ToplevelAlgorithm(std::vector<int> candidates) {
    std::for_each(std::execution::par, candidates.begin(), candidates.end(), [](int candidate) {
            NestedHeavyComputation(candidate);
    });
}

int main() {
    std::vector<std::vector<int>> data;
    
    std::for_each(std::execution::par, data.begin(), data.end(), [](std::vector<int> candidates){
        ToplevelAlgorithm(candidates);
    });

    return 0;
}
0 Answers
Related