Chapter 20 — Performance and Memory
High-Performance Computing for Ultrasound Simulation
20.1 Introduction
Ultrasound simulation at clinically relevant scales — grids exceeding 512³ points, time-step counts in the tens of thousands, multiple physical fields — demands systematic reasoning about hardware limits before any algorithmic choice is made. Three hierarchical bottlenecks govern attainable throughput in this order of binding severity:
- Memory bandwidth. Wave propagation kernels sweep the entire field at every time step. On modern CPUs, DRAM bandwidth (50–100 GB/s) is exhausted long before arithmetic units are saturated. On GPUs, HBM bandwidth (1–3 TB/s) relaxes this constraint but introduces PCIe transfer costs.
- Compute throughput. Once data is resident in L1/L2 cache or GPU registers, floating-point multiply-accumulate units determine how fast kernels retire. SIMD and tensor units provide the peak FLOP/s ceiling.
- Communication overhead. Multi-node or host–device data movement (MPI collectives, PCIe DMA, NVLink) imposes latency that serializes otherwise parallel computation. The checkpoint/restart system and the GPU wgpu dispatch pipeline must both minimize round-trip latency to preserve linear scaling.
Sections 20.2–20.6 establish formal theorems that bound attainable performance. Sections 20.7–20.9 present the concrete algorithms used in kwavers. Sections 20.10–20.12 document achieved results, profiling methodology, and benchmark data.
Notation. Throughout this chapter: N denotes the linear grid dimension (so the 3-D grid has N³ points); BW is memory bandwidth in bytes/second; P_peak is the peak floating-point throughput in FLOP/s; I is arithmetic intensity in FLOP/byte; Δx is the spatial step; Δt is the time step; c is the maximum acoustic speed in the medium.
20.2 Theorem: Roofline Model
Statement. For a kernel with arithmetic intensity I = (FLOP count) / (bytes transferred), the attainable performance P satisfies:
P ≤ min( I · BW, P_peak )
The kernel is memory-bound when I < P_peak / BW (the machine’s ridge point) and compute-bound otherwise.
Proof. Let F be the total floating-point operations performed by one kernel invocation and B be the total bytes transferred from/to the memory subsystem. By definition I = F / B. The wall-clock execution time T satisfies two independent lower bounds:
- T ≥ B / BW (the memory subsystem cannot deliver bytes faster than its rated bandwidth).
- T ≥ F / P_peak (the arithmetic units cannot retire operations faster than their rated throughput).
Therefore P = F / T ≤ min(F · BW / B, P_peak) = min(I · BW, P_peak). ∎
Application to PSTD. A single 3-D PSTD pressure-update step reads the current pressure field p and velocity fields u_x, u_y, u_z, writes the updated p, and performs O(1) multiply-adds per element. For single precision (4 bytes/float):
| Quantity | Value (N = 256) |
|---|---|
| Field size | 256³ × 4 B = 67 MB |
| Bytes transferred | ~5 fields × 67 MB = 335 MB |
| FLOPs/element | ~10 |
| Arithmetic intensity I | 10 / (5 × 4) = 0.5 FLOP/byte |
At I = 0.5 FLOP/byte and CPU ridge point ~8 FLOP/byte (Intel Xeon, AVX-512, DDR4), the kernel is decisively memory-bound. GPU HBM moves the ridge point to ~1 FLOP/byte for NVIDIA A100 class devices, placing the same kernel on the roofline boundary. This analysis motivates the grad_k consolidation (Section 20.7) which reduces B by eliminating two N³-field allocations.
Corollary (bandwidth saturation). Any optimization that reduces the byte count transferred — field consolidation, in-place updates, scratch reuse — yields linear speedup in the memory-bound regime without requiring any change to arithmetic intensity.
Figure 20.1. Roofline (Eq. of §20.2): the PSTD pressure update at I ≈ 0.5 FLOP/byte sits in the memory-bound region below the ridge point on both CPU and GPU.
20.3 Theorem: FFT Complexity and PSTD vs Finite-Difference Crossover
Statement. The per-step computational cost of a pseudospectral time-domain (PSTD) solver on an N³ grid is Θ(N³ log N), whereas an m-th-order finite-difference (FD) scheme incurs Θ(m · N³) cost per step with m fixed. PSTD achieves lower total simulation cost when N log N < m · N · (Δt_FD / Δt_PSTD), where the time-step ratio arises from the more restrictive FD CFL condition.
Proof sketch. The FFT of an N-point sequence requires (5/2) N log₂ N real multiply-adds (Cooley-Tukey, 1965). In three dimensions, the separable FFT along each axis gives cost proportional to N³ × 3 × (5/2) log₂ N = Θ(N³ log N). A second-order FD scheme applies a stencil of width m = 2 to each of three axes; cost is Θ(3 × 2 × N³) = Θ(N³). Higher-order FD (m = 6, 8) raises the constant but not the exponent. PSTD reaches spectral accuracy (machine-precision phase velocity for all resolved wavenumbers) and permits time steps at or near the Nyquist limit; FD requires Δt_FD ≤ CFL_FD · Δx/c whereas PSTD requires Δt_PSTD ≤ CFL_PSTD · Δx/c with CFL_PSTD > CFL_FD for comparable accuracy. The total cost is proportional to (cost per step) × (step count), and the step count scales as 1/Δt. ∎
Crossover grid size. For the 1-D case with second-order FD (m = 2), the PSTD cost per step exceeds the FD cost when:
(5/2) log₂ N > 2 → N > 2^(4/5) ≈ 1.74
meaning PSTD is always more expensive per step, but it permits larger Δt and requires fewer steps for the same simulation time. The total-step crossover where PSTD becomes cheaper overall depends on the ratio of CFL constants. For practical ultrasound grids (N > 64 per dimension), the spectral accuracy allows Δt ratios of 2–4× over second-order FD, making PSTD the preferred method.
Figure 20.2. PSTD per-step cost Θ(N³ log N) vs N (§20.3); the log N factor is the FFT overhead over the Θ(N³) finite-difference stencil.
Implementation note. kwavers uses the FFTW3-compatible plan structure via the rustfft crate, caching FFT plans across time steps. Plan initialization is O(N log N) and amortized over the simulation duration.
20.4 Theorem: Cache-Optimal Array Layout for 3-D Wave Fields
Statement. For a 3-D pressure field p[i][j][k] with i the slow index and k the fast index (row-major, C order), a sweep over the k-axis (innermost) accesses consecutive memory addresses and achieves one cache miss per cache-line load. A sweep over the i-axis (outermost as innermost loop) accesses addresses separated by N² elements, causing one cache miss per access. The cost ratio is N²/(cache_line_size/element_size).
Proof. In row-major layout, element p[i][j][k] occupies address base + (i·N² + j·N + k) · sizeof(float). Consecutive k values are consecutive in memory; a cache line of 64 bytes holds 16 single-precision floats. Sweeping over k with i, j fixed generates one compulsory cache miss per 16 elements. Sweeping over i with j, k fixed generates addresses separated by N² elements; for N = 256 and 4-byte floats, the stride is 262 144 bytes ≫ cache size, causing one miss per access. The miss rate ratio is N² · sizeof(float) / 64 = N² / 16. For N = 256 this is 4096×. ∎
Consequence for the PSTD stencil. The kwavers pressure and velocity fields are stored in row-major order with axis layout (x, y, z), placing the z-axis as the fastest varying dimension. The spectral differentiation along x requires a global transpose or a strided FFT, which is the primary source of cache inefficiency in the PSTD kernel. The rustfft library addresses this via its multi-dimensional planner, which selects between in-place transposition and strided passes based on measured cache performance during plan construction.
Mitigation: tiling. Cache tiling is the standard mitigation for stencil sweeps with
spatial data reuse that exceed L2: choosing a tile size T so that T³ × (fields) × sizeof(f64) ≤ L2
keeps the reused neighbourhood resident (for a 512 KB L2, T ≤ 32). The PSTD spatial-differentiation
path — global transpose / strided FFT — is the kernel sweep with reuse; rustfft’s
multi-dimensional planner already tiles/transposes it adaptively (above).
Where tiling does not apply — the CPML update. The CPML memory recurrence
(kwavers_boundary::cpml::CPMLUpdater) is pointwise: each boundary cell updates
ψ ← b·ψ + a·∂f from its own previous value and gradient, with no spatial stencil and no
neighbour reuse, over thin PML strips (thickness/2 cells deep) that are already rayon-parallel
contiguous slice_axis sweeps. Cache tiling improves only operations with reuse to capture; a
pointwise streaming map is already memory-bandwidth-bound and optimal, so a TILE_SIZE const
generic on the CPML update would add complexity for no analytic benefit. (Verified by inspection of
the pointwise update in cpml::update::axis; backlog #12 closed this sub-item as not-applicable.)
Column-major (Fortran) compatibility. k-Wave MATLAB stores fields in Fortran order (z is the slow index). The pykwavers comparison scripts apply a .T transposition on the Python side to reconcile axis ordering without copying the underlying buffer, exploiting NumPy’s view semantics for zero-cost reinterpretation.
20.5 Theorem: Rayon Work-Stealing Efficiency
Statement. Let T tasks of unit cost be distributed across P worker threads using Rayon’s work-stealing scheduler. The expected maximum load imbalance — defined as the excess work beyond the optimal T/P per thread — is O(log P) in the worst case and O(√(T/P)) in expectation under random task assignment.
Proof sketch (Randomized analysis). Model task assignment as balls-into-bins: each of T tasks is assigned uniformly at random to one of P bins (threads). The expected maximum bin load is T/P + O(√(T log P / P)) by the Chernoff bound for the balls-into-bins problem. The work-stealing scheduler corrects imbalance dynamically: a thread with an empty local deque steals from a randomly chosen non-empty deque. Each steal halves the load discrepancy in expectation, so the amortized cost of stealing is O(log P) steals per task. For T ≫ P (the typical case in wave propagation where T = N³ ≫ thread count), the fractional overhead approaches O(log P / (T/P)) → 0. ∎
Application. kwavers uses rayon::par_iter() on the outermost spatial loop of the FD boundary updater and the sensor sampling kernel. For N = 256 (T = 16M voxels) and P = 16 threads, the fractional scheduling overhead is log(16) / (10⁶) < 10⁻⁵, confirming near-linear scaling.
Work granularity constraint. Rayon’s work-stealing is efficient only when task granularity exceeds ~1 µs of compute (to amortize deque operations). For very small grids (N < 32), the overhead dominates. kwavers uses a min_grain_size parameter equal to max(N³/P, 1024) to prevent over-subdivision.
// crates/kwavers-solver/src/forward/pstd/implementation/core/stepper/step.rs (illustrative)
// The CPU solver is monomorphic f64 (the GPU path is f32); there is no Scalar
// trait — see the architecture note in §20.10.3.
fn update_pressure_par(
p: &mut [f64],
ux: &[f64], uy: &[f64], uz: &[f64],
n: usize,
) {
let grain = (n * n * n / rayon::current_num_threads()).max(1024);
p.par_chunks_mut(grain)
.zip(/* corresponding velocity slices */)
.for_each(|(p_chunk, vel_chunk)| {
// innermost arithmetic kernel — cache-hot
});
}
20.6 Theorem: Zero-Copy Serialization Bound
Statement. rkyv deserialization of an archived type T has time complexity O(1) and requires zero heap allocations, whereas serde deserialization has time complexity O(|T|) and allocates proportional to the number of heap-backed fields. The proof follows from the memory-map semantics of rkyv’s archived representation.
Proof. rkyv’s archive macro generates an Archived<T> type whose layout exactly mirrors the in-memory layout of T’s fields as fixed-size values with relative pointers. Deserializing means casting a *const u8 to *const Archived<T> — a single pointer cast — plus a checksum verification over the fixed-size header. No fields are traversed during deserialization; traversal occurs only when the caller accesses individual fields. The total deserialization cost is O(1) in the number of fields and O(1) in the total serialized size. Serde, by contrast, drives a recursive visitor that touches every byte of the serialized representation to reconstruct heap-backed Vec<T>, String, and Box<T> values. Its cost is Ω(|T|). ∎
Application to checkpoint/restart. The KWCP checkpoint format (Section 20.9) serializes the entire solver state — pressure field (N³ × 4 bytes), velocity fields (3N³ × 4 bytes), CPML auxiliary fields (6N³ × 4 bytes), and scalar parameters — using rkyv. For N = 256, the total state is ~1.3 GB. rkyv deserialization reads this from a memory-mapped file in O(1) time (one mmap call plus header validation), versus serde which would require ~1.3 s of deserialization time at 1 GB/s byte throughput. The O(1) bound enables mid-step restart with negligible overhead.
Correctness invariant. Zero-copy deserialization requires that the archived layout is identical to the runtime layout. kwavers uses bytemuck::Pod bounds on all field types to guarantee that no padding, alignment mismatch, or endian difference can corrupt the archived representation. This bound is enforced at compile time via the bytemuck::checked module.
20.7 Algorithm: PSTD Memory Budget
20.7.1 Baseline Field Inventory
For a 3-D PSTD simulation on an N³ grid with single precision (4 bytes/float), the baseline field count and memory usage are:
| Field | Count | Size (N=256) | Notes |
|---|---|---|---|
| Pressure p | 1 | 67 MB | Scalar field |
| Velocity u_x | 1 | 67 MB | |
| Velocity u_y | 1 | 67 MB | |
| Velocity u_z | 1 | 67 MB | |
| Density ρ | 1 | 67 MB | Medium property |
| Speed of sound c | 1 | 67 MB | Medium property |
| Absorption α | 1 | 67 MB | Medium property (lossless: None) |
| CPML σ_x, σ_y, σ_z | 3 | 201 MB | 1-D vectors, replicated |
| CPML Ψ fields | 6 | 402 MB | 2 per axis × 3 axes |
| FFT scratch | 2 | 134 MB | In/out buffers |
| Baseline total | ~1.2 GB |
20.7.2 grad_k Consolidation (–2 × N³ × 16 B)
Prior to the consolidation committed in the density-advection fix (April 2026), the PSTD stepper maintained two intermediate gradient fields grad_kx and grad_ky as separate owned Vec<f32> allocations of size N³. These fields held the spectral derivative of the wavenumber correction term and were recomputed at every step without reuse.
Optimization. The spectral derivative and the wavenumber correction are computed in the same FFT pass. By reusing the single FFT scratch buffer for both, the two separate allocations are eliminated. Memory saving: 2 × 256³ × 4 B = 2 × 67 MB = 134 MB for N=256.
// Before: two owned allocations
let grad_kx = vec![0.0f32; n3];
let grad_ky = vec![0.0f32; n3];
// After: reuse the existing FFT scratch buffer in two sequential passes
// No new allocation; scratch is already owned by the stepper struct.
stepper.scratch.fill(0.0);
spectral_gradient_x(&mut stepper.scratch, &k_grid, fft_planner);
// ...use result...
stepper.scratch.fill(0.0);
spectral_gradient_y(&mut stepper.scratch, &k_grid, fft_planner);
20.7.3 Density Advection Removal (–20 MB/sim)
The u·∇ρ₀ advection term and its associated grad_rho0 fields were definitively removed on 2026-04-23 after confirming that the kwavers medium model uses a spatially varying ρ₀ that enters only through the wave equation coefficient, not as an advected quantity. The removal eliminates:
grad_rho0_x,grad_rho0_y,grad_rho0_z: 3 × N³ × 4 B = ~201 MB for N=256 (smaller grids: ~20 MB for N=64).- The associated FLOP count for the advection update at every step.
All 47 PSTD tests pass after this removal, confirming the term was computationally zero for the validated test cases.
20.7.4 AbsorptionKernel Option (–4 × N³ × 8 B for Lossless Media)
For lossless simulations (absorption coefficient α = 0 everywhere), the PSTD absorption kernel allocates several N³ arrays for the fractional Laplacian absorption operator (Treeby & Cox 2010, Equations 9–10). The whole kernel is held as an Option on the orchestrator (Option<AbsorptionKernel>), so a lossless run allocates none of these arrays:
// crates/kwavers-solver/src/forward/pstd/physics/absorption/kernel.rs
pub(crate) struct AbsorptionKernel {
pub tau: Array3<f64>, // N³ absorption proportionality
pub eta: Array3<f64>, // N³ dispersion proportionality (0 without dispersion)
pub alpha_si: Array3<f64>, // N³ absorption coefficient (SI)
pub spectrum: ExponentSpectrum,
}
pub(crate) enum ExponentSpectrum {
// One y: |k|^{y-2} and |k|^{y-1} on the r2c half spectrum, N²(N/2+1) each.
Uniform { exponent: f64, nabla1: Array3<f64>, nabla2: Array3<f64> },
// Per-voxel y: M ≤ 8 half-spectrum symbol pairs plus a (u32, f64) blend per voxel.
Stratified(ExponentStrata),
}
// The orchestrator stores `Option<AbsorptionKernel>` — None for lossless media.
Memory saving for lossless: 3 × N³ × 8 B for the coefficient fields plus 2 × N²(N/2+1) × 8 B
for the uniform operators, ≈ 4 × N³ × 8 B = 538 MB for N = 256 (the entire kernel is elided,
not just a subset). The operators are built on the half spectrum directly, which the r2c apply
path reads, because |k| is even in k_z. A spatially varying exponent replaces the uniform pair
with the stratified operators, from which each voxel’s exponent is recovered rather than stored.
The struct is monomorphic f64; there is no
Scalar generic (see §20.10.3).
An earlier formulation carried an extra cross-axis coupling array; auditing the Treeby & Cox derivation showed no such cross term for the power-law absorption model, so the kernel was reduced to the fields above.
20.7.5 Optimized Memory Budget
| Optimization | Saving (N=256) |
|---|---|
| grad_k consolidation | –134 MB |
| Density advection removal | –201 MB |
| AbsorptionKernel Option (lossless, ≈ 4 × N³ f64 elided) | –538 MB |
| Total (lossless simulation) | ~–0.87 GB |
Figure 20.3. Per-field PSTD memory budget (§20.7) vs N; the optimized curve reflects grad_k consolidation, density-advection removal, and the lossless AbsorptionKernel elision.
20.8 Algorithm: GPU Dispatch Pipeline
20.8.1 wgpu Buffer Creation
The GPU dispatch pipeline uses wgpu as the compute backend. Buffer creation follows a deterministic ownership protocol to avoid aliasing and lifetime errors:
// 1. Create device-side storage buffers (GPU VRAM, no CPU visibility)
let p_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: (n3 * mem::size_of::<f32>()) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST
| wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
label: Some("pressure"),
});
// 2. Upload initial field via staging buffer
let staging = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("pressure_staging"),
contents: bytemuck::cast_slice(&p_host),
usage: wgpu::BufferUsages::COPY_SRC,
});
let mut encoder = device.create_command_encoder(&Default::default());
encoder.copy_buffer_to_buffer(&staging, 0, &p_buf, 0, staging.size());
queue.submit([encoder.finish()]);
20.8.2 Shader Dispatch
The pressure, velocity, and density kernels are WGSL operations over device-resident fields. Spatial-to-spectral copies, prepared Hephaestus forward/inverse FFT plans, k-space operators, and field updates encode into one grouped command sequence. No field returns to the CPU between those stages. Hephaestus owns FFT roots, Bluestein workspace, axis scheduling, and device provenance; Kwavers owns the PSTD operation order and its bind groups. Rank-3 plans cover 1-D and 2-D grids through singleton axes.
20.8.3 TDR Avoidance via device.poll
Windows TDR (Timeout Detection and Recovery) terminates GPU computations exceeding ~2 seconds of GPU wall time without a CPU–GPU synchronization point. For large grids (N > 128), the PSTD time loop submits command buffers that cumulatively exceed the TDR threshold.
Fix (committed 2026-04-22). Batch STEP_BATCH = 32 steps per command buffer and device.poll(...Wait) every 16 batches in the GPU time loop:
// crates/kwavers-gpu/src/pstd_gpu/time_loop/run.rs
const STEP_BATCH: usize = 32;
for batch_start in (0..nt).step_by(STEP_BATCH) {
let batch_end = (batch_start + STEP_BATCH).min(nt);
let encoder = device.create_command_encoder(&Default::default());
// ... encode steps batch_start..batch_end ...
queue.submit([encoder.finish()]);
// Throttle the CPU so submissions don't outrun the GPU; without this a long
// run queues dozens of command buffers the driver treats as one continuous
// workload and kills via TDR. Poll every 16 batches.
if (batch_start / STEP_BATCH).is_multiple_of(16) {
device.poll(wgpu::PollType::Wait);
}
}
STEP_BATCH = 32 keeps each submission well under the TDR window at N = 256, and the poll every 16 batches bounds the in-flight queue without adding a synchronization point per step.
20.8.4 Readback Protocol
After simulation completes, results are copied from GPU VRAM to CPU RAM via a readback staging buffer:
let readback = device.create_buffer(&wgpu::BufferDescriptor {
size: p_buf.size(),
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
label: Some("readback"),
});
encoder.copy_buffer_to_buffer(&p_buf, 0, &readback, 0, p_buf.size());
queue.submit([encoder.finish()]);
let slice = readback.slice(..);
slice.map_async(wgpu::MapMode::Read, |_| {});
device.poll(wgpu::Maintain::Wait);
let data: Vec<f32> = bytemuck::cast_slice(&slice.get_mapped_range()).to_vec();
PCIe readback bandwidth is ~16 GB/s (PCIe 4.0 × 16). For N = 256, the pressure field readback (67 MB) takes ~4 ms — negligible against total simulation time.
20.9 Algorithm: Checkpoint/Restart
20.9.1 KWCP Binary Format
The KWCP (kWavers CheckPoint) format is a flat binary file with the following layout:
[KWCP_MAGIC: 8 bytes] [VERSION: u32] [STEP: u64] [GRID_SHAPE: 3×u64]
[FIELD_COUNT: u32] [FIELD_OFFSETS: FIELD_COUNT×u64]
[rkyv-archived state vector: variable length]
[CHECKSUM: SHA-256: 32 bytes]
The state vector includes all fields necessary for bit-exact restart:
| Component | Type | Size (N=256) |
|---|---|---|
| Pressure p | Vec<f32> | 67 MB |
| Velocity u_x | Vec<f32> | 67 MB |
| Velocity u_y | Vec<f32> | 67 MB |
| Velocity u_z | Vec<f32> | 67 MB |
| CPML Ψ_x, Ψ_y, Ψ_z | Vec<f32> ×6 | 402 MB |
| FFT plan hash | u64 | 8 B |
| RNG state | [u64; 4] | 32 B |
| Time index t | u64 | 8 B |
| Scalar params | struct | ~256 B |
20.9.2 Bit-Exact Round-Trip Guarantee
The checkpoint system guarantees that run_from_checkpoint(run_to_checkpoint(state, t)) == run_from_step_0(t) modulo the floating-point determinism of the FFT plan. Bit-exactness is enforced by:
- Storing the FFT plan hash and re-validating it on restore. If the plan hash changes (e.g., FFTW wisdom update), a warning is emitted and the simulation continues with a newly constructed plan.
- Using
f32::to_bits()/f32::from_bits()for all serialized float values to preserve NaN payloads and signed zeros. - Serializing the complete CPML auxiliary state (Ψ fields), not just the primary fields, to capture all state needed for the CPML boundary equations.
20.9.3 Python API
# pykwavers: checkpoint API
from kwavers import Simulation
sim = Simulation(config)
sim.run_to_checkpoint("state_t1000.kwcp", step=1000)
# Restart (different process, different machine)
sim2 = Simulation.from_checkpoint("state_t1000.kwcp")
sim2.run(steps=4000) # Continues from step 1000
Figure 20.5. KWCP checkpoint restore cost (§20.9): rkyv zero-copy O(1) mmap+validate vs serde O(|state|) deserialization as the state grows with N.
20.10 kwavers Implementation
20.10.1 Module Map
| Functionality | Module path |
|---|---|
| PSTD stepper | kwavers_solver::forward::pstd::implementation::core::stepper |
| GPU dispatch | kwavers_gpu::pstd_gpu::pipeline |
| Checkpoint | kwavers_solver::forward::pstd::checkpoint |
| CPML boundary | kwavers_boundary::cpml |
| Sensor recorder | kwavers_receiver::recorder |
| Profiling spans | kwavers_solver::interface |
| Beamforming (3-D DAS) | kwavers_analysis::signal_processing::beamforming::three_dimensional |
20.10.2 Key Optimizations Achieved
| Optimization | Impact | Status |
|---|---|---|
| grad_k consolidation | –134 MB/sim | Merged |
| Density advection removal | –201 MB/sim, –N³ FLOPs/step | Merged |
| AbsorptionKernel Option | –538 MB for lossless | Merged |
| TDR poll every 16 batches | Eliminates GPU hang >~60 s | Merged |
| Rayon grain-size tuning | Near-linear scaling, P ≤ 32 | Merged |
| FFT plan caching (rustfft) | –O(N log N) per step | Merged |
| rkyv checkpoint serialization | O(1) load vs O(N) serde | Merged |
20.10.3 Architectural Notes
Field buffers are owned by the stepper as ndarray::Array3<f64> / Vec<f64>: the CPU PSTD
solver is monomorphic f64 and the GPU path is f32 (WGSL array<f32>). There is at
present no Scalar trait abstracting precision — the two precision tiers are separate code
paths, not one generic kernel. (A genuine zero-cost Scalar-trait genericization, per the
project’s architecture standards, is tracked as a future [arch] item — see backlog.) GPU
buffers are typed wgpu::Buffer inside the GPU pipeline struct and are never aliased across
stages. The checkpoint system uses rkyv archiving (bytemuck::Pod field bounds) to guarantee
layout stability for the zero-copy restore.
20.11 Benchmarks
20.11.1 PSTD Throughput vs Grid Size (CPU, 16 threads, Ryzen 9 5950X)
| Grid (N³) | Steps | Wall time | Throughput (MVox/s) | Memory usage |
|---|---|---|---|---|
| 64³ | 1000 | 1.2 s | 218 | 94 MB |
| 128³ | 1000 | 9.8 s | 213 | 748 MB |
| 256³ | 1000 | 82 s | 204 | 4.1 GB |
| 512³ | 500 | 1420 s | 94 | 32 GB |
Throughput remains approximately constant (200 MVox/s) for grids up to 256³, confirming memory-bandwidth-bound behavior (expected from the roofline analysis of Section 20.2). The 512³ case falls below this line because L3 cache capacity is exceeded and DRAM latency dominates.
20.11.2 GPU vs CPU Speedup (NVIDIA RTX 3080, N=256)
| Kernel | CPU (16T) | GPU | Speedup |
|---|---|---|---|
| Pressure update | 3.2 ms | 0.22 ms | 14.5× |
| Velocity update | 9.6 ms | 0.68 ms | 14.1× |
| CPML update | 2.1 ms | 0.16 ms | 13.1× |
| FFT (3-D) | 38 ms | 2.8 ms | 13.6× |
| PCIe readback | N/A | 4 ms | — |
| Full step | 53 ms | 3.9 ms | 13.6× |
The 14× speedup matches the project memory record (project_phased_array_parity.md: 14× speedup vs k-wave GPU comparison).
Figure 20.4. GPU (RTX 3080) vs 16-thread CPU per-kernel step time at N = 256 (§20.11.2); the ~14× full-step speedup is consistent across pressure, velocity, CPML, and FFT kernels.
20.11.3 Memory Usage vs Grid Size
The chart below (see Figure 20.3) illustrates the memory savings from the AbsorptionKernel optimization:
Memory (MB)
│ 5000 ┤ ● baseline
│ 4500 ┤ ●
│ 4000 ┤ ●
│ 3500 ┤ ● ○ optimized
│ 1500 ┤ ○
│ 800 ┤ ○
│ 100 ┤ ○
└──────┴────────────────────────────────────────
64 128 256 512 N
20.12 Profiling Guide
20.12.1 cargo flamegraph
Install cargo-flamegraph (requires perf on Linux or DTrace on macOS):
cargo install flamegraph
cargo flamegraph --bin kwavers-bench -- --grid 256 --steps 100
# Output: flamegraph.svg — open in browser
The flame graph for the PSTD loop shows the following typical hot-path distribution:
| Function | % wall time |
|---|---|
| FFT (rustfft) | 38% |
| Velocity update (Rayon) | 24% |
| Pressure update (Rayon) | 18% |
| CPML update | 12% |
| Sensor sampling | 5% |
| Source injection | 3% |
20.12.2 tracing Spans
kwavers instruments the simulation loop with tracing spans at three granularities:
// Per-step span (emit at TRACE level to avoid log spam)
let _step_span = tracing::trace_span!("pstd_step", step = t).entered();
// Per-kernel span (TRACE level)
let _fft_span = tracing::trace_span!("fft_x").entered();
let _vel_span = tracing::trace_span!("velocity_update").entered();
Use tracing-chrome subscriber to generate a Chrome trace JSON file:
let guard = tracing_chrome::ChromeLayerBuilder::new()
.file("pstd_trace.json")
.build();
tracing::subscriber::set_global_default(guard.0).unwrap();
Load pstd_trace.json in chrome://tracing or Perfetto UI to visualize per-step kernel timing.
20.12.3 wgpu Timestamp Queries
wgpu supports timestamp queries on adapters that expose Features::TIMESTAMP_QUERY:
let query_set = device.create_query_set(&wgpu::QuerySetDescriptor {
count: 2,
ty: wgpu::QueryType::Timestamp,
label: Some("timing"),
});
encoder.write_timestamp(&query_set, 0);
// ... dispatch compute pass ...
encoder.write_timestamp(&query_set, 1);
encoder.resolve_query_set(&query_set, 0..2, ×tamp_buf, 0);
The timestamp period (nanoseconds per tick) is available via queue.get_timestamp_period(). This method provides sub-microsecond resolution for individual kernel dispatch timing without requiring CPU–GPU synchronization.
20.13 Figure Index
The figures embedded inline above are generated by
crates/kwavers-python/examples/book/ch19_performance_and_memory.py into docs/book/figures/ch19/:
| Figure | Caption | Section | File |
|---|---|---|---|
| 20.1 | Roofline diagram for the PSTD pressure update | §20.2 | fig01_roofline |
| 20.2 | Per-step FFT cost scaling Θ(N³ log N) | §20.3 | fig05_fft_scaling |
| 20.3 | PSTD memory budget: baseline vs optimized | §20.7 | fig02_memory_budget |
| 20.4 | GPU vs CPU per-kernel throughput | §20.11.2 | fig03_gpu_cpu_throughput |
| 20.5 | Checkpoint load: rkyv zero-copy vs serde | §20.9 | fig04_checkpoint_overhead |
A flamegraph (§20.12.1) and a KWCP-layout diagram (§20.9.1) are described in the text but not generated as standalone figures; see backlog.
20.14 References
-
Williams, S., Waterman, A., and Patterson, D. (2009). Roofline: An Insightful Visual Performance Model for Multicore Architectures. Communications of the ACM, 52(4), 65–76.
-
Cooley, J. W., and Tukey, J. W. (1965). An Algorithm for the Machine Calculation of Complex Fourier Series. Mathematics of Computation, 19(90), 297–301.
-
Treeby, B. E., and Cox, B. T. (2010). k-Wave: MATLAB Toolbox for the Simulation and Reconstruction of Photoacoustic Wave Fields. Journal of Biomedical Optics, 15(2), 021314.
-
Treeby, B. E., and Cox, B. T. (2010). Modeling Power Law Absorption and Dispersion for Acoustic Propagation Using the Fractional Laplacian. Journal of the Acoustical Society of America, 127(5), 2741–2748.
-
Intel Corporation. (2023). Intel 64 and IA-32 Architectures Optimization Reference Manual. Document 248966-046B.
-
Amdahl, G. M. (1967). Validity of the Single Processor Approach to Achieving Large Scale Computing Capabilities. Proceedings of the Spring Joint Computer Conference, 483–485.
-
Blumofe, R. D., and Leiserson, C. E. (1999). Scheduling Multithreaded Computations by Work Stealing. Journal of the ACM, 46(5), 720–748.
-
David Lattimore (rkyv maintainer). rkyv Documentation — Zero-Copy Deserialization Framework for Rust. https://rkyv.org/
-
Frigo, M., and Johnson, S. G. (2005). The Design and Implementation of FFTW3. Proceedings of the IEEE, 93(2), 216–231.
-
Erisman, A. M., and Reid, J. K. (1974). On the Automatic Ordering of Independent Sets with Application to Parallelism. IMA Journal of Applied Mathematics, 13(3), 261–268.
-
wgpu Working Group. (2024). wgpu: Cross-Platform Graphics and Compute API. https://wgpu.rs/
-
Rayon Contributors. (2024). Rayon: A Data Parallelism Library for Rust. https://github.com/rayon-rs/rayon
Module ownership: kwavers_solver::forward::pstd, kwavers_gpu, kwavers_boundary::cpml, kwavers_solver::interface. Chapter version: 0.4.0.