Coverage for cuda/core/_memory/_pinned_memory_resource.pyx: 71.97%

132 statements  

« 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 

4  

5from __future__ import annotations 

6  

7cimport cython 

8from cuda.bindings cimport cydriver 

9from cuda.core._memory._buffer cimport Buffer 

10from cuda.core._memory._memory_pool cimport ( 

11 _MemPool, 

12 _MP_allocate, 

13 MP_check_open, 

14 MP_init_create_pool, 

15 MP_init_current_pool, 

16 MP_set_access, 

17) 

18from cuda.core._memory cimport _ipc 

19from cuda.core._memory._ipc cimport IPCAllocationHandle 

20from cuda.core._memory._location cimport cumemlocation_from_id 

21from cuda.core._stream cimport Stream, Stream_accept 

22from cuda.core._utils.cuda_utils cimport ( 

23 check_or_create_options, 

24 HANDLE_RETURN, 

25) 

26from libc.limits cimport INT_MAX 

27from libcpp.vector cimport vector 

28  

29from dataclasses import dataclass 

30import multiprocessing 

31import operator 

32import platform # no-cython-lint 

33import uuid 

34  

35from cuda.core._utils.cuda_utils import check_multiprocessing_start_method 

36  

37from typing import TYPE_CHECKING 

38  

39if TYPE_CHECKING: 

40 from cuda.core.graph import GraphBuilder 

41  

42__all__ = ['PinnedMemoryResource', 'PinnedMemoryResourceOptions'] 

43  

44  

45@dataclass 

46cdef class PinnedMemoryResourceOptions: 

47 """Customizable :obj:`~_memory.PinnedMemoryResource` options. 

48  

49 Attributes 

50 ---------- 

51 ipc_enabled : bool, optional 

52 Specifies whether to create an IPC-enabled memory pool. When set to 

53 True, the memory pool and its allocations can be shared with other 

54 processes. (Default to False) 

55  

56 max_size : int, optional 

57 Maximum pool size. When set to 0, defaults to a system-dependent value. 

58 (Default to 0) 

59  

60 numa_id : int or None, optional 

61 Host NUMA node ID for pool placement. When set to None (the default), 

62 the behavior depends on ``ipc_enabled``: 

63  

64 - ``ipc_enabled=False``: OS-managed placement (location type HOST). 

65 - ``ipc_enabled=True``: automatically derived from the current CUDA 

66 device's ``host_numa_id`` attribute, requiring an active CUDA 

67 context. 

68  

69 When set to a non-negative integer, that NUMA node is used explicitly 

70 regardless of ``ipc_enabled`` (location type HOST_NUMA). 

71 """ 

72 ipc_enabled : bool = False 

73 max_size : int = 0 

74 numa_id : int | None = None 

75  

76  

77cdef class PinnedMemoryResource(_MemPool): 

78 """ 

79 A host-pinned memory resource managing a stream-ordered memory pool. 

80  

81 Parameters 

82 ---------- 

83 options : PinnedMemoryResourceOptions 

84 Memory resource creation options. 

85  

86 If set to `None`, the memory resource uses the driver's current 

87 stream-ordered memory pool. If no memory 

88 pool is set as current, the driver's default memory pool 

89 is used. 

90  

91 If not set to `None`, a new memory pool is created, which is owned by 

92 the memory resource. 

93  

94 When using an existing (current or default) memory pool, the returned 

95 host-pinned memory resource does not own the pool (`is_handle_owned` is 

96 `False`), and closing the resource has no effect. 

97  

98 Notes 

99 ----- 

100 The device associated with ``stream`` must support host memory pools. If 

101 ``numa_id`` is set or derived for IPC, it must support host NUMA memory pools. 

102 You can query these capabilities through 

103 ``Device.properties.host_memory_pools_supported`` and 

104 ``Device.properties.host_numa_memory_pools_supported``. If the required pool 

105 is unsupported and stream-ordered allocation is not needed, use 

106 :class:`LegacyPinnedMemoryResource`. 

107  

108 The driver makes a host memory pool accessible only from the host by 

109 default, so the resource grants read and write access to the pool to every 

110 device that supports this kind of host memory pool (host NUMA memory pools 

111 if the pool is placed on a NUMA node), as ``cudaMallocHost`` does. Access 

112 is per process, so a resource imported with :meth:`from_allocation_handle`, 

113 including by unpickling, makes the same grant in the importing process. 

114 Without options, the grant applies to the driver's current pool, which the 

115 whole process shares. Construction without options does not affect a CUDA 

116 graph capture in progress on any thread. 

117  

118 To create an IPC-Enabled memory resource (MR) that is capable of sharing 

119 allocations between processes, specify ``ipc_enabled=True`` in the initializer 

120 option. When IPC is enabled and ``numa_id`` is not specified, the NUMA node 

121 is automatically derived from the current CUDA device's ``host_numa_id`` 

122 attribute, which requires an active CUDA context. If ``numa_id`` is 

123 explicitly set, that value is used regardless of ``ipc_enabled``. 

124  

125 See :class:`DeviceMemoryResource` for more details on IPC usage patterns. 

126 """ 

127  

128 def __init__(self, options: PinnedMemoryResourceOptions | None = None) -> None: 

129 _PMR_init(self, options) 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

130  

131 def allocate(self, size_t size, *, stream: Stream | GraphBuilder) -> Buffer: 

132 """Allocate a host-pinned buffer asynchronously on the supplied stream. 

133  

134 Parameters 

135 ---------- 

136 size : int 

137 The size of the buffer to allocate, in bytes. 

138 stream : :obj:`~_stream.Stream` | :obj:`~graph.GraphBuilder` 

139 Keyword-only. The stream on which to perform the allocation 

140 asynchronously. Must be passed explicitly; pass 

141 ``device.default_stream`` to use the default stream. 

142  

143 Returns 

144 ------- 

145 Buffer 

146 The allocated buffer object, which is accessible from the host. 

147  

148 Raises 

149 ------ 

150 RuntimeError 

151 If the stream's device does not support the requested host memory 

152 pool. Use :class:`LegacyPinnedMemoryResource` when stream-ordered 

153 allocation is not required. 

154 """ 

155 MP_check_open(self) 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

156 if self.is_mapped: 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

157 raise TypeError("Cannot allocate from a mapped IPC-enabled memory resource") 

158 cdef Stream s = Stream_accept(stream) 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

159 device = s.device 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

160 if not _host_pool_supported(device, self._numa_id >= 0): 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

161 raise RuntimeError( 

162 f"CUDA device {device.device_id} does not support the requested " 

163 "host memory pool for PinnedMemoryResource. Use " 

164 "LegacyPinnedMemoryResource if memory-pool features are not required." 

165 ) 

166 return _MP_allocate(self, size, s) 2r vbwbxbybzbAbd e f g h i j k l m BbVbCbWbDbXbEbObFbGbHbIbJbKbLb| t n u o v w x y mb_ nbz q p a c

167  

168 def __reduce__(self) -> tuple[object, ...]: 

169 MP_check_open(self) 

170 return PinnedMemoryResource.from_registry, (self.uuid,) 

171  

172 @staticmethod 

173 def from_registry(uuid: uuid.UUID) -> PinnedMemoryResource: # no-cython-lint 

174 """ 

175 Obtain a registered mapped memory resource. 

176  

177 Raises 

178 ------ 

179 RuntimeError 

180 If no mapped memory resource is found in the registry. 

181 """ 

182 return <PinnedMemoryResource>(_ipc.MP_from_registry(uuid)) 

183  

184 def register(self, uuid: uuid.UUID) -> PinnedMemoryResource: # no-cython-lint 

185 """ 

186 Register a mapped memory resource. 

187  

188 Returns 

189 ------- 

190 The registered mapped memory resource. If one was previously registered 

191 with the given key, it is returned. 

192  

193 Raises 

194 ------ 

195 RuntimeError 

196 If the memory resource is not IPC-enabled. 

197 ValueError 

198 If the memory resource already has a different UUID. 

199 """ 

200 return <PinnedMemoryResource>(_ipc.MP_register(self, uuid)) 2Yb_b

201  

202 @classmethod 

203 def from_allocation_handle( 

204 cls, alloc_handle: int | IPCAllocationHandle, numa_id: int | None = None 

205 ) -> PinnedMemoryResource: 

206 """Create a host-pinned memory resource from an allocation handle. 

207  

208 Construct a new `PinnedMemoryResource` instance that imports a memory 

209 pool from a shareable handle. The memory pool is marked as owned. Every 

210 device in this process that supports pools at the pool's host location 

211 is granted read and write access to the imported pool. 

212  

213 Parameters 

214 ---------- 

215 alloc_handle : int | IPCAllocationHandle 

216 The shareable handle of the host-pinned memory resource to import. If an 

217 integer is supplied, it must represent a valid platform-specific 

218 handle. It is the caller's responsibility to close that handle. 

219 numa_id : int | None, optional 

220 The host NUMA node the exporting process placed the pool on, that 

221 is, the exporting resource's :attr:`numa_id`. Node IDs are the same 

222 in every process on the host. The pool's node is read from the 

223 driver when possible (a CUDA 13 build of ``cuda.core`` and a 13.2 

224 or later driver); otherwise this value is the imported resource's 

225 :attr:`numa_id`. Unpickling a resource passes the exporter's node 

226 automatically if it was known. If the handle's resource is already 

227 registered in this process with an unknown node, this value becomes 

228 its node. If the node comes from neither the driver nor this 

229 argument, the import succeeds but :attr:`numa_id` raises 

230 ``RuntimeError``. Must be >= 0 if given. 

231  

232 Returns 

233 ------- 

234 A new host-pinned memory resource instance with the imported handle. 

235  

236 Raises 

237 ------ 

238 TypeError 

239 If the handle refers to a device memory pool, which must be 

240 imported with :meth:`DeviceMemoryResource.from_allocation_handle`. 

241 The pool's kind is checked with a CUDA 13 build of ``cuda.core`` 

242 and a 13.2 or later driver. Otherwise it is checked only when the 

243 handle's memory resource is already registered in this process. 

244 Also raised if ``numa_id`` is not an integer. 

245 ValueError 

246 If ``numa_id`` is negative or too large for a C ``int``, or 

247 differs from the node of the pool, as reported by the driver or by 

248 the resource already registered in this process for the handle. 

249 """ 

250 if numa_id is not None: 2Yb^bZb0b

251 # Reject a float or other non-integer instead of truncating it later. 

252 numa_id = operator.index(numa_id) 2^bZb0b

253 # The node is stored as a C int once the pool is imported. 

254 if not 0 <= numa_id <= INT_MAX: 2Zb0b

255 raise ValueError(f"numa_id must be between 0 and {INT_MAX}, got {numa_id}") 2Zb0b

256  

257 # cuMemPoolImportFromShareableHandle requires CUDA to be initialized, but in 

258 # a child process CUDA may not be initialized yet. For DeviceMemoryResource, 

259 # this is not a concern because most likely when retrieving the device_id the 

260 # user would have already initialized CUDA. But since PinnedMemoryResource is 

261 # not device-specific it is unlikelt the case. 

262 HANDLE_RETURN(cydriver.cuInit(0)) 2Yb

263  

264 cdef PinnedMemoryResource mr = <PinnedMemoryResource>( 

265 _ipc.MP_from_allocation_handle(cls, alloc_handle, _PMR_init_imported, numa_id)) 2Yb

266 # The import hook does not run for a resource that is already registered, 

267 # whether found at the start or registered by a concurrent import. 

268 _PMR_apply_numa_id(mr, numa_id) 

269 return mr 

270  

271 @property 

272 def allocation_handle(self) -> IPCAllocationHandle: 

273 """Shareable handle for this memory pool (requires IPC). 

274  

275 The handle can be used to share the memory pool with other processes. 

276 The handle is cached in this `MemoryResource` and owned by it. 

277 """ 

278 MP_check_open(self) 2PbQbvbwbRbYbxbybzbAb1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;bNb=b?b@b[b]bBbSbCbTbDbUbEbObFbGbHbs b IbJbKbLba c

279 if not self.is_ipc_enabled: 2PbQbvbwbRbYbxbybzbAb1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;bNb=b?b@b[b]bBbSbCbTbDbUbEbObFbGbHbs b IbJbKbLba c

280 raise RuntimeError("Memory resource is not IPC-enabled") 1c

281 return self._ipc_data._alloc_handle 2PbQbvbwbRbYbxbybzbAb1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;bNb=b?b@b[b]bBbSbCbTbDbUbEbObFbGbHbs b IbJbKbLba

282  

283 @property 

284 def device_id(self) -> int: 

285 """Return -1. Pinned memory is host memory and is not associated with a specific device.""" 

286 return -1 1|_pac

287  

288 @property 

289 @cython.critical_section 

290 def numa_id(self) -> int: 

291 """The host NUMA node ID used for pool placement, or -1 for OS-managed placement. 

292  

293 For a resource imported with :meth:`from_allocation_handle`, the node 

294 comes from the driver if the pool's location can be queried (a CUDA 13 

295 build of ``cuda.core`` and a 13.2 or later driver), otherwise from the 

296 ``numa_id`` argument of :meth:`from_allocation_handle`, which unpickling 

297 passes automatically if the exporter's node was known. 

298  

299 Raises 

300 ------ 

301 RuntimeError 

302 If the resource was imported and none of these sources gave the 

303 pool's node. 

304 """ 

305 if not self._numa_id_known: 2Nba c { A `

306 raise RuntimeError( 

307 "The NUMA node of this imported memory pool is not known in this " 

308 "process. Pass numa_id to PinnedMemoryResource.from_allocation_handle " 

309 "to name the node the exporting process placed the pool on.") 

310 return self._numa_id 2Nba c { A `

311  

312 @property 

313 def is_device_accessible(self) -> bool: 

314 """Return True. Every device that supports this kind of host memory pool can read and write its buffers.""" 

315 return True 1_pa

316  

317 @property 

318 def is_host_accessible(self) -> bool: 

319 """Return True. This memory resource provides host-accessible buffers.""" 

320 return True 1_pa

321  

322  

323cdef inline bint _host_pool_supported(device, bint host_numa): 

324 return ( 

325 device.properties.host_numa_memory_pools_supported 2B C vbD wbE F G H I xbJ ybK zbL AbM d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 Bb7 Vb8 9 Cb! Wb# $ Db% Xb' ( Eb) Ob* Fb+ Gb, Hb- s b Ib. Jb/ Kb: Lb; = n ? @ [ ] o ^ q a A `

326 if host_numa 2r B C vbD wbE F G H I xbJ ybK zbL AbM d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 Bb7 Vb8 9 Cb! Wb# $ Db% Xb' ( Eb) Ob* Fb+ Gb, Hb- s b Ib. Jb/ Kb: Lb; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

327 else device.properties.host_memory_pools_supported 2r | } t ~ abbbcbu dbebv fbgbhbibjbkblbw x y mb_ nbz p c { obpbqbrbsb

328 ) 

329  

330  

331cdef inline _PMR_init(PinnedMemoryResource self, options): 

332 from .._device import Device 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

333  

334 cdef PinnedMemoryResourceOptions opts = check_or_create_options( 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

335 PinnedMemoryResourceOptions, options, "PinnedMemoryResource options", 

336 keep_none=True 

337 ) 

338 cdef bint ipc_enabled = False 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

339 cdef size_t max_size = 0 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

340 cdef cydriver.CUmemLocationType loc_type 

341 cdef int numa_id = -1 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

342  

343 if opts is not None: 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` Mbobpbqbrbsb

344 ipc_enabled = opts.ipc_enabled 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A ` Mb

345 if ipc_enabled and not _ipc.is_supported(): 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A ` Mb

346 raise RuntimeError(f"IPC is not available on {platform.system()}") 

347 max_size = opts.max_size 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A ` Mb

348  

349 if opts.numa_id is not None: 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A ` Mb

350 numa_id = opts.numa_id 2q ` Mb

351 if numa_id < 0: 2q ` Mb

352 raise ValueError(f"numa_id must be >= 0, got {numa_id}") 2Mb

353 elif ipc_enabled: 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z ubp a c { A

354 dev = Device() 1BCDEFGHIJKLMdeNOPQRSTUVWfghiXjkYlmZ0123456789!#$%'()*+,-sb./:;=n?@[]o^aA

355 numa_id = dev.properties.host_numa_id 1BCDEFGHIJKLMdeNOPQRSTUVWfghiXjkYlmZ0123456789!#$%'()*+,-sb./:;=n?@[]o^aA

356 if numa_id < 0: 1BCDEFGHIJKLMdeNOPQRSTUVWfghiXjkYlmZ0123456789!#$%'()*+,-sb./:;=n?@[]o^aA

357 raise RuntimeError( 

358 "Cannot determine host NUMA ID for IPC-enabled pinned " 

359 "memory pool. The system may not support NUMA, or no " 

360 "CUDA context is active. Set numa_id explicitly or " 

361 "call Device.set_current() first.") 

362  

363 if numa_id >= 0: 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` obpbqbrbsb

364 loc_type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA 1BCDEFGHIJKLMdeNOPQRSTUVWfghiXjkYlmZ0123456789!#$%'()*+,-sb./:;=n?@[]o^qaA`

365 else: 

366 loc_type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST 2r | } t ~ abbbcbu dbebv fbgbhbibjbkblbw x y mb_ nbz ubp c { obpbqbrbsb

367  

368 self._numa_id = numa_id 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` obpbqbrbsb

369 self._numa_id_known = True 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` obpbqbrbsb

370  

371 if opts is None: 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q ubp a c { A ` obpbqbrbsb

372 MP_init_current_pool( 2| mb_ nbobpbqbrbsb

373 self, 

374 loc_type, 

375 numa_id, 

376 cydriver.CUmemAllocationType.CU_MEM_ALLOCATION_TYPE_PINNED, 

377 ) 

378 else: 

379 MP_init_create_pool( 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A `

380 self, 

381 loc_type, 

382 numa_id, 

383 cydriver.CUmemAllocationType.CU_MEM_ALLOCATION_TYPE_PINNED, 

384 ipc_enabled, 

385 max_size, 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y z q ubp a c { A `

386 ) 

387  

388 _PMR_grant_device_access(self, numa_id >= 0, False) 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

389  

390  

391cdef inline int _PMR_grant_device_access( 

392 PinnedMemoryResource self, bint host_numa, bint imported) except? -1: 

393 # A pool on a host location is accessible only from the host by default 

394 # (see cuMemPoolCreate), and access is per process, so both the creating 

395 # and the importing process grant read/write access to every device that 

396 # supports this kind of host pool, as cudaMallocHost memory is. Other 

397 # devices cannot use the pool. 

398 from .._device import Device 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

399  

400 cdef cydriver.CUmemAccessDesc desc 

401 cdef vector[cydriver.CUmemAccessDesc] access 

402 desc.flags = cydriver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

403 for dev in Device.get_all_devices(): 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

404 if _host_pool_supported(dev, host_numa): 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

405 desc.location = cumemlocation_from_id( 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

406 cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE, dev.device_id) 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

407 access.push_back(desc) 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

408 if not access.empty(): 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

409 MP_set_access(self, access.data(), access.size(), imported) 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

410 return 0 2r B C D E F G H I J K L M d e N O P Q R S T U V W f g h i X j k Y l m Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - s b . / : ; | } = t n ~ ? ab@ bb[ cb] u o db^ ebv fbgbhbibjbkblbw x y mb_ nbz q p a c { A ` obpbqbrbsb

411  

412  

413cdef inline int _PMR_apply_numa_id(PinnedMemoryResource self, object numa_id) except? -1: 

414 # A node the caller passed must agree with a node that is known. A resource 

415 # whose node is unknown, such as one registered by an earlier import that 

416 # was given no node, takes the caller's node. 

417 # Concurrent imports of one handle share this resource, so on a free-threaded 

418 # build the check and the write are one step. Otherwise two imports with 

419 # different nodes could both find the node unknown and both succeed. 

420 cdef int node 

421 if numa_id is None: 

422 return 0 

423 node = numa_id 

424 with cython.critical_section(self): 

425 if not self._numa_id_known: 

426 self._numa_id = node 

427 self._numa_id_known = True 

428 elif self._numa_id != node: 

429 raise ValueError( 

430 f"numa_id {node} does not match the NUMA node of the memory pool, " 

431 f"which is {self._numa_id}") 

432 return 0 

433  

434  

435cdef int _PMR_init_imported(_MemPool mr, object numa_id) except? -1: 

436 cdef PinnedMemoryResource self = <PinnedMemoryResource>mr 

437 cdef cydriver.CUmemLocation loc 

438 # IPC-enabled pinned pools created by cuda.core are always placed on a host 

439 # NUMA node. Without a location query (a CUDA 12 build or a driver before 

440 # 13.2), the pool's kind is not checked and the node is the one the caller 

441 # passed, if any. Otherwise it is unknown and the numa_id property raises. 

442 cdef bint host_numa = True 

443 if _ipc.MP_check_import_location(self, True, &loc): 

444 host_numa = loc.type == cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA 

445 self._numa_id = loc.id if host_numa else -1 

446 self._numa_id_known = True 

447 _PMR_apply_numa_id(self, numa_id) 

448 elif numa_id is not None: 

449 self._numa_id = numa_id 

450 self._numa_id_known = True 

451 return _PMR_grant_device_access(self, host_numa, True) 

452  

453  

454def _deep_reduce_pinned_memory_resource(mr: object) -> tuple[object, ...]: 

455 check_multiprocessing_start_method() 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

456 cdef PinnedMemoryResource pmr = <PinnedMemoryResource>mr 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

457 alloc_handle = pmr.allocation_handle 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

458 # NUMA node IDs are the same in every process on the host, so the node needs 

459 # no translation as a device ordinal does. An unknown node is not sent, so 

460 # that the importer does not mistake a placeholder for the pool's node, and 

461 # neither is -1, which from_allocation_handle rejects. 

462 numa_id = None 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

463 with cython.critical_section(pmr): 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

464 if pmr._numa_id_known and pmr._numa_id >= 0: 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

465 numa_id = pmr._numa_id 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

466 return pmr.from_allocation_handle, (alloc_handle, numa_id) 2PbQbvbwbRbxbybzbAbNbBbSbCbTbDbUbEbFbGbHbb IbJbKbLb

467  

468  

469multiprocessing.reduction.register(PinnedMemoryResource, _deep_reduce_pinned_memory_resource)