Repository navigation
perf: coalesce empty DRAM ticks and fix drive() drain accounting - #1
Conversation
Collapse in-flight DRAM ticks with no other pending simulator events into one C++ run_until_progress call, then jump sim time to the cycle that made progress. Completions stay zero-delay events at that cycle. Also reset MemorySystem.drive() completion credits per call so a second drive on the same instance actually drains, and reuse request callback wrappers plus a stored heap key on the DES kernel. Co-authored-by: chenshih <chenshih1@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 5f724aa. Configure here.
| """ | ||
| n = self._mem.tick_until_progress(self._max_tick_burst()) | ||
| if n > 1: | ||
| self.sim._advance_to(self.sim.now + (n - 1) * self.clk.period_ps) |
There was a problem hiding this comment.
Coalescing jumps past run until
High Severity
_max_tick_burst only clips coalesced DRAM ticks to next_time, so _tick can _advance_to a completion cycle far beyond Simulator.run(until=...). That horizon is not a heap event. An in-flight request can push sim.now past until, drop dram.pending to 0, and leave the completion callback unfired until a later run. Incremental run(until=) windows and the documented idle_refresh + run(until=...) path no longer stop at the requested time.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 5f724aa. Configure here.
Cut 0.5.3 from master after PRs #1–#4: - DRAM empty-cycle coalescing and drive() drain accounting - idle-refresh backoff reset, drive(batch=0) hang, cancel accounting - coalescing clipped to Simulator.run(until=) - Pipe+FIFO+Dram composition example, occupancy, and issue-pipe refill Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: chenshih <chenshih1@users.noreply.github.com>


Summary
The DES
Drampath was spending most of its host time on empty per-cycle Python events. This PR ticks those empty in-flight DRAM cycles in C++ when no other simulator event falls in the window, and fixesMemorySystem.drive()so a second call on the same instance actually drains.Changes
run_until_progressticks until a completion, idle, ormax_cycles.Dram._tickuses it when the next pending simulator event is more than one DRAM cycle away, then_advance_tojumpssim.nowto the cycle that made progress. Completions remain zero-delay events at that cycle. Other events are never skipped (now + k*periodmust be strictly beforenext_time).drive()accounting:completed_count_is reset at the start of eachdrive/drive_range. A leftover count from a previous call madeissued - completed_count_negative, skipped the drain loop, and returned with requests still pending._engine/Dramwhen the user callback object is unchanged; store a(time, priority, seq)key on_Eventso heap comparisons do not allocate tuples;Simulator.run()no longer double-peeks.Simulation semantics (timing,
RequestInfo, backpressure, event order vs other components) stay identical. Same-run A/B with coalescing forced off (_max_tick_burst = 1) produced bitwise-identicalRequestInfotuples and the same DRAM cycle counts.sim.processed/event_countsfor DRAM-only runs drop because empty cycles are no longer simulator events.Investigation (what was already tight)
Already in good shape from 0.5.0: C++
run/driveloops, completion batching,send_batchcallback reuse, idle DRAM scheduling nothing, idle-refresh exponential backoff, Pipe retry batching, O(1) cancel, LTO,-march=native, PGO script. RamulatorMemory::tickstats and command scheduling cannot be skipped without touching the submodule. Releasing the GIL around ticks is unsafe while callbacks copypy::object, and would not help a single-threaded DES.Benchmarks (this machine, DDR4-2400R, median of 5)
drive()benchmarks/bench.pyDRAM latency/bandwidth numbers match the expected cycle-accurate tables (DDR4-2400 sequential avg 191.4 cyc, qd=32 BW 13.47 GB/s).examples/accel_sim.pyruns.Testing
ruff check pyramulator/ tests/ benchmarks/ examples/passesruff format --check pyramulator/ tests/ benchmarks/ examples/passesmypy pyramulator/passespytestpasses (191 tests, 97% coverage)pip install -e ".[dev]"from a clean checkout worksNew tests cover coalescing vs a host event in the middle of a DRAM latency,
sim.nowvsdepart_cycle, distinct callbacks,tick_until_progress,_advance_toguards, and twodrive()calls on one instance.Changelog
Yes —
[Unreleased]inCHANGELOG.md.