cuda.core.VirtualMemoryResource#
- class cuda.core.VirtualMemoryResource(
- device_id: Device | int,
- config: VirtualMemoryResourceOptions | None = None,
Create a device memory resource that uses the CUDA VMM APIs to allocate memory.
- Parameters:
device_id (Device | int) – Device for which a memory resource is constructed.
config (VirtualMemoryResourceOptions, optional) – A configuration object for the VirtualMemoryResource
Warning
This is a low-level API that is provided only for convenience. Make sure you fully understand how CUDA Virtual Memory Management works before using this. Other MemoryResource subclasses in cuda.core should already meet the common needs.
Notes
Every buffer this resource returns is a
VirtualMemoryBufferthat owns its address reservations, physical allocations and mappings; closing the buffer releases them.deallocate()is not involved in that path.Methods
- __init__(*args, **kwargs)#
- allocate(
- self,
- size_t size,
- *,
- stream: Stream | GraphBuilder | None = None,
Allocate a buffer of the given size using CUDA virtual memory.
- Parameters:
size (int) – The size in bytes of the buffer to allocate. It is rounded up to the allocation granularity; the returned buffer reports the rounded size.
stream (
Stream|GraphBuilder, optional) – Keyword-only. The allocation itself is synchronous. A real stream is recorded as the buffer’s deallocation stream and synchronized when the buffer closes; with None or a default-stream token the legacy default stream of the resource’s device is recorded instead.
- Returns:
A buffer that owns its reservation, physical allocation and mapping.
- Return type:
- Raises:
CUDAError – If any CUDA driver API call fails during allocation. Nothing is left allocated when this method raises.
- deallocate(
- self,
- ptr: DevicePointerType,
- int size: int,
- *,
- stream: Stream | GraphBuilder | None = None,
Unmap and free one address range that was reserved and mapped outside this resource.
Buffers returned by
allocate()andmodify_allocation()free themselves when they close and never call this method. It exists for raw pointers wrapped withBuffer.from_handle()withmrset to this resource: the range must be exactly one reservation, and the caller must already have released its owncuMemCreatehandle, so the physical memory is freed by the unmap.- Parameters:
ptr (DevicePointerType) – The start of the reservation.
size (int) – The size of the reservation in bytes.
stream (
Stream|GraphBuilder, optional) – Keyword-only. If given,stream.sync()is called before the range is unmapped, except for a default-stream token on a host-located resource, which has no context to synchronize in.
- modify_allocation(
- self,
- Buffer buf: Buffer,
- size_t new_size,
- config: VirtualMemoryResourceOptions | None = None,
Grow a buffer of this resource to at least
new_sizebytes.The buffer passed in stays open and usable. The returned buffer aliases it: both map the same physical memory, which is freed when the last of the two closes. When the driver can extend the address range in place, the returned buffer has the same pointer; otherwise it has a new one and the existing contents are reachable through both.
This method is not thread-safe with respect to two buffers that share an address range.
- Parameters:
buf (VirtualMemoryBuffer) – A buffer returned by
allocate()or by this method.new_size (int) – The requested total size in bytes; rounded up to the granularity.
config (VirtualMemoryResourceOptions, optional) – Configuration for the new physical memory chunk only. Existing chunks keep the access they were created with, and the resource’s own configuration is unchanged. It must name the resource’s
location_typeand passes the same checks as the constructor.
- Returns:
bufitself when it already coversnew_size; otherwise a new buffer of the rounded size.- Return type:
- Raises:
TypeError – If
bufdid not come from this resource.ValueError – If
confignames a different location than the resource, or the constructor would reject it.RuntimeError – If
bufis closed, orconfigrequests GPUDirect RDMA on a device without support.CUDAError – If a driver call fails.
bufis untouched when this method raises.
Attributes
- config#
object
- Type:
config
- device#
object
- Type:
device
- device_id#
Get the device ID associated with this memory resource.
- Returns:
int: CUDA device ID. -1 if the memory resource allocates host memory
- is_device_accessible#
Indicates whether the allocated memory is accessible from the device.
- is_host_accessible#
Indicates whether the allocated memory is accessible from the host.
- is_ipc_enabled#
Return False. Buffers of this resource cannot be shared through IPC descriptors.
- is_managed#
bool
Whether buffers allocated by this resource are CUDA managed (unified) memory.
- Type:
MemoryResource.is_managed