ringkernel_accnet/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, AccNetError>;
7
8#[derive(Error, Debug)]
10pub enum AccNetError {
11 #[error("CUDA is not available on this system")]
14 CudaUnavailable,
15
16 #[error("failed to create CUDA device: {0}")]
18 DeviceCreation(String),
19
20 #[error("GPU synchronize failed: {0}")]
22 GpuSync(String),
23
24 #[error("NVRTC compilation failed for '{kernel}': {reason}")]
27 NvrtcCompilation {
28 kernel: String,
30 reason: String,
32 },
33
34 #[error("failed to load PTX for '{kernel}': {reason}")]
36 PtxLoad {
37 kernel: String,
39 reason: String,
41 },
42
43 #[error("kernel function '{0}' not found")]
46 KernelNotFound(String),
47
48 #[error("kernel launch failed: {0}")]
50 KernelLaunch(String),
51
52 #[error("failed to copy {field} to device: {reason}")]
55 HostToDevice {
56 field: String,
58 reason: String,
60 },
61
62 #[error("failed to copy results from device: {0}")]
64 DeviceToHost(String),
65
66 #[error("failed to allocate GPU memory for '{field}': {reason}")]
68 GpuAlloc {
69 field: String,
71 reason: String,
73 },
74
75 #[error("failed to generate {kernel} kernel: {reason}")]
78 CodeGen {
79 kernel: String,
81 reason: String,
83 },
84
85 #[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}