mcframework.backends.ThreadBackend#

class mcframework.backends.ThreadBackend[source]#

Bases: object

Thread-based parallel execution backend.

Uses concurrent.futures.ThreadPoolExecutor for parallel execution. Effective when NumPy releases the GIL (most numerical operations).

Parameters:
n_workersint

Number of worker threads to use.

chunks_per_workerint, default 8

Number of work chunks per worker for load balancing.

Examples

>>> backend = ThreadBackend(n_workers=4)
>>> results = backend.run(sim, n_simulations=100000, seed_seq=seed_seq, progress_callback=None)

Methods

run

Run simulations in parallel using threads.

run(sim: MonteCarloSimulation, n_simulations: int, seed_seq: np.random.SeedSequence | None, progress_callback: Callable[[int, int], None] | None, **simulation_kwargs: Any) np.ndarray[source]#

Run simulations in parallel using threads.

Parameters:
simMonteCarloSimulation

The simulation instance to run.

n_simulationsint

Number of simulation draws to perform.

seed_seqSeedSequence or None

Seed sequence for spawning independent RNG streams per chunk.

progress_callbackcallable() or None

Optional callback f(completed, total) for progress reporting.

**simulation_kwargsAny

Additional keyword arguments passed to single_simulation.

Returns:
np.ndarray

Array of simulation results with shape (n_simulations,).

__init__(n_workers: int, chunks_per_worker: int = 8)[source]#
classmethod __new__(*args, **kwargs)#