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

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

Fixed-size buffer in unified memory.

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

Implementations

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;

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;

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;
}

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();

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;
}

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.

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) };

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

Performs the conversion.

Performs the conversion.

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.