Skip to content

Add a workspace struct#130

Merged
vchuravy merged 14 commits intomainfrom
vc/workspace
Apr 22, 2026
Merged

Add a workspace struct#130
vchuravy merged 14 commits intomainfrom
vc/workspace

Conversation

@vchuravy
Copy link
Copy Markdown
Member

  • Add NewtonKrylovWorkspace to pre-allocate all Newton-Krylov buffers
  • Add evaluate! and refactor linesearch to operate on workspace

@github-actions
Copy link
Copy Markdown

Once the build has completed, you can preview your PR at this URL: https://NumericalMathematics.github.io/Ariadne.jl/previews/PR130/ in a couple of minutes.

Comment thread libs/Theseus/src/Theseus.jl Outdated
Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl
Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl
Comment thread src/Ariadne.jl Outdated
Base automatically changed from vc/linesearch to main April 20, 2026 19:24
vchuravy and others added 3 commits April 21, 2026 10:13
Introduces `NewtonKrylovWorkspace` that holds `res`, `neg_res`,
`JacobianOperator` (with Enzyme caches), and the Krylov solver workspace,
eliminating per-call allocations in the Newton loop.

The main `newton_krylov!(ws, u, p; ...)` method operates on the pre-allocated
workspace. Existing convenience methods now create a workspace internally and
delegate to it. Passing a different initial-guess array copies it into `ws.u`
automatically when `ws.u !== u`.

Adds `test/workspace.jl` covering correctness, workspace reuse, and a
regression guard showing allocations are significantly reduced vs the
non-workspace API.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
`evaluate!(ws::NewtonKrylovWorkspace)` evaluates `F!(ws.res, ws.u, ws.p)`
in-place and returns `norm(ws.res)`, eliminating the need to pass `F!`, `u`,
and `p` separately after construction.

`NewtonKrylovWorkspace` gains `f` and `p` fields so all inputs are
self-contained. The linesearch interface is updated to `(ws, norm_res_prior, d)`
— both `NoLineSearch` and `BacktrackingLineSearch` now call `evaluate!(ws)`
instead of invoking `F!` and `norm` directly.

The top-level solver is split into `newton_krylov!(ws)` (no extra args) and a
thin `newton_krylov!(ws, u)` wrapper that copies `u` into `ws.u` when needed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Pass `res` as keyword arg to constructor instead of copying via `ws.res .= res`
- Add ghost-cell comment before `Enzyme.make_zero!(res)`
- Fix `newton_krylov!(ws, u₀; ...)` to drop redundant `u₀` arg
- Fix docstring for `newton_krylov!(ws; kwargs...)` to include signature
- Remove unused `krylov_ws` local variable
- Drop `NewtonKrylovWorkspace` from Theseus import

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vchuravy vchuravy requested a review from ranocha April 21, 2026 08:58
Comment thread src/Ariadne.jl Outdated
Copy link
Copy Markdown
Member

@ranocha ranocha left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl Outdated
Comment thread test/workspace.jl Outdated
vchuravy and others added 2 commits April 21, 2026 14:27
Co-authored-by: Hendrik Ranocha <ranocha@users.noreply.github.com>
Comment thread src/Ariadne.jl Outdated
Comment thread src/Ariadne.jl
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 21, 2026

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic main) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/Ariadne.jl b/src/Ariadne.jl
index 14628d2..40d9dbd 100644
--- a/src/Ariadne.jl
+++ b/src/Ariadne.jl
@@ -338,7 +338,7 @@ struct NewtonKrylovWorkspace{F, A, P, JOp <: AbstractJacobianOperator, KW}
 end
 
 function NewtonKrylovWorkspace(
-        F!, u::AbstractArray, p, res::AbstractArray, ::Val{Algo}=Val(:gmres);
+        F!, u::AbstractArray, p, res::AbstractArray, ::Val{Algo} = Val(:gmres);
         assume_p_const::Bool = false
     ) where {Algo}
     # res .= 0 might ignore ghost cells
diff --git a/test/workspace.jl b/test/workspace.jl
index 3ade878..b592a75 100644
--- a/test/workspace.jl
+++ b/test/workspace.jl
@@ -96,5 +96,5 @@ Base.zero(v::WrappedVector) = WrappedVector(zero(v.data))
     res = zero(x₀)
     x, stats = newton_krylov!(F!, x₀, nothing, res)
     @test stats.solved
-    @test x ≈ [1.0, 1.0] atol = 1e-5
+    @test x ≈ [1.0, 1.0] atol = 1.0e-5
 end

vchuravy and others added 4 commits April 21, 2026 16:03
…rator type mismatch

When newton_krylov!(F!, u, p, res) was called with a custom array type,
the supplied `res` was passed as a keyword to the 3-arg NewtonKrylovWorkspace
constructor, which silently dropped it into kwargs and created a fresh
`similar(u, M)` instead. For types where similar(u, ::Int) falls back to
returning a plain Vector, this produced a res/u type mismatch that violated
JacobianOperator's res::A / u::A constraint.

Fix by routing directly to the 5-arg NewtonKrylovWorkspace constructor with
res as a positional argument. Add a regression test using WrappedVector, a
minimal AbstractVector subtype whose similar(v, ::Int) is undefined so it
would trigger the fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vchuravy vchuravy requested a review from ranocha April 21, 2026 19:07
@vchuravy vchuravy merged commit d0d06c7 into main Apr 22, 2026
13 checks passed
@vchuravy vchuravy deleted the vc/workspace branch April 22, 2026 04:56
@vchuravy vchuravy mentioned this pull request Apr 22, 2026
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.

2 participants