Repository navigation
Initialize sparse Jacobian caches with stored values - #3833
ChrisRackauckas merged 1 commit into
Conversation
| J = similar(f.jac_prototype) | ||
| W = similar(J) | ||
| fill!(J, zero(eltype(J))) | ||
| fill!(W, zero(eltype(W))) |
There was a problem hiding this comment.
Should use zero instead of similar to start it. though actually, if it's zero then dropzeros is wrong... so maybe something like fill with ones? Benchmark and see if zeroing has adverse behavior when mixed with a sprase matrix and the default dropzeros of LinearSolve
|
@ChrisRackauckas If anything, should it not initialize with a nonzero number like ones? Initializing with zeros sounds like it would trigger the Alternatively, would it be better if OrdinaryDiffEq just sets the nonstructural zeros operator assumption to e.g. |
|
There's a lot of cases we found where the dropzeros is actually a major performance improvement, and also a numerical improvement. So we want to have it on and scan for it. But we should try to not do it on many problems, yes. The issue is that it hits init, so we should probably just init to ones. |
Co-Authored-By: Chris Rackauckas <[email protected]>
d67e68b to
8a4f275
Compare
d8cbb9e (#3833) initializes the J/W caches allocated in build_J_W with ones so factorizations never see undef values, but fill! on LinearAlgebra's banded structured matrices (Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal) throws for any nonzero value since the off-band entries are constrained to zero. Any structured jac_prototype on the default-linsolve path then errored at cache construction, e.g. solve(ODEProblem(ODEFunction(f; jac, jac_prototype = Diagonal(zeros(2))), ones(2), (1.0, 10.0)), Rosenbrock23()). Add a fill_stored! helper that writes only the stored bands of the structured types and falls back to fill! otherwise, and use it for the non-sparse initialization in build_J_W. Dense and sparse prototypes are unchanged. Co-authored-by: ChrisRackauckas-Claude <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
This PR should be ignored until reviewed by @ChrisRackauckas.
Summary
nzvalentries for sparse matricesJandWkeep their sparse pattern and stored values immediately afterinitFixes #3830.
Local verification
~/.juliaup/bin/julia +1.12 --project=. -e 'using OrdinaryDiffEqRosenbrock, SparseArrays, LinearAlgebra; function f!(du,u,p,t); du .= u; end; jacproto = sparse(1.0I, 4, 4); prob = ODEProblem(ODEFunction(f!; jac_prototype=jacproto), ones(4), (0.0, 1.0)); integ = init(prob, Rodas5P()); @show nnz(integ.cache.J) nnz(integ.cache.W); @show nonzeros(integ.cache.J); @show nonzeros(integ.cache.W); @assert nnz(integ.cache.J) == 4; @assert nnz(integ.cache.W) == 4; @assert all(==(1), nonzeros(integ.cache.J)); @assert all(==(1), nonzeros(integ.cache.W))'ODEDIFFEQ_TEST_GROUP=Sparse ~/.juliaup/bin/julia +1.12 --project=lib/OrdinaryDiffEqDifferentiation -e 'using Pkg; Pkg.test(; test_args=String[])'~/.juliaup/bin/julia +1.12 -e 'using Runic; exit(Runic.main(ARGS))' -- --check lib/OrdinaryDiffEqDifferentiation/src/derivative_utils.jl lib/OrdinaryDiffEqDifferentiation/test/sparse/nonfulldiagonal_sparse_tests.jlNote: repository-wide Runic check was also run with
--check --diff --verbose .; it fails on unrelated files. A clean-master check reproduced that failure and bisected it to661f605637/ PR #3777.