Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 0 additions & 1 deletion cuda_bindings/cuda/bindings/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE

from typing import Any, Callable

from ._ptx_utils import get_minimal_required_cuda_ver_from_ptx_ver, get_ptx_ver
Expand Down
13 changes: 9 additions & 4 deletions cuda_core/cuda/core/experimental/_event.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from cuda.core.experimental._utils.cuda_utils import (
driver,
handle_return,
)

import sys
if TYPE_CHECKING:
import cuda.bindings
from cuda.core.experimental._device import Device
Expand Down Expand Up @@ -108,15 +108,20 @@ cdef class Event:
self._ctx_handle = ctx_handle
return self

cpdef close(self):
"""Destroy the event."""
cdef _shutdown_safe_close(self, is_shutting_down=sys.is_finalizing):
if is_shutting_down and is_shutting_down():
return
if self._handle is not None:
err, = driver.cuEventDestroy(self._handle)
self._handle = None
raise_if_driver_error(err)

cpdef close(self):
"""Destroy the event."""
self._shutdown_safe_close(is_shutting_down=None)

def __del__(self):
self.close()
self._shutdown_safe_close()

def __isub__(self, other):
return NotImplemented
Expand Down
18 changes: 12 additions & 6 deletions cuda_core/cuda/core/experimental/_memory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from cuda.core.experimental._utils.cuda_utils cimport (
_check_driver_error as raise_if_driver_error,
check_or_create_options,
)
import sys

from dataclasses import dataclass
from typing import TypeVar, Union, TYPE_CHECKING
Expand Down Expand Up @@ -69,7 +70,16 @@ cdef class Buffer:
return self

def __del__(self):
self.close()
self._shutdown_safe_close()

cdef _shutdown_safe_close(self, stream: Stream = None, is_shutting_down=sys.is_finalizing):
if is_shutting_down and is_shutting_down():
return
if self._ptr and self._mr is not None:
self._mr.deallocate(self._ptr, self._size, stream)
self._ptr = 0
self._mr = None
self._ptr_obj = None

cpdef close(self, stream: Stream = None):
"""Deallocate this buffer asynchronously on the given stream.
Expand All @@ -83,11 +93,7 @@ cdef class Buffer:
The stream object to use for asynchronous deallocation. If None,
the behavior depends on the underlying memory resource.
"""
if self._ptr and self._mr is not None:
self._mr.deallocate(self._ptr, self._size, stream)
self._ptr = 0
self._mr = None
self._ptr_obj = None
self._shutdown_safe_close(stream, is_shutting_down=None)

@property
def handle(self) -> DevicePointerT:
Expand Down
21 changes: 14 additions & 7 deletions cuda_core/cuda/core/experimental/_stream.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from cuda.core.experimental._utils.cuda_utils cimport (
_check_driver_error as raise_if_driver_error,
check_or_create_options,
)
import sys

import cython
import os
Expand Down Expand Up @@ -186,22 +187,28 @@ cdef class Stream:
return self

def __del__(self):
self.close()
self._shutdown_safe_close()

cpdef close(self):
"""Destroy the stream.
cdef _shutdown_safe_close(self, is_shutting_down=sys.is_finalizing):
if is_shutting_down and is_shutting_down():
return

Destroy the stream if we own it. Borrowed foreign stream
object will instead have their references released.

"""
if self._owner is None:
if self._handle and not self._builtin:
handle_return(driver.cuStreamDestroy(self._handle))
else:
self._owner = None
self._handle = None

cpdef close(self):
"""Destroy the stream.

Destroy the stream if we own it. Borrowed foreign stream
object will instead have their references released.

"""
self._shutdown_safe_close(is_shutting_down=None)

def __cuda_stream__(self) -> tuple[int, int]:
"""Return an instance of a __cuda_stream__ protocol."""
return (0, int(self.handle))
Expand Down