Skip to content

Fix MTLN memory leak and per-step heap allocations - #393

Merged
lmdiazangulo merged 1 commit into
devfrom
copilot/worktree-2026-04-28T15-39-44
Apr 29, 2026
Merged

Fix MTLN memory leak and per-step heap allocations#393
lmdiazangulo merged 1 commit into
devfrom
copilot/worktree-2026-04-28T15-39-44

Conversation

@lmdiazangulo

Copy link
Copy Markdown
Contributor

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 constructor

updateQ3Phi called dotmatrixmul (a function returning an allocatable result) 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 of i_prev/i_now

bundle_advanceCurrent declared 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_prev as a persistent pre-allocated member of mtl_bundle_t (allocated once in initialAllocation). i_now was eliminated entirely by passing this%i directly to updatePhi.

Testing

All 114 unit tests pass (./build/bin/fdtd_tests).

- 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>
@lmdiazangulo lmdiazangulo added MTLN wires AI assisted Mostly created with AI. Needs special review. labels Apr 28, 2026
@lmdiazangulo
lmdiazangulo requested a review from Alberto-o April 28, 2026 16:48

@Alberto-o Alberto-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@lmdiazangulo
lmdiazangulo merged commit b7e47b5 into dev Apr 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI assisted Mostly created with AI. Needs special review. MTLN wires

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants