You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.0 KiB
30 lines
1.0 KiB
"""Run independent Monte-Carlo trials across CPU cores. |
|
|
|
Every trial gets its own random stream spawned from one seed |
|
(numpy SeedSequence), so results are identical for any --jobs value and runs |
|
are reproducible. Import this module before numpy in a script so the BLAS |
|
thread pools stay at one thread per worker process. |
|
""" |
|
import os |
|
for _v in ("OMP_NUM_THREADS", "OPENBLAS_NUM_THREADS", "MKL_NUM_THREADS"): |
|
os.environ.setdefault(_v, "1") |
|
|
|
from concurrent.futures import ProcessPoolExecutor |
|
import numpy as np |
|
|
|
|
|
def default_jobs(): |
|
return os.cpu_count() or 1 |
|
|
|
|
|
def run_trials(fn, n, seed, jobs=1): |
|
"""fn(rng) -> result, called once per trial; returns results in trial order.""" |
|
seqs = np.random.SeedSequence(seed).spawn(n) |
|
if jobs <= 1 or n <= 1: |
|
return [fn(np.random.default_rng(s)) for s in seqs] |
|
with ProcessPoolExecutor(min(jobs, n)) as ex: |
|
return list(ex.map(_call, [fn] * n, seqs, chunksize=max(1, n // (4 * jobs)))) |
|
|
|
|
|
def _call(fn, seq): |
|
return fn(np.random.default_rng(seq))
|
|
|