Skip to content

Commit 61326e9

Browse files
committed
address review comments
- Add note to GILReleaseGuard about behavior when GIL isn't released - Add comment for mutable h_stream in DevicePtrBox - Add comment to get_box() explaining pointer recovery technique - Add _init_handles_table() comments to all .pyx files referencing DESIGN.md - Remove redundant static from items inside anonymous namespace - Wrap *Box structs in anonymous namespace for encapsulation
1 parent 688fee2 commit 61326e9

9 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎cuda_core/cuda/core/_cpp/resource_handles.cpp‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ namespace cuda_core {
2222

2323
namespace {
2424

25-
static std::once_flag driver_load_once;
26-
static std::atomic<bool> driver_loaded{false};
25+
std::once_flag driver_load_once;
26+
std::atomic<bool> driver_loaded{false};
2727

2828
#if PY_VERSION_HEX < 0x030D0000
2929
extern "C" int _Py_IsFinalizing(void);
3030
#endif
3131

32-
static inline bool py_is_finalizing() noexcept {
32+
inline bool py_is_finalizing() noexcept {
3333
#if PY_VERSION_HEX >= 0x030D0000
3434
return Py_IsFinalizing();
3535
#else
@@ -60,6 +60,9 @@ class GILReleaseGuard {
6060
tstate_ = PyEval_SaveThread();
6161
released_ = true;
6262
}
63+
// Note: If the GIL is not released (finalizing, or not held):
64+
// - Reduces parallelism (other Python threads remain blocked)
65+
// - No deadlock risk as long as the guarded code doesn't call back into Python
6366
}
6467

6568
~GILReleaseGuard() {
@@ -108,7 +111,7 @@ class GILAcquireGuard {
108111
};
109112

110113

111-
#define DECLARE_DRIVER_FN(name) using name##_t = decltype(&name); static name##_t p_##name = nullptr
114+
#define DECLARE_DRIVER_FN(name) using name##_t = decltype(&name); name##_t p_##name = nullptr
112115

113116
DECLARE_DRIVER_FN(cuDevicePrimaryCtxRetain);
114117
DECLARE_DRIVER_FN(cuDevicePrimaryCtxRelease);
@@ -142,7 +145,7 @@ DECLARE_DRIVER_FN(cuMemPoolImportPointer);
142145

143146
#undef DECLARE_DRIVER_FN
144147

145-
static bool load_driver_api() noexcept {
148+
bool load_driver_api() noexcept {
146149
struct CudaDriverApiV1 {
147150
std::uint32_t abi_version;
148151
std::uint32_t struct_size;
@@ -261,7 +264,7 @@ static bool load_driver_api() noexcept {
261264
return true;
262265
}
263266

264-
static bool ensure_driver_loaded() noexcept {
267+
bool ensure_driver_loaded() noexcept {
265268
// Fast path: already loaded (no locking needed)
266269
if (driver_loaded.load(std::memory_order_acquire)) {
267270
return true;
@@ -286,7 +289,7 @@ static bool ensure_driver_loaded() noexcept {
286289
// ============================================================================
287290

288291
// Thread-local status of the most recent CUDA API call in this module.
289-
thread_local CUresult err = CUDA_SUCCESS;
292+
static thread_local CUresult err = CUDA_SUCCESS;
290293

291294
CUresult get_last_error() noexcept {
292295
CUresult e = err;
@@ -306,17 +309,19 @@ void clear_last_error() noexcept {
306309
// Context Handles
307310
// ============================================================================
308311

312+
namespace {
309313
struct ContextBox {
310314
CUcontext resource;
311315
};
316+
} // namespace
312317

313318
ContextHandle create_context_handle_ref(CUcontext ctx) {
314319
auto box = std::make_shared<const ContextBox>(ContextBox{ctx});
315320
return ContextHandle(box, &box->resource);
316321
}
317322

318323
// Thread-local cache of primary contexts indexed by device ID
319-
thread_local std::vector<ContextHandle> primary_context_cache;
324+
static thread_local std::vector<ContextHandle> primary_context_cache;
320325

321326
ContextHandle get_primary_context(int device_id) noexcept {
322327
if (!ensure_driver_loaded()) {
@@ -375,9 +380,11 @@ ContextHandle get_current_context() noexcept {
375380
// Stream Handles
376381
// ============================================================================
377382

383+
namespace {
378384
struct StreamBox {
379385
CUstream resource;
380386
};
387+
} // namespace
381388

382389
StreamHandle create_stream_handle(ContextHandle h_ctx, unsigned int flags, int priority) {
383390
if (!ensure_driver_loaded()) {
@@ -435,9 +442,11 @@ StreamHandle get_per_thread_stream() noexcept {
435442
// Event Handles
436443
// ============================================================================
437444

445+
namespace {
438446
struct EventBox {
439447
CUevent resource;
440448
};
449+
} // namespace
441450

442451
EventHandle create_event_handle(ContextHandle h_ctx, unsigned int flags) {
443452
if (!ensure_driver_loaded()) {
@@ -491,9 +500,11 @@ EventHandle create_event_handle_ipc(const CUipcEventHandle& ipc_handle) {
491500
// Memory Pool Handles
492501
// ============================================================================
493502

503+
namespace {
494504
struct MemoryPoolBox {
495505
CUmemoryPool resource;
496506
};
507+
} // namespace
497508

498509
// Helper to clear peer access before destroying a memory pool.
499510
// Works around nvbug 5698116: recycled pool handles inherit peer access state.
@@ -574,11 +585,21 @@ MemoryPoolHandle create_mempool_handle_ipc(int fd, CUmemAllocationHandleType han
574585
// Device Pointer Handles
575586
// ============================================================================
576587

588+
namespace {
577589
struct DevicePtrBox {
578590
CUdeviceptr resource;
591+
// Mutable to allow set_deallocation_stream() to update the stream
592+
// through a const DevicePtrHandle. The stream can be changed after
593+
// allocation (e.g., to synchronize deallocation with a different stream).
579594
mutable StreamHandle h_stream;
580595
};
596+
} // namespace
581597

598+
// Recovers the owning DevicePtrBox from the aliased CUdeviceptr pointer.
599+
// This works because DevicePtrHandle is a shared_ptr alias pointing to
600+
// &box->resource, so we can compute the containing struct using offsetof.
601+
// The const_cast is safe because we only use this to access the mutable
602+
// h_stream member or in the deleter (where the box is being destroyed).
582603
static DevicePtrBox* get_box(const DevicePtrHandle& h) {
583604
const CUdeviceptr* p = h.get();
584605
return reinterpret_cast<DevicePtrBox*>(
@@ -728,13 +749,15 @@ DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner) {
728749
// The cache key is the export_data bytes (CUmemPoolPtrExportData), not the
729750
// returned pointer, because we must check before calling the driver API.
730751

752+
731753
// TODO: When driver fix for nvbug 5570902 is available, consider whether
732754
// the cache is still needed for memory pool allocations (it will still be
733755
// needed for pinned memory).
734756
static bool use_ipc_ptr_cache() {
735757
return true;
736758
}
737759

760+
namespace {
738761
// Wrapper for CUmemPoolPtrExportData to use as map key
739762
struct ExportDataKey {
740763
CUmemPoolPtrExportData data;
@@ -756,6 +779,8 @@ struct ExportDataKeyHash {
756779
}
757780
};
758781

782+
}
783+
759784
static std::mutex ipc_ptr_cache_mutex;
760785
static std::unordered_map<ExportDataKey, std::weak_ptr<DevicePtrBox>, ExportDataKeyHash> ipc_ptr_cache;
761786

‎cuda_core/cuda/core/_device.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ from cuda.core._resource_handles cimport (
2323
cu,
2424
)
2525

26+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2627
_init_handles_table()
28+
2729
from cuda.core._graph import GraphBuilder
2830
from cuda.core._stream import IsStreamT, Stream, StreamOptions
2931
from cuda.core._utils.clear_error_support import assert_type

‎cuda_core/cuda/core/_event.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ from cuda.core._resource_handles cimport (
1919
py,
2020
)
2121

22+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2223
_init_handles_table()
24+
2325
from cuda.core._utils.cuda_utils cimport (
2426
check_or_create_options,
2527
HANDLE_RETURN

‎cuda_core/cuda/core/_memory/_buffer.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ from cuda.core._resource_handles cimport (
2323
set_deallocation_stream,
2424
)
2525

26+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2627
_init_handles_table()
28+
2729
from cuda.core._stream cimport Stream_accept, Stream
2830
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2931

‎cuda_core/cuda/core/_memory/_graph_memory_resource.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ from cuda.core._resource_handles cimport (
1515
cu,
1616
)
1717

18+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
1819
_init_handles_table()
20+
1921
from cuda.core._stream cimport default_stream, Stream_accept, Stream
2022
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2123

‎cuda_core/cuda/core/_memory/_ipc.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ from cuda.core._resource_handles cimport (
1717
cu,
1818
)
1919

20+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2021
_init_handles_table()
22+
2123
from cuda.core._stream cimport default_stream
2224
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2325
from cuda.core._utils.cuda_utils import check_multiprocessing_start_method

‎cuda_core/cuda/core/_memory/_memory_pool.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ from cuda.core._resource_handles cimport (
2525
py,
2626
)
2727

28+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2829
_init_handles_table()
30+
2931
from cuda.core._utils.cuda_utils cimport (
3032
HANDLE_RETURN,
3133
)

‎cuda_core/cuda/core/_memoryview.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ from cuda.core._resource_handles cimport (
2121
cu,
2222
)
2323

24+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
2425
_init_handles_table()
26+
2527
from cuda.core._utils.cuda_utils import handle_return, driver
2628
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2729

‎cuda_core/cuda/core/_stream.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ from cuda.core._resource_handles cimport (
4242
py,
4343
)
4444

45+
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
4546
_init_handles_table()
47+
4648
from cuda.core._graph import GraphBuilder
4749

4850

0 commit comments

Comments
 (0)