Skip to main content

ringkernel_accnet/
error.rs

1//! Error types for accounting network operations.
2
3use thiserror::Error;
4
5/// Result type alias for accounting network operations.
6pub type Result<T> = std::result::Result<T, AccNetError>;
7
8/// Comprehensive error type for accounting network operations.
9#[derive(Error, Debug)]
10pub enum AccNetError {
11    // ===== CUDA Device Errors =====
12    /// CUDA is not available on this system.
13    #[error("CUDA is not available on this system")]
14    CudaUnavailable,
15
16    /// Failed to create or initialize a CUDA device.
17    #[error("failed to create CUDA device: {0}")]
18    DeviceCreation(String),
19
20    /// GPU synchronization failed.
21    #[error("GPU synchronize failed: {0}")]
22    GpuSync(String),
23
24    // ===== Kernel Compilation Errors =====
25    /// NVRTC compilation of CUDA source failed.
26    #[error("NVRTC compilation failed for '{kernel}': {reason}")]
27    NvrtcCompilation {
28        /// Kernel name that failed to compile.
29        kernel: String,
30        /// Compilation error details.
31        reason: String,
32    },
33
34    /// Failed to load a compiled PTX module.
35    #[error("failed to load PTX for '{kernel}': {reason}")]
36    PtxLoad {
37        /// Kernel name.
38        kernel: String,
39        /// Load error details.
40        reason: String,
41    },
42
43    // ===== Kernel Execution Errors =====
44    /// Kernel function was not found in the loaded module.
45    #[error("kernel function '{0}' not found")]
46    KernelNotFound(String),
47
48    /// Kernel launch failed.
49    #[error("kernel launch failed: {0}")]
50    KernelLaunch(String),
51
52    // ===== GPU Memory Errors =====
53    /// Failed to copy data from host to device.
54    #[error("failed to copy {field} to device: {reason}")]
55    HostToDevice {
56        /// Name of the buffer being copied.
57        field: String,
58        /// Error details.
59        reason: String,
60    },
61
62    /// Failed to copy data from device to host.
63    #[error("failed to copy results from device: {0}")]
64    DeviceToHost(String),
65
66    /// Failed to allocate GPU memory.
67    #[error("failed to allocate GPU memory for '{field}': {reason}")]
68    GpuAlloc {
69        /// Name of the buffer being allocated.
70        field: String,
71        /// Error details.
72        reason: String,
73    },
74
75    // ===== Code Generation Errors =====
76    /// Kernel code generation (transpilation) failed.
77    #[error("failed to generate {kernel} kernel: {reason}")]
78    CodeGen {
79        /// Name of the kernel being generated.
80        kernel: String,
81        /// Generation error details.
82        reason: String,
83    },
84
85    // ===== Validation Errors =====
86    /// Configuration validation failed.
87    #[error("validation error: {0}")]
88    Validation(String),
89}
90
91impl From<String> for AccNetError {
92    fn from(s: String) -> Self {
93        AccNetError::Validation(s)
94    }
95}
96
97impl From<&str> for AccNetError {
98    fn from(s: &str) -> Self {
99        AccNetError::Validation(s.to_string())
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_error_display() {
109        let err = AccNetError::CudaUnavailable;
110        assert_eq!(format!("{err}"), "CUDA is not available on this system");
111
112        let err = AccNetError::DeviceCreation("no driver".to_string());
113        assert_eq!(format!("{err}"), "failed to create CUDA device: no driver");
114
115        let err = AccNetError::NvrtcCompilation {
116            kernel: "suspense_detection".to_string(),
117            reason: "syntax error".to_string(),
118        };
119        assert!(format!("{err}").contains("suspense_detection"));
120        assert!(format!("{err}").contains("syntax error"));
121
122        let err = AccNetError::KernelNotFound("pagerank".to_string());
123        assert!(format!("{err}").contains("pagerank"));
124
125        let err = AccNetError::HostToDevice {
126            field: "balance_debit".to_string(),
127            reason: "out of memory".to_string(),
128        };
129        assert!(format!("{err}").contains("balance_debit"));
130
131        let err = AccNetError::CodeGen {
132            kernel: "fraud_detector".to_string(),
133            reason: "unsupported op".to_string(),
134        };
135        assert!(format!("{err}").contains("fraud_detector"));
136    }
137
138    #[test]
139    fn test_from_string() {
140        let err: AccNetError = "bad config".to_string().into();
141        assert!(matches!(err, AccNetError::Validation(_)));
142        assert!(format!("{err}").contains("bad config"));
143    }
144
145    #[test]
146    fn test_from_str() {
147        let err: AccNetError = "bad config".into();
148        assert!(matches!(err, AccNetError::Validation(_)));
149    }
150
151    #[test]
152    fn test_result_alias() {
153        let ok: Result<i32> = Ok(42);
154        assert_eq!(ok.unwrap(), 42);
155
156        let err: Result<i32> = Err(AccNetError::CudaUnavailable);
157        assert!(err.is_err());
158    }
159}