Struct cust::memory::DevicePointer
source · [−]#[repr(transparent)]pub struct DevicePointer<T: ?Sized + DeviceCopy> { /* 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
sourceimpl<T: ?Sized + DeviceCopy> DevicePointer<T>
impl<T: ?Sized + DeviceCopy> DevicePointer<T>
sourcepub fn as_ptr(&self) -> *const T
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!
sourcepub fn as_mut_ptr(&self) -> *mut T
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!
sourcepub fn as_raw(&self) -> CUdeviceptr
pub fn as_raw(&self) -> CUdeviceptr
Returns the contained CUdeviceptr.
sourcepub fn from_raw(ptr: CUdeviceptr) -> Self
pub fn from_raw(ptr: CUdeviceptr) -> Self
Create a DevicePointer from a raw CUDA pointer
sourcepub fn is_null(self) -> bool
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());
}
sourcepub unsafe fn offset(self, count: isize) -> Self where
T: Sized,
pub unsafe fn offset(self, count: isize) -> Self where
T: Sized,
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
}
sourcepub fn wrapping_offset(self, count: isize) -> Self where
T: Sized,
pub fn wrapping_offset(self, count: isize) -> Self where
T: Sized,
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
}
sourcepub unsafe fn add(self, count: usize) -> Self where
T: Sized,
pub unsafe fn add(self, count: usize) -> Self where
T: Sized,
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
}
sourcepub unsafe fn sub(self, count: usize) -> Self where
T: Sized,
pub unsafe fn sub(self, count: usize) -> Self where
T: Sized,
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
}
sourcepub fn wrapping_add(self, count: usize) -> Self where
T: Sized,
pub fn wrapping_add(self, count: usize) -> Self where
T: Sized,
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
}
sourcepub fn wrapping_sub(self, count: usize) -> Self where
T: Sized,
pub fn wrapping_sub(self, count: usize) -> Self where
T: Sized,
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
}
sourcepub fn cast<U: DeviceCopy>(self) -> DevicePointer<U>
pub fn cast<U: DeviceCopy>(self) -> DevicePointer<U>
Casts this device pointer to another type.
Trait Implementations
sourceimpl<T: Clone + ?Sized + DeviceCopy> Clone for DevicePointer<T>
impl<T: Clone + ?Sized + DeviceCopy> Clone for DevicePointer<T>
sourcefn clone(&self) -> DevicePointer<T>
fn clone(&self) -> DevicePointer<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T: Debug + ?Sized + DeviceCopy> Debug for DevicePointer<T>
impl<T: Debug + ?Sized + DeviceCopy> Debug for DevicePointer<T>
sourceimpl<T: Hash + ?Sized + DeviceCopy> Hash for DevicePointer<T>
impl<T: Hash + ?Sized + DeviceCopy> Hash for DevicePointer<T>
sourceimpl<T: Ord + ?Sized + DeviceCopy> Ord for DevicePointer<T>
impl<T: Ord + ?Sized + DeviceCopy> Ord for DevicePointer<T>
sourceimpl<T: PartialEq + ?Sized + DeviceCopy> PartialEq<DevicePointer<T>> for DevicePointer<T>
impl<T: PartialEq + ?Sized + DeviceCopy> PartialEq<DevicePointer<T>> for DevicePointer<T>
sourcefn eq(&self, other: &DevicePointer<T>) -> bool
fn eq(&self, other: &DevicePointer<T>) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &DevicePointer<T>) -> bool
fn ne(&self, other: &DevicePointer<T>) -> bool
This method tests for !=
.
sourceimpl<T: PartialOrd + ?Sized + DeviceCopy> PartialOrd<DevicePointer<T>> for DevicePointer<T>
impl<T: PartialOrd + ?Sized + DeviceCopy> PartialOrd<DevicePointer<T>> for DevicePointer<T>
sourcefn partial_cmp(&self, other: &DevicePointer<T>) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<T: DeviceCopy> Pointer for DevicePointer<T>
impl<T: DeviceCopy> Pointer for DevicePointer<T>
impl<T: Copy + ?Sized + DeviceCopy> Copy for DevicePointer<T>
impl<T: ?Sized + DeviceCopy> DeviceCopy for DevicePointer<T>
impl<T: Eq + ?Sized + DeviceCopy> Eq for DevicePointer<T>
impl<T: ?Sized + DeviceCopy> StructuralEq for DevicePointer<T>
impl<T: ?Sized + DeviceCopy> StructuralPartialEq for DevicePointer<T>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> DeviceCopyExt for T where
T: DeviceCopy,
impl<T> DeviceCopyExt for T where
T: DeviceCopy,
sourcefn as_dbox(&self) -> CudaResult<DeviceBox<Self>>
fn as_dbox(&self) -> CudaResult<DeviceBox<Self>>
Makes a new DeviceBox
from this value.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more