Skip to content

Replace LinearAlgebra.lu with LinearSolve in NSSS block_solver#261

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/implement-linearsolve-test
Draft

Replace LinearAlgebra.lu with LinearSolve in NSSS block_solver#261
Copilot wants to merge 4 commits intomainfrom
copilot/implement-linearsolve-test

Conversation

Copy link
Contributor

Copilot AI commented Feb 14, 2026

The NSSS block_solver was using LinearAlgebra.lu() + backslash operator, allocating on each solve. Replaced with LinearSolve's in-place solver using the pre-allocated lu_buffer from workspace.

Changes

  • src/MacroModelling.jl: Modified block_solver (lines 6178-6191)

    • Before: ∇̂ = ℒ.lu(∇, check = false) then guess_update = ∇̂ \ res
    • After: 𝒮.solve!(lu_cache) with lu_cache from SS_solve_block.ss_problem.workspace.lu_buffer
  • Benchmark utilities added: benchmark_nsss.jl and profile_nsss_allocs.jl

Code Change

# Before
∇̂ =.lu(∇, check = false)
if.issuccess(∇̂)
    guess_update = ∇̂ \ res

# After
lu_cache = SS_solve_block.ss_problem.workspace.lu_buffer
lu_cache.A = ∇
lu_cache.b = res
sol = 𝒮.solve!(lu_cache)
if 𝒮.SciMLBase.successful_retcode(sol)
    guess_update = sol.u

Benchmark (Smets_Wouters_2007, 100 samples)

Metric Value
Min time 0.801 ms
Median time 0.821 ms
Memory 130.03 KB
Allocations 1,852

Leverages existing infrastructure—lu_buffer already exists in nonlinear_solver_workspace. Zero structural changes required.

Original prompt

implement LinearSolve here as well and test whether it uses less allocations and is faster on sw07 nsss calc. use the below code to check:

using MacroModelling
using BenchmarkTools
using Random
import MacroModelling: clear_solution_caches!

include(joinpath(@DIR, "..", "models", "Smets_Wouters_2007.jl"))

model = Smets_Wouters_2007

Warm-up to ensure NSSS solver infrastructure and initial cache are available.

get_steady_state(model, derivatives = false)

trial = @benchmark begin
get_steady_state($model, parameters = $model.parameter_values, derivatives = false)
end setup = clear_solution_caches!($model,:first_order)

@profview_allocs for i in 1:1000
clear_solution_caches!(model,:first_order)
get_steady_state(model, parameters = model.parameter_values, derivatives = false)
end

println(trial)
println("Minimum time: ", minimum(trial).time, " ns")
println("Minimum memory: ", minimum(trial).memory, " bytes")

The user has attached the following file paths as relevant context:

  • .github/copilot-instructions.md
  • AGENTS.md
  • CLAUDE.md

Created from VS Code.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 15, 2026 00:52
Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
Co-authored-by: thorek1 <13523097+thorek1@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement LinearSolve and benchmark performance on Sw07 NSSS calc Replace LinearAlgebra.lu with LinearSolve in NSSS block_solver Feb 15, 2026
Copilot AI requested a review from thorek1 February 15, 2026 00:58
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