Skip to content

Commit ceaa7f2

Browse files
committed
fix
1 parent 93fd3cf commit ceaa7f2

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

cuda_core/cuda/core/experimental/_memory.pyx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from __future__ import annotations
66

7+
from libc.stdint cimport uintptr_t
8+
79
from cuda.core.experimental._utils.cuda_utils cimport (
810
_check_driver_error as raise_if_driver_error,
911
)
@@ -37,7 +39,7 @@ cdef class Buffer:
3739
"""
3840

3941
cdef:
40-
object _ptr
42+
uintptr_t _ptr
4143
size_t _size
4244
object _mr
4345

@@ -47,7 +49,7 @@ cdef class Buffer:
4749
@classmethod
4850
def _init(cls, ptr: DevicePointerT, size_t size, mr: MemoryResource | None = None):
4951
cdef Buffer self = Buffer.__new__(cls)
50-
self._ptr = ptr
52+
self._ptr = <uintptr_t>(int(ptr))
5153
self._size = size
5254
self._mr = mr
5355
return self
@@ -333,6 +335,23 @@ class DeviceMemoryResource(MemoryResource):
333335
self._handle = handle_return(driver.cuDeviceGetMemPool(device_id))
334336
self._dev_id = device_id
335337

338+
# Set a higher release threshold to improve performance when there are no active allocations.
339+
# By default, the release threshold is 0, which means memory is immediately released back
340+
# to the OS when there are no active suballocations, causing performance issues.
341+
# Check current release threshold
342+
current_threshold = handle_return(
343+
driver.cuMemPoolGetAttribute(self._handle, driver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD)
344+
)
345+
# If threshold is 0 (default), set it to maximum to retain memory in the pool
346+
if int(current_threshold) == 0:
347+
handle_return(
348+
driver.cuMemPoolSetAttribute(
349+
self._handle,
350+
driver.CUmemPool_attribute.CU_MEMPOOL_ATTR_RELEASE_THRESHOLD,
351+
driver.cuuint64_t(0xFFFFFFFFFFFFFFFF),
352+
)
353+
)
354+
336355
def allocate(self, size: int, stream: Stream = None) -> Buffer:
337356
"""Allocate a buffer of the requested size.
338357

cuda_core/tests/test_memory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def __init__(self):
4646
def allocate(self, size, stream=None) -> Buffer:
4747
# Allocate a ctypes buffer of size `size`
4848
ptr = (ctypes.c_byte * size)()
49-
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
49+
self._ptr = ptr
50+
return Buffer.from_handle(ptr=ctypes.addressof(ptr), size=size, mr=self)
5051

5152
def deallocate(self, ptr, size, stream=None):
52-
# the memory is deallocated per the ctypes deallocation at garbage collection time
53-
pass
53+
del self._ptr
5454

5555
@property
5656
def is_device_accessible(self) -> bool:

0 commit comments

Comments
 (0)