#[stencil_kernel]Expand description
Attribute macro for defining stencil kernels that transpile to CUDA.
This macro generates CUDA C code from Rust stencil kernel functions at compile time. The generated CUDA source is embedded in the binary and can be compiled at runtime using NVRTC.
§Attributes
id(required) - Unique kernel identifiergrid- Grid dimensionality: “1d”, “2d” (default), or “3d”tile_size- Tile/block size (default: 16)tile_width/tile_height- Non-square tile dimensionshalo- Stencil radius / ghost cell width (default: 1)
§Supported Rust Subset
- Primitives:
f32,f64,i32,u32,i64,u64,bool - Slices:
&[T],&mut [T] - Arithmetic:
+,-,*,/,% - Comparisons:
<,>,<=,>=,==,!= - Let bindings:
let x = expr; - If/else:
if cond { a } else { b } - Stencil intrinsics via
GridPos
§Example
ⓘ
use ringkernel_derive::stencil_kernel;
use ringkernel_cuda_codegen::GridPos;
#[stencil_kernel(id = "fdtd", grid = "2d", tile_size = 16, halo = 1)]
fn fdtd(p: &[f32], p_prev: &mut [f32], c2: f32, pos: GridPos) {
let curr = p[pos.idx()];
let lap = pos.north(p) + pos.south(p) + pos.east(p) + pos.west(p) - 4.0 * curr;
p_prev[pos.idx()] = 2.0 * curr - p_prev[pos.idx()] + c2 * lap;
}
// Access generated CUDA source:
assert!(FDTD_CUDA_SOURCE.contains("__global__"));