Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/underworld3/checkpoint/disk_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,7 @@ def _read_swarm_from_sidecar(swarm, sidecar_path: str) -> None:

# Invalidate canonical-data caches so subsequent var.data reads
# re-resolve to the rebuilt PETSc fields.
if hasattr(swarm._particle_coordinates, "_canonical_data"):
swarm._particle_coordinates._canonical_data = None
for var in swarm._vars.values():
if hasattr(var, "_canonical_data"):
var._canonical_data = None
swarm._invalidate_canonical_data()

# Restore counted as a population change (matches in-memory path).
swarm._population_generation += 1
Expand Down
10 changes: 9 additions & 1 deletion src/underworld3/function/_function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,12 @@ def _project_to_work_variable(expr, mesh, smoothing=1e-6):
)
projector.uw_function = flat_source
projector.smoothing = smoothing
# NB: a plain solve (not _force_setup=True) — the cached projector
# refreshes correctly against changed field data on the current cache
# machinery. Forcing a full DM+SNES rebuild every evaluate() re-introduced
# the O(100 MiB) leak guarded by tests/test_0006_memory_leak.py. If a
# genuine cache-staleness recurs, invalidate the cached projector
# targetedly rather than rebuilding on every call (issue #215, Bug 2).
projector.solve(zero_init_guess=False)

# Fan flat result back to the tensor work variable
Expand Down Expand Up @@ -756,7 +762,9 @@ def _project_to_work_variable(expr, mesh, smoothing=1e-6):

projector.uw_function = scalar_expr
projector.smoothing = smoothing
projector.solve(zero_init_guess=False)
# _force_setup=True: rebuild solver state to avoid stale cached
# projector after Stokes/DM modifications (issue #215, Bug 2).
projector.solve(zero_init_guess=False, _force_setup=True)

return work_var

Expand Down
26 changes: 17 additions & 9 deletions src/underworld3/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,12 @@ def _update(self):

return

# TODO(BUG): Stale proxy DM after swarm data write
# _update() marks proxy as stale, but _update_proxy_if_stale() (lazy
# re-interpolation) only fires when material.sym is accessed. Code that
# reads the proxy MeshVariable DM directly (e.g. a Projection solver
# evaluating its uw_function at quadrature points) gets stale data.
# See GitHub issue #215 (Bug 3).
def _update_proxy_if_stale(self):
"""
Actually update the proxy mesh variable if it's marked as stale.
Expand Down Expand Up @@ -2690,6 +2696,15 @@ def _invalidate_canonical_data(self):
for var in list(self._vars.values()):
if hasattr(var, "_canonical_data"):
var._canonical_data = None
# Mark the proxy stale (lazy) so it re-interpolates on next access.
# NB: set the flag directly rather than calling var._update() —
# IndexSwarmVariable._update() is EAGER (_update_proxy_variables),
# so calling it here re-interpolates the proxy on every invalidation
# (i.e. every swarm.access write), an O(100 MiB) leak over a time loop
# (tests/test_0006_memory_leak.py). The proxy still refreshes lazily
# via .sym / _update_proxy_if_stale().
if hasattr(var, "_proxy_stale"):
var._proxy_stale = True

# Invalidate cached spatial index
self._kdtree = None
Expand Down Expand Up @@ -3617,10 +3632,7 @@ def add_particles_with_coordinates(self, coordinatesArray) -> int:
self.dm.migrate(remove_sent_points=True)

# Invalidate cached data — particle count changed after addNPoints + migrate
self._particle_coordinates._canonical_data = None
for var in self._vars.values():
if hasattr(var, "_canonical_data"):
var._canonical_data = None
self._invalidate_canonical_data()

# Informational: addNPoints + direct dm.migrate path doesn't go
# through Swarm.migrate, so bump explicitly.
Expand Down Expand Up @@ -4249,11 +4261,7 @@ def apply_snapshot_payload(self, payload: dict) -> None:

# Invalidate canonical-data caches — the underlying arrays
# have been reallocated by the addNPoints path.
if hasattr(self._particle_coordinates, "_canonical_data"):
self._particle_coordinates._canonical_data = None
for var in self._vars.values():
if hasattr(var, "_canonical_data"):
var._canonical_data = None
self._invalidate_canonical_data()

# The raw PETSc primitives used above (removePoint loop +
# addNPoints + direct field writes) deliberately bypass
Expand Down
Loading
Loading