rtx-cfd 3D Stage 1 item 2: mg3_vcycle.cu (seven-point red-black half-sweep, residual, coarsest; top/bot neighbour arrays carry the periodic wrap), LevelExport3/export_hierarchy3/vcycle_f32_reference3, DeviceVcycle3 (the K=1 sequence); gate 2 HELD: device = host f32 V-cycle to 4e-7 relative on 96×40×{1,8} (periodic and walls) and 378×62×62; 4.88 ms per V-cycle at 1.45 M cells incl. transfers
CI / Test (ubuntu-latest) (push) Blocked by required conditions
CI / Python Bindings (maturin) (macos-latest) (push) Blocked by required conditions
CI / Python Bindings (maturin) (ubuntu-latest) (push) Blocked by required conditions
CI / WASM Build + Size Check (push) Blocked by required conditions
CI / Distributed Training Tests (push) Blocked by required conditions
CI / Test (macos-latest) (push) Blocked by required conditions
CI / Build (macos-latest) (push) Waiting to run
CI / CI Success (push) Blocked by required conditions
CI / Clippy Check (push) Failing after 4s
CI / Build (ubuntu-latest) (push) Failing after 4s
CI / Build CPU-Only (Explicit) (push) Failing after 4s
Documentation / Build API Documentation (push) Failing after 4s
Documentation / Build User Guide (push) Successful in 4s
CI / Format Check (push) Failing after 17s
Performance Benchmarks / Run Benchmarks (push) Successful in 3m47s

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
Omar Sobh
2026-09-17 11:15:03 -05:00
co-authored by Claude Fable 5.1
parent c13b07b68d
commit 05ed96f383
5 changed files with 696 additions and 0 deletions
@@ -0,0 +1,134 @@
//! 3D Stage 1, gate 2: the device seven-point V-cycle against the host f32
//! reference. `device_vcycle_matches_the_host_reference` is the cheap
//! correctness pin (a small channel with a z-cylinder hole, periodic z);
//! `bench_anchor_size` (ignored) is the 378 × 62 × 62 cylinder channel and
//! prints the ms per V-cycle (recorded, not asserted).
//!
//! `RTX_CUDA_ARCH=sm_120 cargo test --release -p rtx-cfd --features cuda --test three_d_gpu_vcycle_bench -- --nocapture [--ignored]`
#![cfg(feature = "cuda")]
use rtx_cfd::solvers::incompressible::three_d::poisson::device::DeviceVcycle3;
use rtx_cfd::solvers::incompressible::three_d::poisson::{
PoissonProblem3D, export_hierarchy3, vcycle_f32_reference3,
};
use rtx_cfd::solvers::incompressible::{MgSmoother, MultigridParameters};
use std::time::Instant;
/// A channel `2.5 × 0.41 × 0.41` (or a slice of it) on cubic cells with a
/// z-cylinder hole at (0.2, 0.2) r 0.05, an outlet Dirichlet, seeded rhs.
fn channel(nx: usize, ny: usize, nz: usize, periodic_z: bool, seed: u64) -> PoissonProblem3D {
let mut p = PoissonProblem3D::new(nx, ny, nz);
p.periodic_z = periodic_z;
let h = 0.41 / ny as f64;
let dt = 3.24e-4;
let a = dt * h * h / h; // dt · face area / distance on cubic cells
let hole = |i: usize, j: usize| {
let (x, y) = ((i as f64 + 0.5) * h, (j as f64 + 0.5) * h);
(x - 0.2).powi(2) + (y - 0.2).powi(2) < 0.05 * 0.05
};
for k in 0..nz {
for j in 0..ny {
for i in 0..nx {
let idx = p.index(k, j, i);
if hole(i, j) {
p.active[idx] = false;
continue;
}
if i + 1 < nx && !hole(i + 1, j) {
p.ae[idx] = a;
}
if i > 0 && !hole(i - 1, j) {
p.aw[idx] = a;
}
if j + 1 < ny && !hole(i, j + 1) {
p.an[idx] = a;
}
if j > 0 && !hole(i, j - 1) {
p.as_[idx] = a;
}
if k + 1 < nz || periodic_z {
p.at[idx] = a;
}
if k > 0 || periodic_z {
p.ab[idx] = a;
}
if i + 1 == nx {
p.extra_diag[idx] = 2.0 * a;
}
}
}
}
let mut state = seed | 1;
for idx in 0..nx * ny * nz {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
p.rhs[idx] = if p.active[idx] {
1e-3 * ((state >> 11) as f64 / (1u64 << 53) as f64 - 0.5)
} else {
0.0
};
}
p
}
fn params() -> MultigridParameters {
MultigridParameters {
smoother: MgSmoother::RedBlack,
..MultigridParameters::default()
}
}
fn compare(p: &PoissonProblem3D, label: &str) -> (f64, f64) {
let params = params();
let n = p.nx * p.ny * p.nz;
let mut z_ref = vec![0.0; n];
vcycle_f32_reference3(p, &params, &p.rhs, &mut z_ref);
let levels = export_hierarchy3(p, &params);
println!(
" {label}: {} levels, cells per level {:?}",
levels.len(),
levels.iter().map(|l| l.cells.len()).collect::<Vec<_>>()
);
let mut dev = DeviceVcycle3::new(&levels, params.smoother_sweeps.max(1));
let mut z = vec![0.0; n];
dev.apply(&p.rhs, &mut z);
let scale = z_ref.iter().fold(0.0_f64, |m, v| m.max(v.abs()));
let worst = z
.iter()
.zip(&z_ref)
.fold(0.0_f64, |m, (a, b)| m.max((a - b).abs()));
println!(
" {label}: device vs host f32 V-cycle max |Δz| {worst:.3e} on a scale of {scale:.3e}"
);
// Timing (one march, transfers included).
let reps = 20;
dev.apply(&p.rhs, &mut z);
let t0 = Instant::now();
for _ in 0..reps {
dev.apply(&p.rhs, &mut z);
}
let ms = t0.elapsed().as_secs_f64() * 1e3 / reps as f64;
println!(" {label}: {ms:.3} ms per V-cycle including the transfers ({n} cells)");
(worst, scale)
}
#[test]
fn device_vcycle_matches_the_host_reference() {
for (nz, periodic) in [(1usize, false), (8, true), (8, false)] {
let p = channel(96, 40, nz, periodic, 3);
let (worst, scale) = compare(&p, &format!("96×40×{nz} periodic {periodic}"));
assert!(
worst < 1e-4 * scale,
"device V-cycle differs: {worst:.3e} of {scale:.3e}"
);
}
}
#[test]
#[ignore = "the anchor-size bench (378 × 62 × 62): prints ms per V-cycle"]
fn bench_anchor_size() {
let p = channel(378, 62, 62, false, 11);
let (worst, scale) = compare(&p, "378×62×62 walls");
assert!(worst < 1e-4 * scale);
}