Simulation Runner
The SimulationRunner is the high-performance batch simulation engine in
market_model.
Design
#![allow(unused)] fn main() { pub struct SimulationRunner<S: Simulatable> { model: S, config: SimulationConfig, } }
The runner is generic over any Simulatable type. It is monomorphized at
compile time, producing machine code specialized to your model's step()
implementation.
How it works
- Pre-generate all random normals:
n_paths * n_steps * dimvalues from aStdRngwith fixed seed. No RNG calls in the hot loop. - Clone model per path: each parallel path gets its own model instance, seeded RNG, and a slice of the pre-generated random data.
- Step in parallel:
rayondistributes path batches across CPU cores. - Store in SOA layout: outputs are interleaved by
(path, entry), so sequential access strides contiguously through memory.
Output layout
Each path produces n_steps + 1 entries: the initial state plus one per
step. To access:
#![allow(unused)] fn main() { let result = runner.run(); // Entry e of path p: let value = result.get(p, e); // 0 = initial, n_steps = final // All entries for path p: let path_data = result.path(p); // Only the step outputs for path p (no initial): let steps_data = result.path_steps(p); // Final value for path p: let terminal = result.terminal(p); }
Configuration
#![allow(unused)] fn main() { pub struct SimulationConfig { pub n_paths: usize, // number of independent paths pub n_steps: usize, // steps per path pub dt: f64, // step size in years (e.g. 1/252 for daily) pub seed: u64, // reproducibility seed } }
Performance characteristics
- GBM: approximately 5 floating-point operations per step. 100k paths of 252 steps completes in under a second on a modern multi-core CPU.
- Heston: approximately 30 flops per step (2D state, Cholesky, exp). Roughly 6x slower than GBM.
- Jump processes: additional Poisson sampling per step adds ~50% overhead.
- Hawkes: Ogata thinning over the interval; cost depends on the kernel and event intensity.
- Rough OU: cost scales with the number of approximating factors.
Benchmark with: cargo bench -p market_model