1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::{error, fmt};

#[derive(Debug, Clone)]
#[cfg(any(feature = "cuda", feature = "opencl"))]
pub enum ClError {
    DeviceNotFound,
    PlatformNotFound,
    BusIdNotAvailable,
    NvidiaBusIdNotAvailable,
    AmdTopologyNotAvailable,
    PlatformNameNotAvailable,
    CannotCreateContext,
    CannotCreateQueue,
    GetDeviceError,
}

#[cfg(any(feature = "cuda", feature = "opencl"))]
pub type ClResult<T> = Result<T, ClError>;

#[cfg(any(feature = "cuda", feature = "opencl"))]
impl fmt::Display for ClError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            ClError::DeviceNotFound => write!(f, "Device not found."),
            ClError::PlatformNotFound => write!(f, "Platform not found."),
            ClError::BusIdNotAvailable => write!(f, "Cannot extract bus-id for the given device."),
            ClError::NvidiaBusIdNotAvailable => {
                write!(f, "Cannot extract bus-id for the given Nvidia device.")
            }
            ClError::AmdTopologyNotAvailable => {
                write!(f, "Cannot extract bus-id for the given AMD device.")
            }
            ClError::PlatformNameNotAvailable => {
                write!(f, "Cannot extract platform name for the given platform.")
            }
            ClError::CannotCreateContext => write!(f, "Cannot create cl_context."),
            ClError::CannotCreateQueue => write!(f, "Cannot create cl_command_queue."),
            ClError::GetDeviceError => write!(f, "Cannot get Device"),
        }
    }
}

#[derive(Debug, Clone)]
/// Possible error states for the hashing.
pub enum Error {
    /// The allowed number of leaves cannot be greater than the arity of the tree.
    FullBuffer,
    /// Attempt to reference an index element that is out of bounds
    IndexOutOfBounds,
    GpuError(String),
    #[cfg(any(feature = "cuda", feature = "opencl"))]
    ClError(ClError),
    Other(String),
}

#[cfg(any(feature = "cuda", feature = "opencl"))]
impl From<ec_gpu_gen::rust_gpu_tools::GPUError> for Error {
    fn from(e: ec_gpu_gen::rust_gpu_tools::GPUError) -> Self {
        Self::GpuError(format!("GPU tools error: {e}"))
    }
}

#[cfg(any(feature = "cuda", feature = "opencl"))]
impl From<ec_gpu_gen::EcError> for Error {
    fn from(e: ec_gpu_gen::EcError) -> Self {
        Self::GpuError(format!("EC-GPU error: {e}"))
    }
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Error::FullBuffer => write!(
                f,
                "The size of the buffer cannot be greater than the hash arity."
            ),
            Error::IndexOutOfBounds => write!(f, "The referenced index is outs of bounds."),
            Error::GpuError(s) => write!(f, "GPU Error: {s}"),
            #[cfg(any(feature = "cuda", feature = "opencl"))]
            Error::ClError(e) => write!(f, "OpenCL Error: {e}"),
            Error::Other(s) => write!(f, "{s}"),
        }
    }
}