Parallel.For not utilising all cores

丶灬走出姿态 提交于 2019-12-01 14:43:52

问题


I'm doing heavy mathematical computations using Math.Net Numerics parallely inside Parallel.For block.

When I run code in my local system with 4 cores(2*2), it's using all 4 cores.

But when I run same code in our dev server with 8 cores(4*2), it's using only 4 cores.

I've tried setting MaxDegreeOfParallism,but couldn't help.

Any idea why all cores are not being utilised.

Below is sample code.

Parallel.For(0,10000,(i)=>
{

 // heavy math computations using matrices
});

回答1:


From MSDN

By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

The way I read the documentation: if the underlying scheduler only offers a single thread, then setting MaxDegreeOfParallelism > 1 will still result in a single thread.




回答2:


Parallelization is done runtime, based on the current conditions and a lots of other circumstances. You cannot force .NET to use all the cores (in managed code at least).

From MSDN:

"Conversely, by default, the Parallel.ForEach and Parallel.For methods can use a variable number of tasks. That's why, for example, the ParallelOptions class has a MaxDegreeOfParallelism property instead of a "MinDegreeOfParallelism" property. The idea is that the system can use fewer threads than requested to process a loop. The .NET thread pool adapts dynamically to changing workloads by allowing the number of worker threads for parallel tasks to change over time. At run time, the system observes whether increasing the number of threads improves or degrades overall throughput and adjusts the number of worker threads accordingly.

Be careful if you use parallel loops with individual steps that take several seconds or more. This can occur with I/O-bound workloads as well as lengthy calculations. If the loops take a long time, you may experience an unbounded growth of worker threads due to a heuristic for preventing thread starvation that's used by the .NET ThreadPool class's thread injection logic. "



来源:https://stackoverflow.com/questions/32188185/parallel-for-not-utilising-all-cores

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!