cuda.core.VirtualMemoryBuffer#
- class cuda.core.VirtualMemoryBuffer#
A
Bufferreturned byVirtualMemoryResource.The buffer owns its address reservations, physical allocations and mappings through its device pointer handle; closing it is the only way to release them. A buffer returned by
VirtualMemoryResource.modify_allocation()aliases the buffer it was grown from: the two share their physical memory, and that memory is freed when the last buffer that maps it closes.Methods
- __init__(*args, **kwargs)#
- close(
- self,
- stream: Stream | GraphBuilder | None = None,
Release this buffer’s share of its address range.
The mappings, reservations and physical allocations go away when the last buffer that maps them closes. Before it unmaps, the resource synchronizes every deallocation stream the buffers of the range recorded. Virtual memory deallocation is synchronous and cannot be captured. When the stream this close uses, given or recorded, is not a default stream and is capturing, the call raises and leaves the buffer open. A default stream is checked when the range is released instead: if synchronizing it would disturb a capture in its context, the release reports a
CUDAWarningand unmaps without synchronizing that stream.- Parameters:
stream (
Stream|GraphBuilder, optional) – If given, replaces the recorded deallocation stream, as forBuffer.close().
- copy_from(
- self,
- Buffer src: Buffer,
- *,
- stream: Stream | GraphBuilder,
- options: CopyOptions | None = None,
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 copyoptions (
CopyOptions, optional) – Transfer hints (source access order, location hints, overlap mode). Honored when cuda.bindings and the driver are both CUDA 13.2 or newer. Not accepted withLEGACY_DEFAULT_STREAM; usePER_THREAD_DEFAULT_STREAMinstead. Not accepted with a capturing stream either, since a graph cannot represent these attributes; usegraph.GraphNode.memcpy()for a plain, non-attributed copy node, or passoptions=None. On an older cuda.bindings/driver,src_access_ordervalues ofSTREAMandANYare silently ignored;DURING_API_CALLraises instead of silently downgrading its guarantee.
- Raises:
TypeError – If
optionsis not aCopyOptionsinstance, or ifoptionsis given together withLEGACY_DEFAULT_STREAMor a stream currently in graph capture mode.RuntimeError – If
options.src_access_orderisDURING_API_CALLand cuda.bindings or the driver is older than CUDA 13.2: falling back to a plain copy cannot honor that guarantee.
- copy_to(
- self,
- Buffer dst: Buffer | None = None,
- *,
- stream: Stream | GraphBuilder,
- options: CopyOptions | None = None,
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 copyoptions (
CopyOptions, optional) – Transfer hints (source access order, location hints, overlap mode). Honored when cuda.bindings and the driver are both CUDA 13.2 or newer. Not accepted withLEGACY_DEFAULT_STREAM; usePER_THREAD_DEFAULT_STREAMinstead. Not accepted with a capturing stream either, since a graph cannot represent these attributes; usegraph.GraphNode.memcpy()for a plain, non-attributed copy node, or passoptions=None. On an older cuda.bindings/driver,src_access_ordervalues ofSTREAMandANYare silently ignored;DURING_API_CALLraises instead of silently downgrading its guarantee.
- Raises:
TypeError – If
optionsis not aCopyOptionsinstance, or ifoptionsis given together withLEGACY_DEFAULT_STREAMor a stream currently in graph capture mode.RuntimeError – If
options.src_access_orderisDURING_API_CALLand cuda.bindings or the driver is older than CUDA 13.2: falling back to a plain copy cannot honor that guarantee.
- 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).
- static from_handle(
- ptr: DevicePointerType,
- size_t size,
- MemoryResource mr: MemoryResource | None = None,
- owner: object | None = None,
- *,
- stream: Stream | GraphBuilder | None = None,
Create a new
Bufferobject from a pointer.- Parameters:
ptr (
DevicePointerType) – Allocated buffer handle objectsize (int) – Memory size of the buffer
mr (
MemoryResource, optional) – Memory resource associated with the buffer. When provided,MemoryResource.deallocate()is called when the buffer is closed or garbage collected.owner (object, optional) – An object holding external allocation that the
ptrpoints to. The reference is kept as long as the buffer is alive. Theownerandmrcannot be specified together.stream (
Stream|GraphBuilder, optional) – Keyword-only. The stream used to order the buffer’s deallocation whenmrowns the pointer. Defaults todefault_stream(). Recording a default-stream token requires a CUDA context to be current. Host-only resources (mr.is_device_accessibleisFalse) record no stream and need no context. If the buffer may be freed from a different host thread, pass a stream other than the per-thread default stream, which refers to a different stream on each thread.
Note
When neither
mrnorowneris specified, this creates a non-owning reference. The pointer will NOT be freed when theBufferis closed or garbage collected.
- 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.
Note
The descriptor payload and
sizeare supplied by the exporting peer and must be treated as untrusted input unless the peer is known to be cooperating.
- set_deallocation_stream(
- self,
- stream: Stream | GraphBuilder,
Change the stream that orders this buffer’s eventual deallocation.
The buffer remains open and usable. A later
close()without a stream, garbage collection, or release of the final retained device pointer handle uses the replacement stream.This method does not synchronize streams or establish dependencies. The caller must ensure that allocation and all accesses are ordered before the deallocation on
stream.- Parameters:
stream (
Stream|GraphBuilder) – The stream to use for eventual asynchronous deallocation.- Raises:
RuntimeError – If the buffer is already closed, or if a default-stream token cannot be bound because no CUDA context is current.
TypeError – If
streamisNoneor is not an accepted stream object.
Notes
Synchronizing concurrent mutation and destruction of the same buffer is the caller’s responsibility.
Attributes
- device_id#
int
Return the device ordinal of this buffer, or -1 for memory not bound to a device.
- Type:
Buffer.device_id
- handle#
int
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_closed#
bool
Whether this buffer has been closed.
- Type:
Buffer.is_closed
- 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
- size#
int
Return the memory size of this buffer.
- Type:
Buffer.size