Coverage for cuda / core / _memory / _buffer.pyx: 89.40%

217 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-22 01:37 +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 libc.stdint cimport uintptr_t 

9  

10from cuda.bindings cimport cydriver 

11from cuda.core._memory._device_memory_resource import DeviceMemoryResource 

12from cuda.core._memory._pinned_memory_resource import PinnedMemoryResource 

13from cuda.core._memory._ipc cimport IPCBufferDescriptor, IPCDataForBuffer 

14from cuda.core._memory cimport _ipc 

15from cuda.core._resource_handles cimport ( 

16 DevicePtrHandle, 

17 StreamHandle, 

18 deviceptr_create_with_owner, 

19 deviceptr_create_with_mr, 

20 register_mr_dealloc_callback, 

21 as_intptr, 

22 as_cu, 

23 set_deallocation_stream, 

24) 

25from cuda.core.typing import DevicePointerType 

26  

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

28from cuda.core._utils.cuda_utils cimport HANDLE_RETURN, _parse_fill_value 

29  

30import sys 

31from typing import TypeVar 

32  

33from cuda.core._utils.pycompat import BufferProtocol 

34from cuda.core._dlpack import classify_dl_device, make_py_capsule 

35from cuda.core._device import Device 

36  

37  

38# ============================================================================= 

39# MR deallocation callback (invoked from C++ shared_ptr deleter) 

40# ============================================================================= 

41  

42cdef void _mr_dealloc_callback( 

43 object mr, 

44 cydriver.CUdeviceptr ptr, 

45 size_t size, 

46 const StreamHandle& h_stream, 

47) noexcept: 

48 """Called by the C++ deleter to deallocate via MemoryResource.deallocate. 

49  

50 This is the C++ teardown path: there is no Python caller frame from 

51 which to obtain a stream. If the device-pointer handle was created 

52 without ``set_deallocation_stream`` being called (e.g. buffers minted 

53 via ``Buffer.from_handle(ptr, size, mr=mr)`` from DLPack import, 

54 third-party adapters, or other foreign sources), ``h_stream`` is 

55 empty here. Stream-ordered MR ``deallocate`` overrides reject 

56 ``stream=None`` (issue #2001), so without a fallback the destructor 

57 would print a warning and leak the allocation. Fall back to the 

58 legacy/per-thread default stream so the free still happens; this is 

59 the unique exception to the "no implicit default-stream fallback" 

60 policy because the teardown has no other source of truth. 

61 """ 

62 cdef Stream stream 

63 try: 2e f g h i j k l m n o p q r s t u v w x y z A B C D KfE F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M yfzfAfBfJcCfDfEf? FfGf= DbLfed^cEbFbGbHbIbJb_c`c~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bP Q McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%cFcGcEcsdHcd

64 stream = Stream._from_handle(Stream, h_stream) if h_stream else default_stream() 2e f g h i j k l m n o p q r s t u v w x y z A B C D KfE F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M yfzfAfBfJcCfDfEf? FfGf= DbLfed^cEbFbGbHbIbJb_c`c~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bP Q McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%cFcGcEcsdHcd

65 mr.deallocate(int(ptr), size, stream=stream) 2e f g h i j k l m n o p q r s t u v w x y z A B C D KfE F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M yfzfAfBfJcCfDfEf? FfGf= DbLfed^cEbFbGbHbIbJb_c`c~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bP Q McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%cFcGcEcsdHcd

66 except Exception as exc: 2Ec

67 print(f"Warning: mr.deallocate() failed during Buffer destruction: {exc}", 2Ec

68 file=sys.stderr) 2Ec

69  

70register_mr_dealloc_callback(_mr_dealloc_callback) 

71  

72  

73__all__ = ['Buffer', 'MemoryResource'] 

74  

75  

76  

77  

78cdef class Buffer: 

79 """Represent a handle to allocated memory. 

80  

81 This generic object provides a unified representation for how 

82 different memory resources are to give access to their memory 

83 allocations. 

84  

85 Support for data interchange mechanisms are provided by DLPack. 

86 """ 

87 def __cinit__(self): 

88 self._clear() 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

89  

90 def _clear(self): 

91 self._h_ptr.reset() # Release the handle 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

92 self._size = 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

93 self._memory_resource = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

94 self._ipc_data = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

95 self._owner = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

96 self._mem_attrs_inited = False 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T CdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdK L M mbdb7d8d9d!dFdJc#dqfeb$drf%dsf? 'd(dfbgbGdHdIdJdKd= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe3d4dOdtfAdPd/cfd:cBd}c~cadgda b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc; { .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd5d,c6d%c} jbcd(cDc'cddFcGcEcsdHcd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvfnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoepbqbrbsbtbubvb

97  

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

99 raise RuntimeError("Buffer objects cannot be instantiated directly. " 

100 "Please use MemoryResource APIs.") 

101  

102 @classmethod 

103 def _init( 

104 cls, ptr: DevicePointerType, size_t size, mr: MemoryResource | None = None, 

105 ipc_descriptor: IPCBufferDescriptor | None = None, 

106 owner : object | None = None 

107 ): 

108 """Create a Buffer from a raw pointer. 

109  

110 When ``mr`` is provided, the buffer takes ownership: ``mr.deallocate()`` 

111 is called when the buffer is closed or garbage collected. When ``owner`` 

112 is provided, the owner is kept alive but no deallocation is performed. 

113 """ 

114 if mr is not None and owner is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; Mf5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

115 raise ValueError("owner and memory resource cannot be both specified together") 2ecMf

116 cdef Buffer self = Buffer.__new__(cls) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

117 cdef uintptr_t c_ptr = <uintptr_t>(int(ptr)) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

118 if mr is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

119 self._h_ptr = deviceptr_create_with_mr(c_ptr, size, mr) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bP Q McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%cFcGcEcsdHcd

120 else: 

121 self._h_ptr = deviceptr_create_with_owner(c_ptr, owner) 27d8d9d!dJc#d$d%d? 'd(d= N O @ hbibP Q R S cdIcnbobpbqbrbsbtbubvb

122 self._size = size 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

123 self._memory_resource = mr 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

124 self._ipc_data = IPCDataForBuffer(ipc_descriptor, True) if ipc_descriptor is not None else None 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

125 self._owner = owner 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

126 self._mem_attrs_inited = False 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

127 return self 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbK L M 7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb3d4da b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; 5d,c6d%ccdFcGcEcsdHcd Icnbobpbqbrbsbtbubvb

128  

129 @staticmethod 

130 def _reduce_helper(mr, ipc_descriptor): 

131 # The parent process's stream is not portable across processes, so the 

132 # pickle path cannot thread an explicit stream through. Seed the 

133 # imported buffer's deallocation with the current context's default 

134 # stream; the receiver can override via buffer.close(stream). 

135 return Buffer.from_ipc_descriptor(mr, ipc_descriptor, stream=default_stream()) 

136  

137 def __reduce__(self): 

138 # Must not serialize the parent's stream! 

139 return Buffer._reduce_helper, (self.memory_resource, self.ipc_descriptor) 2peqereseKbDbteuevewexeyevdwd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abbbcb

140  

141 @staticmethod 

142 def from_handle( 

143 ptr: DevicePointerType, size_t size, mr: MemoryResource | None = None, 

144 owner: object | None = None, 

145 ) -> Buffer: 

146 """Create a new :class:`Buffer` object from a pointer. 

147  

148 Parameters 

149 ---------- 

150 ptr : :obj:`~_memory.DevicePointerType` 

151 Allocated buffer handle object 

152 size : int 

153 Memory size of the buffer 

154 mr : :obj:`~_memory.MemoryResource`, optional 

155 Memory resource associated with the buffer. When provided, 

156 :meth:`MemoryResource.deallocate` is called when the buffer is 

157 closed or garbage collected. 

158 owner : object, optional 

159 An object holding external allocation that the ``ptr`` points to. 

160 The reference is kept as long as the buffer is alive. 

161 The ``owner`` and ``mr`` cannot be specified together. 

162  

163 Note 

164 ---- 

165 When neither ``mr`` nor ``owner`` is specified, this creates a 

166 non-owning reference. The pointer will NOT be freed when the 

167 :class:`Buffer` is closed or garbage collected. 

168 """ 

169 return Buffer._init(ptr, size, mr=mr, owner=owner) 2[ ] ^ _ wbxbybzbAbBbCb7d8d9d!dJc#d$d%d? 'd(d= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cLcAcmcKc{c-c|c` | %bN O @ hbibP Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czc; Mf5d,c6d%ccdFcGcEcsdHcIcnbobpbqbrbsbtbubvb

170  

171 @classmethod 

172 def from_ipc_descriptor( 

173 cls, mr: DeviceMemoryResource | PinnedMemoryResource, ipc_descriptor: IPCBufferDescriptor, 

174 *, stream: Stream 

175 ) -> Buffer: 

176 """Import a buffer that was exported from another process. 

177  

178 Parameters 

179 ---------- 

180 mr : :obj:`~_memory.DeviceMemoryResource` | :obj:`~_memory.PinnedMemoryResource` 

181 The IPC-enabled memory resource matching the exporting process. 

182 ipc_descriptor : :obj:`~_memory.IPCBufferDescriptor` 

183 The descriptor exported from another process. 

184 stream : :obj:`~_stream.Stream` 

185 Keyword-only. The stream used for asynchronous deallocation when 

186 the buffer is closed or garbage collected. 

187 """ 

188 return _ipc.Buffer_from_ipc_descriptor(cls, mr, ipc_descriptor, stream) 2bddd

189  

190 @property 

191 def ipc_descriptor(self) -> IPCBufferDescriptor: 

192 """Descriptor for sharing this buffer with other processes.""" 

193 if self._ipc_data is None: 2peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbbd'cddIeJe

194 self._ipc_data = IPCDataForBuffer(_ipc.Buffer_get_ipc_descriptor(self), False) 2peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbbd'cddIeJe

195 return self._ipc_data.ipc_descriptor 2peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcb'cIeJe

196  

197 def close(self, stream: Stream | GraphBuilder | None = None): 

198 """Deallocate this buffer asynchronously on the given stream. 

199  

200 This buffer is released back to their memory resource 

201 asynchronously on the given stream. 

202  

203 Parameters 

204 ---------- 

205 stream : :obj:`~_stream.Stream` | :obj:`~graph.GraphBuilder`, optional 

206 The stream object to use for asynchronous deallocation. If None, 

207 the deallocation stream stored in the handle is used. 

208 """ 

209 Buffer_close(self, stream) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

210  

211 def __enter__(self): 

212 return self 2/cfd:c~cadgd

213  

214 def __exit__(self, exc_type, exc_val, exc_tb): 

215 self.close() 2/cfd:c~cadgd

216 return False 2/cfd:c~cadgd

217  

218 def copy_to(self, dst: Buffer = None, *, stream: Stream | GraphBuilder) -> Buffer: 

219 """Copy from this buffer to the dst buffer asynchronously on the given stream. 

220  

221 Copies the data from this buffer to the provided dst buffer. 

222 If the dst buffer is not provided, then a new buffer is first 

223 allocated using the associated memory resource before the copy. 

224  

225 Parameters 

226 ---------- 

227 dst : :obj:`~_memory.Buffer`, optional 

228 Destination buffer to copy data to. If not provided, a new buffer 

229 is allocated using this buffer's memory resource. 

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

231 Keyword argument specifying the stream for the 

232 asynchronous copy 

233  

234 """ 

235 cdef Stream s = Stream_accept(stream) 2a b c mcBcCcDcd

236 cdef size_t src_size = self._size 2a b c mcBcCcDcd

237  

238 if dst is None: 2a b c mcBcCcDcd

239 if self._memory_resource is None: 

240 raise ValueError("a destination buffer must be provided (this " 

241 "buffer does not have a memory_resource)") 

242 dst = self._memory_resource.allocate(src_size, stream=s) 

243  

244 cdef size_t dst_size = dst._size 2a b c mcBcCcDcd

245 if dst_size != src_size: 2a b c mcBcCcDcd

246 raise ValueError( "buffer sizes mismatch between src and dst (sizes " 

247 f"are: src={src_size}, dst={dst_size})" 

248 ) 

249 with nogil: 2a b c mcBcCcDcd

250 HANDLE_RETURN(cydriver.cuMemcpyAsync( 2a b c mcBcCcDcd

251 as_cu(dst._h_ptr), as_cu(self._h_ptr), src_size, as_cu(s._h_stream))) 

252 return dst 2a b c mcBcCcDcd

253  

254 def copy_from(self, src: Buffer, *, stream: Stream | GraphBuilder): 

255 """Copy from the src buffer to this buffer asynchronously on the given stream. 

256  

257 Parameters 

258 ---------- 

259 src : :obj:`~_memory.Buffer` 

260 Source buffer to copy data from 

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

262 Keyword argument specifying the stream for the 

263 asynchronous copy 

264  

265 """ 

266 cdef Stream s = Stream_accept(stream) 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

267 cdef size_t dst_size = self._size 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

268 cdef size_t src_size = src._size 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

269  

270 if src_size != dst_size: 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

271 raise ValueError( "buffer sizes mismatch between src and dst (sizes " 

272 f"are: src={src_size}, dst={dst_size})" 

273 ) 

274 with nogil: 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

275 HANDLE_RETURN(cydriver.cuMemcpyAsync( 2[ ] ^ _ wbxbybzbAbBbCbKbDbEbLbFbGbHbMbIbJb~ abkblbbbcb)c*c+cAc

276 as_cu(self._h_ptr), as_cu(src._h_ptr), dst_size, as_cu(s._h_stream))) 

277  

278 def fill(self, value: int | BufferProtocol, *, stream: Stream | GraphBuilder): 

279 """Fill this buffer with a repeating byte pattern. 

280  

281 Parameters 

282 ---------- 

283 value : int | :obj:`collections.abc.Buffer` 

284 - int: Must be in range [0, 256). Converted to 1 byte. 

285 - :obj:`collections.abc.Buffer`: Must be 1, 2, or 4 bytes. 

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

287 Stream for the asynchronous fill operation. 

288  

289 Raises 

290 ------ 

291 TypeError 

292 If value is not an int and does not support the buffer protocol. 

293 ValueError 

294 If value byte length is not 1, 2, or 4. 

295 If buffer size is not divisible by value byte length. 

296 OverflowError 

297 If int value is outside [0, 256). 

298  

299 """ 

300 cdef Stream s_stream = Stream_accept(stream) 2tdud[ ] ^ _ McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$c

301 cdef unsigned int val 

302 cdef unsigned int elem_size 

303 val, elem_size = _parse_fill_value(value) 2tdud[ ] ^ _ McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$c

304  

305 cdef size_t buffer_size = self._size 2tdud[ ] ^ _ fcnc'boc(bgc)bhcicjc*b+b,b-b.b/bkclc:b;bpc=b?bqc@brcNbscOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbtcYbZbuc|bvc0bwc1b}b2b~bacbc3b4b5b6b7b8bccdc9b!bxc#b$byc

306 cdef cydriver.CUdeviceptr dst = as_cu(self._h_ptr) 2tdud[ ] ^ _ fcnc'boc(bgc)bhcicjc*b+b,b-b.b/bkclc:b;bpc=b?bqc@brcNbscOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbtcYbZbuc|bvc0bwc1b}b2b~bacbc3b4b5b6b7b8bccdc9b!bxc#b$byc

307 cdef cydriver.CUstream s = as_cu(s_stream._h_stream) 2tdud[ ] ^ _ fcnc'boc(bgc)bhcicjc*b+b,b-b.b/bkclc:b;bpc=b?bqc@brcNbscOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbtcYbZbuc|bvc0bwc1b}b2b~bacbc3b4b5b6b7b8bccdc9b!bxc#b$byc

308  

309 if elem_size == 1: 2tdud[ ] ^ _ fcnc'boc(bgc)bhcicjc*b+b,b-b.b/bkclc:b;bpc=b?bqc@brcNbscOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbtcYbZbuc|bvc0bwc1b}b2b~bacbc3b4b5b6b7b8bccdc9b!bxc#b$byc

310 with nogil: 2tdud[ ] ^ _ fcgchcicjckclc@b[b]b^b_b`b{b|b}b~bacbcccdc

311 HANDLE_RETURN(cydriver.cuMemsetD8Async(dst, val, buffer_size, s)) 2tdud[ ] ^ _ fcgchcicjckclc@b[b]b^b_b`b{b|b}b~bacbcccdc

312 elif elem_size == 2: 

313 if buffer_size & 0x1: 2nc'b*b+b,b:b;bpcrcNbQbRbSbWbXbtcvc0b3b4b5b9b!bxc

314 raise ValueError(f"buffer size ({buffer_size}) must be divisible by 2") 2ncpcrctcvcxc

315 with nogil: 2'b*b+b,b:b;bNbQbRbSbWbXb0b3b4b5b9b!b

316 HANDLE_RETURN(cydriver.cuMemsetD16Async(dst, val, buffer_size // 2, s)) 2'b*b+b,b:b;bNbQbRbSbWbXb0b3b4b5b9b!b

317 elif elem_size == 4: 

318 if buffer_size & 0x3: 2oc(b)b-b.b/b=b?bqcscObPbTbUbVbYbZbucwc1b2b6b7b8b#b$byc

319 raise ValueError(f"buffer size ({buffer_size}) must be divisible by 4") 2ocqcscucwcyc

320 with nogil: 2(b)b-b.b/b=b?bObPbTbUbVbYbZb1b2b6b7b8b#b$b

321 HANDLE_RETURN(cydriver.cuMemsetD32Async(dst, val, buffer_size // 4, s)) 2(b)b-b.b/b=b?bObPbTbUbVbYbZb1b2b6b7b8b#b$b

322  

323 def __dlpack__( 

324 self, 

325 *, 

326 stream: int | None = None, 

327 max_version: tuple[int, int] | None = None, 

328 dl_device: tuple[int, int] | None = None, 

329 copy: bool | None = None, 

330 ) -> TypeVar("PyCapsule"): 

331 # Note: we ignore the stream argument entirely (as if it is -1). 

332 # It is the user's responsibility to maintain stream order. 

333 if dl_device is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc%b; { d

334 raise BufferError("Sorry, not supported: dl_device other than None") 2%b

335 if copy is True: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc%b; { d

336 raise BufferError("Sorry, not supported: copy=True") 2%b

337 if max_version is None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc%b; { d

338 versioned = False 2Kc%b

339 else: 

340 if not isinstance(max_version, tuple) or len(max_version) != 2: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : %b; { d

341 raise BufferError(f"Expected max_version tuple[int, int], got {max_version}") 2%b

342 versioned = max_version >= (1, 0) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : %b; { d

343 capsule = make_py_capsule(self, versioned) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc%b; { d

344 return capsule 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : %b; { d

345  

346 def __dlpack_device__(self) -> tuple[int, int]: 

347 return classify_dl_device(self) 2{c-c|c` | ; {

348  

349 def __buffer__(self, flags: int, /) -> memoryview: 

350 # Support for Python-level buffer protocol as per PEP 688. 

351 # This raises a BufferError unless: 

352 # 1. Python is 3.12+ 

353 # 2. This Buffer object is host accessible 

354 raise NotImplementedError("WIP: Buffer.__buffer__ hasn't been implemented yet.") 

355  

356 def __release_buffer__(self, buffer: memoryview, /): 

357 # Supporting method paired with __buffer__. 

358 raise NotImplementedError("WIP: Buffer.__release_buffer__ hasn't been implemented yet.") 

359  

360 @property 

361 def device_id(self) -> int: 

362 """Return the device ordinal of this buffer.""" 

363 if self._memory_resource is not None: 2MeNeOePeQe-c%bN O @ hbibP Q R S hdidReSeTe(cFcGcHcnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejekelemeneoepbqbrbsbtbubvb

364 return self._memory_resource.device_id 2MeNeOePeQe-c%bhdidReSeTe(cFcGcHc)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejekelemeneoe

365 _init_mem_attrs(self) 2N O @ hbibP Q R S nbobpbqbrbsbtbubvb

366 return self._mem_attrs.device_id 2N O @ hbibP Q R S nbobpbqbrbsbtbubvb

367  

368 @property 

369 def handle(self) -> DevicePointerType: 

370 """Return the buffer handle object. 

371  

372 .. caution:: 

373  

374 This handle is a Python object. To get the memory address of the underlying C 

375 handle, call ``int(Buffer.handle)``. 

376 """ 

377 # Return raw integer for compatibility with ctypes and other tools 

378 # that expect a raw pointer value 

379 return as_intptr(self._h_ptr) 2e f g h i j k l m n o p q r s t u v w x y z A B C D tdudE F G H I J T [ ] ^ _ wbxbybzbAbBbCbMeNeOeK L M db;c7d=c8d?c9d!dJc#deb@c$d[c%d? 'd]c(dfbgb= KbDbEbLbFbGbHbMbIbJb~ abkblbbbcbPeQe3d4dAd/c:c}cada b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : LcAcmcKc%bN O P Q @bNbOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbYbZb|b0b1b}b2b~bacbc3b4b5b6b7b8bccdc9b!b#b$bzcBc; { CcReSeTeDcEcsdHcd zeIcnbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejekelemeneoepbqbrbsbtbubvb

380  

381 def __eq__(self, other) -> bool: 

382 if not isinstance(other, Buffer): 2zeIcWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eVeKeLe

383 return NotImplemented 2zeWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,e

384 cdef Buffer other_buf = <Buffer>other 2zeIcVeKeLe

385 return (as_intptr(self._h_ptr) == as_intptr(other_buf._h_ptr) and 2zeIcVeKeLe

386 self._size == other_buf._size) 2zeIcKeLe

387  

388 def __hash__(self) -> int: 

389 return hash((as_intptr(self._h_ptr), self._size)) 2Ic/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkflfmfKeLe

390  

391 def __repr__(self) -> str: 

392 maybe_is_mapped = " is_mapped=True" if self.is_mapped else "" 2KbDbLdedvdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czdUe

393 return f"<Buffer ptr={as_intptr(self._h_ptr):#x} size={self._size}{maybe_is_mapped}>" 2KbDbLdedvdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czdUe

394  

395 @property 

396 def is_device_accessible(self) -> bool: 

397 """Return True if this buffer can be accessed by the GPU, otherwise False.""" 

398 if self._memory_resource is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc{c-c|c` | %bN O @ hbibP Q R S zc; { .c(c'cd nbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejekelemeneoepbqbrbsbtbubvb

399 return self._memory_resource.is_device_accessible 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc{c-c|c` | %bzc; { .c(c'cd )d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejekelemeneoe

400 _init_mem_attrs(self) 2N O @ hbibP Q R S nbobpbqbrbsbtbubvb

401 return self._mem_attrs.is_device_accessible 2N O @ hbibP Q R S nbobpbqbrbsbtbubvb

402  

403 @property 

404 def is_host_accessible(self) -> bool: 

405 """Return True if this buffer can be accessed by the CPU, otherwise False.""" 

406 if self._memory_resource is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc{c-c|c` | %bN O @ hbibP Q R S fc'b(bgc)bhcicjc*b+b,b-b.b/bkclc:b;b=b?b@bNbOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbYbZb|b0b1b}b2b~bacbc3b4b5b6b7b8bccdc9b!b#b$bzc; { .c(c'cd

407 return self._memory_resource.is_host_accessible 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc{c-c|c` | %bfc'b(bgc)bhcicjc*b+b,b-b.b/bkclc:b;b=b?b@bNbOb[bPb]b^b_bQbRbSbTbUbVb`b{bWbXbYbZb|b0b1b}b2b~bacbc3b4b5b6b7b8bccdc9b!b#b$bzc; { .c(c'cd

408 _init_mem_attrs(self) 2N O @ hbibP Q R S

409 return self._mem_attrs.is_host_accessible 2N O @ hbibP Q R S

410  

411 @property 

412 def is_managed(self) -> bool: 

413 """Return True if this buffer is CUDA managed (unified) memory, otherwise False.""" 

414 _init_mem_attrs(self) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | ; { } jbd

415 if self._mem_attrs.is_managed: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | ; { } jbd

416 return True 2mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb| ; {

417 # Pool-allocated managed memory does not set CU_POINTER_ATTRIBUTE_IS_MANAGED, 

418 # so fall back to the memory resource when it advertises managed allocations. 

419 return self._memory_resource is not None and self._memory_resource.is_managed 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M = a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` } jbd

420  

421 @property 

422 def is_mapped(self) -> bool: 

423 """Return True if this buffer is mapped into the process via IPC.""" 

424 return getattr(self._ipc_data, "is_mapped", False) 2KbDbLdedvdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czdzcUe

425  

426  

427 @property 

428 def memory_resource(self) -> MemoryResource: 

429 """Return the memory resource associated with this buffer.""" 

430 return self._memory_resource 2[ ] ^ _ wbxbybzbAbBbCbpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbLczcBc.cCcbd(cDc'cddFcGcIeJenbob)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-eleme.eneoepbqbrbsbtbubvb

431  

432 @property 

433 def size(self) -> int: 

434 """Return the memory size of this buffer.""" 

435 return self._size 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T [ ] ^ _ wbxbybzbAbBbCbMeNeOeK L M db;c7d=c8d?c9d!dJc#deb@c$d[c%d? 'd]c(dfbgb= peqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQe/c:cBd}c~ca b c )c*c+cU V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : Kc%bzchdidBc; { .cCcReSeTe(cDc'cFcGcEcsdHcd IcIeJe)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-eleme.eneoe

436  

437 @property 

438 def owner(self) -> object: 

439 """Return the object holding external allocation.""" 

440 return self._owner 

441  

442  

443# Memory Attribute Query Helpers 

444# ------------------------------ 

445cdef inline void _init_mem_attrs(Buffer self): 

446 """Initialize memory attributes by querying the pointer.""" 

447 if not self._mem_attrs_inited: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

448 _query_memory_attrs(self._mem_attrs, as_cu(self._h_ptr)) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

449 self._mem_attrs_inited = True 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

450  

451  

452cdef inline int _query_memory_attrs( 

453 _MemAttrs& out, 

454 cydriver.CUdeviceptr ptr 

455) except -1 nogil: 

456 """Query memory attributes for a device pointer.""" 

457 cdef unsigned int memory_type = 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

458 cdef int is_managed = 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

459 cdef int device_id = 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

460 cdef cydriver.CUpointer_attribute attrs[3] 

461 cdef uintptr_t vals[3] 

462  

463 attrs[0] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_MEMORY_TYPE 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

464 attrs[1] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_IS_MANAGED 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

465 attrs[2] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

466 vals[0] = <uintptr_t><void*>&memory_type 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

467 vals[1] = <uintptr_t><void*>&is_managed 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

468 vals[2] = <uintptr_t><void*>&device_id 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

469  

470 cdef cydriver.CUresult ret 

471 ret = cydriver.cuPointerGetAttributes(3, attrs, <void**>vals, ptr) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

472 if ret == cydriver.CUresult.CUDA_ERROR_NOT_INITIALIZED: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

473 with cython.gil: 

474 # Device class handles the cuInit call internally 

475 Device() 

476 ret = cydriver.cuPointerGetAttributes(3, attrs, <void**>vals, ptr) 

477 HANDLE_RETURN(ret) 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

478  

479 # TODO: HMM/ATS-enabled sysmem should also report is_managed=True; the 

480 # CU_POINTER_ATTRIBUTE_IS_MANAGED query does not capture that yet. 

481 out.is_managed = is_managed != 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

482  

483 if memory_type == 0: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

484 # unregistered host pointer 

485 out.is_host_accessible = True 2@ R S nbobpbqbrbsbtbubvb

486 out.is_device_accessible = False 2@ R S nbobpbqbrbsbtbubvb

487 out.device_id = -1 2@ R S nbobpbqbrbsbtbubvb

488 elif ( 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O hbibP Q R S ; { } jbd

489 is_managed 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O hbibP Q R S ; { } jbd

490 or memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_HOST 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M = a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` N O P Q R S } jbd

491 ): 

492 # Managed memory or pinned host memory 

493 out.is_host_accessible = True 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgba b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | hbibP Q R S ; { jbd

494 out.is_device_accessible = True 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgba b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | hbibP Q R S ; { jbd

495 out.device_id = device_id 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgba b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | hbibP Q R S ; { jbd

496 elif memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_DEVICE: 1=NO}

497 out.is_host_accessible = False 1=NO}

498 out.is_device_accessible = True 1=NO}

499 out.device_id = device_id 1=NO}

500 else: 

501 with cython.gil: 

502 raise ValueError(f"Unsupported memory type: {memory_type}") 

503 return 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M mbdb;c=c?cjdkdldeb@c[cmdnd? od]cfbgb= a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` | N O @ hbibP Q R S ; { } jbd nbobpbqbrbsbtbubvb

504  

505  

506cdef class MemoryResource: 

507 """Abstract base class for memory resources that manage allocation and 

508 deallocation of buffers. 

509  

510 Subclasses must implement methods for allocating and deallocation, as well 

511 as properties associated with this memory resource from which all allocated 

512 buffers will inherit. (Since all :class:`Buffer` instances allocated and 

513 returned by the :meth:`allocate` method would hold a reference to self, the 

514 buffer properties are retrieved simply by looking up the underlying memory 

515 resource's respective property.) 

516 """ 

517  

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

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

520  

521 Parameters 

522 ---------- 

523 size : int 

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

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

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

527 asynchronously. Must be passed explicitly; pass 

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

529  

530 Returns 

531 ------- 

532 Buffer 

533 The allocated buffer object, which can be used for device or host operations 

534 depending on the resource's properties. 

535 """ 

536 raise TypeError("MemoryResource.allocate must be implemented by subclasses.") 

537  

538 def deallocate(self, ptr: DevicePointerType, size_t size, *, stream: Stream | GraphBuilder): 

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

540  

541 Parameters 

542 ---------- 

543 ptr : :obj:`~_memory.DevicePointerType` 

544 The pointer or handle to the buffer to deallocate. 

545 size : int 

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

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

548 Keyword-only. The stream on which to perform the deallocation 

549 asynchronously. Must be passed explicitly; pass 

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

551 """ 

552 raise TypeError("MemoryResource.deallocate must be implemented by subclasses.") 

553  

554 @property 

555 def is_device_accessible(self) -> bool: 

556 """Whether buffers allocated by this resource are device-accessible.""" 

557 raise TypeError("MemoryResource.is_device_accessible must be implemented by subclasses.") 

558  

559 @property 

560 def is_host_accessible(self) -> bool: 

561 """Whether buffers allocated by this resource are host-accessible.""" 

562 raise TypeError("MemoryResource.is_host_accessible must be implemented by subclasses.") 

563  

564 @property 

565 def is_managed(self) -> bool: 

566 """Whether buffers allocated by this resource are CUDA managed (unified) memory.""" 

567 return False 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J T K L M a b c U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ` } jbd

568  

569 @property 

570 def device_id(self) -> int: 

571 """Device ID associated with this memory resource, or -1 if not applicable.""" 

572 raise TypeError("MemoryResource.device_id must be implemented by subclasses.") 

573  

574  

575# Buffer Implementation Helpers 

576# ----------------------------- 

577cdef Buffer Buffer_from_deviceptr_handle( 

578 DevicePtrHandle h_ptr, 

579 size_t size, 

580 MemoryResource mr, 

581 object ipc_descriptor = None, 

582 type cls = Buffer, 

583): 

584 """Create a Buffer (or subclass instance) from an existing DevicePtrHandle.""" 

585 cdef Buffer buf = cls.__new__(cls) 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

586 buf._h_ptr = h_ptr 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

587 buf._size = size 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

588 buf._memory_resource = mr 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

589 buf._ipc_data = IPCDataForBuffer(ipc_descriptor, True) if ipc_descriptor is not None else None 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

590 buf._owner = None 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

591 buf._mem_attrs_inited = False 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

592 return buf 2tdudCdDdEdnfofpf[ ] ^ _ wbxbybzbAbBbCbMeNeOepdqdrdmbdbFdqfebrfsffbgbGdHdIdJdKdpeqereseAeBeKbDbLdedteCeueDeveEeweFexeGeyeHevdwdMdNd^cxdEbLbFbGbHbMbIbJb_cyd`czd~ abkblbbbcbPeQeOdtfAdPd/cfd:cBd}c~cadgda b c N O hdidBc{ .cCcReSeTeQdRdSdTdUdVdWdXdYdZd0d1d2dbd} jb(cDc'cddd zeIc/e:e;e=e?e@e[e]e^e_e`e{e|e}e~eafbfcfdfefffgfhfifjfkfWeXeYeZe0e1e2e3e4e5e6e7e8e9e!e#e$e%e'e(e)e*e+e,eIeJeUeVelfmfKeLeufvf)d*d+d,d-d.d/d:d;d=d?d@d[d]d^d_d`d{d|d}d~daebecedeeefegeheiejeke-ewfxfleme.eneoe

593  

594  

595cdef inline void Buffer_close(Buffer self, object stream): 

596 """Close a buffer, freeing its memory.""" 

597 cdef Stream s 

598 if not self._h_ptr: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

599 return 

600 # Update deallocation stream if provided 

601 if stream is not None: 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

602 s = Stream_accept(stream) 2pdqdrd}cBcCc%cDc

603 set_deallocation_stream(self._h_ptr, s._h_stream) 2pdqdrd}cBcCc%cDc

604 # Reset handle - RAII deleter will free the memory (and release owner ref in C++) 

605 self._h_ptr.reset() 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

606 self._size = 0 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

607 self._memory_resource = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

608 self._ipc_data = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc

609 self._owner = None 2e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J CdDdEd[ ] ^ _ wbxbybzbAbBbCbpdqdrdK L M mbdbyfzfAfBfFdJcCfHfebDfIfEfJf? FfGffbgbGdHdIdJdKd= ~ abkblbbbcbOdAdPd/cfd:cBd}c~cadgdLcAcmcN O @ P Q R S McNcfcnc'bOcoc(bgcPcQcRc)bSchcicjc*b+b,b-b.b/bTckclc:b;bpc=b?bqcUcVcWc@brcNbXcscOb[bYcZc0cPb1c]b^b_bQbRbSbTbUbVb2c`b{bWbXbtcYbZbuc3c4c5c|bvc0b6cwc1b}b7c8c9c2b!c~bacbc3b4b5b6b7b8b#cccdc9b!bxc#b$byc$czchdidBc.cCcQdRdSdTdUdVdWdXdYdZd0d1d2dbd,c%c} jbcd(cDc'cddFcGcEcHc