我在 Parallel.For
block 中并行使用 Math.Net Numerics
进行繁重的数学计算。
当我在具有 4 个内核 (2*2) 的本地系统中运行代码时,它使用了所有 4 个内核。
但是当我在具有 8 个内核 (4*2) 的开发服务器中运行相同的代码时,它只使用了 4 个内核。
我试过设置 MaxDegreeOfParallism,但无济于事。
知道为什么没有使用所有内核。
下面是示例代码。
Parallel.For(0,10000,(i)=>
{
// heavy math computations using matrices
});
最佳答案
来自 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.
我阅读文档的方式:如果底层调度程序仅提供单线程,则设置 MaxDegreeOfParallelism > 1
仍将导致单线程。
关于c# - Parallel.For 不利用所有核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32188185/