use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::IntervalOperand;
use crate::ops::impl_generic::interval::{IntervalBinary, LaneOperand, lane_operand};
use crate::runtime::cuda::kernels::{IntervalArg, MAX_DIMS, launch_interval, launch_strided_copy};
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::tensor::Tensor;
type CudaTensor = Tensor<CudaRuntime>;
pub(super) fn suffix(dtype: DType) -> &'static str {
if dtype == DType::F32 { "f32" } else { "f64" }
}
pub(super) fn lanes(count: usize, op: &'static str) -> Result<u32> {
u32::try_from(count).map_err(|_| Error::InvalidArgument {
arg: "shape",
reason: format!("CUDA {op} lane count {count} exceeds u32::MAX. Split the batch."),
})
}
fn materialize(client: &CudaClient, view: &CudaTensor) -> Result<CudaTensor> {
if view.ndim() > MAX_DIMS {
return Err(Error::InvalidArgument {
arg: "operand",
reason: format!(
"CUDA lane operand rank {} exceeds {MAX_DIMS}. Collapse compatible dimensions.",
view.ndim()
),
});
}
let out = CudaTensor::empty(view.shape(), view.dtype(), &client.device)?;
let elem_size = view.dtype().size_in_bytes();
unsafe {
launch_strided_copy(
&client.context,
&client.stream,
client.device.index,
view.storage().ptr(),
out.ptr(),
view.shape(),
view.strides(),
view.numel(),
view.ndim(),
elem_size,
view.offset() * elem_size,
)?;
}
Ok(out)
}
pub(super) fn cuda_operand<'a>(
client: &CudaClient,
value: &'a CudaTensor,
shape: &[usize],
) -> Result<LaneOperand<'a, CudaRuntime>> {
lane_operand(value, shape, |view| materialize(client, view))
}
pub(super) fn operand_args(operand: &LaneOperand<'_, CudaRuntime>) -> [IntervalArg; 2] {
[
IntervalArg::Ptr(operand.addr()),
IntervalArg::U32(operand.step),
]
}
pub(super) fn launch(
client: &CudaClient,
name: &str,
args: &[IntervalArg],
count: usize,
) -> Result<()> {
unsafe {
launch_interval(
&client.context,
&client.stream,
client.device.index,
name,
args,
count,
)
}
}
pub(super) fn flag_interval(
client: &CudaClient,
dtype: DType,
value: IntervalOperand<'_, CudaRuntime>,
positive: bool,
status: &CudaTensor,
) -> Result<()> {
let count = value.lower.numel();
if count == 0 {
return Ok(());
}
let n = lanes(count, "interval_flag")?;
let lower = cuda_operand(client, value.lower, value.lower.shape())?;
let upper = cuda_operand(client, value.upper, value.upper.shape())?;
let [lp, ls] = operand_args(&lower);
let [up, us] = operand_args(&upper);
let args = [
lp,
ls,
up,
us,
IntervalArg::I32(i32::from(positive)),
IntervalArg::Ptr(status.ptr()),
IntervalArg::U32(n),
];
launch(
client,
&format!("interval_flag_{}", suffix(dtype)),
&args,
count,
)
}
pub(super) fn input_flags(
client: &CudaClient,
op: IntervalBinary,
dtype: DType,
a: IntervalOperand<'_, CudaRuntime>,
b: IntervalOperand<'_, CudaRuntime>,
status: &CudaTensor,
) -> Result<()> {
match op {
IntervalBinary::DivPositive => flag_interval(client, dtype, b, true, status),
IntervalBinary::DivPositiveInterval => {
flag_interval(client, dtype, a, false, status)?;
flag_interval(client, dtype, b, true, status)
}
_ => Ok(()),
}
}