Skip to content

Fix unsafe id()-keyed matrix cache and stale recompute - #64

Open
finsberg wants to merge 3 commits into
dokken/interpolate_func_into_spacefrom
finsberg/interpolate_func_into_space
Open

Fix unsafe id()-keyed matrix cache and stale recompute#64
finsberg wants to merge 3 commits into
dokken/interpolate_func_into_spacefrom
finsberg/interpolate_func_into_space

Conversation

@finsberg

Copy link
Copy Markdown
Member

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.petsc import that petsc_mat=True silently relied on via import order, and the monkey-patched _row_vec/_col_vec attributes on MatrixCSR.

…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
finsberg marked this pull request as ready for review August 21, 2026 13:36
finsberg and others added 2 commits August 21, 2026 13:45
…) 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>
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.

1 participant