Repository navigation
Krylov: support an ArrayPartition right-hand side - #1206
Conversation
| # The previous method here returned `nothing` to dodge the workspace allocation, but | ||
| # it declared no `zeroinit`, so the `solve!` path below (which always passes | ||
| # `zeroinit = false`) never reached it and hit the error anyway. | ||
| _krylov_flat(x::RecursiveArrayTools.ArrayPartition) = copyto!( |
There was a problem hiding this comment.
Instead of a special new function here, it should use the ArrayInterface.jl primitive for vector flattening.
There was a problem hiding this comment.
@ChrisRackauckas second one applied, restructure is better than the copyto! here.
On the first: safevec returns an AbstractVector unchanged (safevec(v::AbstractVector) = v), and ArrayPartition is one, so it does not flatten and cannot build the workspace Krylov needs. There is no RecursiveArrayTools extension in ArrayInterface adding a method either.
Did you have a different primitive in mind? Otherwise the options are Vector(b), which forces a CPU array and would break GPU-backed partitions, or the current similar(first(b.x), length(b)) form which keeps the backing type. Happy to inline it rather than name it if the objection is to the helper itself.
Blocked on this one, the rest of the PR is green.
There was a problem hiding this comment.
I guess this is a better fit to SciMLStructures.jl and its canonicalize framework.
There was a problem hiding this comment.
Checked this. canonicalize(::Tunable, p::AbstractArray) = vec(p), ArrayRepack(p), true, and vec of an ArrayPartition returns the same ArrayPartition, so the buffer comes back unflattened. The repack half then errors:
The original type ArrayPartition{...} does not support the SciMLStructures interface via
the AbstractArray `repack` rules. No method exists to take in a regular array and construct
the parent type back. Please define the SciMLStructures interface for this type.
RecursiveArrayTools does not define canonicalize for ArrayPartition, so it falls back to the generic array method that assumes vec flattens. ArrayRepack itself calls ArrayInterface.restructure, which is what this PR already uses for the write-back.
Happy to add the SciMLStructures interface for ArrayPartition in RecursiveArrayTools and then simplify this to a canonicalize/repack pair. Should that land first, or go with the flat cache here and switch over once it does?
There was a problem hiding this comment.
Happy to add the SciMLStructures interface for ArrayPartition in RecursiveArrayTools and then simplify this to a canonicalize/repack pair. Should that land first, or go with the flat cache here and switch over once it does?
Yes let's do that. And then we should document that any type that wants to be compatible with this should define the SciMLStructures.jl interface. That makes it a general solution.
| flat.abstol, flat.reltol, flat.maxiters = cache.abstol, cache.reltol, cache.maxiters | ||
|
|
||
| sol = SciMLBase.solve!(flat, alg; kwargs...) | ||
| copyto!(cache.u, sol.u) |
There was a problem hiding this comment.
This is making too strong of an assumption. It should use an ArrayInterface.restructure
|
A few months ago, I added this constructor in Krylov.jl such that we can use |
Krylov.jl allocates its workspace as `S(undef, n)` with `S = typeof(b)`, and `ArrayPartition(undef, n)` is defined only for a single partition, since there is no way to know how to split `n` across several. Krylov also requires `ktypeof(b)` to match the workspace storage, so giving it plain `Vector` storage next to a partitioned right-hand side is rejected too. There was already a method here meant to dodge this, but it declared no `zeroinit`, so the `solve!` path (which always passes `zeroinit = false`) never reached it and hit the error anyway. Returning `nothing` would not have been enough either: `solve!` hands the cacheval straight to Krylov as the workspace. The cacheval is now the flat cache the solve runs on, built once and reused, with the answer copied back into the partitioned `u`. `A` needs no wrapping: an `ArrayPartition` and the vector it flattens to have the same length, so a matrix or operator sized for one multiplies the other unchanged. Closes SciML#384.
Review feedback: writing the flat result back with `copyto!` assumes `u` is writable and laid out to receive it. `ArrayInterface.restructure` rebuilds the partitioned shape without that assumption.
Review feedback: the flattening and the way back belong to the SciMLStructures canonicalize framework rather than to a helper here. `canonicalize(Tunable(), b)` gives the flat buffer, and its own `repack` rebuilds the container, replacing both the hand-rolled flatten and the `ArrayInterface.restructure` call. `ArrayPartition` gained the interface in RecursiveArrayTools 4.4, so the compat bound moves up to it. The body is split into `_init_flat_krylov_cacheval` and `_solve_flat_krylov!`, so another right-hand side type Krylov cannot allocate for opts in with two forwarding methods rather than a copy of the logic. Documented in the FAQ, since that is the general answer: a type works here if it implements the SciMLStructures interface.
Krylov.jl has a `KrylovConstructor` that allocates the workspace with `similar` rather than `S(undef, n)`, which is exactly what an `ArrayPartition` cannot provide. Using it, the solve runs on the partitioned vectors directly: no flattening, no copying, and `u` comes back in the type it went in as. That drops the flat cache, the SciMLStructures dependency, the RecursiveArrayTools compat bump, and the `solve!` specialization, since the workspace now holds the caller's array type throughout and the generic path writes straight into it. The FAQ says what the real requirement is: `similar` support, rather than the SciMLStructures interface. Pointed out by @amontoison on the PR.
A local workaround for a stale registry leaked into the previous commit.
235ab4c to
676b5ab
Compare
|
@amontoison thanks, that is much better than what this had. Rewritten to build the workspace with @ChrisRackauckas this replaces the SciMLStructures approach. That version canonicalized to a flat buffer and repacked, which worked but copied twice per solve and needed a SciMLStructures dependency plus a RecursiveArrayTools 4.4 bound. The Krylov constructor needs neither, so the diff is now four files with no SciML/RecursiveArrayTools.jl#643 still stands on its own: The FAQ entry changed accordingly: the requirement is |
|
It is better to use Another advantage is that I have an internal I will not suggest to use One constructor is for performance while the other one is for portability. |
|
That matches what this does. Three methods now: the generic one, the batched matrix one, and the |
|
Great! I think it is the current best solution. |
It shouldn't allocate at all. You only do it once in the
This does not seem equivalent at all. What does it do on an MTKParameters object? @AJ0070 test it with this object: https://sciml.github.io/SciMLStructures.jl/stable/example/ . The |
You mean an |
| # than guessing. `Krylov.KrylovConstructor` builds the workspace with `similar` | ||
| # instead, which such a type does support, so the solve runs on the partitioned | ||
| # vectors directly with no flattening and no copying. |
There was a problem hiding this comment.
The general type does not support similar
|
Tested the
One thing worth deciding before I build the third path. It cannot be an
So the three paths are not the same shape: Do you want that in this PR, or land the two array paths here for #384 and do the structure path separately? Happy either way, just checking since it widens the blast radius from the Krylov wrapper to the shared init path. |
|
I also don't think this is something Krylov specific at all: it should be done at a higher level. At least the SciMLStructures interfacing. So it's probably fine to finish this PR as a limited Krylov-specific abstractarray thing, but we should then bubble through more general structure support. |
Review feedback: the comment claimed `similar` support in general. It is an `ArrayPartition` property. A right-hand side that is not an array at all, such as a parameter object implementing the SciMLStructures interface, supports neither `S(undef, n)` nor `similar` and needs canonicalizing instead, which this does not handle.
Review asked that the docs say a type wanting to work here should implement the SciMLStructures interface, which is the general solution. The entry said only that `similar` is needed, which is true of what this PR implements but drops that direction, and it advised writing an `init_cacheval` method, which is not something a caller can do from outside the package. Says what works today, and points at the interface and SciML#1208 for the non-array case.
|
@ChrisRackauckas this is at the scope you described, a Krylov-specific Everything raised is in:
Could you clear the changes-requested when you get a chance? |
|
Thanks @ChrisRackauckas and @amontoison for the reviews on this one. The result ended up a good deal smaller than where it started. @amontoison pointing at Also useful was the request to test an |
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
KrylovJL_*on anArrayPartitionright-hand side failed before any iteration ran, which is what the issue'sKenCarp47(linsolve = KrylovJL_GMRES())example hits. The factorization algorithms already worked.S(undef, n)withS = typeof(b), which anArrayPartitioncannot provide: it is stored as several separate arrays, so there is no way to know how to splitnacross them.Krylov.KrylovConstructorbuilds the workspace withsimilarinstead, so the solve runs on the partitioned vectors directly: no flattening, no copying, anducomes back in the type it went in as. Pointed out by @amontoison. It is present in every Krylov 0.10.x, so the existing compat bound already covers it and this PR adds no dependency and changes noProject.tomlentry.solve!needs no specialization: the workspace holds the caller's array type throughout, so the generic path writes the answer straight into it.zeroinit, so thesolve!path (which always passeszeroinit = false) never reached it. Adding the keyword alone would not have been enough:solve!hands the cacheval straight to Krylov as the workspace, so returningnothingjust fails later.band a newA, and that the partitioned shape survives. They were confirmed to fail without the fix, with the original error from the issue.similarsupport, rather than any particular interface.