Parallel. Do not use all cores

I am doing heavy mathematical calculations using Math.Net Numerics in parallel inside a Parallel.For block.

When I run the code on my local system with 4 cores (2 * 2), it uses all 4 cores.

But when I run the same code on our dev server with 8 cores (4 * 2), it uses only 4 cores.

I tried installing MaxDegreeOfParallism but could not help.

Any idea why all cores are not used.

The following is sample code.

 Parallel.For(0,10000,(i)=> { // heavy math computations using matrices }); 
+6
source share
2 answers

Parallelization is performed at runtime based on current conditions and many other circumstances. You cannot force .NET to use all cores (at least in managed code).

From MSDN :

"Conversely, by default, the Parallel.ForEach and Parallel.For methods can use a variable number of tasks. Therefore, for example, the ParallelOptions class has the MaxDegreeOfParallelism property instead of the" MinDegreeOfParallelism "property. That the system can use fewer threads than request loop processing. Thread pool .NET dynamically adapts to changing workloads, allowing you to change the number of workflows for concurrent tasks over time, while the system monitors whether the number of threads increases or worsens the overall flow. accelerated ability and accordingly adjusts the number of work flows.

Be careful if you use parallel loops with separate steps that take a few seconds or more. This can happen with workloads related to I / O, as well as lengthy calculations. If loops take a lot of time, you may encounter unlimited growth in workflows due to heuristics to prevent starvation used by Thread Thread's threading logic. "

+1
source

From MSDN

By default, For and ForEach will use as many threads as the main scheduler provides, so changing MaxDegreeOfParallelism by default limits the number of simultaneous tasks.

As I read the documentation: if the base scheduler offers only one thread, then setting MaxDegreeOfParallelism > 1 will still lead to one thread.

0
source

All Articles