Parallel.For not utilising all cores

后端 未结 2 962
-上瘾入骨i
-上瘾入骨i 2021-01-17 20:22

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

When I run code in my local system

2条回答
  •  隐瞒了意图╮
    2021-01-17 21:05

    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. "

提交回复
热议问题