Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducing memory allocations #6

Open
wo80 opened this issue Aug 3, 2020 · 5 comments
Open

Reducing memory allocations #6

wo80 opened this issue Aug 3, 2020 · 5 comments

Comments

@wo80
Copy link
Collaborator

wo80 commented Aug 3, 2020

I played around with the ILinearSolver interface of the sparse-matrices branch. Here's an implemetation which comes with zero additional memory allocations (using a custom LU implementation):

class OptimizedLinearSolverForTesting : ILinearSolver
{
    private ReusableLU lu;
    private Vector<double> solution;

    public OptimizedLinearSolverForTesting(int dimension)
    {
        this.lu = new ReusableLU(dimension);
        this.solution = new DenseVector(dimension);
    }

    public void Update(DenseMatrix stiffness)
    {
        lu.Compute(stiffness);
    }

    public Vector<double> Solve(Vector<double> input)
    {
        lu.Solve(input.AsArray(), solution.AsArray());
        return solution;
    }
}

It would be used like this:

[Fact]
public void SolveQuadraticFunctionSmallIncrements()
{
    // ALLOCATE MEMORY
    var reaction = new DenseVector(2);
    var stiffness = new DenseMatrix(2, 2);

    var solver = new OptimizedLinearSolverForTesting(2);

    // ARRANGE
    Vector<double> Reaction(Vector<double> u)
    {
        reaction[0] = u[0] * u[0] + 2 * u[1] * u[1];
        reaction[1] = 2 * u[0] * u[0] + u[1] * u[1];
        return reaction;
    };

    ILinearSolver Stiffness(Vector<double> u)
    {
        stiffness[0, 0] = 2 * u[0];
        stiffness[0, 1] = 4 * u[1];
        stiffness[1, 0] = 4 * u[0];
        stiffness[1, 1] = 2 * u[1];

        solver.Update(stiffness);

        return solver;
    }

    DenseVector force = new DenseVector(2) { [0] = 3, [1] = 3 };
    NonLinearSolver Solver = NonLinearSolver.Builder
        .Solve(2, Reaction, Stiffness)
        .Under(force)
        .WithInitialConditions(0.1, DenseVector.Create(2, 0), DenseVector.Create(2, 1))
        .UsingStandardNewtonRaphsonScheme(0.01)
        .Build();

    // ACT
    List<LoadState> states = Solver.Broadcast().TakeWhile(x => x.Lambda <= 1).ToList();

    // ASSERT
    AssertSolutionsAreCorrect(Reaction, force, states);
}

As already mentioned in #2 (comment), there are a couple of places in the NonLinearEquationsSolver code, where further optimization can be done (memory allocations in an iterative process should always be reduced as much as possible).

That's also a point where CSparse.NET has to be improved, if it should be used efficiently with this library (at the moment, matrix factors cannot be reused).

@EduardBargues
Copy link
Owner

Great test! Thank you.
Ill include it in the tests suite.
I'll check your comment and act accordingly (maybe another pull request).
In the mean time, I think we can move forward with the pull reqeust. If you dont have any additional comments please approve 👍 :)

@EduardBargues
Copy link
Owner

A pull request has been added @epsi1on and @wo80 :) !

@EduardBargues
Copy link
Owner

btw @wo80 , where can i get the "ReusableLU" class? Im trying to include the test in the test-suite. Thanks!

@wo80
Copy link
Collaborator Author

wo80 commented Sep 14, 2020

https://github.com/wo80/mathnet-extensions/blob/master/src/Numerics/LinearAlgebra/Double/Factorization/ReusableLU.cs

In #8 you are using operator overloads in the Vector class, which means a new array allocation for each vector operation. Are you planning to address this in a separate pull request resolving this issue?

@EduardBargues
Copy link
Owner

yes, thats the idea :) !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants