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

update precs when cache.isfresh #533

Conversation

oscardssmith
Copy link
Contributor

successor to #528

Comment on lines +135 to +139
if hasproperty(alg, :precs) && !isnothing(alg.precs)
Pl, Pr = cache.alg.precs(x, cache.p)
cache.Pl = Pl
cache.Pr = Pr
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed here ? Pl, Pr are not used by Pardiso.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, is krylov the only one that uses them? I thought there was another...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IterativeSolvers, KrylovKit.

@@ -226,6 +226,11 @@ end

function SciMLBase.solve!(cache::LinearCache, alg::KrylovJL; kwargs...)
if cache.isfresh
if hasproperty(alg, :precs) && !isnothing(alg.precs)
Pl, Pr = cache.alg.precs(x, cache.p)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we may need

Pl, Pr = cache.alg.precs(cache.A, cache.p)

Pl, Pr = cache.alg.precs(x, cache.p)
cache.Pl = Pl
cache.Pr = Pr
end
Copy link
Contributor

@j-fu j-fu Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And generally, could we have it like this ?

function SciMLBase.solve!(cache::LinearCache, alg::KrylovJL; reuse_precs=false, kwargs...)
    if cache.isfresh
        if hasproperty(alg, :precs) && !isnothing(alg.precs)
            if !reuse_precs || (cache.Pl == nothing && cache.Pr == nothing)
                Pl, Pr = cache.alg.precs(cache.A, cache.p)
                cache.Pl = Pl
                cache.Pr = Pr
            end
        end
 ...

EDIT: I meant reuse_precs=false as default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in the moment this leads to precs being called twice: once in init and once here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init should set fresh to false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that's tricky. I think I will need to add an extra flag to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisRackauckas really? I think the code relies on it being true to set up some of the C libraries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with the current way is that it makes the fallback slow. if we didn't have the "fast path", we could set cache.isfresh=false which would recover the speed of the fast path for all types, as long as the user inits on a matrix they actually want to solve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just set it to false on the slow path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current approach is better by 1 factorization for users that create a LinearCache with garbage data, but those users will almost always be doing lots of factorizations, minimizing the advantage. OTOH, the current approach has overhead for users who just call solve (since the fake factorization isn't free), or users of the cache form who init on data they want to solve eventually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could set it to false on the slow path, but doing so would be pretty hard since init_cacheval doesn't get the cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and actually users that are initing with fake matrices can init with a 1x1 themselves, which will completely remove the penalty of my suggestion

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

Successfully merging this pull request may close these issues.

3 participants