Struct rustacuda::memory::LockedBuffer [−][src]
pub struct LockedBuffer<T: DeviceCopy> { /* fields omitted */ }
Expand description
Fixed-size host-side buffer in page-locked memory.
See the module-level documentation
for more details on page-locked
memory.
Implementations
Allocate a new page-locked buffer large enough to hold size
T
’s and initialized with
clones of value
.
Errors
If the allocation fails, returns the error from CUDA. If size
is large enough that
size * mem::sizeof::<T>()
overflows usize, then returns InvalidMemoryAllocation.
Examples
use rustacuda::memory::*; let mut buffer = LockedBuffer::new(&0u64, 5).unwrap(); buffer[0] = 1;
Allocate a new page-locked buffer of the same size as slice
, initialized with a clone of
the data in slice
.
Errors
If the allocation fails, returns the error from CUDA.
Examples
use rustacuda::memory::*; let values = [0u64; 5]; let mut buffer = LockedBuffer::from_slice(&values).unwrap(); buffer[0] = 1;
Allocate a new page-locked buffer large enough to hold size
T
’s, but without
initializing the contents.
Errors
If the allocation fails, returns the error from CUDA. If size
is large enough that
size * mem::sizeof::<T>()
overflows usize, then returns InvalidMemoryAllocation.
Safety
The caller must ensure that the contents of the buffer are initialized before reading from the buffer.
Examples
use rustacuda::memory::*; let mut buffer = unsafe { LockedBuffer::uninitialized(5).unwrap() }; for i in buffer.iter_mut() { *i = 0u64; }
Extracts a slice containing the entire buffer.
Equivalent to &s[..]
.
Examples
use rustacuda::memory::*; let buffer = LockedBuffer::new(&0u64, 5).unwrap(); let sum : u64 = buffer.as_slice().iter().sum();
Extracts a mutable slice of the entire buffer.
Equivalent to &mut s[..]
.
Examples
use rustacuda::memory::*; let mut buffer = LockedBuffer::new(&0u64, 5).unwrap(); for i in buffer.as_mut_slice() { *i = 12u64; }
Creates a LockedBuffer<T>
directly from the raw components of another locked buffer.
Safety
This is highly unsafe, due to the number of invariants that aren’t checked:
ptr
needs to have been previously allocated viaLockedBuffer
orcuda_malloc_locked
.ptr
’sT
needs to have the same size and alignment as it was allocated with.capacity
needs to be the capacity that the pointer was allocated with.
Violating these may cause problems like corrupting the CUDA driver’s internal data structures.
The ownership of ptr
is effectively transferred to the
LockedBuffer<T>
which may then deallocate, reallocate or change the
contents of memory pointed to by the pointer at will. Ensure
that nothing else uses the pointer after calling this
function.
Examples
use std::mem; use rustacuda::memory::*; let mut buffer = LockedBuffer::new(&0u64, 5).unwrap(); let ptr = buffer.as_mut_ptr(); let size = buffer.len(); mem::forget(buffer); let buffer = unsafe { LockedBuffer::from_raw_parts(ptr, size) };
Destroy a LockedBuffer
, returning an error.
Deallocating page-locked memory can return errors from previous asynchronous work. This function destroys the given buffer and returns the error and the un-destroyed buffer on failure.
Example
use rustacuda::memory::*; let x = LockedBuffer::new(&0u64, 5).unwrap(); match LockedBuffer::drop(x) { Ok(()) => println!("Successfully destroyed"), Err((e, buf)) => { println!("Failed to destroy buffer: {:?}", e); // Do something with buf }, }