gpt4 book ai didi

c# - Math.NET Numerics 性能不佳且 CPU 使用率低

转载 作者:行者123 更新时间:2023-11-30 21:27:30 27 4
gpt4 key购买 nike

我希望能够在 C# 中求解大型稀疏 线性方程组(dim = 1,000,000)。为此,我将 Math.NET Numerics 与 MKL 提供程序结合使用。

我创建了以下测试程序来检查 Math.NET Numerics 的性能,但是,即使对于 int dim = 5000;,该程序也需要很长时间才能等待运行结束。另外,我的机器的 cpu 使用率没有超过 25%(机器有 4 个内核)。

将 MKL 与 Fortran 结合使用,我可以更快地解决更大的系统 (dim = 1,000,000)(CPU 使用率接近 100%)。我正在使用直接稀疏求解器 (DSS) 接口(interface)例程。

using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra;

namespace ConsoleApp1
{
class Program
{
static void Main()
{
Control.UseNativeMKL();
Control.UseMultiThreading();

int dim = 5000;

var A = Matrix<double>.Build.Sparse(dim, dim);
var F = Vector<double>.Build.Dense(dim);

for (int i = 0; i < dim; i++)
{
A[i, i] = 1.0;
F[i] = 1.0;
}

// THIS TAKES FOREVER
Vector<double> X = A.Solve(F);
}
}
}

我可以做些什么来提高上述程序的性能?

更新

尽管 ALGLIB 免费版确实有一些限制:

First limitation is performance. Free Editions do not include multithreading functionality, SIMD optimizations, native HPC kernelsfor C# apps and integration with Intel MKL.

Second limitation is license. Our flagship products (ALGLIB for C++ and ALGLIB for C#) are distributed under GPL 2+ license, which isnot suited for commercial distribution; other products are distributedunder Personal and Academic Use License.

它在解决稀疏系统方面确实提供了更好的性能(与 Math.Net Numerics 相比)。它还确实提供了处理稀疏矩阵的便捷方法。

最佳答案

根据 this comment一年半之前 Math.Net 中没有稀疏矩阵的直接求解器。

I believe Math.Net doesn't provide any direct solvers for sparse matrices. It will try to solve it as a dense matrix which will take a very long time. I use CSparse.Net for sparse matrices (https://github.com/wo80/CSparse.NET/wiki/Math.NET-Numerics-and-CSparse)

以下注释来自 an article八岁以某种方式证实了这一点:

The examples so far used only dense vectors and matrices. Math.NET Numerics provides types SparseVector and SparseMatrix and has a good design for future support for working with them. However, the current implementation for them is very limited. For instance, the types do not have a constructor to build a sparse vector using indexed non-zero values. Very few math libraries provide full support for sparse matrices and sparse linear algebra because there is no clear standard for sparse matrix linear algebra (unlike BLAS/LAPACK for dense matrices)

我浏览了库代码的相关部分,没有找到任何直接的稀疏矩阵求解器。

关于c# - Math.NET Numerics 性能不佳且 CPU 使用率低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57644573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com