UnifiedBox

Struct UnifiedBox 

Source
pub struct UnifiedBox<T: DeviceCopy> { /* private fields */ }
Expand description

A pointer type for heap-allocation in CUDA unified memory.

See the module-level documentation for more information on unified memory. Should behave equivalently to std::boxed::Box, except that the allocated memory can be seamlessly shared between host and device.

Implementations§

Source§

impl<T: DeviceCopy> UnifiedBox<T>

Source

pub fn new(val: T) -> CudaResult<Self>

Allocate unified memory and place val into it.

This doesn’t actually allocate if T is zero-sized.

§Errors

If a CUDA error occurs, returns that error.

§Examples
use rustacuda::memory::*;
let five = UnifiedBox::new(5).unwrap();
Source

pub unsafe fn uninitialized() -> CudaResult<Self>

Allocate unified memory without initializing it.

This doesn’t actually allocate if T is zero-sized.

§Safety

Since the backing memory is not initialized, this function is not safe. The caller must ensure that the backing memory is set to a valid value before it is read, else undefined behavior may occur.

§Errors

If a CUDA error occurs, returns that error.

§Examples
use rustacuda::memory::*;
let mut five = unsafe{ UnifiedBox::uninitialized().unwrap() };
*five = 5u64;
Source

pub unsafe fn from_raw(ptr: *mut T) -> Self

Constructs a UnifiedBox from a raw pointer.

After calling this function, the raw pointer and the memory it points to is owned by the UnifiedBox. The UnifiedBox destructor will free the allocated memory, but will not call the destructor of T. This function may accept any pointer produced by the cuMemAllocManaged CUDA API call.

§Safety

This function is unsafe because improper use may lead to memory problems. For example, a double free may occur if this function is called twice on the same pointer, or a segfault may occur if the pointer is not one returned by the appropriate API call.

§Examples
use rustacuda::memory::*;
let x = UnifiedBox::new(5).unwrap();
let ptr = UnifiedBox::into_unified(x).as_raw_mut();
let x = unsafe { UnifiedBox::from_raw(ptr) };
Source

pub unsafe fn from_unified(ptr: UnifiedPointer<T>) -> Self

Constructs a UnifiedBox from a UnifiedPointer.

After calling this function, the pointer and the memory it points to is owned by the UnifiedBox. The UnifiedBox destructor will free the allocated memory, but will not call the destructor of T. This function may accept any pointer produced by the cuMemAllocManaged CUDA API call, such as one taken from UnifiedBox::into_unified.

§Safety

This function is unsafe because improper use may lead to memory problems. For example, a double free may occur if this function is called twice on the same pointer, or a segfault may occur if the pointer is not one returned by the appropriate API call.

§Examples
use rustacuda::memory::*;
let x = UnifiedBox::new(5).unwrap();
let ptr = UnifiedBox::into_unified(x);
let x = unsafe { UnifiedBox::from_unified(ptr) };
Source

pub fn into_unified(b: UnifiedBox<T>) -> UnifiedPointer<T>

Consumes the UnifiedBox, returning the wrapped UnifiedPointer.

After calling this function, the caller is responsible for the memory previously managed by the UnifiedBox. In particular, the caller should properly destroy T and deallocate the memory. The easiest way to do so is to create a new UnifiedBox using the UnifiedBox::from_unified function.

Note: This is an associated function, which means that you have to all it as UnifiedBox::into_unified(b) instead of b.into_unified() This is so that there is no conflict with a method on the inner type.

§Examples
use rustacuda::memory::*;
let x = UnifiedBox::new(5).unwrap();
let ptr = UnifiedBox::into_unified(x);
Source

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

Returns the contained unified pointer without consuming the box.

This is useful for passing the box to a kernel launch.

§Examples
use rustacuda::memory::*;
let mut x = UnifiedBox::new(5).unwrap();
let ptr = x.as_unified_ptr();
println!("{:p}", ptr);
Source

pub fn leak<'a>(b: UnifiedBox<T>) -> &'a mut T
where T: 'a,

Consumes and leaks the UnifiedBox, returning a mutable reference, &’a mut T. Note that the type T must outlive the chosen lifetime ’a. If the type has only static references, or none at all, this may be chosen to be ’static.

This is mainly useful for data that lives for the remainder of the program’s life. Dropping the returned reference will cause a memory leak. If this is not acceptable, the reference should be wrapped with the UnifiedBox::from_raw function to produce a new UnifiedBox. This UnifiedBox can then be dropped, which will properly destroy T and release the allocated memory.

Note: This is an associated function, which means that you have to all it as UnifiedBox::leak(b) instead of b.leak() This is so that there is no conflict with a method on the inner type.

Source

pub fn drop(uni_box: UnifiedBox<T>) -> DropResult<UnifiedBox<T>>

Destroy a UnifiedBox, returning an error.

Deallocating unified memory can return errors from previous asynchronous work. This function destroys the given box and returns the error and the un-destroyed box on failure.

§Example
use rustacuda::memory::*;
let x = UnifiedBox::new(5).unwrap();
match UnifiedBox::drop(x) {
    Ok(()) => println!("Successfully destroyed"),
    Err((e, uni_box)) => {
        println!("Failed to destroy box: {:?}", e);
        // Do something with uni_box
    },
}

Trait Implementations§

Source§

impl<T: DeviceCopy> AsMut<T> for UnifiedBox<T>

Source§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T: DeviceCopy> AsRef<T> for UnifiedBox<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: DeviceCopy> Borrow<T> for UnifiedBox<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: DeviceCopy> BorrowMut<T> for UnifiedBox<T>

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T: Debug + DeviceCopy> Debug for UnifiedBox<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: DeviceCopy> Deref for UnifiedBox<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: DeviceCopy> DerefMut for UnifiedBox<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T: Display + DeviceCopy> Display for UnifiedBox<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: DeviceCopy> Drop for UnifiedBox<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: DeviceCopy + Hash> Hash for UnifiedBox<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: DeviceCopy + Ord> Ord for UnifiedBox<T>

Source§

fn cmp(&self, other: &UnifiedBox<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: DeviceCopy + PartialEq> PartialEq for UnifiedBox<T>

Source§

fn eq(&self, other: &UnifiedBox<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: DeviceCopy + PartialOrd> PartialOrd for UnifiedBox<T>

Source§

fn partial_cmp(&self, other: &UnifiedBox<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &UnifiedBox<T>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &UnifiedBox<T>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn ge(&self, other: &UnifiedBox<T>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

fn gt(&self, other: &UnifiedBox<T>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

impl<T: DeviceCopy> Pointer for UnifiedBox<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: DeviceCopy + Eq> Eq for UnifiedBox<T>

Auto Trait Implementations§

§

impl<T> Freeze for UnifiedBox<T>

§

impl<T> RefUnwindSafe for UnifiedBox<T>
where T: RefUnwindSafe,

§

impl<T> !Send for UnifiedBox<T>

§

impl<T> !Sync for UnifiedBox<T>

§

impl<T> Unpin for UnifiedBox<T>

§

impl<T> UnwindSafe for UnifiedBox<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.