Struct DevicePointer

Source
pub struct DevicePointer<T>
where T: DeviceCopy + ?Sized,
{ /* private fields */ }
Expand description

A pointer to device memory.

DevicePointer cannot be dereferenced by the CPU, as it is a pointer to a memory allocation in the device. It can be safely copied to the device (eg. as part of a kernel launch) and either unwrapped or transmuted to an appropriate pointer.

DevicePointer is guaranteed to have an equivalent internal representation to a raw pointer. Thus, it can be safely reinterpreted or transmuted to *mut T. It is safe to pass a DevicePointer through an FFI boundary to C code expecting a *mut T, so long as the code on the other side of that boundary does not attempt to dereference the pointer on the CPU. It is thus possible to pass a DevicePointer to a CUDA kernel written in C.

Implementations§

Source§

impl<T> DevicePointer<T>
where T: DeviceCopy + ?Sized,

Source

pub fn as_ptr(&self) -> *const T

Returns a rust pointer created from this pointer, meant for FFI purposes. The pointer is not dereferenceable from the CPU!

Source

pub fn as_mut_ptr(&self) -> *mut T

Returns a rust pointer created from this pointer, meant for FFI purposes. The pointer is not dereferenceable from the CPU!

Source

pub fn as_raw(&self) -> u64

Returns the contained CUdeviceptr.

Source

pub fn from_raw(ptr: u64) -> DevicePointer<T>

Create a DevicePointer from a raw CUDA pointer

Source

pub fn is_null(self) -> bool

Returns true if the pointer is null.

§Examples
use cust::memory::*;
use std::ptr;
unsafe {
    let null : *mut u64 = ptr::null_mut();
    assert!(DevicePointer::wrap(null).is_null());
}
Source

pub fn null() -> DevicePointer<T>

Returns a null device pointer.

Source

pub unsafe fn offset(self, count: isize) -> DevicePointer<T>

Calculates the offset from a device pointer.

count is in units of T; eg. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

If any of the following conditions are violated, the result is Undefined Behavior:

  • Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object.

  • The computed offset, in bytes, cannot overflow an isize.

  • The offset being in bounds cannot rely on “wrapping around” the address space. That is, the infinite-precision sum, in bytes must fit in a usize.

Consider using wrapping_offset instead if these constraints are difficult to satisfy. The only advantage of this method is that it enables more aggressive compiler optimizations.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.offset(1); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub fn wrapping_offset(self, count: isize) -> DevicePointer<T>

Calculates the offset from a device pointer using wrapping arithmetic.

count is in units of T; eg. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

The resulting pointer does not need to be in bounds, but it is potentially hazardous to dereference (which requires unsafe). In particular, the resulting pointer may not be used to access a different allocated object than the one self points to. In other words, x.wrapping_offset(y.wrapping_offset_from(x)) is not the same as y, and dereferencing it is undefined behavior unless x and y point into the same allocated object.

Always use .offset(count) instead when possible, because offset allows the compiler to optimize better. If you need to cross object boundaries, cast the pointer to an integer and do the arithmetic there.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.wrapping_offset(1); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub unsafe fn add(self, count: usize) -> DevicePointer<T>

Calculates the offset from a pointer (convenience for .offset(count as isize)).

count is in units of T; e.g. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

If any of the following conditions are violated, the result is Undefined Behavior:

  • Both the starting and resulting pointer must be either in bounds or one byte past the end of an allocated object.

  • The computed offset, in bytes, cannot overflow an isize.

  • The offset being in bounds cannot rely on “wrapping around” the address space. That is, the infinite-precision sum must fit in a usize.

Consider using wrapping_offset instead if these constraints are difficult to satisfy. The only advantage of this method is that it enables more aggressive compiler optimizations.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.add(1); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub unsafe fn sub(self, count: usize) -> DevicePointer<T>

Calculates the offset from a pointer (convenience for .offset((count as isize).wrapping_neg())).

count is in units of T; e.g. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

If any of the following conditions are violated, the result is Undefined Behavior:

  • Both the starting and resulting pointer must be either in bounds or one byte past the end of an allocated object.

  • The computed offset, in bytes, cannot overflow an isize.

  • The offset being in bounds cannot rely on “wrapping around” the address space. That is, the infinite-precision sum must fit in a usize.

Consider using wrapping_offset instead if these constraints are difficult to satisfy. The only advantage of this method is that it enables more aggressive compiler optimizations.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.add(4).sub(3); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub fn wrapping_add(self, count: usize) -> DevicePointer<T>

Calculates the offset from a pointer using wrapping arithmetic. (convenience for .wrapping_offset(count as isize))

count is in units of T; e.g. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

The resulting pointer does not need to be in bounds, but it is potentially hazardous to dereference.

Always use .add(count) instead when possible, because add allows the compiler to optimize better.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.wrapping_add(1); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub fn wrapping_sub(self, count: usize) -> DevicePointer<T>

Calculates the offset from a pointer using wrapping arithmetic. (convenience for .wrapping_offset((count as isize).wrapping_sub()))

count is in units of T; e.g. a count of 3 represents a pointer offset of 3 * size_of::<T>() bytes.

§Safety

The resulting pointer does not need to be in bounds, but it is potentially hazardous to dereference (which requires unsafe).

Always use .sub(count) instead when possible, because sub allows the compiler to optimize better.

§Examples
use cust::memory::*;
unsafe {
    let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
    let offset = dev_ptr.wrapping_add(4).wrapping_sub(3); // Points to the 2nd u64 in the buffer
    cuda_free(dev_ptr); // Must free the buffer using the original pointer
}
Source

pub fn cast<U>(self) -> DevicePointer<U>
where U: DeviceCopy,

Casts this device pointer to another type.

Trait Implementations§

Source§

impl<T> Clone for DevicePointer<T>
where T: Clone + DeviceCopy + ?Sized,

Source§

fn clone(&self) -> DevicePointer<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for DevicePointer<T>
where T: Debug + DeviceCopy + ?Sized,

Source§

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

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

impl<T> Hash for DevicePointer<T>
where T: Hash + DeviceCopy + ?Sized,

Source§

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

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> Ord for DevicePointer<T>
where T: Ord + DeviceCopy + ?Sized,

Source§

fn cmp(&self, other: &DevicePointer<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> PartialEq for DevicePointer<T>
where T: PartialEq + DeviceCopy + ?Sized,

Source§

fn eq(&self, other: &DevicePointer<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> PartialOrd for DevicePointer<T>
where T: PartialOrd + DeviceCopy + ?Sized,

Source§

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

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

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

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

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

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

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

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

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

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

impl<T> Pointer for DevicePointer<T>
where T: DeviceCopy,

Source§

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

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

impl<T> Copy for DevicePointer<T>
where T: Copy + DeviceCopy + ?Sized,

Source§

impl<T> DeviceCopy for DevicePointer<T>
where T: DeviceCopy + ?Sized,

Source§

impl<T> Eq for DevicePointer<T>
where T: Eq + DeviceCopy + ?Sized,

Source§

impl<T> StructuralPartialEq for DevicePointer<T>
where T: DeviceCopy + ?Sized,

Auto Trait Implementations§

§

impl<T> Freeze for DevicePointer<T>

§

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

§

impl<T> !Send for DevicePointer<T>

§

impl<T> !Sync for DevicePointer<T>

§

impl<T> Unpin for DevicePointer<T>

§

impl<T> UnwindSafe for DevicePointer<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeviceCopyExt for T
where T: DeviceCopy,

Source§

fn as_dbox(&self) -> Result<DeviceBox<Self>, CudaError>

Makes a new DeviceBox from this value.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,