ringkernel-wavesim 0.4.0

Interactive 2D wave propagation showcase for RingKernel
Documentation
# WaveSim Performance Evaluation

## System Under Test

- **Application**: RingKernel WaveSim - 2D Acoustic Wave Propagation Simulator
- **Physics**: Finite-Difference Time-Domain (FDTD) wave equation solver
- **Test System**: Linux, RTX Ada Generation GPU (CUDA/WGPU available)
- **Build**: Release mode (`--release`)

## Executive Summary

| Metric | Before Optimization | After Optimization | Improvement |
|--------|---------------------|-------------------|-------------|
| Maximum stable grid (CPU) | 1024x1024 | 1024x1024 | - |
| Peak throughput (256x256) | ~17 million cell-steps/sec | ~2.3 billion cell-steps/sec | **135x** |
| GPU tile grid (256x256) | 843 steps/sec | 112,837 steps/sec | **134x** |
| GPU vs CPU (512x512) | N/A | **9.9x faster (GPU)** | GPU wins |
| Actor-based grid (256x256) | Crashes (65K actors) | 1,357 steps/sec (256 tiles) | **Stable** |

---

## Part 1: CPU SimulationGrid Performance

The optimized CPU SimulationGrid (SoA + SIMD + Rayon) delivers excellent throughput:

### Throughput Analysis (1000 steps)

| Grid Size | Cells | Time (ms) | Steps/sec | Throughput (M cells/s) |
|-----------|-------|-----------|-----------|------------------------|
| 32x32 | 1,024 | 0.80 | 1,246,041 | **1,276** |
| 64x64 | 4,096 | 2.30 | 434,932 | **1,782** |
| 128x128 | 16,384 | 7.99 | 125,181 | **2,051** |
| 256x256 | 65,536 | 28.23 | 35,418 | **2,321** |
| 512x512 | 262,144 | 153.48 | 6,515 | **1,708** |

**Key Finding**: Peak throughput of **2.3 billion cell-steps/sec** at 256x256. Performance scales well up to 256x256, then memory bandwidth becomes limiting at 512x512.

### Optimizations Applied

1. **SoA Memory Layout** - Replaced HashMap with contiguous Vec arrays for cache-friendly access
2. **SIMD Intrinsics** - Using std::simd (nightly) with f32x8 for 8-wide AVX2 vectorization
3. **Rayon Parallel Processing** - Multi-threaded row processing for large grids (≥512)

---

## Part 2: TileKernelGrid CPU Performance (Actor Model)

The TileKernelGrid uses 16x16 tile actors with K2K messaging for halo exchange, showcasing RingKernel's actor paradigm:

### Tile Actor Throughput (100 steps)

| Grid Size | Tiles | Time (ms) | Steps/sec | K2K Messages | Msgs/step |
|-----------|-------|-----------|-----------|--------------|-----------|
| 32x32 | 4 | 0.60 | 167,816 | 800 | 8 |
| 64x64 | 16 | 3.80 | 26,284 | 4,800 | 48 |
| 128x128 | 64 | 16.37 | 6,107 | 22,400 | 224 |
| 256x256 | 256 | 73.67 | 1,357 | 96,000 | 960 |

**Key Findings**:
- **Scalability**: 256x256 grid now works with 256 tile actors (vs 65,536 per-cell actors that would crash)
- **K2K Overhead**: Each tile exchanges halos with 4 neighbors, yielding 4×tiles messages/step
- **Actor Showcase**: Demonstrates RingKernel's K2K messaging working correctly at scale

---

## Part 3: GPU Performance - CUDA Packed Backend

The CUDA Packed Backend represents our most optimized GPU implementation, featuring **GPU-only halo exchange** with zero host transfers during simulation.

### Architecture

```
Traditional GPU Approach (slow):
  GPU → extract halo → Host → K2K broker → Host → inject halo → GPU
  Result: Thousands of small host transfers per step

CUDA Packed Approach (fast):
  [All tiles packed in single GPU buffer]
  GPU kernel 1: Exchange all halos (GPU-to-GPU copies)
  GPU kernel 2: Compute FDTD for all tiles
  Result: Zero host transfers, only 2 kernel launches per step
```

### Performance Results (10,000 steps)

| Grid Size | Tiles | Halo Copies | Time (ms) | Steps/sec | Throughput (M cells/s) |
|-----------|-------|-------------|-----------|-----------|------------------------|
| 32x32 | 4 | 128 | 66.73 | 149,864 | 153.5 |
| 64x64 | 16 | 768 | 67.62 | 147,880 | 605.7 |
| 128x128 | 64 | 3,584 | 70.45 | 141,953 | 2,325.8 |
| 256x256 | 256 | 15,360 | 88.62 | **112,837** | **7,394.9** |
| 512x512 | 1,024 | 63,488 | 140.21 | **71,324** | **18,697.1** |

### GPU vs CPU Comparison

| Grid Size | CPU Steps/sec | CUDA Packed Steps/sec | Speedup |
|-----------|---------------|----------------------|---------|
| 128x128 | 129,818 | 141,953 | **1.1x (GPU)** |
| 256x256 | 35,932 | 112,837 | **3.1x (GPU)** |
| 512x512 | 7,229 | 71,324 | **9.9x (GPU)** |

**Key Finding**: The CUDA Packed backend achieves **9.9x speedup** over the highly-optimized CPU implementation at 512x512, with throughput of **18.7 million cells/second**.

### Why GPU Wins at Scale

1. **Zero Host Transfers**: All halo exchange happens via GPU memory copies
2. **Batched Execution**: Single kernel launch processes all 1,024 tiles in parallel
3. **Memory Efficiency**: Packed buffer layout maximizes memory bandwidth utilization
4. **Kernel Launch Amortization**: Only 2-3 kernel launches per step regardless of tile count

### Correctness Verification

The CUDA Packed backend produces **bit-identical results** to the CPU TileKernelGrid:

```
Step 100: CPU center=0.031000, CUDA center=0.031000, max_diff=0.000000
Energy diff: 0.000001 (0.00%)
✓ Results match within floating-point tolerance
```

---

## Part 4: Legacy GPU Performance (WGPU - For Reference)

The original WGPU implementation used per-tile host transfers, resulting in poor performance:

| Grid Size | Tiles | Legacy WGPU Steps/sec | CUDA Packed Steps/sec | Improvement |
|-----------|-------|----------------------|----------------------|-------------|
| 64x64 | 16 | 891 | 147,880 | **166x** |
| 128x128 | 64 | 201 | 141,953 | **706x** |

The legacy approach was bottlenecked by:
- Per-tile upload/download cycles
- Synchronous host-GPU transfers for halo exchange
- Thousands of small memory operations per step

---

## Part 5: Backend Comparison Summary

| Backend | 256x256 Steps/sec | 512x512 Steps/sec | Best Use Case |
|---------|-------------------|-------------------|---------------|
| CPU SimulationGrid | 35,932 | 7,229 | Small grids, portability |
| CPU TileKernelGrid | 1,357 | - | Actor model demonstration |
| CUDA Packed | **112,837** | **71,324** | Production, large grids |
| WGPU Legacy | ~800 | - | Cross-platform fallback |

### Recommendations

| Use Case | Recommended Backend | Expected Performance |
|----------|---------------------|---------------------|
| Large simulations (≥256x256) | CUDA Packed | 70K-113K steps/sec |
| Small grids (<128x128) | CPU SimulationGrid | 125K-1.2M steps/sec |
| Cross-platform deployment | CPU SimulationGrid | Best portability |
| Actor model showcase | CPU TileKernelGrid | Demonstrates K2K messaging |
| Maximum throughput | CUDA Packed @ 512x512 | 18.7M cells/sec |

---

## Part 6: Memory Layout

### CUDA Packed Backend

All tiles are stored in a single contiguous GPU buffer:

```
GPU Memory: [Tile(0,0)][Tile(1,0)][Tile(0,1)]...[Tile(n,m)]

Each tile: 18×18 floats = 324 floats = 1,296 bytes
  - 16×16 interior cells
  - 1-cell halo border on all sides

Total for 512×512 grid (1,024 tiles):
  - Pressure buffers: 2 × 1,024 × 1,296 = 2.65 MB
  - Halo copy indices: 63,488 × 8 = 0.5 MB
  - Output buffer: 512 × 512 × 4 = 1 MB
  - Total GPU memory: ~4.2 MB
```

### Halo Exchange Pattern

```
Per step, per tile pair:
  - 16 floats copied per shared edge
  - 4 bytes per float = 64 bytes per edge

512×512 grid (1,024 tiles):
  - 63,488 halo copies per step
  - ~254 KB of GPU-to-GPU memory copies
  - Zero CPU involvement
```

---

## Part 7: Architecture Evolution

### Phase 1: Per-Cell Actors (KernelGrid) - Deprecated
```
256×256 grid = 65,536 actors → CRASH
- Each cell is a separate actor
- O(n²) actors, massive overhead
```

### Phase 2: Tile Actors (TileKernelGrid)
```
256×256 grid = 256 actors → STABLE
- 16×16 tiles as actors
- K2K messaging for halos
- CPU or legacy GPU compute
```

### Phase 3: GPU-Only Halo Exchange (CUDA Packed)
```
512×512 grid = 71,324 steps/sec
- All tiles in single GPU buffer
- Halo exchange via GPU kernels
- Zero host transfers during simulation
- 9.9x faster than optimized CPU
```

---

## Part 8: Educational Simulation Modes

WaveSim includes animated educational modes that visually demonstrate the evolution of parallel computing paradigms. These modes are designed for teaching and presentations.

### Available Modes

| Mode | Era | Visualization | Lesson |
|------|-----|---------------|--------|
| **Standard** | Modern | Full-speed parallel | Production simulation |
| **CellByCell** | 1950s | Green cells animate sequentially | Sequential computing, one operation at a time |
| **RowByRow** | 1970s | Yellow row highlight sweeps down | Vector processors and SIMD (Cray-style) |
| **ChaoticParallel** | 1990s | Random cells flash green | Parallel without coordination = race conditions |
| **SynchronizedParallel** | 2000s | All cells compute, then pause | Barriers work but create bottlenecks |
| **ActorBased** | Modern | Cyan tiles with borders | Actors + HLC = parallelism without chaos |

### Visual Indicators

- **Green overlay**: Cell currently being processed
- **Yellow row**: Active row (RowByRow mode)
- **Cyan tile with cyan border**: Active tile (ActorBased mode)

### Configuration

```rust
use ringkernel_wavesim::simulation::{SimulationMode, EducationalProcessor};

let mut processor = EducationalProcessor::new(SimulationMode::ActorBased);
processor.cells_per_frame = 32;  // Cells processed per animation frame
processor.rows_per_frame = 2;    // Rows per frame (RowByRow mode)
processor.tiles_per_frame = 4;   // Tiles per frame (ActorBased mode)
```

These modes are accessible from the WaveSim GUI via the "Simulation Mode" dropdown.

---

## Conclusion

WaveSim now provides three production-ready backends with clear performance characteristics:

1. **CPU SimulationGrid**: Excellent for small-to-medium grids, highly portable
2. **CPU TileKernelGrid**: Demonstrates RingKernel's actor model with K2K messaging
3. **CUDA Packed**: Best performance for large grids, 9.9x faster than CPU at 512×512

Additionally, the **Educational Modes** provide visual demonstrations of parallel computing evolution for teaching and presentations.

The CUDA Packed backend's GPU-only halo exchange design eliminates the traditional bottleneck of host-GPU transfers, achieving throughput of **18.7 million cells/second** at 512×512. This demonstrates that GPU acceleration for tile-based simulations requires careful attention to memory transfer patterns—keeping data GPU-resident and using batched kernel launches is essential for competitive performance.

### Benchmark Commands

```bash
# CPU benchmarks
cargo run -p ringkernel-wavesim --bin full_benchmark --release

# CUDA Packed benchmark (requires NVIDIA GPU)
cargo run -p ringkernel-wavesim --bin bench_packed --release --features cuda

# Verify CUDA correctness
cargo run -p ringkernel-wavesim --bin verify_packed --release --features cuda
```