Skip to content

Commit 0fac800

Browse files
committed
Cythonize DeviceMemoryResourceAttributes.
1 parent 7c97d22 commit 0fac800

File tree

1 file changed

+31
-30
lines changed
  • cuda_core/cuda/core/experimental/_memory

1 file changed

+31
-30
lines changed

cuda_core/cuda/core/experimental/_memory/_dmr.pyx

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,66 +53,67 @@ cdef class DeviceMemoryResourceOptions:
5353
max_size : cython.size_t = 0
5454

5555

56-
# TODO: cythonize this?
57-
class DeviceMemoryResourceAttributes:
56+
cdef class DeviceMemoryResourceAttributes:
57+
cdef:
58+
object _mr_weakref
59+
5860
def __init__(self, *args, **kwargs):
5961
raise RuntimeError("DeviceMemoryResourceAttributes cannot be instantiated directly. Please use MemoryResource APIs.")
6062

6163
@classmethod
62-
def _init(cls, mr : DeviceMemoryReference):
63-
self = DeviceMemoryResourceAttributes.__new__(cls)
64-
self._mr = mr
64+
def _init(cls, mr):
65+
cdef DeviceMemoryResourceAttributes self = DeviceMemoryResourceAttributes.__new__(cls)
66+
self._mr_weakref = mr
6567
return self
6668

67-
def mempool_property(property_type: type):
68-
def decorator(stub):
69-
attr_enum = getattr(
70-
driver.CUmemPool_attribute, f"CU_MEMPOOL_ATTR_{stub.__name__.upper()}"
71-
)
72-
73-
def fget(self) -> property_type:
74-
mr = self._mr()
75-
if mr is None:
76-
raise RuntimeError("DeviceMemoryResource is expired")
77-
value = DMRA_getattribute(<cydriver.CUmemoryPool><uintptr_t> mr.handle,
78-
<cydriver.CUmemPool_attribute><uintptr_t> attr_enum)
79-
return property_type(value)
80-
return property(fget=fget, doc=stub.__doc__)
81-
return decorator
82-
83-
@mempool_property(bool)
69+
@DMRA_mempool_attribute(bool)
8470
def reuse_follow_event_dependencies(self):
8571
"""Allow memory to be reused when there are event dependencies between streams."""
8672

87-
@mempool_property(bool)
73+
@DMRA_mempool_attribute(bool)
8874
def reuse_allow_opportunistic(self):
8975
"""Allow reuse of completed frees without dependencies."""
9076

91-
@mempool_property(bool)
77+
@DMRA_mempool_attribute(bool)
9278
def reuse_allow_internal_dependencies(self):
9379
"""Allow insertion of new stream dependencies for memory reuse."""
9480

95-
@mempool_property(int)
81+
@DMRA_mempool_attribute(int)
9682
def release_threshold(self):
9783
"""Amount of reserved memory to hold before OS release."""
9884

99-
@mempool_property(int)
85+
@DMRA_mempool_attribute(int)
10086
def reserved_mem_current(self):
10187
"""Current amount of backing memory allocated."""
10288

103-
@mempool_property(int)
89+
@DMRA_mempool_attribute(int)
10490
def reserved_mem_high(self):
10591
"""High watermark of backing memory allocated."""
10692

107-
@mempool_property(int)
93+
@DMRA_mempool_attribute(int)
10894
def used_mem_current(self):
10995
"""Current amount of memory in use."""
11096

111-
@mempool_property(int)
97+
@DMRA_mempool_attribute(int)
11298
def used_mem_high(self):
11399
"""High watermark of memory in use."""
114100

115-
del mempool_property
101+
102+
cdef DMRA_mempool_attribute(property_type: type):
103+
def decorator(stub):
104+
attr_enum = getattr(
105+
driver.CUmemPool_attribute, f"CU_MEMPOOL_ATTR_{stub.__name__.upper()}"
106+
)
107+
108+
def fget(DeviceMemoryResourceAttributes self) -> property_type:
109+
cdef DeviceMemoryResource mr = <DeviceMemoryResource> self._mr_weakref()
110+
if mr is None:
111+
raise RuntimeError("DeviceMemoryResource is expired")
112+
value = DMRA_getattribute(<cydriver.CUmemoryPool><uintptr_t> mr.handle,
113+
<cydriver.CUmemPool_attribute><uintptr_t> attr_enum)
114+
return property_type(value)
115+
return property(fget=fget, doc=stub.__doc__)
116+
return decorator
116117

117118

118119
cdef int DMRA_getattribute(

0 commit comments

Comments
 (0)