"""Companion code for Chapter 15: Parallel Computing. Computational Physics: From Equations to Simulation — Alexander Godunov. FIRST STEPS WITH GPU HEAT DIFFUSION (CuPy). Run: python3 ch15_gpu_heat_diffusion.py Requires: Python 3, NumPy and, for the GPU examples, CuPy plus a compatible CUDA-capable NVIDIA GPU and driver. Install the CuPy package that matches your CUDA environment (see https://docs.cupy.dev/en/stable/install.html). No GPU? The independent NumPy CPU calculation and analytical checks still run. This program does not support Apple-silicon GPUs through CuPy CUDA. Physics: u_t = alpha * (u_xx + u_yy) on the periodic unit square. Explicit five-point update, dx=dy=1/n, r=alpha*dt/dx**2 <= 1/4. Every time step reads the OLD field and produces a NEW field. A single cosine mode has both an exact DISCRETE decay and an analytical PDE decay. They are independent correctness and discretization checks. Three comparable computations: 1. Optimized NumPy CPU stencil (the serial CPU reference; NumPy functions may internally use multiple CPU threads). 2. GPU-resident: transfer input once, perform all steps on the GPU, copy the final field once; display compute, transfer and end-to-end times. 3. GPU with repeated transfers: copy the entire field GPU->CPU->GPU after EACH step. This deliberately inefficient mode demonstrates transfer cost. All timings include the same number of steps. GPU work is explicitly synchronized before the stopwatch stops. Initialization and warm-up of GPU kernels are excluded. Each result is checked against the CPU and known solutions; small floating-point differences across devices are expected. No GPU speedup is assumed or manufactured. """ from statistics import median from time import perf_counter import math import numpy as np # ----- Editable example settings ----- N_SIDE = 512 # Try 768 or 1024 to explore larger workloads. TIME_STEPS = 200 REPETITIONS = 3 DIFFUSIVITY = 1.0 R = 0.20 # alpha * dt / dx**2; must be <= 0.25. AMPLITUDE = 0.25 MODE_X = 8 MODE_Y = 6 RUN_GPU = True # False runs only the CPU and numerical checks. RUN_TRANSFER_DEMO = True # GPU->CPU->GPU every step (deliberately slow). def initial_field(n): k = np.arange(n, dtype=np.float64) xx = np.cos(2.0 * np.pi * MODE_X * k / n) yy = np.cos(2.0 * np.pi * MODE_Y * k / n) return 1.0 + AMPLITUDE * yy[:, None] * xx[None, :] def stencil(u, r, xp): """One periodic five-point step, using NumPy or CuPy ('xp'). The returned field is NEW storage, not an in-place update. """ lap = (xp.roll(u, 1, axis=0) + xp.roll(u, -1, axis=0) + xp.roll(u, 1, axis=1) + xp.roll(u, -1, axis=1) - 4.0 * u) return u + r * lap def evolve(u, steps, r, xp): for _ in range(steps): u = stencil(u, r, xp) return u def decay_factors(n, steps, r): discrete_factor = 1.0 - 4.0 * r * ( math.sin(math.pi * MODE_X / n)**2 + math.sin(math.pi * MODE_Y / n)**2) discrete_decay = discrete_factor ** steps dt = r / (DIFFUSIVITY * n**2) time_final = steps * dt pde_decay = math.exp(-4.0 * math.pi**2 * (MODE_X**2 + MODE_Y**2) * DIFFUSIVITY * time_final) return discrete_decay, pde_decay, dt, time_final def check_field(name, final, reference, exact_discrete, exact_pde): max_cpu_difference = float(np.max(np.abs(final - reference))) discrete_error = float(np.max(np.abs(final - exact_discrete))) pde_error = float(np.max(np.abs(final - exact_pde))) mean_drift = abs(float(np.mean(final)) - 1.0) print(f"{name}:") print(f" Max difference from NumPy CPU: {max_cpu_difference:.6e}") print(f" Max error vs exact discrete: {discrete_error:.6e}") print(f" Max error vs analytical PDE: {pde_error:.6e}") print(f" Mean-temperature drift: {mean_drift:.6e}") tolerance = 1e-10 passed = max_cpu_difference < tolerance and discrete_error < tolerance \ and mean_drift < tolerance print(" Validation: " + ("PASS" if passed else "FAIL")) if not passed: raise RuntimeError(name + " failed its numerical validation") def cpu_trial(initial): start = perf_counter() output = evolve(initial.copy(), TIME_STEPS, R, np) return output, perf_counter() - start def gpu_resident_trial(initial, cp): # Include the first host-to-device and last device-to-host transfers. start = perf_counter() device = cp.asarray(initial) cp.cuda.Stream.null.synchronize() after_input = perf_counter() device = evolve(device, TIME_STEPS, R, cp) cp.cuda.Stream.null.synchronize() # GPU execution can be asynchronous. after_compute = perf_counter() output = cp.asnumpy(device) # Also waits for the final transfer. end = perf_counter() return output, (after_input - start, after_compute - after_input, end - after_compute, end - start) def gpu_transfer_trial(initial, cp): # Same GPU stencil as above. Transfer the ENTIRE field each time step. start = perf_counter() device = cp.asarray(initial) for _ in range(TIME_STEPS): device = stencil(device, R, cp) host = cp.asnumpy(device) device = cp.asarray(host) output = cp.asnumpy(device) cp.cuda.Stream.null.synchronize() return output, perf_counter() - start def main(): n = N_SIDE if n < 2 or TIME_STEPS < 1 or REPETITIONS < 1: raise ValueError("N_SIDE >= 2, TIME_STEPS >= 1, REPETITIONS >= 1 required") if not (0 < R <= 0.25 and DIFFUSIVITY > 0): raise ValueError("Require 0 < R <= 0.25 and DIFFUSIVITY > 0") if MODE_X < 1 or MODE_Y < 1 or 2 * max(MODE_X, MODE_Y) >= n: raise ValueError("Choose positive Fourier mode numbers well below n/2") init = initial_field(n) disc, pde, dt, tf = decay_factors(n, TIME_STEPS, R) discrete_field = 1.0 + (init - 1.0) * disc pde_field = 1.0 + (init - 1.0) * pde print("Chapter 15: GPU heat diffusion — Python / CuPy") print(f"Grid: {n} x {n}; steps: {TIME_STEPS}; repetitions: {REPETITIONS}") print(f"alpha={DIFFUSIVITY:g}, r={R:g}, dt={dt:.8e}, final t={tf:.8e}") print(f"Initial field: 1 + {AMPLITUDE} cos(2*pi*{MODE_X}*x) cos(2*pi*{MODE_Y}*y)") print(f"Approximate size of one double-precision field: {8*n*n/2**20:.2f} MiB") print("NumPy CPU reference may use internally threaded library functions.") cpu_times = [] cpu_result = None for _ in range(REPETITIONS): cpu_result, elapsed = cpu_trial(init) cpu_times.append(elapsed) cpu_median = median(cpu_times) predicted = abs(AMPLITUDE) * abs(disc-pde) print(f"\nNumPy CPU median: {cpu_median:.6f} s") print(f"Predicted maximum PDE discretization error: {predicted:.6e}") check_field("NumPy CPU", cpu_result, cpu_result, discrete_field, pde_field) if not RUN_GPU: print("GPU runs disabled in the editable settings.") return try: import cupy as cp gpu_count = cp.cuda.runtime.getDeviceCount() if gpu_count < 1: print("No compatible CUDA GPU detected: CPU validation completed only.") return except (ImportError, RuntimeError, OSError) as exc: print("CuPy or a compatible CUDA GPU is unavailable: CPU validation completed only.") print(f"Reason: {exc}") return name = cp.cuda.runtime.getDeviceProperties(0)["name"] if isinstance(name, bytes): name = name.decode("utf-8", errors="replace") print(f"\nGPU: {name}; CuPy {cp.__version__}") # Warm up CuPy's runtime, allocations and stencil before timing. warmup = cp.asarray(init[:32, :32]) warmup = stencil(warmup, R, cp) cp.cuda.Stream.null.synchronize() _ = cp.asnumpy(warmup) resident_times, input_times, compute_times, output_times = [], [], [], [] gpu_final = None for _ in range(REPETITIONS): gpu_final, (ti, tc, to, total) = gpu_resident_trial(init, cp) input_times.append(ti) compute_times.append(tc) output_times.append(to) resident_times.append(total) check_field("GPU resident", gpu_final, cpu_result, discrete_field, pde_field) print("GPU resident (input once; output once):") print(f" Median input transfer: {median(input_times):.6f} s") print(f" Median GPU computation: {median(compute_times):.6f} s") print(f" Median output transfer: {median(output_times):.6f} s") print(f" Median end-to-end: {median(resident_times):.6f} s") print(f" End-to-end speedup vs NumPy CPU: {cpu_median/median(resident_times):.3f}x") print(" Component medians need not sum to the total median.") if RUN_TRANSFER_DEMO: repeated_times = [] repeated_final = None for _ in range(REPETITIONS): repeated_final, elapsed = gpu_transfer_trial(init, cp) repeated_times.append(elapsed) check_field("GPU transfers every step", repeated_final, cpu_result, discrete_field, pde_field) repeated = median(repeated_times) print("GPU with full-field GPU->CPU->GPU transfer after EVERY step:") print(f" Median end-to-end: {repeated:.6f} s") print(f" Time ratio (transfer-every-step / GPU-resident): {repeated/median(resident_times):.3f}x") print("GPU speedups depend on grid size, device, data transfers and hardware.") if __name__ == "__main__": main()