openvm_cuda_common/
error.rs1use std::{
2 ffi::CStr,
3 os::raw::{c_char, c_int},
4};
5
6use thiserror::Error;
7
8#[link(name = "cudart")]
9extern "C" {
10 fn cudaGetErrorString(error: c_int) -> *const c_char;
11 fn cudaGetErrorName(error: c_int) -> *const c_char;
12}
13
14fn cstr_to_string(ptr: *const c_char) -> String {
16 if ptr.is_null() {
17 return "Unknown CUDA error (null pointer)".to_string();
18 }
19 unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() }
20}
21
22pub fn get_cuda_error_name(error_code: i32) -> String {
24 let name_ptr = unsafe { cudaGetErrorName(error_code) };
25 cstr_to_string(name_ptr)
26}
27
28pub fn get_cuda_error_string(error_code: i32) -> String {
30 let str_ptr = unsafe { cudaGetErrorString(error_code) };
31 cstr_to_string(str_ptr)
32}
33
34#[derive(Error, Debug)]
36#[error("{message} ({name})")]
37pub struct CudaError {
38 pub code: i32,
39 pub name: String,
40 pub message: String,
41}
42
43impl CudaError {
44 pub fn new(code: i32) -> Self {
46 CudaError {
47 code,
48 name: get_cuda_error_name(code),
49 message: get_cuda_error_string(code),
50 }
51 }
52
53 pub fn from_result(code: i32) -> Result<(), Self> {
55 if code == 0 {
56 Ok(())
57 } else {
58 Err(Self::new(code))
59 }
60 }
61}
62
63#[inline]
64pub fn check(code: i32) -> Result<(), CudaError> {
65 CudaError::from_result(code)
66}
67
68#[derive(Error, Debug)]
69pub enum MemoryError {
70 #[error(transparent)]
71 Cuda(#[from] CudaError),
72
73 #[error("Attempted to free null pointer")]
74 NullPointer,
75
76 #[error("Attempted to free untracked pointer")]
77 UntrackedPointer,
78
79 #[error("Failed to acquire memory manager lock")]
80 LockError,
81}
82
83#[derive(Error, Debug)]
84pub enum MemCopyError {
85 #[error(transparent)]
86 Cuda(#[from] CudaError),
87 #[error("Size mismatch in {operation}: host len={host_len}, device len={device_len}")]
88 SizeMismatch {
89 operation: &'static str,
90 host_len: usize,
91 device_len: usize,
92 },
93}
94
95#[derive(Error, Debug)]
96pub enum KernelError {
97 #[error(transparent)]
98 Cuda(#[from] CudaError),
99
100 #[error("Unsupported type size {size}")]
101 UnsupportedTypeSize { size: usize },
102}