本文介绍了依次拆分Parallel.Foreach循环上的负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表中有100万个元素要处理.
将它们粗略地放入 Parallel.ForEach 中只会使CPU饱和.
取而代之的是,我将Elements Master列表分成几部分,然后将Sublists放入并行循环中.

I have a million elements in a List to process.
Dropping them crudely into a Parallel.ForEach would just saturate the CPU.
Instead I split the Elements Master Lists into pieces and drop the Sublists into a parallel loop.

List<Element> MasterList = new List<Element>();
Populate(MasterList); // puts a Million elements into list;

//Split Master List into 100 Lists of 10.0000 elements each
List<List<Element>> ListOfSubLists = Split(MasterList,100);

foreach (List<Element> EL in ListOfSubLists )
{
   Parallel.ForEach(EL, E =>
   {
     // Do Stuff
   }

  //wait for all parallel iterations to end before continuing   
}    

等待所有并行迭代结束然后再继续上循环的下一次迭代的最佳方法是什么?

What is the best way for waiting for all parallel iteration to end before continuing to next iteration of the upper loop ?


如一些答案所述,使CPU饱和"不是一个准确的表述.
实际上,我只想限制CPU使用率,并避免此处理带来的过多负担.

Edit :
as some answers stated, "saturate the CPU" is not an accurate expression.
Actually I just want to limit the CPU usage, and avoid excessive load on it coming from this processing.

推荐答案

Parallel.ForEach不会使CPU饱和.它使用一些智能来决定要同时运行多少个并行线程,最多可以运行63个.

Parallel.ForEach will not saturate the CPU; it uses some intelligence to decide how many parallel threads to run simultaneously, with a max of 63.

请参阅:是否并行.ForEach限制了活动数量线程?

如果需要,还可以通过提供ParallelOptions之类的ParallelOptions作为Parallel.ForEach的第二个参数来设置最大并行度.

You can also set the max degree of parallelism if you want, by supplying a ParallelOptions like new ParallelOptions { MaxDegreeOfParallelism = 5 } as the second argument to Parallel.ForEach.

最后一点,Parallel.ForEach将阻塞,直到所有迭代都完成为止.因此,您编写的代码可以正常工作.您无需等待迭代完成.

As a last point, Parallel.ForEach blocks until all of the iterations have completed. So your code, as written, works. You do not need to wait for the iterations to complete.

这篇关于依次拆分Parallel.Foreach循环上的负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:14