Skip to content

Commit 1f67ea2

Browse files
authored
cythonize device mr attributes (#1274)
1 parent 1e23bf8 commit 1f67ea2

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

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

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ from cuda.core.experimental._utils.cuda_utils cimport (
2020

2121
from dataclasses import dataclass
2222
from typing import Optional, TYPE_CHECKING
23-
import cython
2423
import platform # no-cython-lint
2524
import uuid
2625
import weakref
@@ -49,8 +48,8 @@ cdef class DeviceMemoryResourceOptions:
4948
Maximum pool size. When set to 0, defaults to a system-dependent value.
5049
(Default to 0)
5150
"""
52-
ipc_enabled : cython.bint = False
53-
max_size : cython.size_t = 0
51+
ipc_enabled : bool = False
52+
max_size : int = 0
5453

5554

5655
cdef class DeviceMemoryResourceAttributes:
@@ -66,63 +65,70 @@ cdef class DeviceMemoryResourceAttributes:
6665
self._mr_weakref = mr
6766
return self
6867

69-
@DMRA_mempool_attribute(bool)
68+
cdef int _getattribute(self, cydriver.CUmemPool_attribute attr_enum, void* value) except?-1:
69+
cdef DeviceMemoryResource mr = <DeviceMemoryResource>(self._mr_weakref())
70+
if mr is None:
71+
raise RuntimeError("DeviceMemoryResource is expired")
72+
cdef cydriver.CUmemoryPool pool_handle = mr._handle
73+
with nogil:
74+
HANDLE_RETURN(cydriver.cuMemPoolGetAttribute(pool_handle, attr_enum, value))
75+
return 0
76+
77+
@property
7078
def reuse_follow_event_dependencies(self):
7179
"""Allow memory to be reused when there are event dependencies between streams."""
80+
cdef int value
81+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_FOLLOW_EVENT_DEPENDENCIES, &value)
82+
return bool(value)
7283

73-
@DMRA_mempool_attribute(bool)
84+
@property
7485
def reuse_allow_opportunistic(self):
7586
"""Allow reuse of completed frees without dependencies."""
87+
cdef int value
88+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_ALLOW_OPPORTUNISTIC, &value)
89+
return bool(value)
7690

77-
@DMRA_mempool_attribute(bool)
91+
@property
7892
def reuse_allow_internal_dependencies(self):
7993
"""Allow insertion of new stream dependencies for memory reuse."""
94+
cdef int value
95+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_REUSE_ALLOW_INTERNAL_DEPENDENCIES, &value)
96+
return bool(value)
8097

81-
@DMRA_mempool_attribute(int)
98+
@property
8299
def release_threshold(self):
83100
"""Amount of reserved memory to hold before OS release."""
101+
cdef cydriver.cuuint64_t value
102+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD, &value)
103+
return int(value)
84104

85-
@DMRA_mempool_attribute(int)
105+
@property
86106
def reserved_mem_current(self):
87107
"""Current amount of backing memory allocated."""
108+
cdef cydriver.cuuint64_t value
109+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RESERVED_MEM_CURRENT, &value)
110+
return int(value)
88111

89-
@DMRA_mempool_attribute(int)
112+
@property
90113
def reserved_mem_high(self):
91114
"""High watermark of backing memory allocated."""
115+
cdef cydriver.cuuint64_t value
116+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RESERVED_MEM_HIGH, &value)
117+
return int(value)
92118

93-
@DMRA_mempool_attribute(int)
119+
@property
94120
def used_mem_current(self):
95121
"""Current amount of memory in use."""
122+
cdef cydriver.cuuint64_t value
123+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_USED_MEM_CURRENT, &value)
124+
return int(value)
96125

97-
@DMRA_mempool_attribute(int)
126+
@property
98127
def used_mem_high(self):
99128
"""High watermark of memory in use."""
100-
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
117-
118-
119-
cdef int DMRA_getattribute(
120-
cydriver.CUmemoryPool pool_handle, cydriver.CUmemPool_attribute attr_enum
121-
):
122-
cdef int value
123-
with nogil:
124-
HANDLE_RETURN(cydriver.cuMemPoolGetAttribute(pool_handle, attr_enum, <void *> &value))
125-
return value
129+
cdef cydriver.cuuint64_t value
130+
self._getattribute(cydriver.CUmemPool_attribute.CU_MEMPOOL_ATTR_USED_MEM_HIGH, &value)
131+
return int(value)
126132

127133

128134
cdef class DeviceMemoryResource(MemoryResource):

0 commit comments

Comments
 (0)