Reputation: 616
I want to be able to solve large sparse systems of linear equations in C# (dim = 1,000,000). For this I'm using Math.NET Numerics with the MKL provider.
I created the following test program to check the performance of Math.NET Numerics, however, even for int dim = 5000;
the program takes too long to even wait for the end of run. Also, the cpu usage of my machine does not go past 25% (machine has 4 cores).
Using MKL with Fortran I can solve way bigger systems (dim = 1,000,000) way faster (cpu usage close to 100%). I'm using the Direct Sparse Solver (DSS) Interface Routines.
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);
}
}
}
What can I do to improve performance of the above program?
UPDATE
Although ALGLIB Free Edition does have some limitations:
First limitation is performance. Free Editions do not include multithreading functionality, SIMD optimizations, native HPC kernels for 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 is not suited for commercial distribution; other products are distributed under Personal and Academic Use License.
It does offer much better performance in solving sparse systems (compared to Math.Net Numerics). It does also offer convenient ways to handle sparse matrices.
Upvotes: 3
Views: 3102
Reputation: 19019
According to this comment which a year and a half old there is no direct solvers for sparse matrices in 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)
The following note from an article which is eight years old somehow confirms this:
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)
And I skimmed through the relevant part of the library's code and haven't found any direct sparse matrix solver.
Upvotes: 3