Fix MTLN memory leak and per-step heap allocations - #393
Merged
Conversation
- Replace allocatable-function-in-array-constructor in updateQ3Phi with an explicit nested loop. The previous pattern called dotmatrixmul (allocatable result) inside an implied-do array constructor; gfortran does not always free such intermediate allocatables, causing RAM to grow progressively during the time-stepping loop. - Pre-allocate i_prev as a persistent member of mtl_bundle_t. Previously bundle_advanceCurrent allocated and deallocated two local allocatable arrays (i_prev, i_now) on every time step, causing repeated malloc/free overhead. i_prev is now allocated once in initialAllocation; i_now is eliminated by passing this%i directly to updatePhi. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Alberto-o
approved these changes
Apr 29, 2026
Alberto-o
left a comment
Collaborator
There was a problem hiding this comment.
Ok, changes seems sensible, and tests pass. This is expected, since transferImpedance phi term is only needed when there is a pole-residue description of Z_T, for which there are now no tests (due to lack of examples, mainly)
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.
Fixes #392.
Root causes
Two sources of per-time-step dynamic heap allocation were found in the MTLN hot path:
1.
updateQ3Phi— allocatable function inside array constructorupdateQ3Phicalleddotmatrixmul(a function returning anallocatableresult) inside an implied-do array constructor on every time step. gfortran does not always properly release intermediate allocatables produced inside such constructors, causing RAM usage to grow progressively over the simulation.Fix: replaced the array constructor pattern with an explicit nested loop that accumulates directly into
this%q3_phi, eliminating all dynamic allocation from this routine.2.
bundle_advanceCurrent— repeated malloc/free ofi_prev/i_nowbundle_advanceCurrentdeclared two local allocatable arrays (i_prev,i_now) that were automatically allocated and deallocated on every call (every FDTD time step). This contributes both to heap pressure and to the slowdown observed when MTLN is enabled.Fix: added
i_prevas a persistent pre-allocated member ofmtl_bundle_t(allocated once ininitialAllocation).i_nowwas eliminated entirely by passingthis%idirectly toupdatePhi.Testing
All 114 unit tests pass (
./build/bin/fdtd_tests).