Repository navigation
One SUNContext per solver object (CVode, IDA, KINSOL) - #135
Open
hubertus65 wants to merge 1 commit into
Open
hubertus65 wants to merge 1 commit into
hubertus65 wants to merge 1 commit into
Conversation
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
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.
Problem
Since the SUNDIALS 6 port,
assimulo/solvers/sundials.pyx,kinsol.pyxand the helpers inlib/sundials_callbacks.pxicallSUNContext_Createwherever a context is needed — ininitialize_cvode/initialize_ida/initialize_kinsol(i.e. at every event), in everyinterpolate,get_local_errors,get_weighted_local_errorsand sensitivity getter, and insidearr2nv()/arr2nv_euclidean()at every array-to-N_Vector conversion — and never callSUNContext_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:
SUNContext(with itsSUNProfiler/SUNLogger) per event and per conversion — small, but unbounded over a long simulation with events;SUNLogger, which reopensSUNLOGGER_*_FILENAMEwith"w+": a log written for debugging (SUNDIALS_LOGGING_LEVEL ≥ 3builds) 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_PushErrHandlerwas called on the new context at every (re)initialization; with one context it must be pushed once.Change (one commit)
cdef void* ctx, created in__cinit__and freed in__dealloc__after the memory block, matrix and linear solver (SUNDIALS ≥ 6; a no-op before).SUNContext_Createbecomescdef 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._err_handler_pushed).SUNContext_Freedeclared insundials_includes.pxd.After the change: 2003 created, 2003 freed on the same script (one per object). Results are unchanged:
pytest testsgives 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_VERSIONcompile-time branches for SUNDIALS < 6 (no context) and 7 (SUNComm).🤖 Generated with Claude Code
https://claude.ai/code/session_01PyrxGeWuwCmAH9LishMW6p