Skip to content

Build W through the allocating path for a sparse GPU Jacobian with a scalar mass matrix - #4452

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
AJ0070:fix/gpu-sparse-w-scalar-massmatrix
Sep 7, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
AJ0070:fix/gpu-sparse-w-scalar-massmatrix

Conversation

@AJ0070

@AJ0070 AJ0070 commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

jacobian2W! forms W = J - M/dtgamma. When the mass matrix is scalar (I, the default), it writes W's diagonal in place, either by scalar indexing or by broadcasting into @view(W[idxs]). Neither works for a CuSparseMatrix: the view falls back to getindex(W, i, j), so every implicit solver given a sparse GPU jac_prototype threw Scalar indexing is disallowed on the first step.

The allocating path for this already exists and is what dae_jacobian2W! uses, but in jacobian2W! it sits in an elseif that a scalar mass matrix never reaches. This adds the guard ahead of the diagonal write, using the same copyto!(W, ...) form. The CPU branches are untouched, and the dense Matrix method is not affected.

This unblocks the Brusselator from #1566. On a T4 that example now solves with a CuSparseMatrixCSR jac_prototype under Rosenbrock23, Rosenbrock23 with KrylovJL_GMRES, and FBDF, matching the CPU reference to about 1e-10. With this branch disabled all three fail with scalar indexing, so the change is required rather than incidental. That example also needs its RHS to broadcast over a device-resident index set, which is a property of the script rather than of the solver, and is noted on the issue.

Tests: a CPU unit test that the allocating formula agrees with the diagonal write, and that CPU sparse and dense still take the old branch; plus test/gpu/sparse_jac_prototype.jl, which gpu_group() picks up automatically. That GPU test passes with the fix and errors on all four solver configurations without it.

jacobian2W! is a hot path. The new condition is a branch ahead of the CPU code and should fold away for non-GPU arrays, but it is worth a second opinion.

AI Disclosure: Used Opus 5

@singhharsh1708

Copy link
Copy Markdown
Member

Does this hold when the prototype has a structural zero on the diagonal? J + (λ * invdtgamma) * I fills those in, so the sum has more nnz than the preallocated W and copyto! has nowhere to put them.

@singhharsh1708 singhharsh1708 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me, copyto! carries the structure so the filled-in diagonal is fine.

@AJ0070

AJ0070 commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

@ChrisRackauckas

@ChrisRackauckas
ChrisRackauckas merged commit 386f6c5 into SciML:master Sep 7, 2026
187 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
- DiffEqBase: wrap mass-matrix problems on the finite-difference promote_f path (#4459)
- Preserve the DAE step start during callback interpolation (#4466)
- Release DiffEqBase 7.20.1 (#4474)
- Release OrdinaryDiffEqCore 4.16.1 (#4475)
- Release OrdinaryDiffEqNonlinearSolve 2.9.5 (#4476)
- Run IDSolve's nonlinear solve at the integrator's tolerances (#4468)
- Build W through the allocating path for a sparse GPU Jacobian with a scalar mass matrix (#4452)
- Fix threaded stochastic work-precision isolation (#4462)
- Differentiate wrapped DAEFunctions with chunk size 1, like ODEFunctions (#4464)
- Rebuild MethodOfSteps W after history updates (#4396)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

3 participants