Struct rustacuda::memory::UnifiedBox[][src]

pub struct UnifiedBox<T: DeviceCopy> { /* fields omitted */ }
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

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

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;

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

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

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

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

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.

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

Performs the conversion.

Performs the conversion.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

Formats the value using the given formatter.

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.

Converts the given value to a String. Read more

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.