async_cuda_core/ffi/error.rs
1use cpp::cpp;
2
3/// Returns the description string for an error code.
4///
5/// Note that this function is not executed on the runtime thread, since it is purely a utility
6/// function and should have no side-effects with regards to CUDA devices.
7///
8/// [CUDA documentation](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__ERROR.html#group__CUDART__ERROR_1g4bc9e35a618dfd0877c29c8ee45148f1)
9///
10/// # Arguments
11///
12/// * `error_code` - CUDA error code.
13pub fn error_description(error_code: i32) -> String {
14 let error_description = cpp!(unsafe [
15 error_code as "std::int32_t"
16 ] -> *const std::ffi::c_char as "const char*" {
17 return cudaGetErrorString(static_cast<cudaError_t>(error_code));
18 });
19 // SAFETY: The pointer returned by `cudaGetErrorString` actually has a static lifetime so this
20 // is safe for sure. We even copy inside the unsafe block so we just need it to remain for a
21 // little bit.
22 unsafe {
23 std::ffi::CStr::from_ptr(error_description)
24 .to_string_lossy()
25 .to_string()
26 }
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn test_correct_description() {
35 assert_eq!(error_description(1), "invalid argument");
36 }
37}