简体   繁体   English

限制 Concurrency::parallel_for 中使用的线程数

[英]Limit number of threads used in Concurrency::parallel_for

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;我看到了调度程序/任务的想法,我没有在并行内部使用它,因为有很多逻辑,我需要传递参数,任务的所有示例都只包含 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
}
}

You are referring to https://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/concurrency-namespace-functions?view=msvc-160#parallel_for .您指的是https://docs.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.根据文档,您需要更改当前调度程序的策略,以便您运行的任何parallel_for都受到调度程序的限制,只能使用特定数量的资源。 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.因此,您希望获取当前的 SchedulerPolicy,更新 SetConcurrencyLimits(),然后通过使用修改后的策略调用 concurrency::CurrentScheduler::Create() 来更新当前策略。 This should let you limit the number of total threads when the parallel_for executes.这应该可以让您在 parallel_for 执行时限制总线程数。 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 (ie chunk the parallel_for calls so that each parallel_for only executes the the number of concurrent threads you want to execute).重构您的代码可能更容易(即,将parallel_for 调用分块,以便每个parallel_for 只执行您想要执行的并发线程数)。 That will be less efficient but may be easier to write and maintain.这会降低效率,但可能更容易编写和维护。

References:参考:

https://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/schedulerpolicy-class?view=msvc-160 https://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/schedulerpolicy-class?view=msvc-160

https://docs.microsoft.com/en-us/cpp/parallel/concrt/scheduler-instances?view=msvc-160 https://docs.microsoft.com/en-us/cpp/parallel/concrt/scheduler-instances?view=msvc-160

I haven't used the interface - but the following might work (assuming 8 workers and parallel for 100 cases - otherwise adjust the 100/8).我没有使用过界面 - 但以下可能有效(假设 8 个工人并并行 100 个案例 - 否则调整 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://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/simple-partitioner-class?view=msvc-160 https://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/concurrency-namespace-functions?view=msvc-160#parallel_for该想法也可以在以下位置找到: https : //katyscode.wordpress.com/2013/08/17/c11-multi-core-programming-ppl-parallel-aggregation-explained/ https://docs.microsoft.com/ en-us/cpp/parallel/concrt/reference/simple-partitioner-class?view=msvc-160 https://docs.microsoft.com/en-us/cpp/parallel/concrt/reference/concurrency-namespace-functions ?view=msvc-160#parallel_for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM