Reputation: 1137
How to limit number of threads used in
Concurrency::parallel_for<int>(0, 100, 1, [&](int k)
I saw the scheduler/task idea, I fail to use it cause inside the parallel for there is a lot of logic and I need to pass arguments, all the examples for tasks is containing only std::cout<<"Hey"<<std::endl; inside the task.
Hope you have some ideas.
bool func1(int x,int y....) //A lot of params{
Concurrency::parallel_for<int>(0, 100, 1, [&](int k) {
//a lot of logic depends on the input
}
}
Upvotes: 2
Views: 1568
Reputation: 12507
I haven't used the interface - but the following might work (assuming 8 workers and parallel for 100 cases - otherwise adjust the 100/8).
Concurrency::simple_partitioner splitter(100/8);
Concurrency::parallel_for<int>(0, 100, 1, [&](int k) {
//a lot of logic depends on the input
}, splitter);
This does not limit the number of threads directly, but chunk the data achieving the same.
The idea could also be found on: https://katyscode.wordpress.com/2013/08/17/c11-multi-core-programming-ppl-parallel-aggregation-explained/ https://learn.microsoft.com/en-us/cpp/parallel/concrt/reference/simple-partitioner-class?view=msvc-160 https://learn.microsoft.com/en-us/cpp/parallel/concrt/reference/concurrency-namespace-functions?view=msvc-160#parallel_for
Upvotes: 1
Reputation: 108
You are referring to https://learn.microsoft.com/en-us/cpp/parallel/concrt/reference/concurrency-namespace-functions?view=msvc-160#parallel_for.
I don't think there is a super simple "one function call" solution to this. Based on the docs you need to change the policy of the current scheduler so that any parallel_for you run is limited by the scheduler to use only a specific number of resources. So you'd want to get the current SchedulerPolicy, update SetConcurrencyLimits() on it, and then update the current policy by calling concurrency::CurrentScheduler::Create() with your modified policy. This should let you limit the number of total threads when the parallel_for executes. You'd definitely need to experiment and test including disabling your modified scheduling policy when you were done with your calls.
It may be easier to restructure your code (i.e. chunk the parallel_for calls so that each parallel_for only executes the the number of concurrent threads you want to execute). That will be less efficient but may be easier to write and maintain.
References:
https://learn.microsoft.com/en-us/cpp/parallel/concrt/reference/schedulerpolicy-class?view=msvc-160
https://learn.microsoft.com/en-us/cpp/parallel/concrt/scheduler-instances?view=msvc-160
Upvotes: 1