Skip to main content

baracuda_driver/
error.rs

1//! Error type for `baracuda-driver`.
2
3use baracuda_cuda_sys::{driver, CUresult};
4
5/// A driver-API error: either a non-success `CUresult`, a loader failure, or
6/// a feature-not-supported-on-this-driver error.
7pub type Error = baracuda_core::Error<CUresult>;
8
9/// Convenient `Result` alias.
10pub type Result<T, E = Error> = core::result::Result<T, E>;
11
12/// Turn a raw `CUresult` into `Result<()>`.
13#[inline]
14pub(crate) fn check(status: CUresult) -> Result<()> {
15    Error::check(status)
16}
17
18/// Look up the symbolic name of a `CUresult` (e.g. `"CUDA_ERROR_OUT_OF_MEMORY"`).
19/// Returns `"CUDA_UNKNOWN_ERROR"` if the driver doesn't recognize the code.
20pub fn error_name(status: CUresult) -> Result<&'static str> {
21    let d = driver()?;
22    let cu = d.cu_get_error_name()?;
23    let mut p: *const core::ffi::c_char = core::ptr::null();
24    check(unsafe { cu(status, &mut p) })?;
25    if p.is_null() {
26        return Ok("CUDA_UNKNOWN_ERROR");
27    }
28    Ok(unsafe { core::ffi::CStr::from_ptr(p) }
29        .to_str()
30        .unwrap_or("CUDA_UNKNOWN_ERROR"))
31}
32
33/// Look up the human-readable description of a `CUresult`.
34pub fn error_string(status: CUresult) -> Result<&'static str> {
35    let d = driver()?;
36    let cu = d.cu_get_error_string()?;
37    let mut p: *const core::ffi::c_char = core::ptr::null();
38    check(unsafe { cu(status, &mut p) })?;
39    if p.is_null() {
40        return Ok("unknown error");
41    }
42    Ok(unsafe { core::ffi::CStr::from_ptr(p) }
43        .to_str()
44        .unwrap_or("unknown error"))
45}