Parallel.ForEach stops being parallel for the last few items

I have an external singlethreaded program that needs to be run multiple hundred times with different parameters. To make it faster I want to run it once for each core at the same time. To do that I used Parallel.ForEach running on a list with the different parameters to pass to the external program:

var parallelOptions = new ParallelOptions {
    MaxDegreeOfParallelism = Environment.ProcessorCount // 8 for me
};

Parallel.ForEach(ListWithAllTheParams, parallelOptions, DoTheStuff);

...

private void DoTheStuff(ParamType parameter, ParallelLoopState parallelLoopState, long index)
{
    // prepare process parameters etc.
    theProcess.Start();
    theProcess.WaitForExit();
}

Pretty straightforward and works nicely... until the last ~10 items - they don't get parallelized for some reason and just run one after another. I've confirmed this by looking at the cpu usage and the running programs in the Task Manager.

This does not happen when I populate the parameter list with only a few (say, 10) items.

Can somebody explain this behavior to me? Any hints or tips appreciated!


With Scott's tip in the comments above changing it to the following code made it behave as I wanted:

OrderablePartitioner<ParamType> partitioner =
    Partitioner.Create(ListWithAllTheParams, EnumerablePartitionerOptions.NoBuffering);

var parallelOptions = new ParallelOptions {
    MaxDegreeOfParallelism = Environment.ProcessorCount // 8 for me
};

Parallel.ForEach(partitioner, parallelOptions, DoTheStuff);

Thanks a lot!

链接地址: http://www.djcxy.com/p/50156.html

上一篇: C#并行编程修改xDocument

下一篇: Parallel.ForEach停止对最后几项进行平行处理