forked from 1b-agent/fieldcraft-upstream
The collide and stream passes each moved the whole population field through two buffers (304 bytes per cell update). The AA pattern (Bailey et al. 2009; Wittmann et al. 2013) keeps one buffer: even steps collide each cell in place with its directions swapped, odd steps read a cell's incoming populations at its neighbours and write the outgoing ones where the next step reads them, so each thread writes the addresses it read and streaming happens inside the collision (152 bytes). A cell's coordinates are computed once and neighbours wrap with a compare and an add. The per-link wall rules gather their sources before a scatter writes them, so no link overwrites another's post-collision value, and the momentum exchange is taken in the gather of the step whose forces are read back. Options::streaming picks the layout; Auto, what a solve uses, streams in place, which measured faster on every device tried: 1.5x on an RTX 4090, 1.85x on the GB10 and 1.45x on an M4 Max at periodic 128^3. Options::profile times each kernel with timestamp queries for the throughput test, which now runs both layouts. The verification tests run in both layouts, with two new ones comparing fields, mass, forces and heat with the CPU after odd and even step counts across submission boundaries. Part of #562.
27 lines
1.5 KiB
Rust
27 lines
1.5 KiB
Rust
//! The lattice Boltzmann solver on the GPU: [`GpuFlow`] steps a flow that
|
|
//! [`fieldcraft_lbm::Flow`] prepared (grid, links, open cells, initial state) with the compute
|
|
//! shaders of `lbm.wgsl`, which are the CPU scheme's collision, streaming rules, open boundaries
|
|
//! and momentum exchange, and with heat its temperature lattice, written for the device; `fieldcraft_lbm::run` drives it through the
|
|
//! [`Backend`] trait in chunks of steps, so populations stay on the device and only frames,
|
|
//! force histories and the stability check come back.
|
|
//!
|
|
//! Single precision: the CPU scheme is the reference, and this backend is deterministic on its
|
|
//! device and agrees with it within the tolerances its tests state (`tests/verification.rs`).
|
|
//! The device is the one `fieldcraft_gpu`'s adapter policy picks (`FIELDCRAFT_GPU`).
|
|
//!
|
|
//! Sources: the same as `fieldcraft_lbm` (Krüger et al. 2017; Ginzburg et al. 2008; Guo et al.
|
|
//! 2002; Ladd 1994; Mei et al. 2002; Zou & He 1997; Hecht & Harting 2010). No code of any
|
|
//! lattice Boltzmann package was read.
|
|
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
|
#![forbid(unsafe_code)]
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
mod native;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub use native::{GpuFlow, KernelTime, Options, Streaming, register};
|
|
|
|
/// In the browser there is no device backend yet (WebGPU later): registering is a no-op and
|
|
/// `solve.fluid` with `backend: "gpu"` reports that none is available.
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub fn register() {}
|