Skip to main content

sp1_gpu_cudart/
device.rs

1use crate::{CudaError, TaskScope};
2use slop_alloc::{mem::CopyError, CopyIntoBackend, CopyToBackend, CpuBackend};
3use sp1_gpu_sys::runtime::cuda_mem_get_info;
4
5pub trait DeviceCopy: Copy + 'static + Sized {}
6
7impl<T: Copy + 'static + Sized> DeviceCopy for T {}
8
9/// Returns a pair `(free, total)` of the amount of free and total memory on the device.
10pub fn cuda_memory_info() -> Result<(usize, usize), CudaError> {
11    let mut free: usize = 0;
12    let mut total: usize = 0;
13    CudaError::result_from_ffi(unsafe { cuda_mem_get_info(&mut free, &mut total) })?;
14    Ok((free, total))
15}
16
17pub trait IntoDevice: CopyIntoBackend<TaskScope, CpuBackend> + Sized {
18    fn into_device_in(self, backend: &TaskScope) -> Result<Self::Output, CopyError> {
19        self.copy_into_backend(backend)
20    }
21}
22
23impl<T> IntoDevice for T where T: CopyIntoBackend<TaskScope, CpuBackend> + Sized {}
24
25pub trait ToDevice: CopyToBackend<TaskScope, CpuBackend> + Sized {
26    fn to_device_in(&self, backend: &TaskScope) -> Result<Self::Output, CopyError> {
27        self.copy_to_backend(backend)
28    }
29}
30
31impl<T> ToDevice for T where T: CopyToBackend<TaskScope, CpuBackend> + Sized {}
32
33#[macro_export]
34macro_rules! args {
35    ($($arg:expr),*) => {
36        [
37            $(
38                &$arg as *const _ as *mut std::ffi::c_void
39            ),*
40        ]
41    };
42}