use cudarc::driver::LaunchConfig;
const COPY_THREADS: usize = 256;
const MAX_GRID_YZ: usize = 65535;
pub use tract_gpu::utils::{compute_broadcast_strides, reshape_to_rank_2, reshape_to_rank_3};
pub fn cuda_launch_cfg_for_cpy(shape: &[usize]) -> LaunchConfig {
let rank = shape.len();
assert!((1..=6).contains(&rank), "Unsupported rank {rank} for cuda copy launch config");
if rank == 1 {
let block = shape[0].clamp(1, COPY_THREADS);
return LaunchConfig {
grid_dim: (shape[0].div_ceil(block) as _, 1, 1),
block_dim: (block as _, 1, 1),
shared_mem_bytes: 0,
};
}
let inner = shape[rank - 1];
let prev = shape[rank - 2];
let outer: usize = shape[..rank - 2].iter().product();
assert!(
outer <= MAX_GRID_YZ * MAX_GRID_YZ,
"A copy of {shape:?} spans {outer} of the axes beyond its innermost two, over the {} two grid dimensions hold",
MAX_GRID_YZ * MAX_GRID_YZ
);
let (block, rows) = if inner > COPY_THREADS {
(COPY_THREADS, 0)
} else {
let rows = (COPY_THREADS / inner).min(prev);
(inner * rows, rows)
};
LaunchConfig {
grid_dim: (
if rows == 0 { prev as _ } else { prev.div_ceil(rows) as _ },
outer.min(MAX_GRID_YZ) as _,
outer.div_ceil(MAX_GRID_YZ) as _,
),
block_dim: (block as _, 1, 1),
shared_mem_bytes: 0,
}
}