Files
leleka/crates/cuda-driver/src/experimental_gpu/stream_map.rs
T

122 lines
5.1 KiB
Rust

//! Argument binding for experimental launches: Call adds checked, context-matched pointer and scalar operands
//! (read-only / read-write, with admitted extents) and StreamMap binds the page-table and slot arrays of the KV stream.
use super::{
Admitted, Arg, CTL_INTS, Call, DeviceBuffer, DriverResult, Entry, Error, ExperimentalError, Launch, Result,
StreamMap, combine, ffi, invalid,
};
impl StreamMap<'_, '_> {
/// The first five (or seven, for resolve) pointer arguments of `reset` / `resolve`.
pub(super) fn bind<'g, 'c>(
&self,
c: Call<'g, 'c>,
n_blocks: u64,
n_slots: u64,
with_miss: bool,
) -> Result<Call<'g, 'c>> {
let mut c = c
.rw(&*self.page_table, n_blocks * 4)?
.rw(&*self.slot_block, n_slots * 4)?
.rw(&*self.slot_stamp, n_slots * 4)?
.rw(&*self.slot_ref, n_slots * 4)?
.rw(&*self.ctl, CTL_INTS as u64 * 4)?;
if with_miss {
c = c.rw(&*self.miss_block, n_slots * 4)?.rw(&*self.miss_slot, n_slots * 4)?;
}
Ok(c)
}
}
impl Call<'_, '_> {
pub(super) fn pointer(mut self, buffer: &DeviceBuffer<'_>, min: u64, offset: u64, writes: bool) -> Result<Self> {
let context = self.gpu.context;
if context.poisoned.get() {
return Err(Error::ContextPoisoned.into());
}
if !std::ptr::eq(context, buffer.context) {
return Err(Error::ContextMismatch.into());
}
let handle = buffer.handle.ok_or(Error::ContextPoisoned)?;
let min = usize::try_from(min).or_else(|_| invalid("experimental buffer size exceeds usize"))?;
if min == 0 || buffer.bytes < min {
return invalid("experimental buffer is smaller than the geometry requires");
}
handle.validate_projection_extent(buffer.bytes, 16)?;
self.admitted.push(Admitted { handle, bytes: buffer.bytes, writes });
let at = handle.experimental_offset(offset)?;
self.args.push(ffi::KarArgument::Scalar64(at.qwen_address()));
self.kinds.push(Arg::Ptr);
Ok(self)
}
pub(super) fn ro(self, buffer: &DeviceBuffer<'_>, min: u64) -> Result<Self> {
self.pointer(buffer, min, 0, false)
}
pub(super) fn rw(self, buffer: &DeviceBuffer<'_>, min: u64) -> Result<Self> {
self.pointer(buffer, min, 0, true)
}
pub(super) fn ro_at(self, buffer: &DeviceBuffer<'_>, min: u64, offset: u64) -> Result<Self> {
self.pointer(buffer, min, offset, false)
}
pub(super) fn rw_at(self, buffer: &DeviceBuffer<'_>, min: u64, offset: u64) -> Result<Self> {
self.pointer(buffer, min, offset, true)
}
pub(super) fn null(mut self) -> Self {
self.args.push(ffi::KarArgument::Scalar64(0));
self.kinds.push(Arg::PtrOpt);
self
}
pub(super) fn i32(mut self, value: i32) -> Self {
// The bit pattern of a two's-complement i32 in the low four bytes of the slot.
self.args.push(ffi::KarArgument::Scalar(value as u32));
self.kinds.push(Arg::I32);
self
}
pub(super) fn i64(mut self, value: i64) -> Self {
self.args.push(ffi::KarArgument::Scalar64(value as u64));
self.kinds.push(Arg::I64);
self
}
pub(super) fn finish(self, grid: [u32; 3]) -> Result<Launch> {
let entry: &Entry = &self.gpu.program.entries()[self.entry];
if !self.signature_matches(entry) {
return invalid("experimental launch arguments differ from the entry's fixed signature");
}
self.check_no_aliasing()?;
let function = self.gpu.loaded.functions[self.entry]
.ok_or(ExperimentalError::Invalid("experimental entry is not loaded"))?;
Ok(Launch { function, grid, block: entry.block, args: ffi::KarLevelsArgs::new(&self.args)? })
}
/// The recorded argument kinds equal the entry's fixed signature (an optional pointer takes a null or a buffer).
fn signature_matches(&self, entry: &Entry) -> bool {
entry.args.len() == self.kinds.len()
&& entry.args.iter().zip(&self.kinds).all(|(want, got)| match (want, got) {
(Arg::PtrOpt, Arg::Ptr | Arg::PtrOpt) => true,
(a, b) => a == b,
})
}
/// No written buffer overlaps another admitted buffer.
fn check_no_aliasing(&self) -> Result<()> {
for (i, a) in self.admitted.iter().enumerate() {
for b in &self.admitted[i + 1..] {
// The same allocation admitted twice (attention scratch) is one buffer, not an alias.
if (a.writes || b.writes)
&& !(a.handle == b.handle && a.bytes == b.bytes)
&& a.handle.overlaps(a.bytes, b.handle, b.bytes)
{
return Err(Error::OutputAliasesInput.into());
}
}
}
Ok(())
}
}
/// A primary driver failure plus the outcome of the mandatory context restore.
pub(super) fn failure(primary: Error, restore: DriverResult<()>) -> ExperimentalError {
combine::<()>(Err(primary), restore).expect_err("a primary failure always stays an error").into()
}