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

docs: Fixes for solver developing information #565

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions docs/src/advanced/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ Let's create a new wrapper for a simple LU-factorization which uses only the
basic machinery. A simplified version is:

```julia
struct MyLUFactorization{P} <: SciMLBase.AbstractLinearAlgorithm end
struct MyLUFactorization{P} <: LinearSolve.SciMLLinearSolveAlgorithm end

function init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters, abstol, reltol,
verbose)
function LinearSolve.init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters::Int, abstol, reltol,
verbose::Bool, assump::LinearSolve.OperatorAssumptions)
lu!(convert(AbstractMatrix, A))
end

function SciMLBase.solve!(cache::LinearCache, alg::MyLUFactorization; kwargs...)
function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::MyLUFactorization; kwargs...)
if cache.isfresh
A = cache.A
A = convert(AbstractMatrix, A)
fact = lu!(A)
cache = set_cacheval(cache, fact)
cache.cacheval = fact
cache.isfresh = false
end
y = ldiv!(cache.u, cache.cacheval, cache.b)
SciMLBase.build_linear_solution(alg, y, nothing, cache)
Expand All @@ -39,7 +41,7 @@ need to cache their own things, and so there's one value `cacheval` that is
for the algorithms to modify. The function:

```julia
init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters, abstol, reltol, verbose)
init_cacheval(alg::MyLUFactorization, A, b, u, Pl, Pr, maxiters, abstol, reltol, verbose, assump)
```

is what is called at `init` time to create the first `cacheval`. Note that this
Expand All @@ -51,13 +53,13 @@ LU-factorization to get an `LU{T, Matrix{T}}` which it puts into the `cacheval`
so it is typed for future use.

After the `init_cacheval`, the only thing left to do is to define
`SciMLBase.solve(cache::LinearCache, alg::MyLUFactorization)`. Many algorithms
`SciMLBase.solve!(cache::LinearCache, alg::MyLUFactorization)`. Many algorithms
may use a lazy matrix-free representation of the operator `A`. Thus, if the
algorithm requires a concrete matrix, like LU-factorization does, the algorithm
should `convert(AbstractMatrix,cache.A)`. The flag `cache.isfresh` states whether
`A` has changed since the last `solve`. Since we only need to factorize when
`A` is new, the factorization part of the algorithm is done in a `if cache.isfresh`.
`cache = set_cacheval(cache, fact)` puts the new factorization into the cache,
`cache.cacheval = fact; cache.isfresh = false` puts the new factorization into the cache,
so it's updated for future solves. Then `y = ldiv!(cache.u, cache.cacheval, cache.b)`
performs the solve and a linear solution is returned via
`SciMLBase.build_linear_solution(alg,y,nothing,cache)`.
Loading