"""Companion code for Chapter 15: Parallel Computing. Computational Physics: From Equations to Simulation. FIRST STEPS WITH PYTHON MULTIPROCESSING: independent sums and timing. Run as a standalone script (not by pasting into an interactive notebook): python3 ch15_parallel_reduction.py Requires Python 3 and NumPy; uses the standard-library concurrent.futures module for separate worker PROCESSES. No GPU or cluster is required. Problem: S = sum [sin(x_i) + sqrt(x_i)] for x_i > x_cut, with x_i = i/N, i = 1,...,N. Then h*S approximates the integral from x_cut to 1, h=1/N. Workers receive only index ranges, not huge arrays. Each worker constructs its own x values and returns a partial sum. The parent combines partial sums in the original batch order, allowing a reproducible comparison for a fixed Python/NumPy environment, regardless of worker count. Other languages may use different arithmetic and get slightly different final digits. This code uses NumPy's fast array operations in BOTH serial and parallel routines. It measures multiprocessing, not the interpreter-overhead savings from replacing a slow Python for-loop with NumPy. Edit the settings below. Optional load-imbalance demonstration is OFF. Python processes have separate memory; see the Fortran/C++ examples for unsafe shared-memory accumulation and OpenMP atomic/reduction constructs. Speedups depend on CPU, process startup, operating system, and other activity. """ import math import multiprocessing as mp import os import statistics import time from concurrent.futures import ProcessPoolExecutor import numpy as np # ---- Editable example settings ---- N_TERMS = 50_000_000 REPETITIONS = 5 MAXIMUM_WORKERS = 4 BATCH_SIZE = 250_000 # More work per task than an OpenMP 4096-iteration chunk. X_CUT = 0.5 SHOW_SCHEDULE_DEMO = False def available_processors(): """Number of CPUs available to this process, possibly logical CPUs.""" if hasattr(os, "sched_getaffinity"): return max(1, len(os.sched_getaffinity(0))) return max(1, os.cpu_count() or 1) def make_tasks(n, cut, batch_size): """Small task descriptions, not arrays; each task owns an index range.""" return [(n, cut, start, min(n, start + batch_size - 1)) for start in range(1, n + 1, batch_size)] def batch_sum(task): """Reusable numerical routine: one independent block of the sum. Must be defined at module level so spawned Python workers can import it. """ n, cut, first, last = task h = 1.0 / float(n) x = np.arange(first, last + 1, dtype=np.float64) * h x = x[x > cut] if x.size == 0: return 0.0 return float(np.sum(np.sin(x) + np.sqrt(x), dtype=np.float64)) def serial_sum(tasks): """Optimized serial reference; identical batches and computation as workers.""" return math.fsum(batch_sum(task) for task in tasks) def parallel_sum(pool, tasks): """Dispatch independent batches and combine results in fixed batch order.""" # pool.map yields results in input order, even if workers finish differently. return math.fsum(pool.map(batch_sum, tasks, chunksize=1)) def contiguous_sum(pool, n, cut, workers, batch_size): """Optional: only 'workers' large tasks, each split internally for memory. With a cutoff near one half, many tasks have little or no useful work. """ blocks = [] for b in range(workers): first = (b * n) // workers + 1 last = ((b + 1) * n) // workers blocks.append((n, cut, first, last, batch_size)) return math.fsum(pool.map(contiguous_worker, blocks, chunksize=1)) def contiguous_worker(task): """Evaluate one long range without allocating its full array.""" n, cut, first, last, batch_size = task parts = [] for start in range(first, last + 1, batch_size): parts.append(batch_sum((n, cut, start, min(last, start + batch_size - 1)))) return math.fsum(parts) def time_median(call, repetitions): """Return result and median elapsed wall time of repeated calls.""" elapsed = [] value = None for _ in range(repetitions): t0 = time.perf_counter() value = call() elapsed.append(time.perf_counter() - t0) return value, statistics.median(elapsed) def main(): if not isinstance(N_TERMS, int) or N_TERMS < 1: raise ValueError("N_TERMS must be a positive integer") if not isinstance(REPETITIONS, int) or REPETITIONS < 1: raise ValueError("REPETITIONS must be a positive integer") if not isinstance(MAXIMUM_WORKERS, int) or MAXIMUM_WORKERS < 1: raise ValueError("MAXIMUM_WORKERS must be a positive integer") if not isinstance(BATCH_SIZE, int) or BATCH_SIZE < 1: raise ValueError("BATCH_SIZE must be a positive integer") if not math.isfinite(X_CUT) or not 0.0 <= X_CUT < 1.0: raise ValueError("X_CUT must satisfy 0 <= X_CUT < 1") if N_TERMS > 2**53: raise ValueError("N_TERMS exceeds the exact-integer range of float64") processors = available_processors() max_p = min(MAXIMUM_WORKERS, processors) h = 1.0 / N_TERMS exact = math.cos(X_CUT) - math.cos(1.0) + (2.0 / 3.0) * (1.0 - X_CUT**1.5) tasks = make_tasks(N_TERMS, X_CUT, BATCH_SIZE) print("Chapter 15: first steps with Python multiprocessing") print(f"Number of terms N: {N_TERMS}") print(f"Threshold x_cut: {X_CUT:.3f}") print(f"Timing repetitions: {REPETITIONS}") print(f"Available processors (may include logical CPUs): {processors}") print(f"Maximum workers requested: {MAXIMUM_WORKERS}") print(f"Testing up to this many workers: {max_p}") print(f"Batch size: {BATCH_SIZE} indices ({len(tasks)} independent batches)") print("Workers receive index ranges and generate values locally.") reference = serial_sum(tasks) # Untimed warm-up. reference, serial_median = time_median(lambda: serial_sum(tasks), REPETITIONS) print(f"Serial/vectorized sum S: {reference: .15e}") print(f"Integral h*S: {h * reference: .15e}") print(f"Exact integral: {exact: .15e}") print(f"Quadrature error: {abs(h * reference - exact):.4e}") grid_cut = N_TERMS * X_CUT if abs(grid_cut - round(grid_cut)) <= 1.0e-9: # Leading signed right-endpoint-rule error when cutoff is grid-aligned. leading_error = (0.5 * h * (math.sin(1.0) + 1.0 - math.sin(X_CUT) - math.sqrt(X_CUT))) print(f"Leading right-rule error (signed): {leading_error:.4e}") else: print("Cutoff lies between grid points; a partial cell affects the error.") print(f"Serial/vectorized median: {serial_median:.5f} seconds\n") print(" Workers Relative sum difference Median (s) Speedup") chunked_medians = {} # "spawn" works on Windows, macOS, and Linux when called from a script. # Pool creation/import/startup and teardown are outside timed sections; # the task transfer and result collection are included in timings. context = mp.get_context("spawn") for p in range(1, max_p + 1): try: with ProcessPoolExecutor(max_workers=p, mp_context=context) as pool: value = parallel_sum(pool, tasks) # Warm-up starts the workers. value, elapsed = time_median( lambda: parallel_sum(pool, tasks), REPETITIONS) chunked_medians[p] = elapsed relative = abs(value - reference) / max(float(np.finfo(float).tiny), abs(reference)) print(f"{p:8d} {relative:12.4e} {elapsed:9.5f} " f"{serial_median / elapsed:8.3f}") if relative > 1.0e-10: print("WARNING: parallel and serial sums differ more than expected.") if SHOW_SCHEDULE_DEMO and p == max_p and max_p >= 2: bad_schedule = lambda: contiguous_sum( pool, N_TERMS, X_CUT, max_p, BATCH_SIZE) value_contig = bad_schedule() # Warm-up. value_contig, contig_time = time_median( bad_schedule, REPETITIONS) rel_contig = abs(value_contig - reference) / max( float(np.finfo(float).tiny), abs(reference)) print("\nOPTIONAL: few contiguous tasks versus many smaller batches") print(f"Contiguous blocks ({max_p} tasks) median (s): {contig_time:.5f}") print(f"Many balanced batches median (s): {elapsed:.5f}") print(f"Contiguous sum relative difference: {rel_contig:.4e}") print("Equal index counts can conceal unequal useful work.") except (OSError, RuntimeError) as exc: print(f"Unable to create/use a {p}-worker process pool: {exc}") print("Run this file directly with Python, not inside a notebook cell.") return if SHOW_SCHEDULE_DEMO and max_p < 2: print("Optional scheduling comparison needs at least two processors.") print("\nSpeedup compares multiprocessing with optimized NumPy serial code.") print("Process startup is excluded; sending tasks and receiving sums are included.") print("Worker processes have separate memory: there is no shared accumulator.") print("The order of combining batch sums is fixed across worker counts.") print("Small problems, process overhead, and system load can reduce speedup.") print("A measured speedup above the core count may be timing noise.") if __name__ == "__main__": # Required for portable process spawning. main()