cuda.core.ManagedBuffer#
- class cuda.core.ManagedBuffer#
Managed (unified) memory buffer with a property-style advice API.
Returned by
ManagedMemoryResource.allocate(), or wrap an existing managed-memory pointer withManagedBuffer.from_handle().Examples
>>> buf = mr.allocate(size) >>> buf.read_mostly = True >>> buf.preferred_location = Device(0) >>> buf.accessed_by.add(Device(1)) >>> buf.prefetch(Device(0), stream=stream)
Note
On CUDA 13 builds,
preferred_locationround-trips full NUMA information. On CUDA 12 builds,Host(numa_id=...)andHost.numa_current()are rejected withTypeErrorat the call boundary — onlyDevice(...)and the genericHost()are accepted. UseHost()to target the host on CUDA 12.Methods
- __init__(*args, **kwargs)#
- close(self, stream: Stream | GraphBuilder | None = None)#
Deallocate this buffer asynchronously on the given stream.
This buffer is released back to their memory resource asynchronously on the given stream.
- Parameters:
stream (
Stream|GraphBuilder, optional) – The stream object to use for asynchronous deallocation. If None, the deallocation stream stored in the handle is used.
- copy_from(
- self,
- Buffer src: Buffer,
- *,
- stream: Stream | GraphBuilder,
Copy from the src buffer to this buffer asynchronously on the given stream.
- Parameters:
src (
Buffer) – Source buffer to copy data fromstream (
Stream|GraphBuilder) – Keyword argument specifying the stream for the asynchronous copy
- copy_to(
- self,
- Buffer dst: Buffer = None,
- *,
- stream: Stream | GraphBuilder,
Copy from this buffer to the dst buffer asynchronously on the given stream.
Copies the data from this buffer to the provided dst buffer. If the dst buffer is not provided, then a new buffer is first allocated using the associated memory resource before the copy.
- Parameters:
dst (
Buffer, optional) – Destination buffer to copy data to. If not provided, a new buffer is allocated using this buffer’s memory resource.stream (
Stream|GraphBuilder) – Keyword argument specifying the stream for the asynchronous copy
- discard(*, stream: Stream | GraphBuilder) None#
Discard this range’s resident pages on
stream(CUDA 13+).
- discard_prefetch(
- location: Device | Host,
- *,
- stream: Stream | GraphBuilder,
Discard this range and prefetch to
locationonstream(CUDA 13+).
- fill(
- self,
- value: int | BufferProtocol,
- *,
- stream: Stream | GraphBuilder,
Fill this buffer with a repeating byte pattern.
- Parameters:
value (int |
collections.abc.Buffer) –int: Must be in range [0, 256). Converted to 1 byte.
collections.abc.Buffer: Must be 1, 2, or 4 bytes.
stream (
Stream|GraphBuilder) – Stream for the asynchronous fill operation.
- Raises:
TypeError – If value is not an int and does not support the buffer protocol.
ValueError – If value byte length is not 1, 2, or 4. If buffer size is not divisible by value byte length.
OverflowError – If int value is outside [0, 256).
- classmethod from_handle(
- ptr,
- size: int,
- mr: MemoryResource | None = None,
- owner: object | None = None,
Wrap an existing managed-memory pointer in a
ManagedBuffer.Use this when you have an externally-allocated managed pointer and want the property-style advice API (
read_mostly,preferred_location,accessed_by).- Parameters:
ptr (
DevicePointerT) – Pointer to a managed allocation.size (int) – Allocation size in bytes.
mr (
MemoryResource, optional) – Memory resource that ownsptr. When provided, itsdeallocateis called when the buffer is closed.owner (object, optional) – An object that keeps the underlying allocation alive.
ownerandmrcannot both be specified.
- classmethod from_ipc_descriptor(
- cls,
- mr: DeviceMemoryResource | PinnedMemoryResource,
- IPCBufferDescriptor ipc_descriptor: IPCBufferDescriptor,
- *,
- Stream stream: Stream,
Import a buffer that was exported from another process.
- Parameters:
mr (
DeviceMemoryResource|PinnedMemoryResource) – The IPC-enabled memory resource matching the exporting process.ipc_descriptor (
IPCBufferDescriptor) – The descriptor exported from another process.stream (
Stream) – Keyword-only. The stream used for asynchronous deallocation when the buffer is closed or garbage collected.
- prefetch(
- location: Device | Host,
- *,
- stream: Stream | GraphBuilder,
Prefetch this range to
locationonstream.
Attributes
- accessed_by#
Live set-like view of
set_accessed_bylocations.
- device_id#
int
Return the device ordinal of this buffer.
- Type:
Buffer.device_id
- handle#
DevicePointerType
Return the buffer handle object.
Caution
This handle is a Python object. To get the memory address of the underlying C handle, call
int(Buffer.handle).- Type:
Buffer.handle
- ipc_descriptor#
IPCBufferDescriptor
Descriptor for sharing this buffer with other processes.
- Type:
Buffer.ipc_descriptor
- is_device_accessible#
bool
Return True if this buffer can be accessed by the GPU, otherwise False.
- Type:
Buffer.is_device_accessible
- is_host_accessible#
bool
Return True if this buffer can be accessed by the CPU, otherwise False.
- Type:
Buffer.is_host_accessible
- is_managed#
bool
Return True if this buffer is CUDA managed (unified) memory, otherwise False.
- Type:
Buffer.is_managed
- is_mapped#
bool
Return True if this buffer is mapped into the process via IPC.
- Type:
Buffer.is_mapped
- memory_resource#
MemoryResource
Return the memory resource associated with this buffer.
- Type:
Buffer.memory_resource
- owner#
object
Return the object holding external allocation.
- Type:
Buffer.owner
- preferred_location#
Currently applied
set_preferred_locationtarget, orNone.On CUDA 13 builds, fully round-trips
Host(numa_id=N). On CUDA 12 the legacy attribute carries only a device ordinal (or-1for host), soHost(numa_id=N)set via the setter round-trips back asHost().
- read_mostly#
Whether
set_read_mostlyadvice is currently applied.
- size#
int
Return the memory size of this buffer.
- Type:
Buffer.size