[][src]Struct rustacuda::memory::UnifiedBuffer

pub struct UnifiedBuffer<T: DeviceCopy> { /* fields omitted */ }

Fixed-size buffer in unified memory.

See the module-level documentation for more details on unified memory.

Implementations

impl<T: DeviceCopy + Clone> UnifiedBuffer<T>[src]

pub fn new(value: &T, size: usize) -> CudaResult<Self>[src]

Allocate a new unified 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 = UnifiedBuffer::new(&0u64, 5).unwrap();
buffer[0] = 1;

pub fn from_slice(slice: &[T]) -> CudaResult<Self>[src]

Allocate a new unified 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 = UnifiedBuffer::from_slice(&values).unwrap();
buffer[0] = 1;

impl<T: DeviceCopy> UnifiedBuffer<T>[src]

pub unsafe fn uninitialized(size: usize) -> CudaResult<Self>[src]

Allocate a new unified 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 { UnifiedBuffer::uninitialized(5).unwrap() };
for i in buffer.iter_mut() {
    *i = 0u64;
}

pub fn as_slice(&self) -> &[T][src]

Extracts a slice containing the entire buffer.

Equivalent to &s[..].

Examples

use rustacuda::memory::*;
let buffer = UnifiedBuffer::new(&0u64, 5).unwrap();
let sum : u64 = buffer.as_slice().iter().sum();

pub fn as_mut_slice(&mut self) -> &mut [T][src]

Extracts a mutable slice of the entire buffer.

Equivalent to &mut s[..].

Examples

use rustacuda::memory::*;
let mut buffer = UnifiedBuffer::new(&0u64, 5).unwrap();
for i in buffer.as_mut_slice() {
    *i = 12u64;
}

pub fn as_unified_ptr(&mut self) -> UnifiedPointer<T>[src]

Returns a UnifiedPointer<T> to the buffer.

The caller must ensure that the buffer outlives the returned pointer, or it will end up pointing to garbage.

Modifying the buffer is guaranteed not to cause its buffer to be reallocated, so pointers cannot be invalidated in that manner, but other types may be added in the future which can reallocate.

pub unsafe fn from_raw_parts(
    ptr: UnifiedPointer<T>,
    capacity: usize
) -> UnifiedBuffer<T>
[src]

Creates a UnifiedBuffer<T> directly from the raw components of another unified buffer.

Safety

This is highly unsafe, due to the number of invariants that aren't checked:

  • ptr needs to have been previously allocated via UnifiedBuffer or cuda_malloc_unified.
  • ptr's T 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 UnifiedBuffer<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 = UnifiedBuffer::new(&0u64, 5).unwrap();
let ptr = buffer.as_unified_ptr();
let size = buffer.len();

mem::forget(buffer);

let buffer = unsafe { UnifiedBuffer::from_raw_parts(ptr, size) };

pub fn drop(uni_buf: UnifiedBuffer<T>) -> DropResult<UnifiedBuffer<T>>[src]

Destroy a UnifiedBuffer, returning an error.

Deallocating unified 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 = UnifiedBuffer::from_slice(&[10u32, 20, 30]).unwrap();
match UnifiedBuffer::drop(x) {
    Ok(()) => println!("Successfully destroyed"),
    Err((e, buf)) => {
        println!("Failed to destroy buffer: {:?}", e);
        // Do something with buf
    },
}

Trait Implementations

impl<T: DeviceCopy> AsMut<[T]> for UnifiedBuffer<T>[src]

impl<T: DeviceCopy> AsRef<[T]> for UnifiedBuffer<T>[src]

impl<T: Debug + DeviceCopy> Debug for UnifiedBuffer<T>[src]

impl<T: DeviceCopy> Deref for UnifiedBuffer<T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<T: DeviceCopy> DerefMut for UnifiedBuffer<T>[src]

impl<T: DeviceCopy> Drop for UnifiedBuffer<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for UnifiedBuffer<T> where
    T: RefUnwindSafe

impl<T> !Send for UnifiedBuffer<T>

impl<T> !Sync for UnifiedBuffer<T>

impl<T> Unpin for UnifiedBuffer<T>

impl<T> UnwindSafe for UnifiedBuffer<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.