44
55from __future__ import annotations
66
7+ from libc.stdint cimport uintptr_t
8+
79from 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
0 commit comments