IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

DevicePointer

struct DevicePointer[dtype: DType]

A host-side representation of a pointer to device memory that resides within an owning DeviceBuffer.

  • Supports pointer arithmetic which may result in a new DevicePointer instance referring to the same DeviceBuffer with a new offset.
  • Supports equality comparison and ordering of DevicePointers pointing into the same DeviceBuffer.
  • May support accessing device pointer address on supported hardware.
  • Does not support load/store operations.

At the device function execution boundary a DevicePointer is transformed into an UnsafePointer on the device at the point of being handed over to the device driver.

Parameters

  • dtype (DType): Data dtype to be stored in the pointer.

Implemented traits

AnyType, Copyable, DevicePassable, Equatable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, Writable

comptime members

device_type

comptime device_type = UnsafePointer[Scalar[dtype], MutAnyOrigin]

DevicePointer is remapped to UnsafePointer when passed to accelerator devices.

Methods

__init__

def __init__(out self, buffer: DeviceBuffer[dtype])

Constructs a DevicePointer referencing the start of buffer.

Args:

Raises:

If buffer has size 0.

def __init__(out self, buffer: DeviceBuffer[dtype], offset: Int)

Constructs a DevicePointer into buffer at offset with size elements in range.

Args:

  • buffer (DeviceBuffer[dtype]): The owning DeviceBuffer this pointer references.
  • offset (Int): Element offset from the start of buffer.

Raises:

If buffer has size 0, or if offset is outside the half-open range [0, len(buffer)).

__lt__

def __lt__(self, other: Self) -> Bool

Returns True if self precedes other within the same buffer.

Args:

  • other (Self): The other DevicePointer to compare.

Returns:

Bool: True if self is ordered before other.

Raises:

If self and other reference different DeviceBuffers.

__le__

def __le__(self, other: Self) -> Bool

Returns True if self precedes or equals other within the same buffer.

Args:

  • other (Self): The other DevicePointer to compare.

Returns:

Bool: True if self is ordered before or equal to other.

Raises:

If self and other reference different DeviceBuffers.

__eq__

def __eq__(self, other: Self) -> Bool

Returns True if self and other reference the same buffer and offset.

Args:

  • other (Self): The other DevicePointer to compare.

Returns:

Bool: True if equal.

__gt__

def __gt__(self, other: Self) -> Bool

Returns True if self follows other within the same buffer.

Args:

  • other (Self): The other DevicePointer to compare.

Returns:

Bool: True if self is ordered after other.

Raises:

If self and other reference different DeviceBuffers.

__ge__

def __ge__(self, other: Self) -> Bool

Returns True if self follows or equals other within the same buffer.

Args:

  • other (Self): The other DevicePointer to compare.

Returns:

Bool: True if self is ordered after or equal to other.

Raises:

If self and other reference different DeviceBuffers.

__add__

def __add__(self, n: Int) -> Self

Returns a new DevicePointer offset forward by n elements.

Args:

  • n (Int): Number of elements to offset by.

Returns:

Self: A new DevicePointer referencing the same DeviceBuffer at the new offset.

Raises:

If the resulting offset is outside the bounds of the owning DeviceBuffer.

__sub__

def __sub__(self, n: Int) -> Self

Returns a new DevicePointer offset backward by n elements.

Args:

  • n (Int): Number of elements to offset by.

Returns:

Self: A new DevicePointer referencing the same DeviceBuffer at the new offset.

Raises:

If the resulting offset is outside the bounds of the owning DeviceBuffer.

__iadd__

def __iadd__(mut self, n: Int)

Offsets this DevicePointer forward by n elements in place.

Args:

  • n (Int): Number of elements to offset by.

Raises:

If the resulting offset is outside the bounds of the owning DeviceBuffer.

__isub__

def __isub__(mut self, n: Int)

Offsets this DevicePointer backward by n elements in place.

Args:

  • n (Int): Number of elements to offset by.

Raises:

If the resulting offset is outside the bounds of the owning DeviceBuffer.

buffer

def buffer(self) -> DeviceBuffer[dtype]

Returns the owning DeviceBuffer this pointer references.

Returns:

DeviceBuffer[dtype]: The owning DeviceBuffer.

offset

def offset(self) -> Int

Returns the element offset from the start of the owning buffer.

Returns:

Int: The element offset.

write_to

def write_to(self, mut writer: T)

Writes a string representation of this DevicePointer to writer.

Args:

  • writer (T): The writer to output the formatted string to.

get_type_name

static def get_type_name() -> String

Gets this type's name, for use in error messages when handing arguments to kernels.

Returns:

String: This type's name.