Fix unsafe id()-keyed matrix cache and stale recompute - #64
Open
finsberg wants to merge 3 commits into
Open
Conversation
…rpolationBlock The interpolation matrix cache was a module-level dict keyed by (id(space_from), id(space_to)) without holding strong references, so a garbage-collected FunctionSpace's id could be reused by an unrelated object and silently return the wrong matrix. Cache the matrix as a per-instance attribute on InterpolationBlock instead, tied to the block's own space references. recompute_component also wrapped a separately cached Function instead of updating the tape's real output object, permanently disconnecting it from the Python object returned by interpolate(). It now mutates block_variable.saved_output in place, matching FunctionAssignBlock's convention. Note this only holds until the output is used as a dependency elsewhere, at which point pyadjoint freezes its own checkpoint copy (an existing, codebase-wide characteristic, not specific to interpolation) - documented inline. Also fixes the mypy failures in this file: the missing `dolfinx.fem.petsc` import that `petsc_mat=True` silently relied on via import order, and the monkey-patched `_row_vec`/`_col_vec` attributes on MatrixCSR. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
finsberg
marked this pull request as ready for review
August 21, 2026 13:36
…) reuse The previous commit removed the shared matrix cache entirely (making it per-instance on InterpolationBlock) to close the id()-collision hole, but that gives up the redundant-assembly/MPI-communicator-exhaustion protection the cache was originally for, since every new block (e.g. each optimization iteration that creates fresh spaces) reassembles its own matrix from scratch. Bring back a shared, module-level cache, but keyed safely this time: each FunctionSpace that contributes to a cache key gets a weakref.finalize callback that purges every entry mentioning its id. Finalizers run at the point the object is actually deallocated, which is necessarily before CPython can hand that id out again, so a cache hit always corresponds to spaces that are still alive - no stale matrix can be served, and entries don't accumulate forever for short-lived spaces. Add regression tests: same-space-pair reuse, cache entries getting purged once their space is garbage collected, and no unbounded growth across many transient spaces. Verified these tests fail against a naive id()-keyed cache without the weakref purge (i.e. they would have caught the original bug). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
attach_working_array() dynamically attached _row_vec/_col_vec onto dolfinx.la.MatrixCSR instances, which mypy can't see, so every access needed typing.cast(Any, ...). Replace it with _MatrixCSRWorkspace, a small class built once alongside the matrix that holds the working vectors as real, statically-typed fields, threaded through get_mult(), _build_interpolation_matrix(), and the cache in place of the bare matrix. No behavior change (the full working arrays are still zeroed before each multiply); no more casts or dynamic attributes. Co-Authored-By: Claude Sonnet 5 <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.
Fix unsafe id()-keyed matrix cache and stale recompute output in InterpolationBlock
The interpolation matrix cache was a module-level dict keyed by (id(space_from), id(space_to)) without holding strong references, so a garbage-collected FunctionSpace's id could be reused by an unrelated object and silently return the wrong matrix. Cache the matrix as a per-instance attribute on InterpolationBlock instead, tied to the block's own space references.
recompute_component also wrapped a separately cached Function instead of updating the tape's real output object, permanently disconnecting it from the Python object returned by interpolate(). It now mutates block_variable.saved_output in place, matching FunctionAssignBlock's convention. Note this only holds until the output is used as a dependency elsewhere, at which point pyadjoint freezes its own checkpoint copy (an existing, codebase-wide characteristic, not specific to interpolation) - documented inline.
Also fixes the mypy failures in this file: the missing
dolfinx.fem.petscimport thatpetsc_mat=Truesilently relied on via import order, and the monkey-patched_row_vec/_col_vecattributes on MatrixCSR.