use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CudaError {
NoDevice(String),
Driver {
call: &'static str,
message: String,
},
OutOfMemory(usize),
Tile(String),
InvalidArgument(String),
}
pub type Result<T> = std::result::Result<T, CudaError>;
impl fmt::Display for CudaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoDevice(msg) => write!(f, "no CUDA device: {msg}"),
Self::Driver { call, message } => write!(f, "CUDA {call}: {message}"),
Self::OutOfMemory(n) => write!(f, "CUDA out of memory: cannot allocate {n} bytes"),
Self::Tile(msg) => write!(f, "CUDA Tile kernel: {msg}"),
Self::InvalidArgument(msg) => write!(f, "CUDA: {msg}"),
}
}
}
impl std::error::Error for CudaError {}
pub(crate) fn driver(call: &'static str) -> impl Fn(cuda_core::DriverError) -> CudaError {
move |e| CudaError::Driver {
call,
message: e.to_string(),
}
}
pub(crate) fn check(call: &'static str, r: cuda_core::sys::CUresult) -> Result<()> {
if r == cuda_core::sys::cudaError_enum_CUDA_SUCCESS {
Ok(())
} else {
Err(driver(call)(cuda_core::DriverError(r)))
}
}