#![cfg_attr(test, allow(clippy::unwrap_used, reason = "test scope"))]
use crate::KernelResourceBudget;
use core::num::{NonZeroU32, NonZeroUsize};
use themis::GpuTopology;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LaunchShape {
pub grid_blocks: u32,
pub threads_per_block: u32,
}
#[must_use]
pub const fn plan_launch(budget: KernelResourceBudget, work_items: u64) -> LaunchShape {
let threads = budget.threads_per_block();
let blocks = work_items.div_ceil(threads as u64);
LaunchShape {
grid_blocks: if blocks > u32::MAX as u64 {
u32::MAX
} else {
blocks as u32
},
threads_per_block: threads,
}
}
#[must_use]
pub fn resident_blocks(topology: &GpuTopology, budget: KernelResourceBudget) -> Option<u32> {
let units = topology.compute_units()?;
let per_unit = budget
.occupancy_limits(
topology.registers_per_unit().map_or(0, NonZeroU32::get),
topology
.shared_mem_per_unit_bytes()
.map_or(0, NonZeroUsize::get),
topology.max_threads_per_unit().map_or(0, NonZeroU32::get),
)
.blocks_per_unit();
if per_unit == u32::MAX {
return None;
}
let total = (per_unit as u64) * u64::from(units.get());
Some(if total > u32::MAX as u64 {
u32::MAX
} else {
total as u32
})
}
#[must_use]
pub fn plan_persistent_launch(
topology: &GpuTopology,
budget: KernelResourceBudget,
work_items: u64,
) -> Option<LaunchShape> {
let resident = resident_blocks(topology, budget)?;
let covering = plan_launch(budget, work_items);
Some(LaunchShape {
grid_blocks: resident.min(covering.grid_blocks),
threads_per_block: budget.threads_per_block(),
})
}
#[cfg(test)]
mod tests {
use super::*;
use themis::GpuDeviceProperties;
fn ampere_like() -> GpuTopology {
GpuTopology::from_provider(GpuDeviceProperties {
compute_units: NonZeroU32::new(46),
warp_width: NonZeroU32::new(32),
max_threads_per_unit: NonZeroU32::new(1_536),
registers_per_unit: NonZeroU32::new(65_536),
shared_mem_per_unit_bytes: NonZeroUsize::new(102_400),
l2_bytes: NonZeroUsize::new(4 * 1024 * 1024),
memory_tier: themis::MemoryTier::Gddr,
memory_bytes: core::num::NonZeroU64::new(8 * 1024 * 1024 * 1024),
})
}
fn budget() -> KernelResourceBudget {
KernelResourceBudget::new(64, 16 * 1024, 256).unwrap()
}
#[test]
fn covering_grid_is_ceil_division() {
let shape = plan_launch(budget(), 1_000_000);
assert_eq!(shape.grid_blocks, 3_907);
assert_eq!(shape.threads_per_block, 256);
assert_eq!(plan_launch(budget(), 0).grid_blocks, 0);
assert_eq!(plan_launch(budget(), 1).grid_blocks, 1);
assert_eq!(plan_launch(budget(), 256).grid_blocks, 1);
assert_eq!(plan_launch(budget(), 257).grid_blocks, 2);
}
#[test]
fn resident_capacity_is_per_unit_times_units() {
assert_eq!(resident_blocks(&ere_like(), budget()), Some(184));
}
#[test]
fn persistent_launch_is_resident_capped_by_work() {
let shape = plan_persistent_launch(&ere_like(), budget(), 1_000_000).unwrap();
assert_eq!(shape.grid_blocks, 184); assert_eq!(shape.threads_per_block, 256);
let small = plan_persistent_launch(&ere_like(), budget(), 300).unwrap();
assert_eq!(small.grid_blocks, 2);
}
#[test]
fn no_information_topologies_yield_none_not_fabrication() {
let wgpu_like = GpuTopology::from_provider(GpuDeviceProperties {
compute_units: None,
warp_width: NonZeroU32::new(32),
max_threads_per_unit: None,
registers_per_unit: None,
shared_mem_per_unit_bytes: None,
l2_bytes: None,
memory_tier: themis::MemoryTier::Dram,
memory_bytes: None,
});
assert_eq!(resident_blocks(&wgpu_like, budget()), None);
assert!(plan_persistent_launch(&wgpu_like, budget(), 1_000).is_none());
assert_eq!(plan_launch(budget(), 1_000).grid_blocks, 4);
}
#[test]
fn unconstrained_budget_on_known_units_is_still_no_information() {
let topology = ampere_like();
let unbounded = KernelResourceBudget::new(0, 0, 256).unwrap();
assert_eq!(resident_blocks(&topology, unbounded), Some(276));
}
}