pub mod buffer;
pub mod ffi;
pub mod kernels;
pub use buffer::{CudaBuffer, empty_cache};
use std::sync::OnceLock;
use crate::error::{Error, Result};
pub fn init(device: usize) -> Result<()> {
static INIT: OnceLock<std::result::Result<(), String>> = OnceLock::new();
INIT.get_or_init(|| {
let code = unsafe { ffi::fastnn_cuda_init(device as i32) };
if code == 0 {
Ok(())
} else {
Err(format!("could not initialise device {device}"))
}
})
.clone()
.map_err(Error::Cuda)
}
pub fn device_count() -> usize {
unsafe { ffi::fastnn_cuda_device_count().max(0) as usize }
}
pub fn synchronize() -> Result<()> {
match unsafe { ffi::fastnn_cuda_synchronize() } {
0 => Ok(()),
code => Err(Error::Cuda(format!("synchronize failed (status {code})"))),
}
}
pub fn memory_info() -> (usize, usize) {
let (mut free, mut total) = (0usize, 0usize);
unsafe { ffi::fastnn_cuda_get_memory_info(&mut free, &mut total) };
(free, total)
}