Repository navigation
Add selinv_extract: read the selected inverse at a sparse pattern - #12
Merged
Merged
Conversation
Adds `selinv_extract` / `selinv_extract!` / `selinv_extract_setup`, the read analogue of `dot(S, B)`: the same supernodal block traversal, but storing Σ's values at B's nonzero positions instead of accumulating `tr(ΣB)`. This lets a consumer read Σ at a small pattern (e.g. `pattern(AᵀA)`) without materializing `sparse(selinv(F).Z)`. - `selinv_extract(S, B)`: allocating; returns a SparseMatrixCSC with exactly B's pattern (off-pattern positions kept as 0.0), bit-identical to `sparse(S)` masked to B's pattern. - `selinv_extract_setup(S, B)` + `selinv_extract!(dest, S, plan)`: precompute a reuse plan once (the supernodal structure is invariant across refactorizations), then fill in O(nnz) with zero allocation. - Generic AbstractMatrix fallback for the simplicial case, where `selinv(F).Z` is a `Symmetric`/`SparseMatrixCSC` rather than a `SupernodalMatrix`, so callers need not branch on factor type. Benchmark (benchmark/bench_extract.jl; 2D Laplacian factor, n=14400, obs-local pattern B/fill ≈ 0.18): materialize+mask 22.9 ms, getindex 14.8 ms, selinv_extract 9.8 ms, extract!+plan 0.27 ms / 0 alloc (≈85x vs materialize). Tests in test/test_extract.jl cover supernodal + simplicial factors, both depermute modes, random and AᵀA patterns, off-pattern zeros, and the allocation-free reuse path. Bump version to 0.2.1. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add the selected-inverse extraction functions to the SupernodalMatrix API page so Documenter's checkdocs=:exports passes (selinv_extract, selinv_extract!, selinv_extract_setup were exported but undocumented). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
selinv_extract/selinv_extract!/selinv_extract_setup— a primitive that reads the selected-inverse values at a given sparse pattern straight from the supernodal blocks, without materializing the fullsparse(selinv(F).Z).It is the read analogue of the existing
dot(S::SupernodalMatrix, B::SparseMatrixCSC): the same block traversal, but instead of accumulating the scalartr(ΣB), it stores Σ's values at B's nonzero positions.Motivation (GaussianMarkovRandomFields.jl #168): the workspace selinv path materializes the entire
sparse(selinv.Z)on every refactorization, but the real consumer —diag(A Σ Aᵀ)for predictor marginals — only needs Σ at the observation-local pattern (≈pattern(AᵀA)), a tiny subset of the factor fill. Streaming the extract (and reusing a precomputed plan) avoids that materialization and restores parallel scaling.API
Semantics.
Z = selinv_extract(S, B)has exactly B's sparsity pattern, andZ[i,j] == sparse(S)[i,j]for every(i,j) ∈ pattern(B)— including positions outside the stored selinv pattern, which are kept as0.0. A genericAbstractMatrixfallback handles the simplicial case (whereselinv(F).Zis aSymmetric/SparseMatrixCSC), so callers never branch on factor type.The supernodal structure is invariant across refactorizations of the same symbolic factor (only
S.valschanges), soselinv_extract_setupbuilds a plan once andselinv_extract!(dest, S, plan)reuses it with zero allocation.Benchmark
benchmark/bench_extract.jl, 2D Laplacian factorn = 14400, observation-local pattern (B/fill ≈ 0.18), depermuted supernodal.Z:sparse(S)+ maskgetindexover Sselinv_extract(allocating)selinv_extract!+ planThe plan-based fill is ~85× faster than materialize-and-mask with zero allocation; even the one-shot allocating
selinv_extractis ~2.3× faster while bit-identical.Tests
test/test_extract.jl(wired intoruntests.jl) covers:depermute = true/false,sprandpatterns and obs-styleAᵀApatterns,@allocated == 0).Full suite (including Aqua) passes locally.
Notes
Project.tomlversion bumped0.2.0→0.2.1so GaussianMarkovRandomFields can depend on the new release.🤖 Generated with Claude Code