Skip to content

One SUNContext per solver object (CVode, IDA, KINSOL) - #135

Open
hubertus65 wants to merge 1 commit into
modelon-community:masterfrom
hubertus65:sundials-context-leak
Open

hubertus65 wants to merge 1 commit into
modelon-community:masterfrom
hubertus65:sundials-context-leak

Conversation

@hubertus65

Copy link
Copy Markdown

Problem

Since the SUNDIALS 6 port, assimulo/solvers/sundials.pyx, kinsol.pyx and the helpers in lib/sundials_callbacks.pxi call SUNContext_Create wherever a context is needed — in initialize_cvode / initialize_ida / initialize_kinsol (i.e. at every event), in every interpolate, get_local_errors, get_weighted_local_errors and sensitivity getter, and inside arr2nv() / arr2nv_euclidean() at every array-to-N_Vector conversion — and never call SUNContext_Free. Measured with a breakpoint counter on a bouncing ball (179 events), an IDA bouncing ball (149 events) and 2000 CVode objects: 2907 contexts created, 0 freed.

Two consequences:

  1. a leak of a SUNContext (with its SUNProfiler/SUNLogger) per event and per conversion — small, but unbounded over a long simulation with events;
  2. with SUNDIALS ≥ 7 every context created from the environment re-creates the SUNLogger, which reopens SUNLOGGER_*_FILENAME with "w+": a log written for debugging (SUNDIALS_LOGGING_LEVEL ≥ 3 builds) is truncated at every event and every conversion, so only the last segment survives. This is how it was found — the Newton log of a 226-state FMU held 46 of 805 step attempts.

A third, smaller effect: SUNContext_PushErrHandler was called on the new context at every (re)initialization; with one context it must be pushed once.

Change (one commit)

  • Each solver class holds cdef void* ctx, created in __cinit__ and freed in __dealloc__ after the memory block, matrix and linear solver (SUNDIALS ≥ 6; a no-op before).
  • Every local SUNContext_Create becomes cdef SUNDIALS.SUNContext ctx = <SUNDIALS.SUNContext>self.ctx, so the code that follows is unchanged.
  • arr2nv(x, ctx), arr2nv_euclidean(x, ctx), N_VNewEmpty_Euclidean(n, ctx) take the caller's context.
  • The error handler is pushed once per solver object (_err_handler_pushed).
  • SUNContext_Free declared in sundials_includes.pxd.

After the change: 2003 created, 2003 freed on the same script (one per object). Results are unchanged: pytest tests gives the same outcome before and after (323 passed; the 7 failures on the test machine are the SuperLU-less Radau5 build, identical on the base).

Compatible with the SUNDIALS_VERSION compile-time branches for SUNDIALS < 6 (no context) and 7 (SUNComm).

🤖 Generated with Claude Code

https://claude.ai/code/session_01PyrxGeWuwCmAH9LishMW6p

Every initialization of the SUNDIALS memory, every interpolation and error
getter, and every arr2nv() conversion created its own SUNContext and never
freed it: a leak per event and per call, and -- since SUNDIALS 7 -- a
SUNLogger re-created from the environment each time, which reopens the
SUNLOGGER_*_FILENAME files with "w+" and truncates any log written for
debugging at every event. The context is now created once in __cinit__,
freed in __dealloc__ and passed to the vector helpers; the error handler
is pushed onto it once instead of at every (re)initialization. Results
are unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PyrxGeWuwCmAH9LishMW6p
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