pub use cudarc::driver::safe::LaunchConfig;
pub const BLOCK_SIZE: u32 = 256;
#[inline]
pub fn elementwise_launch_config(numel: usize) -> (u32, u32, u32) {
let grid_size = (((numel as u32) + BLOCK_SIZE - 1) / BLOCK_SIZE).max(1);
(grid_size, 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)
}
const MAX_GRID_DIM_X: u32 = 2_147_483_647;
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)
}
#[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,
}
}