c# - Parallel.For 不利用所有核心

标签 c# task-parallel-library math.net mathnet-numerics parallel.for

我在 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/

相关文章:

c# - AsyncTaskMethodBuilder AwaitUnsafeOnCompleted 与等待时的 AwaitOnCompleted

c# - 对 AggregateException Handle 方法的混淆

c# - LINQ to Entities 无法识别方法 'System.String StringConvert(System.Nullable` 1[System.Double])

c# - C# 中是否有 ShouldSerialize[PropertyName] 的替代方案?

c# - "Streaming"从 SQL Server 中的表读取超过 1000 万行

c# - 异步/等待。方法的可等待部分的延续在哪里执行?

random - F# 使用 System.Random() 获取随机数列表给出相同的列表

c# - 使用 Mathnet 数字库进行 Svd 重组似乎是错误的

c# - 使用 Math.NET 进行约束的线性回归

c# - 使用来自 $http post 服务的模型重定向到 MVC View