cuda.core.experimental.utils.StridedMemoryView

class cuda.core.experimental.utils.StridedMemoryView(obj=None, stream_ptr=None)

A dataclass holding metadata of a strided dense array/tensor.

A StridedMemoryView instance can be created in two ways:

  1. Using the args_viewable_as_strided_memory decorator (recommended)

  2. Explicit construction, see below

This object supports both DLPack (up to v1.0) and CUDA Array Interface (CAI) v3. When wrapping an arbitrary object it will try the DLPack protocol first, then the CAI protocol. A BufferError is raised if neither is supported.

Since either way would take a consumer stream, for DLPack it is passed to obj.__dlpack__() as-is (except for None, see below); for CAI, a stream order will be established between the consumer stream and the producer stream (from obj.__cuda_array_interface__()["stream"]), as if cudaStreamWaitEvent is called by this method.

To opt-out of the stream ordering operation in either DLPack or CAI, please pass stream_ptr=-1. Note that this deviates (on purpose) from the semantics of obj.__dlpack__(stream=None, ...) since cuda.core does not encourage using the (legacy) default/null stream, but is consistent with the CAI’s semantics. For DLPack, stream=-1 will be internally passed to obj.__dlpack__() instead.

ptr

Pointer to the tensor buffer (as a Python int).

Type:

int

shape

Shape of the tensor.

Type:

tuple

strides

Strides of the tensor (in counts, not bytes).

Type:

tuple

dtype

Data type of the tensor.

Type:

numpy.dtype

device_id

The device ID for where the tensor is located. It is -1 for CPU tensors (meaning those only accessible from the host).

Type:

int

is_device_accessible

Whether the tensor data can be accessed on the GPU.

Type:

bool

readonly

Whether the tensor data can be modified in place.

Type:

bool

exporting_obj

A reference to the original tensor object that is being viewed.

Type:

Any

Parameters:
  • obj (Any) – Any objects that supports either DLPack (up to v1.0) or CUDA Array Interface (v3).

  • stream_ptr (int) – The pointer address (as Python int) to the consumer stream. Stream ordering will be properly established unless -1 is passed.