rust_gpu_tools/
error.rs

1#[cfg(feature = "opencl")]
2use opencl3::error_codes::ClError;
3#[cfg(feature = "cuda")]
4use rustacuda::error::CudaError;
5
6/// Error types of this library.
7#[derive(thiserror::Error, Debug)]
8#[allow(clippy::upper_case_acronyms)]
9pub enum GPUError {
10    /// Error from the underlying `opencl3` library, e.g. a memory allocation failure.
11    #[cfg(feature = "opencl")]
12    #[error("Opencl3 Error: {0}{}", match .1 {
13       Some(message) => format!(" {}", message),
14       None => "".to_string(),
15    })]
16    Opencl3(ClError, Option<String>),
17
18    /// Error for OpenCL `clGetProgramInfo()` call failures.
19    #[cfg(feature = "opencl")]
20    #[error("Program info not available!")]
21    ProgramInfoNotAvailable(ClError),
22
23    /// Error for OpenCL `clGetDeviceInfo()` call failures.
24    #[cfg(feature = "opencl")]
25    #[error("Device info not available!")]
26    DeviceInfoNotAvailable(ClError),
27
28    /// Error from the underlying `RustaCUDA` library, e.g. a memory allocation failure.
29    #[cfg(feature = "cuda")]
30    #[error("Cuda Error: {0}")]
31    Cuda(#[from] CudaError),
32
33    /// Error when a device cannot be found.
34    #[error("Device not found!")]
35    DeviceNotFound,
36
37    /// Error when a kernel with the given name cannot be found.
38    #[error("Kernel with name {0} not found!")]
39    KernelNotFound(String),
40
41    /// Error when standard I/O fails.
42    #[error("IO Error: {0}")]
43    IO(#[from] std::io::Error),
44
45    /// Error when the device is from an unsupported vendor.
46    #[error("Vendor {0} is not supported.")]
47    UnsupportedVendor(String),
48
49    /// Error when the string representation of a unique identifier (PCI-ID or UUID) cannot be
50    /// parsed.
51    #[error("{0}")]
52    InvalidId(String),
53
54    /// Errors that rarely happen and don't deserve their own error type.
55    #[error("{0}")]
56    Generic(String),
57}
58
59/// Convenience type alias for [`GPUError`] based [`Result`]s.
60#[allow(clippy::upper_case_acronyms)]
61pub type GPUResult<T> = std::result::Result<T, GPUError>;
62
63#[cfg(feature = "opencl")]
64impl From<ClError> for GPUError {
65    fn from(error: ClError) -> Self {
66        GPUError::Opencl3(error, None)
67    }
68}