1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//! Defines Cuda Device Memory.
//!
//! Hold a pointer and size of the cuda device memory.
//! This is only a convenience wrapper to interact in a
//! defined mamner with cudnn, which requires scrap/temporary
//! memory for some operations, i.e. dropout.
use super::{Error, API};
#[derive(Debug)]
/// A pointer to memory existing on a nvidia GPU
pub struct CudaDeviceMemory {
ptr: *mut ::libc::c_void,
size: usize,
}
impl CudaDeviceMemory {
/// Saw fun X Y Z
pub fn new(size: usize) -> Result<CudaDeviceMemory, Error> {
let ptr = API::cuda_allocate_device_memory(size)?;
Ok(CudaDeviceMemory { ptr, size })
}
/// Initializes a new CUDA Device Memory from its C type.
pub fn from_c(ptr: *mut ::libc::c_void, size: usize) -> CudaDeviceMemory {
CudaDeviceMemory { ptr, size }
}
/// Returns the CUDA Device Memory ptr as its C type.
pub fn id_c(&self) -> &*mut ::libc::c_void {
&self.ptr
}
/// Returns the size of the CUDA Device Memory chunk.
pub fn size(&self) -> usize {
self.size
}
}
impl Drop for CudaDeviceMemory {
#[allow(unused_must_use)]
fn drop(&mut self) {
self.size = 0;
API::cuda_free_device_memory(*self.id_c()).unwrap()
}
}