"""Companion code for Chapter 15: Parallel Computing. Computational Physics: From Equations to Simulation. First steps with process-based Monte Carlo, reproducible batches, and scaling. Run: python3 ch15_parallel_monte_carlo.py Needs Python 3 and NumPy. Run as a .py file, not inside an interactive shell. Estimate pi from random (x,y) in the unit square: x*x+y*y <= 1. Every batch has an independent NumPy SeedSequence child derived from its batch index, not the process ID. Identical batches and an integer reduction ensure serial and parallel hit counts match EXACTLY within this Python version. Random streams and resulting hit counts may differ across languages. The serial baseline also uses NumPy vectorized calculations (not a Python loop). Pool creation and warm-up are excluded from the timed parallel region; process communications and task scheduling during each calculation ARE included. Strong scaling fixes total work; weak scaling fixes work per worker. """ from __future__ import annotations import math import os import statistics import time from concurrent.futures import ProcessPoolExecutor from dataclasses import dataclass import numpy as np # ---- Editable example settings ---- SAMPLES_PER_BATCH = 100_000 STRONG_BATCHES = 240 # 24 million samples WEAK_BATCHES_PER_WORKER = 240 # 24 million samples per worker REPETITIONS = 3 MAX_WORKERS = 4 BASE_SEED = 13579 # Fixed for reproducibility FRESH_SEED = False # True: fresh entropy-derived base seed # Set to True only if you explicitly want a fresh random experiment. @dataclass(frozen=True) class Job: batch_index: int samples: int seed: int def count_batch(job: Job) -> int: """Reusable independent batch. Batch index determines random stream.""" # spawn_key=(batch_index,) is equivalent to the corresponding child # of SeedSequence(seed).spawn(k), without materializing all k children. sequence = np.random.SeedSequence(job.seed, spawn_key=(job.batch_index,)) rng = np.random.default_rng(sequence) x = rng.random(job.samples) y = rng.random(job.samples) return int(np.count_nonzero(x*x + y*y <= 1.0)) def jobs_for(count: int, samples: int, seed: int) -> list[Job]: return [Job(i, samples, seed) for i in range(count)] def serial_count(jobs: list[Job]) -> int: return sum(map(count_batch, jobs)) def parallel_count(pool: ProcessPoolExecutor, jobs: list[Job]) -> int: # map produces results in input order, regardless of worker completion order. # Only compact batch descriptors and integer counts travel between processes. return sum(pool.map(count_batch, jobs, chunksize=1)) def median_time(fn, reps: int) -> float: times = [] for _ in range(reps): start = time.perf_counter() fn() times.append(time.perf_counter() - start) return statistics.median(times) def report(label: str, hits: int, samples: int) -> None: p = hits / samples pi_estimate = 4.0*p se = 4.0*math.sqrt(p*(1.0-p)/samples) z = abs(pi_estimate-math.pi)/se print(f"{label}: hits={hits:,}, pi={pi_estimate:.9f}, " f"SE={se:.6g}, |difference|/SE={z:.3f}") def main() -> None: settings = (SAMPLES_PER_BATCH, STRONG_BATCHES, WEAK_BATCHES_PER_WORKER, REPETITIONS, MAX_WORKERS) if any(not isinstance(v, int) or v < 1 for v in settings): raise ValueError("Samples, batches, repetitions, and workers must be positive integers.") if SAMPLES_PER_BATCH * max(STRONG_BATCHES, WEAK_BATCHES_PER_WORKER*MAX_WORKERS) > 10**12: raise ValueError("Reduce the requested sample count below 10^12 for this example.") seed = int(np.random.SeedSequence().entropy) if FRESH_SEED else BASE_SEED if seed < 0: raise ValueError("The base seed must be nonnegative.") available = os.cpu_count() or 1 max_p = min(available, MAX_WORKERS) strong = jobs_for(STRONG_BATCHES, SAMPLES_PER_BATCH, seed) strong_n = STRONG_BATCHES*SAMPLES_PER_BATCH print("Chapter 15: Python multiprocessing Monte Carlo") print("Quarter-circle estimate of pi; fixed SeedSequence child per batch.") print(f"Seed={seed}; samples/batch={SAMPLES_PER_BATCH:,}; " f"strong batches={STRONG_BATCHES}; weak batches/worker={WEAK_BATCHES_PER_WORKER}") print(f"Logical CPUs detected={available}; workers tested=1..{max_p}; " f"timing repetitions={REPETITIONS}") serial_hits = serial_count(strong) # Warm NumPy and verify the baseline. serial_seconds = median_time(lambda: serial_count(strong), REPETITIONS) report("Strong-scaling serial", serial_hits, strong_n) print(f"Optimized NumPy serial median: {serial_seconds:.5f} s") print("Strong scaling: SAME number of samples with all worker counts") print(" Workers Hits Median (s) Speedup") # One pool per requested worker count. Startup is explicitly excluded. for p in range(1, max_p+1): with ProcessPoolExecutor(max_workers=p) as pool: observed = parallel_count(pool, strong) if observed != serial_hits: raise RuntimeError(f"Strong-scaling hit count mismatch for {p} workers") elapsed = median_time(lambda: parallel_count(pool, strong), REPETITIONS) print(f"{p:7d} {observed:13,d} {elapsed:16.5f} {serial_seconds/elapsed:10.3f}") weak_ref_jobs = jobs_for(WEAK_BATCHES_PER_WORKER, SAMPLES_PER_BATCH, seed) with ProcessPoolExecutor(max_workers=1) as pool: assert parallel_count(pool, weak_ref_jobs) == serial_count(weak_ref_jobs) weak_reference = median_time(lambda: parallel_count(pool, weak_ref_jobs), REPETITIONS) print(f"\nWeak-scaling 1-worker reference: {weak_reference:.5f} s") print("Weak scaling: SAME samples per worker, increasing total work") print(" Workers Samples Pi estimate SE Median (s) Efficiency") for p in range(1, max_p+1): batches = p*WEAK_BATCHES_PER_WORKER jobs = jobs_for(batches, SAMPLES_PER_BATCH, seed) n = batches*SAMPLES_PER_BATCH # Serial reference checks the SAME batches, outside timed region. expected = serial_count(jobs) with ProcessPoolExecutor(max_workers=p) as pool: hits = parallel_count(pool, jobs) if hits != expected: raise RuntimeError(f"Weak-scaling hit count mismatch for {p} workers") elapsed = median_time(lambda: parallel_count(pool, jobs), REPETITIONS) phat = hits/n se = 4*math.sqrt(phat*(1-phat)/n) print(f"{p:7d} {n:12,d} {4*phat:15.9f} {se:10.4g} " f"{elapsed:13.5f} {weak_reference/elapsed:10.3f}") print("\nLarger weak-scaling runs contain earlier batches, so estimates are correlated.") print("Exact integer matches verify parallel bookkeeping, not random-number quality.") print("Worker startup is excluded; dispatch and returning batch counts are included.") print("NumPy vectorization reduces interpreter overhead; it is not itself multicore parallelism.") print("More workers may be slower due to resource limits and scheduling overhead.") if __name__ == "__main__": main()