From 47ebab617ae0df2689bd47a03659b1c3ea0c42dd Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Tue, 13 Oct 2026 18:55:34 +0100 Subject: [PATCH 1/3] Zero a sparse DAE J_du through its stored values `build_nlsolver` allocated the separated DAE Jacobian as `J_du = fill!(similar(J), false)`. A `CuSparseMatrixCSR` has no `fill!`, so Base fell back to `setindex!` and `alg_cache` threw a `CanonicalIndexError` for any GPU `DAEProblem` with a cuSPARSE `jac_prototype`. Sparse `J_du` is now zeroed with `set_all_nzval!`, which keeps the prototype's pattern; dense `J_du` still uses `fill!`. Agent-Harness: Claude Code 2.1.296 Agent-Model: claude-opus-5-5 Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9 Agent-Review: unreviewed Co-authored-by: Claude Code --- .../test/dae_nlsolve_path_tests.jl | 39 ++++++++++++++++++- lib/OrdinaryDiffEqBDF/test/gpu/Project.toml | 4 ++ lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl | 18 ++++++++- .../src/OrdinaryDiffEqNonlinearSolve.jl | 2 +- lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl | 5 ++- 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl b/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl index bbcb8fda575..78858e09007 100644 --- a/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl +++ b/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl @@ -1,5 +1,8 @@ using OrdinaryDiffEqBDF, OrdinaryDiffEqCore, Test -using SciMLBase: DAEProblem +using SciMLBase: DAEFunction, DAEProblem, NoInit +using ADTypes: AutoFiniteDiff +using LinearAlgebra: I +using SparseArrays function dae_lin!(res, du, u, p, t) @. res = du - u @@ -28,3 +31,37 @@ const dae_bdf_algs = (DImplicitEuler(), DABDF2(), DFBDF(), DNordsieckBDF()) end end end + +# A sparse matrix type without `setindex!`, as `CuSparseMatrixCSR`: `fill!` on it throws, +# so the separated DAE Jacobian `J_du` has to be zeroed through its stored values. +struct NoSetindexSparse{T} <: SparseArrays.AbstractSparseMatrix{T, Int} + A::SparseMatrixCSC{T, Int} +end +Base.size(M::NoSetindexSparse) = size(M.A) +Base.getindex(M::NoSetindexSparse, i::Int, j::Int) = M.A[i, j] +Base.similar(M::NoSetindexSparse, ::Type{T}) where {T} = NoSetindexSparse(similar(M.A, T)) +SparseArrays.nonzeros(M::NoSetindexSparse) = nonzeros(M.A) +SparseArrays.rowvals(M::NoSetindexSparse) = rowvals(M.A) +SparseArrays.getcolptr(M::NoSetindexSparse) = SparseArrays.getcolptr(M.A) +SparseArrays.nnz(M::NoSetindexSparse) = nnz(M.A) + +@testset "DAE J_du for a sparse jac_prototype without setindex!" begin + sparse_dae!(res, du, u, p, t) = (res .= du .+ u; nothing) + sparse_dae_jac!(J, du, u, p, gamma, t) = (nonzeros(J) .= 1 + gamma; nothing) + pattern = sparse(1.0I, 4, 4) + prototype = NoSetindexSparse(pattern) + for (label, f) in ( + ("user jac", DAEFunction{true}(sparse_dae!; jac = sparse_dae_jac!, jac_prototype = prototype)), + ("AD jac", DAEFunction{true}(sparse_dae!; jac_prototype = prototype)), + ) + @testset "$label" begin + prob = DAEProblem(f, fill(-1.0, 4), ones(4), (0.0, 1.0)) + integrator = init(prob, DFBDF(autodiff = AutoFiniteDiff()); initializealg = NoInit()) + J_du = integrator.cache.nlsolver.cache.dae_jacobians.J_du + @test J_du isa NoSetindexSparse{Float64} + @test rowvals(J_du) == rowvals(pattern) + @test SparseArrays.getcolptr(J_du) == SparseArrays.getcolptr(pattern) + @test all(iszero, nonzeros(J_du)) + end + end +end diff --git a/lib/OrdinaryDiffEqBDF/test/gpu/Project.toml b/lib/OrdinaryDiffEqBDF/test/gpu/Project.toml index 2f648fa6985..806f44f6e18 100644 --- a/lib/OrdinaryDiffEqBDF/test/gpu/Project.toml +++ b/lib/OrdinaryDiffEqBDF/test/gpu/Project.toml @@ -1,6 +1,9 @@ [deps] CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" +CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [sources.OrdinaryDiffEqBDF] @@ -8,4 +11,5 @@ path = "../.." [compat] CUDA = "6" +CUDSS = "0.8" OrdinaryDiffEqBDF = "2" diff --git a/lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl b/lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl index 498080eb771..b0a09b08041 100644 --- a/lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl +++ b/lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl @@ -1,5 +1,6 @@ #fixing gpu tolerances -using OrdinaryDiffEqBDF, CUDA, Test +using OrdinaryDiffEqBDF, CUDA, CUDSS, LinearAlgebra, SparseArrays, Test +using CUDA.CUSPARSE: CuSparseMatrixCSR CUDA.allowscalar(false) function f_gpu!(du, u, p, t) @@ -30,3 +31,18 @@ end @test sol_gpu.u[end] isa CuArray @test isapprox(Array(sol_gpu.u[end]), sol_cpu.u[end], rtol = 1.0e-3) end + +# The separated DAE Jacobian J_du is allocated from the CuSparseMatrixCSR prototype, +# which has no `fill!` +@testset "DFBDF GPU sparse jac_prototype" begin + dae!(res, du, u, p, t) = (res .= du .+ u; nothing) + N = 8 + prob_gpu = DAEProblem( + DAEFunction{true}(dae!; jac_prototype = CuSparseMatrixCSR(sparse(1.0I, N, N))), + CUDA.fill(-1.0, N), CUDA.ones(Float64, N), (0.0, 1.0) + ) + sol_gpu = solve(prob_gpu, DFBDF(), abstol = 1.0e-6, reltol = 1.0e-6) + @test SciMLBase.successful_retcode(sol_gpu) + @test sol_gpu.u[end] isa CuArray + @test isapprox(Array(sol_gpu.u[end]), fill(exp(-1), N), rtol = 1.0e-3) +end diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl index 0893cf6f760..0bef39bf0ce 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl @@ -68,7 +68,7 @@ import OrdinaryDiffEqCore: _initialize_dae!, import OrdinaryDiffEqDifferentiation: update_W!, is_always_new, build_uf, build_J_W, WOperator, StaticWOperator, wrapprecs, default_krylov_warm_start, build_jac_config, dolinsolve, set_linear_reltol!, - resize_jac_config!, jacobian2W!, jacobian!, calc_J + resize_jac_config!, jacobian2W!, jacobian!, calc_J, is_sparse, set_all_nzval! import StaticArraysCore: StaticArray diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl index 82bda81be13..6dfc29c6b41 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl @@ -952,10 +952,12 @@ function build_nlsolver( # Build separated DAE Jacobian cache if applicable if isdae + # cuSPARSE matrices have no `fill!`, so sparse J_du is zeroed through its stored values + J_du = is_sparse(J) ? set_all_nzval!(similar(J), false) : + fill!(similar(J), false) if islinear(f) || SciMLBase.has_jac(f) || (SciMLBase.has_jac_u(f) && SciMLBase.has_jac_du(f)) # User provides Jacobian(s) — no wrappers, but still need J_du storage - J_du = fill!(similar(J), false) dae_jacobians = DAEJacobiansCache( J_du, nothing, nothing, nothing, nothing ) @@ -968,7 +970,6 @@ function build_nlsolver( jac_config_du = build_jac_config( alg, nf, uf_du, du1, uprev, u, ztmp, dz ) - J_du = fill!(similar(J), false) dae_jacobians = DAEJacobiansCache( J_du, uf_u, uf_du, jac_config_u, jac_config_du ) From d0219e64ad6fdd74306919cb273ea8efd6921766 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Tue, 13 Oct 2026 22:14:20 +0100 Subject: [PATCH 2/3] Ignore is_sparse and set_all_nzval! in the NonlinearSolve public-import QA check Both are OrdinaryDiffEqDifferentiation internals, imported alongside the other Differentiation names already on the ignore list. Agent-Harness: Claude Code 2.1.296 Agent-Model: claude-opus-5-5 Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9 Agent-Review: unreviewed Co-authored-by: Claude Code --- lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl index 6aebff8f213..7032bf674a5 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl @@ -62,7 +62,7 @@ run_qa( # OrdinaryDiffEqDifferentiation: :build_J_W, :build_jac_config, :build_uf, :dolinsolve, :is_always_new, :jacobian!, :jacobian2W!, :resize_jac_config!, - :update_W!, :wrapprecs, + :update_W!, :wrapprecs, :is_sparse, :set_all_nzval!, # ForwardDiff / StaticArraysCore internals. :Dual, :StaticArray, ), From 263fc634dd3aba78bae43d735371ced1a13f57d2 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Wed, 21 Oct 2026 18:57:08 +0100 Subject: [PATCH 3/3] Zero sparse J_du through public SparseArrays operations Replace the OrdinaryDiffEqDifferentiation internals is_sparse and set_all_nzval! with an AbstractSparseMatrix check and a broadcast into nonzeros, and drop their QA public-API exemptions. The test fixture forwards the public nzrange instead of the non-public getcolptr. CuSparseMatrixCSR subtypes AbstractSparseMatrix through GPUArrays.AbstractGPUSparseArray and its nonzeros is the device nzVal vector, so the zeroing stays a GPU broadcast with no scalar indexing. Agent-Harness: Claude Code 2.1.306 Agent-Model: claude-opus-5-5 Agent-Session: aa65e380-52df-443a-9f57-8c20c56babb9 Agent-Review: unreviewed Co-authored-by: Claude Code --- lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl | 5 +++-- .../src/OrdinaryDiffEqNonlinearSolve.jl | 3 ++- lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl | 8 ++++++-- lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl b/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl index 78858e09007..356180717dc 100644 --- a/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl +++ b/lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl @@ -42,7 +42,7 @@ Base.getindex(M::NoSetindexSparse, i::Int, j::Int) = M.A[i, j] Base.similar(M::NoSetindexSparse, ::Type{T}) where {T} = NoSetindexSparse(similar(M.A, T)) SparseArrays.nonzeros(M::NoSetindexSparse) = nonzeros(M.A) SparseArrays.rowvals(M::NoSetindexSparse) = rowvals(M.A) -SparseArrays.getcolptr(M::NoSetindexSparse) = SparseArrays.getcolptr(M.A) +SparseArrays.nzrange(M::NoSetindexSparse, j::Integer) = nzrange(M.A, j) SparseArrays.nnz(M::NoSetindexSparse) = nnz(M.A) @testset "DAE J_du for a sparse jac_prototype without setindex!" begin @@ -60,7 +60,8 @@ SparseArrays.nnz(M::NoSetindexSparse) = nnz(M.A) J_du = integrator.cache.nlsolver.cache.dae_jacobians.J_du @test J_du isa NoSetindexSparse{Float64} @test rowvals(J_du) == rowvals(pattern) - @test SparseArrays.getcolptr(J_du) == SparseArrays.getcolptr(pattern) + @test [nzrange(J_du, j) for j in axes(J_du, 2)] == + [nzrange(pattern, j) for j in axes(pattern, 2)] @test all(iszero, nonzeros(J_du)) end end diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl index 0bef39bf0ce..310db93ffea 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/OrdinaryDiffEqNonlinearSolve.jl @@ -1,6 +1,7 @@ module OrdinaryDiffEqNonlinearSolve using ADTypes: ADTypes, AutoForwardDiff, AutoFiniteDiff +using SparseArrays: AbstractSparseMatrix, nonzeros using CommonSolve: init, solve, solve!, step! import SciMLBase @@ -68,7 +69,7 @@ import OrdinaryDiffEqCore: _initialize_dae!, import OrdinaryDiffEqDifferentiation: update_W!, is_always_new, build_uf, build_J_W, WOperator, StaticWOperator, wrapprecs, default_krylov_warm_start, build_jac_config, dolinsolve, set_linear_reltol!, - resize_jac_config!, jacobian2W!, jacobian!, calc_J, is_sparse, set_all_nzval! + resize_jac_config!, jacobian2W!, jacobian!, calc_J import StaticArraysCore: StaticArray diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl index 6dfc29c6b41..6d47eec8672 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl @@ -953,8 +953,12 @@ function build_nlsolver( # Build separated DAE Jacobian cache if applicable if isdae # cuSPARSE matrices have no `fill!`, so sparse J_du is zeroed through its stored values - J_du = is_sparse(J) ? set_all_nzval!(similar(J), false) : - fill!(similar(J), false) + J_du = similar(J) + if J isa AbstractSparseMatrix + nonzeros(J_du) .= false + else + fill!(J_du, false) + end if islinear(f) || SciMLBase.has_jac(f) || (SciMLBase.has_jac_u(f) && SciMLBase.has_jac_du(f)) # User provides Jacobian(s) — no wrappers, but still need J_du storage diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl index 7032bf674a5..6aebff8f213 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/qa/qa.jl @@ -62,7 +62,7 @@ run_qa( # OrdinaryDiffEqDifferentiation: :build_J_W, :build_jac_config, :build_uf, :dolinsolve, :is_always_new, :jacobian!, :jacobian2W!, :resize_jac_config!, - :update_W!, :wrapprecs, :is_sparse, :set_all_nzval!, + :update_W!, :wrapprecs, # ForwardDiff / StaticArraysCore internals. :Dual, :StaticArray, ),