pub use cudarc::driver::safe::LaunchConfig;
use crate::error::{Error, Result};
use crate::runtime::Device;
use crate::runtime::cuda::CudaDevice;
pub const BLOCK_SIZE: u32 = 256;
#[inline]
pub fn elementwise_launch_config(numel: usize) -> Result<(u32, u32, u32)> {
let grid_size = ((numel as u64) + BLOCK_SIZE as u64 - 1) / BLOCK_SIZE as u64;
let grid_size = grid_size.max(1);
if grid_size > MAX_GRID_DIM_X as u64 {
return Err(Error::InvalidArgument {
arg: "numel",
reason: format!(
"{numel} elements need a 1-D grid of {grid_size} blocks, exceeding the \
CUDA max grid extent of {MAX_GRID_DIM_X}"
),
});
}
Ok((grid_size as u32, 1, 1))
}
#[inline]
#[allow(dead_code)] pub fn reduce_launch_config(numel: usize) -> (u32, u32) {
let block_size = BLOCK_SIZE;
let grid_size = ((numel as u32) + block_size - 1) / block_size;
let grid_size = grid_size.min(1024);
(grid_size, block_size)
}
pub const MAX_GRID_DIM_X: u32 = 2_147_483_647;
pub const MAX_GRID_DIM_YZ: u32 = 65_535;
#[inline]
pub fn reduce_dim_launch_config(outer: usize, inner: usize) -> ((u32, u32, u32), u32) {
let grid_x = (outer.min(MAX_GRID_DIM_X as usize) as u32).max(1);
let grid_y = (inner.min(MAX_GRID_DIM_YZ as usize) as u32).max(1);
let block = BLOCK_SIZE;
((grid_x, grid_y, 1), block)
}
#[inline]
pub fn softmax_launch_config(outer: usize, dim_size: usize) -> (u32, u32, u32) {
let block_size = BLOCK_SIZE.min(dim_size as u32).next_power_of_two();
let block_size = block_size.min(BLOCK_SIZE);
let grid_size = outer as u32;
let shared_mem = 2 * block_size * 4; (grid_size, block_size, shared_mem)
}
#[inline]
#[allow(dead_code)] pub fn softmax_dim_launch_config(outer: usize, inner: usize) -> ((u32, u32, u32), (u32, u32, u32)) {
let total_elements = (outer * inner) as u32;
let grid_x = (total_elements + BLOCK_SIZE - 1) / BLOCK_SIZE;
let grid = (grid_x, 1, 1);
let block = (BLOCK_SIZE, 1, 1);
(grid, block)
}
pub fn check_shared_mem_fits(
device_index: usize,
shared_mem: u32,
operation: &'static str,
context: impl FnOnce() -> String,
) -> Result<()> {
let limit = CudaDevice::new(device_index).profile().shared_mem_per_block;
if shared_mem > limit {
let context = context();
return Err(Error::BackendLimitation {
backend: "cuda",
operation,
reason: format!(
"{context} needs {shared_mem} bytes of shared memory, exceeding this \
device's {limit}-byte per-block limit"
),
});
}
Ok(())
}
#[inline]
pub fn launch_config(
grid: (u32, u32, u32),
block: (u32, u32, u32),
shared_mem: u32,
) -> LaunchConfig {
LaunchConfig {
grid_dim: grid,
block_dim: block,
shared_mem_bytes: shared_mem,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_numel_floors_to_one_block() {
assert_eq!(elementwise_launch_config(0).unwrap(), (1, 1, 1));
}
#[test]
fn one_numel_needs_one_block() {
assert_eq!(elementwise_launch_config(1).unwrap(), (1, 1, 1));
}
#[test]
fn numel_at_block_size_minus_one_needs_one_block() {
assert_eq!(
elementwise_launch_config(BLOCK_SIZE as usize - 1).unwrap(),
(1, 1, 1)
);
}
#[test]
fn numel_at_block_size_needs_one_block() {
assert_eq!(
elementwise_launch_config(BLOCK_SIZE as usize).unwrap(),
(1, 1, 1)
);
}
#[test]
fn numel_at_block_size_plus_one_needs_two_blocks() {
assert_eq!(
elementwise_launch_config(BLOCK_SIZE as usize + 1).unwrap(),
(2, 1, 1)
);
}
#[test]
fn numel_past_1d_grid_capacity_is_rejected_not_truncated() {
let numel = (MAX_GRID_DIM_X as u64) * (BLOCK_SIZE as u64) + 1;
let result = elementwise_launch_config(numel as usize);
assert!(
result.is_err(),
"numel beyond 1-D grid capacity must error, not silently truncate the grid"
);
}
#[test]
fn numel_at_1d_grid_capacity_still_fits() {
let numel = (MAX_GRID_DIM_X as u64) * (BLOCK_SIZE as u64);
let (grid_x, grid_y, grid_z) = elementwise_launch_config(numel as usize).unwrap();
assert_eq!(grid_x, MAX_GRID_DIM_X);
assert_eq!((grid_y, grid_z), (1, 1));
}
#[test]
fn shared_mem_check_accepts_a_typical_request() {
let result = check_shared_mem_fits(0, 0, "matmul", || "128x128 matmul tile".to_string());
assert!(result.is_ok());
}
#[test]
fn shared_mem_check_rejects_an_oversized_request() {
let result =
check_shared_mem_fits(0, u32::MAX, "matmul", || "128x128 matmul tile".to_string());
assert!(result.is_err());
}
#[test]
fn shared_mem_check_error_names_request_limit_and_context() {
let err =
check_shared_mem_fits(0, u32::MAX, "matmul", || "128x128 matmul tile".to_string())
.unwrap_err()
.to_string();
assert!(
err.contains("128x128 matmul tile"),
"missing context: {err}"
);
assert!(
err.contains(&u32::MAX.to_string()),
"missing requested bytes: {err}"
);
assert!(err.contains("byte per-block limit"), "missing limit: {err}");
}
}