Python host for DRAMsim3.
The public API is Memory: submit, then advance with a typed stop
condition (completion(), cycles(n), until(t), idle(),
credit(n), accept(a, w)), and collect with take_events() — or use
wait() when the host must react at the earliest pending timestamp.
DRAMsim3 itself still runs cycle-accurately in C++.
Use it to drop a timing-accurate DRAM model into a CPU, GPU, or accelerator simulator and read per-request latency, energy, and bandwidth.
The cycle-driven MemoryController API lives on branch
0.3.0 (tag v0.3.0).
Build from source (Python >= 3.8, C++17). pybind11 and CMake are pulled in by the build backend. There are no prebuilt wheels.
git clone --recursive https://github.com/chenshih1/pydramsim3.git
# or: git clone --recursive https://gitee.com/chenshih1/pydramsim3.git
cd pydramsim3
pip install .--recursive fetches the pinned DRAMsim3 submodule. If it is missing,
the build downloads that commit into third_party/ automatically.
Override the tarball with -DPYDRAMSIM3_DRAMSIM3_TARBALL_URL=..., or
disable fetch with -DPYDRAMSIM3_FETCH_DRAMSIM3=OFF.
Sdists are on GitHub Releases:
pip install pydramsim3-0.6.0.tar.gzRelease builds use LTO and link DRAMsim3 statically. CI is Linux; any C++17 + CMake platform should build.
import pydramsim3
mem = pydramsim3.Memory.from_config("DDR4_8Gb_x8_2400")
mem.submit(0x1000, is_write=False, tag=1)
ev = mem.wait()[0] # completions at the next timestamp
print(ev.cycle, ev.latency, ev.tag)
mem.submit(0x2000, is_write=True)
mem.advance(mem.until(mem.current_cycle + 50)) # host deadline
tracker = pydramsim3.LatencyTracker()
mem.advance(mem.idle()) # drain
tracker.add(mem.take_events())
print(tracker.summary())
s = mem.stats() # sums every channel
print(s.bandwidth, "GB/s of", s.peak_bandwidth, "peak,",
s.energy_pj, "pJ")Memory.from_config takes a bundled config stem (list_configs()).
Pass working_dir= to keep dramsim3.json; otherwise stats go to a
temporary directory removed on close() / with / GC.
One time-advance entry point, with typed stop conditions:
mem.advance(mem.completion()) # to the next completion callback
mem.advance(mem.cycles(100)) # exactly 100 clocks
mem.advance(mem.until(10_000)) # a host deadline
mem.advance(mem.idle()) # drain
mem.advance(mem.credit(8)) # until in_flight < 8
mem.advance(mem.accept(a, True)) # until that write would be acceptedadvance returns the cycles executed. A typo in a condition name is an
attribute error and a wrong argument type is a mypy error, so a
mis-specified step fails at the call site rather than silently.
| Call | Returns |
|---|---|
submit(addr, is_write, tag=None) |
Tag, or None if rejected. Check is not None — tag 0 is valid. |
submit_range(addr, count, stride, is_write, tag=None) |
How many bursts were parked (contiguous streams, zero allocation). |
submit_batch(addrs, writes, tags=None) |
How many were accepted (one C++ call for inhomogeneous requests). |
advance(cond, max_cycles=...) |
Cycles executed |
step(cond, max_cycles=...) |
(cycles, events) — advance and collect together |
wait(max_cycles=...) |
Completions at the earliest timestamp (DES react-now) |
take_events() |
Every buffered completion (whole backlog) |
take_events_numpy() |
Same data as numpy columns |
set_tag_quota(tag, n) |
Remaining bursts for a logical request |
stats() |
Bandwidth, peak, utilization, energy, per-channel |
cycles_to_ns / ns_to_cycles |
Clock-domain conversion |
wait returns every completion sharing the earliest timestamp so a DES
host can issue follow-up traffic at that cycle; later events stay
buffered. wait raises RuntimeError if still busy after max_cycles
(default 10 million); the message includes debug_state.
Backpressure. queue_size is DRAMsim3's per-channel
trans_queue_size, not a global cap — each channel has separate read
and write queues. outstanding_cap=Window.HARDWARE is
channels × trans_queue_size + channels. Default frontend_queue=True:
submit parks overflow in C++ until the outstanding window is full.
frontend_queue=False: submit returns None when that address and
direction are not accepted; a later call to a free queue can still succeed.
A write to an address with an in-flight read stays on the frontend until that read completes (DRAMsim3 deadlocks if the write buffer fills while its head matches a pending read). Later writes on the same channel are not HOL-blocked.
Posted writes. DRAMsim3 completes writes one cycle after accept.
busy / in_flight follow those callbacks, not the write buffer.
will_accept(addr, True) is False while that channel's write buffer is
full.
Completion: addr, latency, tag, is_write, cycle
(engine clock after the ClockTick that produced the event).
Memory is meant to be driven live, not just replayed: submit, wake on
completions, decide, submit again — all against the same controller.
mem.submit(0x1000, False, tag=1) # A: load a tile
while mem.busy:
for ev in mem.wait(): # every event at this timestamp
if ev.tag == 1:
mem.submit(0x2000, False, tag=2) # depends on A
elif ev.tag == 2:
mem.submit(0x3000, True, tag=3) # write backwait() returns all completions sharing the earliest timestamp, so a
follow-up request is issued at that cycle. advance(until(deadline))
advances DRAM only as far as the host's next event, and will_accept() /
in_flight are cheap enough to poll per cycle.
To issue a tile of independent requests, use one call instead of N:
accepted = mem.submit_batch(addrs, writes, tags)
# short count -> retry the tail after advance(credit(n))Measured per-call cost (Python round trip): current_cycle 0.24 µs,
in_flight 0.32 µs, will_accept 0.80 µs, advance(cycles(1)) 1.06 µs,
submit 1.2 µs, submit_batch of 64 bursts 3.9 µs (vs 64 µs one by
one). Interactive loops run ~77 ktx/s (wake per completion) and
~0.39 Mtick/s (per-cycle lockstep).
A closed trace is just a batch that may need credit, so it uses the same primitives as a live host:
import numpy as np
addrs = (0x1000 + np.arange(1_000_000) * 64).astype(np.uint64)
writes = np.arange(1_000_000) % 4 == 3
mem.submit_trace(addrs, writes) # earns credit as it goes
mem.advance(mem.idle()) # drain
tracker.add(mem.take_events()) # completions kept, not droppedsubmit_trace returns how many bursts it issued and works with or without
the frontend queue and with any outstanding_cap. gap_cycles= spaces
transactions out (issued one at a time, since a gap belongs between them).
Bundled .ini files cover DDR3/4, HBM, GDDR5/6, LPDDR, HMC:
pydramsim3.list_configs()
mem = pydramsim3.Memory.from_config("HBM2_8Gb_x128")
mem = pydramsim3.Memory("/path/to/custom.ini")s = mem.stats()
s.bandwidth # GB/s, every channel summed
s.peak_bandwidth # pin-rate ceiling
s.utilization # 0..1
s.energy_pj # already converted with tCK
s.per_channel[0]["average_read_latency"]
s.raw # the untouched DRAMsim3 JSON
ch0["read_latency"] # {cycles: count}
mem.raw_stats(refresh=False) # last snapshot; no extra idle energy
mem.stats_json_path # working_dir/dramsim3.jsonMemory(config_file, working_dir=None, *,
frontend_queue=True, outstanding_cap=None)
Memory.from_config(config_name, working_dir=None, *, ...)| Role | |
|---|---|
submit / submit_range / submit_batch |
Issue |
advance + cycles / until / completion / idle / credit / accept |
Move time (returns cycles) |
step |
Advance and take events in one call |
wait / take_events / take_events_numpy |
Collect |
submit_trace |
Static traces |
stats / raw_stats / reset_stats / stats_json_path |
DRAMsim3 statistics |
cycles_to_ns / ns_to_cycles |
Clock domains |
close() / closed |
Drop the engine; delete a default temp working_dir |
Occupancy
| Property | Meaning |
|---|---|
busy / in_flight |
DRAMsim3 outstanding + frontend (posted writes drop on the completion callback, one cycle after accept) |
num_outstanding_reads / num_outstanding_writes |
DRAMsim3 only (excludes frontend) |
queue_size |
Per-channel trans_queue_size |
outstanding_cap |
Admit window ("hw" or an int) |
Also: current_cycle, clock_period, burst_size, burst_cycle,
peak_bandwidth, frontend_size, num_channels, memory_size,
debug_state, unmatched_callbacks, frontend_blocked_writes.
Supports with. Module helpers: configs_dir(), list_configs(),
resolve_config(name).
LatencyTracker.add(completions) records Completion.latency.
Then read_stats / write_stats / all_stats (count, avg, min,
max, p50/p90/p95/p99, percentile, values), plus
num_reads, num_writes, reset(), summary().
pydramsim3._dramsim3 is the private C++ extension behind Memory; it is
not part of the public API and its Python class is _SimEngine.
Hot path in C++: submit, batched ticks, backpressure waits, outstanding tracking, per-transaction latency. Completions export in bulk; the GIL is released on long runs.
DDR4-2400, 100k mixed transactions, one thread
(benchmarks/benchmark.py):
| Path | Throughput |
|---|---|
submit_trace() (batch) |
~138 ktx/s |
submit_batch() chunked |
~119 ktx/s |
submit_trace() + LatencyTracker |
~118 ktx/s |
pip install ".[test]"
pytest tests/
ruff check src/ tests/ examples/ benchmarks/
mypy src/pydramsim3/Example: examples/accelerator_sim.py. DES host pattern used by a real accelerator simulator (cycle heap, one callback per logical request, finite outstanding window): examples/gnn_layer_memory.py.
Integrating with an accelerator simulator (time-advance choice,
outstanding_cap, batch issue, stats and energy pitfalls, measured
costs): docs/accelerator_integration.md.
DES modelling best practices -- the recommended loop, why
outstanding_cap=Window.HARDWARE, when wait() beats take_events(), and a
table of anti-patterns: docs/des_best_practice.md
(runnable: examples/des_best_practice.py,
contract tests: tests/test_des_best_practice.py).
HBM2 access patterns and what actually limits bandwidth:
examples/hbm2_channels.py.
HBM1 vs HBM2 (same map, half the tCK): examples/hbm1_vs_hbm2.py.
Per-burst request granularity and the outstanding window:
examples/burst_granularity.py.
Prefetching (lookahead, in-flight coalescing, window contention):
examples/prefetch.py.
Prefetch depth for a PipeGCN-shaped host: examples/prefetch_depth.py.
Sparse formats (CSR / CSC / COO) and burst waste:
examples/sparse_formats.py.
How to read each CSR array (ptr resident, indices/data streamed):
examples/csr_arrays.py.
PyDRAMsim3 is MIT. Vendored DRAMsim3 is also MIT.