Coverage for cuda/core/_memory/_device_memory_resource.pyx: 91.46%
82 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
5from __future__ import annotations
7from cuda.bindings cimport cydriver
8from cuda.core._memory._location cimport cumemlocation_from_id
9from cuda.core._memory._memory_pool cimport (
10 _MemPool, MP_check_open, MP_init_create_pool, MP_raise_release_threshold,
11)
12from cuda.core._memory cimport _ipc
13from cuda.core._memory._ipc cimport IPCAllocationHandle
14from cuda.core._rt cimport as_cu, get_device_mempool, get_last_error
15from cuda.core._utils.cuda_utils cimport (
16 check_or_create_options,
17 HANDLE_RETURN,
18)
20from dataclasses import dataclass
21import multiprocessing
22import platform # no-cython-lint
23import uuid
25from cuda.core._memory._peer_access_utils import PeerAccessibleBySetProxy, replace_peer_accessible_by
26from cuda.core._utils.cuda_utils import check_multiprocessing_start_method
28from typing import TYPE_CHECKING
30if TYPE_CHECKING:
31 from cuda.core._device import Device
33__all__ = ['DeviceMemoryResource', 'DeviceMemoryResourceOptions']
36@dataclass
37cdef class DeviceMemoryResourceOptions:
38 """Customizable :obj:`~_memory.DeviceMemoryResource` options.
40 Attributes
41 ----------
42 ipc_enabled : bool, optional
43 Specifies whether to create an IPC-enabled memory pool. When set to
44 True, the memory pool and its allocations can be shared with other
45 processes. (Default to False)
47 max_size : int, optional
48 Maximum pool size. When set to 0, defaults to a system-dependent value.
49 (Default to 0)
50 """
51 ipc_enabled : bool = False
52 max_size : int = 0
55cdef class DeviceMemoryResource(_MemPool):
56 """
57 A device memory resource managing a stream-ordered memory pool.
59 Parameters
60 ----------
61 device_id : Device | int
62 Device or Device ordinal for which a memory resource is constructed.
64 options : DeviceMemoryResourceOptions
65 Memory resource creation options.
67 If set to `None`, the memory resource uses the driver's current
68 stream-ordered memory pool for the specified `device_id`. If no memory
69 pool is set as current, the driver's default memory pool for the device
70 is used.
72 If not set to `None`, a new memory pool is created, which is owned by
73 the memory resource.
75 When using an existing (current or default) memory pool, the returned
76 device memory resource does not own the pool (`is_handle_owned` is
77 `False`), and closing the resource has no effect.
79 Notes
80 -----
81 To create an IPC-Enabled memory resource (MR) that is capable of sharing
82 allocations between processes, specify ``ipc_enabled=True`` in the initializer
83 option. Sharing an allocation is a two-step procedure that involves
84 mapping a memory resource and then mapping buffers owned by that resource.
85 These steps can be accomplished in several ways.
87 An IPC-enabled memory resource can allocate memory buffers but cannot
88 receive shared buffers. Mapping an MR to another process creates a "mapped
89 memory resource" (MMR). An MMR cannot allocate memory buffers and can only
90 receive shared buffers. MRs and MMRs are both of type
91 :class:`DeviceMemoryResource` and can be distinguished via
92 :attr:`DeviceMemoryResource.is_mapped`.
94 An MR is shared via an allocation handle accessed through the
95 :attr:`DeviceMemoryResource.allocation_handle` property. The allocation
96 handle has a platform-specific interpretation; however, memory IPC is
97 currently only supported for Linux, and in that case allocation handles
98 are file descriptors. After sending an allocation handle to another
99 process, it can be used to create an MMR by invoking
100 :meth:`DeviceMemoryResource.from_allocation_handle`.
102 Buffers can be shared as serializable descriptors accessed through the
103 :attr:`Buffer.ipc_descriptor` property. In a receiving process, a shared
104 buffer is created by invoking :meth:`Buffer.from_ipc_descriptor` with an
105 MMR and buffer descriptor, where the MMR corresponds to the MR that
106 created the described buffer.
108 To help manage the association between memory resources and buffers, a
109 registry is provided. Every MR has a unique identifier (UUID). MMRs can be
110 registered by calling :meth:`DeviceMemoryResource.register` with the UUID
111 of the corresponding MR. Registered MMRs can be looked up via
112 :meth:`DeviceMemoryResource.from_registry`. When registering MMRs in this
113 way, the use of buffer descriptors can be avoided. Instead, buffer objects
114 can themselves be serialized and transferred directly. Serialization embeds
115 the UUID, which is used to locate the correct MMR during reconstruction.
117 IPC-enabled memory resources interoperate with the :mod:`multiprocessing`
118 module to provide a simplified interface. This approach can avoid direct
119 use of allocation handles, buffer descriptors, MMRs, and the registry. When
120 using :mod:`multiprocessing` to spawn processes or send objects through
121 communication channels such as :class:`multiprocessing.Queue`,
122 :class:`multiprocessing.Pipe`, or :class:`multiprocessing.Connection`,
123 :class:`Buffer` objects may be sent directly, and in such cases the process
124 for creating MMRs and mapping buffers will be handled automatically.
126 For greater efficiency when transferring many buffers, one may also send
127 MRs and buffers separately. When an MR is sent via :mod:`multiprocessing`,
128 an MMR is created and registered in the receiving process, for the device
129 with the same UUID as the MR's device, whatever its ordinal is there; if
130 that device is not visible, unpickling raises ``RuntimeError``. Subsequently,
131 buffers may be serialized and transferred using ordinary :mod:`pickle`
132 methods. The reconstruction procedure uses the registry to find the
133 associated MMR. Unpickling a :class:`Buffer` performs an IPC import from
134 the embedded descriptor; only unpickle buffers received from trusted peers.
136 Warning
137 -------
138 IPC descriptors and pickled buffers cross a trust boundary between
139 cooperating same-host processes. A malicious peer can supply crafted
140 descriptor fields. Use :meth:`Buffer.from_ipc_descriptor` only with
141 descriptors from trusted peers, and do not unpickle buffers from
142 untrusted sources.
143 """
145 def __cinit__(self, *args, **kwargs) -> None:
146 self._dev_id = cydriver.CU_DEVICE_INVALID 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8b!dkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
148 def __init__(
149 self,
150 device_id: Device | int,
151 options: DeviceMemoryResourceOptions | None = None
152 ) -> None:
153 _DMR_init(self, device_id, options) 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
155 def __reduce__(self) -> tuple[object, ...]:
156 MP_check_open(self) 1bxde
157 return DeviceMemoryResource.from_registry, (self.uuid,) 1bxde
159 @staticmethod
160 def from_registry(uuid: uuid.UUID) -> DeviceMemoryResource: # no-cython-lint
161 """
162 Obtain a registered mapped memory resource.
164 Raises
165 ------
166 RuntimeError
167 If no mapped memory resource is found in the registry.
168 """
169 return <DeviceMemoryResource>(_ipc.MP_from_registry(uuid)) 2a y P Q R S !d
171 def register(self, uuid: uuid.UUID) -> DeviceMemoryResource: # no-cython-lint
172 """
173 Register a mapped memory resource.
175 Returns
176 -------
177 The registered mapped memory resource. If one was previously registered
178 with the given key, it is returned.
180 Raises
181 ------
182 RuntimeError
183 If the memory resource is not IPC-enabled.
184 ValueError
185 If the memory resource already has a different UUID.
186 """
187 return <DeviceMemoryResource>(_ipc.MP_register(self, uuid)) 2`da y weP Q R S !d
189 @classmethod
190 def from_allocation_handle(
191 cls, device_id: Device | int, alloc_handle: int | IPCAllocationHandle
192 ) -> DeviceMemoryResource:
193 """Create a device memory resource from an allocation handle.
195 Construct a new `DeviceMemoryResource` instance that imports a memory
196 pool from a shareable handle. The memory pool is marked as owned, and
197 the resource is associated with the specified `device_id`.
199 Parameters
200 ----------
201 device_id : int | Device
202 The ID of the device or a Device object for which the memory
203 resource is created.
205 alloc_handle : int | IPCAllocationHandle
206 The shareable handle of the device memory resource to import. If an
207 integer is supplied, it must represent a valid platform-specific
208 handle. It is the caller's responsibility to close that handle.
210 Returns
211 -------
212 A new device memory resource instance with the imported handle. If
213 the handle's memory resource is already registered in this process,
214 that resource is returned with its original device.
216 Raises
217 ------
218 TypeError
219 If the handle refers to a host memory pool, which must be imported
220 with :meth:`PinnedMemoryResource.from_allocation_handle`. The
221 pool's kind is checked with a CUDA 13 build of ``cuda.core`` and a
222 13.2 or later driver. Otherwise it is checked only when the
223 handle's memory resource is already registered in this process.
224 The pool's device is not checked against ``device_id``.
225 """
226 from .._device import Device 2`dc a !d
227 dev_id = Device(device_id).device_id 2`dc a !d
228 return <DeviceMemoryResource>( 2c a !d
229 _ipc.MP_from_allocation_handle(cls, alloc_handle, _DMR_init_imported, dev_id)) 2`dc a !d
231 @property
232 def allocation_handle(self) -> IPCAllocationHandle:
233 """Shareable handle for this memory pool (requires IPC).
235 The handle can be used to share the memory pool with other processes.
236 The handle is cached in this `MemoryResource` and owned by it.
237 """
238 MP_check_open(self) 2#db $d%df s `dc a 'd(d)d*d{d|d}d~daebecedeeefegeheiejekelemeneoepeqereseteue+d,d-d.d/d:d;dve=d?d@d[dg h ]d^d_di j k l d e z v m w n o
239 if not self.is_ipc_enabled: 2#db $d%df s `dc a 'd(d)d*d{d|d}d~daebecedeeefegeheiejekelemeneoepeqereseteue+d,d-d.d/d:d;dve=d?d@d[dg h ]d^d_di j k l d e v m w n o
240 raise RuntimeError("Memory resource is not IPC-enabled") 1v
241 return self._ipc_data._alloc_handle 2#db $d%df s `dc a 'd(d)d*d{d|d}d~daebecedeeefegeheiejekelemeneoepeqereseteue+d,d-d.d/d:d;dve=d?d@d[dg h ]d^d_di j k l d e m w n o
243 @property
244 def device_id(self) -> int:
245 """The associated device ordinal."""
246 return self._dev_id 2H I J K L M N O xeye} ~ zeabAebbBeCeDeEe#db $d%df c 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_d!di j k l d e 9b!bA B t *bm n o C D E F BcCcV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { G
248 @property
249 def peer_accessible_by(self) -> PeerAccessibleBySetProxy:
250 """
251 Get or set the devices that can access allocations from this memory
252 pool. Access can be modified at any time and affects all allocations
253 from this memory pool.
255 Returns a set-like proxy of :obj:`~_device.Device` objects that manages
256 peer access. Inputs are accepted as either :obj:`~_device.Device`
257 objects or device-ordinal :class:`int` values.
259 Examples
260 --------
261 >>> dmr = DeviceMemoryResource(0)
262 >>> dmr.peer_accessible_by = {1} # grant access to device 1
263 >>> assert 1 in dmr.peer_accessible_by
264 >>> dmr.peer_accessible_by.add(2) # update access to include device 2
265 >>> dmr.peer_accessible_by = [] # revoke peer access
266 """
267 MP_check_open(self) 1zTu
268 return PeerAccessibleBySetProxy(self) 1zTu
270 @peer_accessible_by.setter
271 def peer_accessible_by(self, devices) -> None:
272 replace_peer_accessible_by(self, devices) 1u
274 @property
275 def is_device_accessible(self) -> bool:
276 """Return True. This memory resource provides device-accessible buffers."""
277 return True 1ABtUCDEFVWXYZ0123456789!#$%'()*+,-./:;=?@[]^_`{G
279 @property
280 def is_host_accessible(self) -> bool:
281 """Return False. This memory resource does not provide host-accessible buffers."""
282 return False 2'bA B t C D E F G
285cdef inline _DMR_init(DeviceMemoryResource self, device_id, options):
286 from .._device import Device 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
287 cdef int dev_id = Device(device_id).device_id 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
288 cdef DeviceMemoryResourceOptions opts = check_or_create_options( 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
289 DeviceMemoryResourceOptions, options, "DeviceMemoryResource options",
290 keep_none=True
291 )
292 cdef bint ipc_enabled = False 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
293 cdef size_t max_size = 0 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
295 self._dev_id = dev_id 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
297 if opts is not None: 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
298 ipc_enabled = opts.ipc_enabled 2H I J K L M N O | cbdbebfbb gbx hbibf jbkbs lbc mba y nbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bi j k l d e #b$b%b(bt )bU r u +b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjckcm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAc
299 if ipc_enabled and not _ipc.is_supported(): 2H I J K L M N O | cbdbebfbb gbx hbibf jbkbs lbc mba y nbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bi j k l d e #b$b%b(bt )bU r u +b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjckcm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAc
300 raise RuntimeError(f"IPC is not available on {platform.system()}")
301 max_size = opts.max_size 2H I J K L M N O | cbdbebfbb gbx hbibf jbkbs lbc mba y nbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bi j k l d e #b$b%b(bt )bU r u +b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjckcm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAc
303 if opts is None: 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!cH I J K L M N O #c$c%c'c(c)c*c+c| ,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdcbdbebfbb gbx hbibf jbkbs lbc mba y nbP Q R S obpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bkdi j k l d e 9b!bldmdnd#b$b%bodpdqdrd'bsdtdudvdwdxdydzdz A B (bT t )bU r q u *b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjcAdkcBdCdDdEdm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAcFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
304 self._h_pool = get_device_mempool(dev_id) 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c$c%c'c(c)c*c+c,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdP Q R S kd9b!bldmdndodpdqdrd'bsdtdudvdwdxdydzdz A B T r q *bAdBdCdDdEdFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
305 if not self._h_pool: 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c$c%c'c(c)c*c+c,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdP Q R S kd9b!bldmdndodpdqdrd'bsdtdudvdwdxdydzdz A B T r q *bAdBdCdDdEdFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
306 HANDLE_RETURN(get_last_error())
307 raise RuntimeError(
308 f"Failed to initialize DeviceMemoryResource for device {dev_id}: "
309 "cuda-core returned an empty memory pool handle without recording a CUDA error. "
310 "This is an internal cuda-core error; please report it with your CUDA driver, "
311 "CUDA Toolkit, and cuda-python versions."
312 )
313 self._mempool_owned = False 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c$c%c'c(c)c*c+c,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdP Q R S kd9b!bldmdndodpdqdrd'bsdtdudvdwdxdydzdz A B T r q *bAdBdCdDdEdFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
314 MP_raise_release_threshold(self) 2DcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c$c%c'c(c)c*c+c,c-c.c} ~ /c:c;c=c?c@c[cab]c^cbb_c`c{c|c}c~cadbdcdddedfdgdhdidjdP Q R S kd9b!bldmdndodpdqdrd'bsdtdudvdwdxdydzdz A B T r q *bAdBdCdDdEdFdGdC D E HdIdJdKdLdMdNdF OdPdQdRdSdTdUdVdWdXdYdZd0d1d2d3dBcCc4dV W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? 5d6d7d@ [ 8d] ^ _ 9d` { G
315 else:
316 MP_init_create_pool( 2H I J K L M N O | cbdbebfbb gbx hbibf jbkbs lbc mba y nbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bi j k l d e #b$b%b(bt )bU r u +b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjckcm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAc
317 self,
318 cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE,
319 dev_id,
320 cydriver.CUmemAllocationType.CU_MEM_ALLOCATION_TYPE_PINNED,
321 ipc_enabled,
322 max_size, 2H I J K L M N O | cbdbebfbb gbx hbibf jbkbs lbc mba y nbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b3b4b5bg h 6b7b8bi j k l d e #b$b%b(bt )bU r u +b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccv dcecfcgchcicjckcm w n o lcmcncocpcqcrcsctcucvcwcxcyczcAc
323 )
326cdef int _DMR_init_imported(_MemPool mr, object dev_id) except? -1:
327 cdef cydriver.CUmemLocation loc
328 _ipc.MP_check_import_location(mr, False, &loc) 2c a !d
329 # Runs before the resource is registered: a concurrent import of the same
330 # UUID returns it from the registry and must not write to it.
331 (<DeviceMemoryResource>mr)._dev_id = dev_id 2c a !d
332 return 0 2c a !d
335# Note: this is referenced in instructions to debug nvbug 5698116.
336cpdef str DMR_mempool_get_access(DeviceMemoryResource dmr, int device_id):
337 """
338 Probes peer access from the given device using cuMemPoolGetAccess.
340 Parameters
341 ----------
342 device_id : int or Device
343 The device to query access for.
345 Returns
346 -------
347 str
348 Access permissions: "rw" for read-write, "r" for read-only, "" for no access.
349 """
350 from .._device import Device 1q
352 cdef int dev_id = Device(device_id).device_id 1q
353 cdef cydriver.CUmemAccess_flags flags
354 cdef cydriver.CUmemLocation location = cumemlocation_from_id( 1q
355 cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE, dev_id)
357 with nogil: 1q
358 HANDLE_RETURN(cydriver.cuMemPoolGetAccess(&flags, as_cu(dmr._h_pool), &location)) 1q
360 if flags == cydriver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE: 1q
361 return "rw" 1q
362 elif flags == cydriver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READ:
363 return "r"
364 else:
365 return ""
368def _deep_reduce_device_memory_resource(mr) -> tuple[object, ...]:
369 check_multiprocessing_start_method() 2#db $d%df 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_di j k l d e m n o
370 from .._device import Device 2#db $d%df 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_di j k l d e m n o
371 # Device ordinals are per process; the receiver maps the UUID to its own device.
372 device_uuid = Device(mr.device_id).uuid 2#db $d%df 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_di j k l d e m n o
373 alloc_handle = mr.allocation_handle 2#db $d%df 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_di j k l d e m n o
374 return _reconstruct_device_memory_resource, (type(mr), device_uuid, alloc_handle) 2#db $d%df 'd(d)d*d+d,d-d.d/d:d;d=d?d@d[dg h ]d^d_di j k l d e m n o
377def _reconstruct_device_memory_resource(
378 cls: type, device_uuid: str, IPCAllocationHandle alloc_handle not None: IPCAllocationHandle
379) -> DeviceMemoryResource:
380 from .._device import _device_from_uuid 2!d
381 return cls.from_allocation_handle(_device_from_uuid(device_uuid), alloc_handle) 2!d
384multiprocessing.reduction.register(DeviceMemoryResource, _deep_reduce_device_memory_resource)