Skip to content

Add Buffer.release() #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions cuda_core/cuda/core/experimental/_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class Buffer:
"""

class _MembersNeededForFinalize:
__slots__ = ("ptr", "size", "mr")
__slots__ = ("ptr", "size", "mr", "finalizer")

def __init__(self, buffer_obj, ptr, size, mr):
self.ptr = ptr
self.size = size
self.mr = mr
weakref.finalize(buffer_obj, self.close)
self.finalizer = weakref.finalize(buffer_obj, self.close)

def close(self, stream=None):
if self.ptr and self.mr is not None:
Expand Down Expand Up @@ -83,6 +83,15 @@ def close(self, stream=None):
"""
self._mnff.close(stream)

def release(self):
"""Release this buffer from being subject to the garbage collection.

After this method is called, the caller is responsible for calling :meth:`close`
when done using this buffer, otherwise there would be a memory leak!
"""
self._mnff.finalizer.detach()
self._mnff.finalizer = None

@property
def handle(self) -> DevicePointerT:
"""Return the buffer handle object.
Expand Down
2 changes: 2 additions & 0 deletions cuda_core/docs/source/release/0.3.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ New features
------------

- :class:`Kernel` adds :property:`Kernel.num_arguments` and :property:`Kernel.arguments_info` for introspection of kernel arguments. (#612)
- :class:`Buffer` adds a method :meth:`Buffer.release` allowing users to have full control over its lifetime without the garbage collector's
interference

New examples
------------
Expand Down
47 changes: 41 additions & 6 deletions cuda_core/tests/test_memory.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# Copyright 2024 NVIDIA Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

try:
from cuda.bindings import driver
except ImportError:
from cuda import cuda as driver

import ctypes
import gc

import pytest

from cuda.core.experimental import Device
from cuda.core.experimental._memory import Buffer, DLDeviceType, MemoryResource
from cuda.core.experimental._utils.cuda_utils import handle_return
from cuda.core.experimental._utils.cuda_utils import driver, handle_return


class DummyDeviceMemoryResource(MemoryResource):
Expand Down Expand Up @@ -257,3 +253,42 @@ def test_buffer_dunder_dlpack_device_failure():
buffer = dummy_mr.allocate(size=1024)
with pytest.raises(BufferError, match=r"^buffer is neither device-accessible nor host-accessible$"):
buffer.__dlpack_device__()


class DummyTrackingMemoryResource(MemoryResource):
def __init__(self):
self.live_counts = 0

def allocate(self, size, stream=None) -> Buffer:
self.live_counts += 1
return Buffer(ptr=1, size=size, mr=self)

def deallocate(self, ptr, size, stream=None):
self.live_counts -= 1

@property
def is_device_accessible(self) -> bool:
return False

@property
def is_host_accessible(self) -> bool:
return False

@property
def device_id(self) -> int:
return 0


@pytest.mark.parametrize("close_buffer", (True, False))
def test_buffer_release_closed(close_buffer):
mr = DummyTrackingMemoryResource()
buf = mr.allocate(123)
buf.release()
if close_buffer:
buf.close()
del buf
gc.collect()
# If Buffer.close() is explicitly called, it would end up calling MR.deallocate()
# which then decrease the count. If the buffer does not have the finalizer attached,
# this is the only way to trigger deallocation.
assert mr.live_counts == 0 if close_buffer else 1
Loading