- Date
- 2026-11-01
- Simulator
- ngspice-46+, built from our development fork (branch
ver_51) - Context
- The Monte Carlo engine behind ASE-Integrator, our open-source take on an ADE-Assembler-style test bench manager on top of xschem
- Short answer
- Several long-lived ngspice processes in parallel, each looping over a list of seeds it is handed. ngspice never knows it is doing an N-sample Monte Carlo.
Which ngspice
We run an ngspice-46+ build from our own fork, branch ver_51. It reports itself as ngspice-46+. The distribution's ngspice 45.2 can simulate the same decks, but our Monte Carlo refuses to use it. That refusal is on purpose; the next section explains why.
What the fork adds to stock ngspice
- A draw log (
set rnddraws=<file>). This is the essential one. It records every random number drawn while the netlist is read: which parameter, which device, and what value. That lets us see each sample's per-device mismatch rather than only its outputs. Stock 45.2 has nothing like it. Our Monte Carlo refuses 45.2 with a clear message (mc_ngspice_min: "Monte Carlo needs an ngspice that writes the draw log") rather than run blind. - Seeding fixes:
- The startup random stream is seeded from
rndseed, not from the process id. - The draw log is closed properly when the circuit is reset.
- A warning is printed once when
.option seed=silently overridessetseed.
- The startup random stream is seeded from
- Resistor fixes: the resistor model survives ngspice's internal conversion of a resistor to a B-source. Without this, sky130's high-value resistor loses its temperature coefficient.
- Many smaller fixes: case handling of node names,
.savename lookup, the exit status of a batch run, an.opcard with no netlist, and others. Several have been offered upstream.
One process per sample? No.
We do not launch a new ngspice per sample. One ngspice process, which we call a lane, handles a whole range of samples. Inside its .control block it loops over the seeds it has been given. For each sample:
set rnddraws=<sample dir>/draws * where this sample's draw log goes
setseed <seed> * this sample's own, literal seed
reset * re-read the netlist: mismatch is drawn fresh
set temp=<T>
op * write this sample's raw file and measures
echo ASEMC_DONE s=<seed> ... * tell the supervisor this sample is finished
unset rnddraws
destroy all * drop this sample's results from memory
The order matters, and each step is there because a measurement showed it was needed:
setseedhas to come beforereset, because the mismatch values are drawn while the netlist is read.destroy allkeeps memory flat over hundreds of samples.- The
ASEMC_DONEline is how the outside world learns that a sample has finished.
Several lanes in parallel
A small Python worker sits between the GUI and ngspice:
- It splits the N samples into contiguous ranges across L lanes. L is the smallest of the setup's job limit, the number of CPUs and N.
- Lane 1 also runs the nominal sample (sample 0), with mismatch switched off.
- For example, 200 samples on a 20-core machine can be 20 ngspice processes of about 10 samples each, all running at once.
The worker supervises the lanes:
- It reads each lane's log as it is written, and it records a sample (raw file, measures and draw log) the moment that sample's
ASEMC_DONEline appears. - A sample that errors is marked as a simulation failure, and the lane carries on.
- A lane that stops making progress is killed. Its stalled sample is marked as timed out, and a fresh lane restarts from the next sample.
- The Stop button kills every lane. Unfinished samples are left pending, not half-written.
Does ngspice know it is doing Monte Carlo?
No. ngspice has no "run N Monte Carlo samples" command, and we don't need one. Each lane receives a deck whose seeds are written out as a literal list. There is no arithmetic and no counter inside ngspice. It runs the loop it has been handed, without knowing that it is a Monte Carlo or how big the whole run is.
Everything Monte Carlo-specific lives in the worker:
- N, and the seed for each sample.
- The split into lanes, and which sample is which.
- The nominal run.
- Retries, stop and progress.
- The results database the GUI reads (yield, histograms, re-evaluation).
Why it is built this way
- Speed: the netlist is redrawn inside one long-lived process, so there is no start-up cost per sample.
- Reproducibility: every seed is explicit, and every draw is logged. Any single sample can be re-run on its own and give exactly the same numbers.
- Robustness: one bad sample costs one sample, not the run. A stalled lane costs one sample, then the work continues on a fresh process.
- Simplicity: ngspice does what it is good at, simulating one circuit. The bookkeeping lives in ordinary code that can be tested.
