Coverage for cuda / core / _memory / _memory_pool.pyx: 86.92%

107 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-25 01:07 +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  

7from libc.limits cimport ULLONG_MAX 

8from libc.stdint cimport uintptr_t 

9from libc.string cimport memset 

10  

11from cuda.bindings cimport cydriver 

12from cuda.core._memory._buffer cimport Buffer, Buffer_from_deviceptr_handle, MemoryResource 

13from cuda.core._memory cimport _ipc 

14from cuda.core._stream cimport default_stream, Stream_accept, Stream 

15from cuda.core._resource_handles cimport ( 

16 MemoryPoolHandle, 

17 DevicePtrHandle, 

18 create_mempool_handle, 

19 create_mempool_handle_ref, 

20 deviceptr_alloc_from_pool, 

21 as_cu, 

22 as_py, 

23) 

24  

25from cuda.core._utils.cuda_utils cimport ( 

26 HANDLE_RETURN, 

27) 

28  

29  

30cdef class _MemPoolAttributes: 

31 """Provides access to memory pool attributes.""" 

32  

33 def __init__(self, *args, **kwargs): 

34 raise RuntimeError("_MemPoolAttributes cannot be instantiated directly. Please use MemoryResource APIs.") 

35  

36 @staticmethod 

37 cdef _MemPoolAttributes _init(MemoryPoolHandle h_pool): 

38 cdef _MemPoolAttributes self = _MemPoolAttributes.__new__(_MemPoolAttributes) 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

39 self._h_pool = h_pool 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

40 return self 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

41  

42 def __repr__(self): 

43 return f"{self.__class__.__name__}(%s)" % ", ".join( 1bac

44 f"{attr}={getattr(self, attr)}" for attr in dir(self) 1bac

45 if not attr.startswith("_") 1bac

46 ) 

47  

48 cdef int _getattribute(self, cydriver.CUmemPool_attribute attr_enum, void* value) except?-1: 

49 with nogil: 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

50 HANDLE_RETURN(cydriver.cuMemPoolGetAttribute(as_cu(self._h_pool), attr_enum, value)) 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

51 return 0 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

52  

53 @property 

54 def reuse_follow_event_dependencies(self): 

55 """Allow memory to be reused when there are event dependencies between streams.""" 

56 cdef int value 

57 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_FOLLOW_EVENT_DEPENDENCIES, &value) 1!Zv'3bac

58 return bool(value) 1!Zv'3bac

59  

60 @property 

61 def reuse_allow_opportunistic(self): 

62 """Allow reuse of completed frees without dependencies.""" 

63 cdef int value 

64 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_ALLOW_OPPORTUNISTIC, &value) 19Yu%2bac

65 return bool(value) 19Yu%2bac

66  

67 @property 

68 def reuse_allow_internal_dependencies(self): 

69 """Allow insertion of new stream dependencies for memory reuse.""" 

70 cdef int value 

71 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_ALLOW_INTERNAL_DEPENDENCIES, &value) 18Xt$1bac

72 return bool(value) 18Xt$1bac

73  

74 @property 

75 def release_threshold(self): 

76 """Amount of reserved memory to hold before OS release.""" 

77 cdef cydriver.cuuint64_t value 

78 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD, &value) 17Ws#0bac

79 return int(value) 17Ws#0bac

80  

81 @property 

82 def reserved_mem_current(self): 

83 """Current amount of backing memory allocated.""" 

84 cdef cydriver.cuuint64_t value 

85 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RESERVED_MEM_CURRENT, &value) 1jfIAdolhKCbac

86 return int(value) 1jfIAdolhKCbac

87  

88 @property 

89 def reserved_mem_high(self): 

90 """High watermark of backing memory allocated.""" 

91 cdef cydriver.cuuint64_t value 

92 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RESERVED_MEM_HIGH, &value) 1IAoKCbac

93 return int(value) 1IAoKCbac

94  

95 @property 

96 def used_mem_current(self): 

97 """Current amount of memory in use.""" 

98 cdef cydriver.cuuint64_t value 

99 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_USED_MEM_CURRENT, &value) 1kgJBepmiLDbac

100 return int(value) 1kgJBepmiLDbac

101  

102 @property 

103 def used_mem_high(self): 

104 """High watermark of memory in use.""" 

105 cdef cydriver.cuuint64_t value 

106 self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_USED_MEM_HIGH, &value) 1JBpLDynzbac

107 return int(value) 1JBpLDynzbac

108  

109  

110cdef class _MemPool(MemoryResource): 

111  

112 def __cinit__(self): 

113 # Note: subclasses use MP_init_create_pool or MP_init_current_pool to initialize. 

114 self._mempool_owned = False 2- . / { | } ~ abbbcbdbfcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H ebfbgbhb: ; 4 vd= wd? @ [ xdq ibw ] 7 W j f I A 8 X 9 Y ! Z k g J B s ydd zdo Adt Bdu Cdv Dde Edp Fd# 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 ^ 6 r x jb_ ( Gdkb) * + , lbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

115 self._ipc_data = None 27c- . / { | } ~ abbbcbdbfcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H ebfbgbhb: ; 4 vd= wd? @ [ xdq ibw ] 7 W j f I A 8 X 9 Y ! Z k g J B s ydd zdo Adt Bdu Cdv Dde Edp Fd# 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 ^ 6 r x jb_ ( Gdkb) * + , lbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

116 self._attributes = None 2- . / { | } ~ abbbcbdbfcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H ebfbgbhb: ; 4 vd= wd? @ [ xdq ibw ] 7 W j f I A 8 X 9 Y ! Z k g J B s ydd zdo Adt Bdu Cdv Dde Edp Fd# 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 ^ 6 r x jb_ ( Gdkb) * + , lbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

117  

118 def close(self): 

119 """ 

120 Close the memory resource and destroy the associated memory pool 

121 if owned. 

122 """ 

123 _MP_close(self) 2y n z r x jb_ ( ) * + ,

124  

125 def allocate(self, size_t size, stream: Stream | GraphBuilder | None = None) -> Buffer: 

126 """Allocate a buffer of the requested size. 

127  

128 Parameters 

129 ---------- 

130 size : int 

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

132 stream : :obj:`~_stream.Stream` | :obj:`~_graph.GraphBuilder`, optional 

133 The stream on which to perform the allocation asynchronously. 

134 If None, an internal stream is used. 

135  

136 Returns 

137 ------- 

138 Buffer 

139 The allocated buffer object, which is accessible on the device that this memory 

140 resource was created for. 

141 """ 

142 if self.is_mapped: 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

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

144 stream = Stream_accept(stream) if stream is not None else default_stream() 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

145 return _MP_allocate(self, size, <Stream> stream) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

146  

147 def deallocate(self, ptr: DevicePointerT, size_t size, stream: Stream | GraphBuilder | None = None): 

148 """Deallocate a buffer previously allocated by this resource. 

149  

150 Parameters 

151 ---------- 

152 ptr : :obj:`~_memory.DevicePointerT` 

153 The pointer or handle to the buffer to deallocate. 

154 size : int 

155 The size of the buffer to deallocate, in bytes. 

156 stream : :obj:`~_stream.Stream` | :obj:`~_graph.GraphBuilder`, optional 

157 The stream on which to perform the deallocation asynchronously. 

158 If the buffer is deallocated without an explicit stream, the allocation stream 

159 is used. 

160 """ 

161 stream = Stream_accept(stream) if stream is not None else default_stream() 

162 _MP_deallocate(self, <uintptr_t>ptr, size, <Stream> stream) 

163  

164 @property 

165 def attributes(self) -> _MemPoolAttributes: 

166 """Memory pool attributes.""" 

167 if self._attributes is None: 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

168 self._attributes = _MemPoolAttributes._init(self._h_pool) 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

169 return self._attributes 17WjfIA8X9Y!ZkgJBsdotuvep#0lhKC$1%2'3miLDynzbac

170  

171 @property 

172 def handle(self) -> object: 

173 """Handle to the underlying memory pool.""" 

174 return as_py(self._h_pool) 

175  

176 @property 

177 def is_handle_owned(self) -> bool: 

178 """Whether the memory resource handle is owned. If False, ``close`` has no effect.""" 

179 return self._mempool_owned 

180  

181 @property 

182 def is_ipc_enabled(self) -> bool: 

183 """Whether this memory resource has IPC enabled.""" 

184 return self._ipc_data is not None 2HdfcIdgcE hcF ic!cjc#ckcM lcN mc$cnc%coc'cpc(cqcJdrcKdscLdtc)cuc*cvcMdwcNdxc+cyc,czcOdAcPdBc-cCc.cDcQdEcRdFc/cGc:cHcSdIcTdJc;cKc=cLcUdMcVdNc?cOc@cPcWdQc[cRc]cSc^cTc_cUc`cVc{cWc|cXc}cYc~cZcad0cO P bd1ccd2cdd3ced4c8c5c9c6cQ R S T G H : ; 4 q 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D 5 6 r x ) * + , U V

185  

186 @property 

187 def is_mapped(self) -> bool: 

188 """ 

189 Whether this is a mapping of an IPC-enabled memory resource from 

190 another process. If True, allocation is not permitted. 

191 """ 

192 return self._ipc_data is not None and self._ipc_data._is_mapped 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

193  

194 @property 

195 def uuid(self) -> uuid.UUID | None: 

196 """ 

197 A universally unique identifier for this memory resource. Meaningful 

198 only for IPC-enabled memory resources. 

199 """ 

200 return getattr(self._ipc_data, 'uuid', None) 2E F 8c9cG H

201  

202  

203cdef int MP_init_create_pool( 

204 _MemPool self, 

205 cydriver.CUmemLocationType loc_type, 

206 int loc_id, 

207 cydriver.CUmemAllocationType alloc_type, 

208 bint ipc_enabled, 

209 size_t max_size, 

210) except? -1: 

211 """Initialize a _MemPool by creating a new memory pool with the given 

212 parameters. 

213  

214 Sets ``_h_pool`` (owning), ``_mempool_owned``, and ``_ipc_data``. 

215 """ 

216 cdef cydriver.CUmemPoolProps properties 

217 memset(&properties, 0, sizeof(cydriver.CUmemPoolProps)) 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

218  

219 properties.allocType = alloc_type 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

220 properties.handleTypes = ( 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

221 _ipc.IPC_HANDLE_TYPE if ipc_enabled 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

222 else cydriver.CUmemAllocationHandleType.CU_MEM_HANDLE_TYPE_NONE 24 ? @ [ q w 7 j I 8 9 ! k J s d o t u v e p # l K $ % ' m L y n z b a c 5 6 x jb(

223 ) 

224 properties.location.id = loc_id 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

225 properties.location.type = loc_type 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

226 properties.maxSize = max_size 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

227  

228 self._mempool_owned = True 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

229 self._h_pool = create_mempool_handle(properties) 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

230  

231 if ipc_enabled: 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

232 alloc_handle = _ipc.MP_export_mempool(self) 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H W f A X Y Z g B 0 h C 1 2 3 i D r _ ( ) * + , U V

233 self._ipc_data = _ipc.IPCDataForMR(alloc_handle, False) 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H W f A X Y Z g B 0 h C 1 2 3 i D r _ ( ) * + , U V

234  

235 return 0 2fcgcE hcF icjckcM lcN mcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0cO P 1c2c3c4c5c6cQ R S T G H 4 ? @ [ q w 7 W j f I A 8 X 9 Y ! Z k g J B s d o t u v e p # 0 l h K C $ 1 % 2 ' 3 m i L D y n z b a c 5 6 r x jb_ ( ) * + , U V

236  

237  

238cdef int MP_init_current_pool( 

239 _MemPool self, 

240 cydriver.CUmemLocationType loc_type, 

241 int loc_id, 

242 cydriver.CUmemAllocationType alloc_type, 

243) except? -1: 

244 """Initialize a _MemPool by getting the driver's current pool for a 

245 location and allocation type. 

246  

247 Sets ``_h_pool`` (non-owning) via ``cuMemGetMemPool``. 

248 Requires CUDA 13+. 

249 """ 

250 IF CUDA_CORE_BUILD_MAJOR >= 13: 

251 cdef cydriver.CUmemLocation loc 

252 cdef cydriver.CUmemoryPool pool 

253 loc.id = loc_id 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

254 loc.type = loc_type 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

255 with nogil: 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

256 HANDLE_RETURN(cydriver.cuMemGetMemPool(&pool, &loc, alloc_type)) 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

257 self._h_pool = create_mempool_handle_ref(pool) 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

258 self._mempool_owned = False 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

259 ELSE: 

260 raise RuntimeError("not supported") 

261 return 0 2vd= wd? @ [ xdq w ] s ydd zdo Adt Bdu Cdv Dde Edp Fdn a ^

262  

263  

264cdef int MP_raise_release_threshold(_MemPool self) except? -1: 

265 """Raise the pool's release threshold to ULLONG_MAX if currently zero. 

266  

267 By default the release threshold is 0, meaning memory is returned to 

268 the OS as soon as there are no active suballocations. Setting it to 

269 ULLONG_MAX avoids repeated OS round-trips. 

270 """ 

271 cdef cydriver.cuuint64_t current_threshold 

272 cdef cydriver.cuuint64_t max_threshold = ULLONG_MAX 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

273 with nogil: 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

274 HANDLE_RETURN( 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

275 cydriver.cuMemPoolGetAttribute( 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

276 as_cu(self._h_pool), 

277 cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD, 

278 &current_threshold 

279 ) 

280 ) 

281 if current_threshold == 0: 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

282 HANDLE_RETURN(cydriver.cuMemPoolSetAttribute( 1`

283 as_cu(self._h_pool), 

284 cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD, 

285 &max_threshold 

286 )) 

287 return 0 2- . / { | } ~ abbbcbdbebfbgbhb: ; ibkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]b^b_b`b{b|b}b~bacbcccdcec

288  

289  

290# Raise an exception if the given stream is capturing. 

291# A result of CU_STREAM_CAPTURE_STATUS_INVALIDATED is considered an error. 

292cdef inline int check_not_capturing(cydriver.CUstream s) except?-1 nogil: 

293 cdef cydriver.CUstreamCaptureStatus capturing 

294 HANDLE_RETURN(cydriver.cuStreamIsCapturing(s, &capturing)) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

295 if capturing != cydriver.CUstreamCaptureStatus.CU_STREAM_CAPTURE_STATUS_NONE: 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

296 raise RuntimeError("_MemPool cannot perform memory operations on " 1-./

297 "a capturing stream (consider using GraphMemoryResource).") 

298  

299  

300cdef inline Buffer _MP_allocate(_MemPool self, size_t size, Stream stream): 

301 cdef cydriver.CUstream s = as_cu(stream._h_stream) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

302 cdef DevicePtrHandle h_ptr 

303 with nogil: 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

304 check_not_capturing(s) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

305 h_ptr = deviceptr_alloc_from_pool(size, self._h_pool, stream._h_stream) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

306 if not h_ptr: 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

307 raise RuntimeError("Failed to allocate memory from pool") 

308 return Buffer_from_deviceptr_handle(h_ptr, size, self, None) 2- . / { | } ~ abbbcbdbE F !c#cM N $c%c'c(c)c*c+c,c-c.c/c:c;c=c?c@c[c]c^c_c`c{c|c}c~cadO P bdcddded8c9cQ R S T G H ebfbgbhb: ; 4 = q ibw ] j f k g d e l h m i b a c 5 ^ 6 r x kblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbTbUbVbWbXbYbZb0b1b2b` 3b4b5b6b7b8b9bU V !b#b$b%b'b(b)b*b+bfd,b-b.b/bgd:bhd;bidjd=bkd?b@bld[b]b^b_b`bmdndodpd{b|b}b~bqdrdsdacbctdudccdcec

309  

310  

311cdef inline void _MP_deallocate( 

312 _MemPool self, uintptr_t ptr, size_t size, Stream stream 

313) noexcept nogil: 

314 cdef cydriver.CUstream s = as_cu(stream._h_stream) 

315 cdef cydriver.CUdeviceptr devptr = <cydriver.CUdeviceptr>ptr 

316 cdef cydriver.CUresult r 

317 with nogil: 

318 r = cydriver.cuMemFreeAsync(devptr, s) 

319 if r != cydriver.CUDA_ERROR_INVALID_CONTEXT: 

320 HANDLE_RETURN(r) 

321  

322  

323cdef inline _MP_close(_MemPool self): 

324 if not self._h_pool: 2y n z r x jb_ ( ) * + ,

325 return 

326  

327 # Reset members in declaration order. 

328 # The RAII deleter calls cuMemPoolDestroy if this is an owning handle. 

329 self._h_pool.reset() 2y n z r x jb_ ( ) * + ,

330 self._mempool_owned = False 2y n z r x jb_ ( ) * + ,

331 self._ipc_data = None 2y n z r x jb_ ( ) * + ,

332 self._attributes = None 2y n z r x jb_ ( ) * + ,