Skip to content

Commit da58b7d

Browse files
committed
Simplify green context descriptor handling
1 parent 7c35ef3 commit da58b7d

6 files changed

Lines changed: 22 additions & 68 deletions

File tree

cuda_core/cuda/core/_cpp/resource_handles.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ struct GreenCtxBox {
234234
CUgreenCtx resource;
235235
};
236236

237-
struct DevResourceDescBox {
238-
CUdevResourceDesc resource;
239-
};
240-
241237
static const ContextBox* get_box(const ContextHandle& h) noexcept {
242238
const CUcontext* p = h.get();
243239
return reinterpret_cast<const ContextBox*>(
@@ -302,13 +298,19 @@ GreenCtxHandle get_context_green_ctx(const ContextHandle& h) noexcept {
302298
return get_box(h)->h_green_ctx;
303299
}
304300

305-
GreenCtxHandle create_green_ctx_handle(CUdevResourceDesc desc, CUdevice dev, unsigned int flags) {
306-
if (!p_cuGreenCtxCreate || !p_cuGreenCtxDestroy) {
301+
GreenCtxHandle create_green_ctx_handle(CUdevResource* resources, unsigned int nbResources,
302+
CUdevice dev, unsigned int flags) {
303+
if (!p_cuDevResourceGenerateDesc || !p_cuGreenCtxCreate || !p_cuGreenCtxDestroy) {
307304
err = CUDA_ERROR_NOT_SUPPORTED;
308305
return {};
309306
}
310307

311308
GILReleaseGuard gil;
309+
CUdevResourceDesc desc = nullptr;
310+
if (CUDA_SUCCESS != (err = p_cuDevResourceGenerateDesc(&desc, resources, nbResources))) {
311+
return {};
312+
}
313+
312314
CUgreenCtx green_ctx = nullptr;
313315
if (CUDA_SUCCESS != (err = p_cuGreenCtxCreate(&green_ctx, desc, dev, flags))) {
314316
return {};
@@ -333,22 +335,6 @@ GreenCtxHandle create_green_ctx_handle_ref(CUgreenCtx green_ctx) {
333335
return GreenCtxHandle(box, &box->resource);
334336
}
335337

336-
DevResourceDescHandle create_dev_resource_desc_handle(CUdevResource* resources, unsigned int nbResources) {
337-
if (!p_cuDevResourceGenerateDesc) {
338-
err = CUDA_ERROR_NOT_SUPPORTED;
339-
return {};
340-
}
341-
342-
GILReleaseGuard gil;
343-
CUdevResourceDesc desc = nullptr;
344-
if (CUDA_SUCCESS != (err = p_cuDevResourceGenerateDesc(&desc, resources, nbResources))) {
345-
return {};
346-
}
347-
348-
auto box = std::make_shared<const DevResourceDescBox>(DevResourceDescBox{desc});
349-
return DevResourceDescHandle(box, &box->resource);
350-
}
351-
352338
// Thread-local cache of primary contexts indexed by device ID
353339
static thread_local std::vector<ContextHandle> primary_context_cache;
354340

cuda_core/cuda/core/_cpp/resource_handles.hpp

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ extern NvJitLinkDestroyFn p_nvJitLinkDestroy;
147147

148148
using ContextHandle = std::shared_ptr<const CUcontext>;
149149
using GreenCtxHandle = std::shared_ptr<const CUgreenCtx>;
150-
using DevResourceDescHandle = std::shared_ptr<const CUdevResourceDesc>;
151150
using StreamHandle = std::shared_ptr<const CUstream>;
152151
using EventHandle = std::shared_ptr<const CUevent>;
153152
using MemoryPoolHandle = std::shared_ptr<const CUmemoryPool>;
@@ -178,16 +177,13 @@ ContextHandle create_context_handle_from_green_ctx(const GreenCtxHandle& h_green
178177
// Return the green context dependency associated with a ContextHandle, if any.
179178
GreenCtxHandle get_context_green_ctx(const ContextHandle& h) noexcept;
180179

181-
// Create an owning green context handle.
182-
GreenCtxHandle create_green_ctx_handle(CUdevResourceDesc desc, CUdevice dev, unsigned int flags);
180+
// Create an owning green context handle from a list of device resources.
181+
GreenCtxHandle create_green_ctx_handle(CUdevResource* resources, unsigned int nbResources,
182+
CUdevice dev, unsigned int flags);
183183

184184
// Create a non-owning green context handle.
185185
GreenCtxHandle create_green_ctx_handle_ref(CUgreenCtx ctx);
186186

187-
// Generate a descriptor for a resource list. CUDA exposes no explicit destroy
188-
// API for CUdevResourceDesc; this handle only carries the opaque value.
189-
DevResourceDescHandle create_dev_resource_desc_handle(CUdevResource* resources, unsigned int nbResources);
190-
191187
// Get handle to the primary context for a device (with thread-local caching)
192188
// Returns empty handle on error (caller must check)
193189
ContextHandle get_primary_context(int device_id);
@@ -532,10 +528,6 @@ inline CUgreenCtx as_cu(const GreenCtxHandle& h) noexcept {
532528
return h ? *h : nullptr;
533529
}
534530

535-
inline CUdevResourceDesc as_cu(const DevResourceDescHandle& h) noexcept {
536-
return h ? *h : nullptr;
537-
}
538-
539531
inline CUstream as_cu(const StreamHandle& h) noexcept {
540532
return h ? *h : nullptr;
541533
}
@@ -598,10 +590,6 @@ inline std::intptr_t as_intptr(const GreenCtxHandle& h) noexcept {
598590
return reinterpret_cast<std::intptr_t>(as_cu(h));
599591
}
600592

601-
inline std::intptr_t as_intptr(const DevResourceDescHandle& h) noexcept {
602-
return reinterpret_cast<std::intptr_t>(as_cu(h));
603-
}
604-
605593
inline std::intptr_t as_intptr(const StreamHandle& h) noexcept {
606594
return reinterpret_cast<std::intptr_t>(as_cu(h));
607595
}
@@ -696,10 +684,6 @@ inline PyObject* as_py(const GreenCtxHandle& h) noexcept {
696684
return detail::make_py("cuda.bindings.driver", "CUgreenCtx", as_intptr(h));
697685
}
698686

699-
inline PyObject* as_py(const DevResourceDescHandle& h) noexcept {
700-
return detail::make_py("cuda.bindings.driver", "CUdevResourceDesc", as_intptr(h));
701-
}
702-
703687
inline PyObject* as_py(const StreamHandle& h) noexcept {
704688
return detail::make_py("cuda.bindings.driver", "CUstream", as_intptr(h));
705689
}

cuda_core/cuda/core/_device.pyx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ from cuda.core._device_resources cimport DeviceResources, SMResource, WorkqueueR
1818
from cuda.core._device_resources import (
1919
DeviceResources,
2020
SMResource,
21-
SMResourceOptions,
2221
WorkqueueResource,
23-
WorkqueueResourceOptions,
2422
)
2523
from cuda.core._event cimport Event as cyEvent
2624
from cuda.core._event import Event, EventOptions
2725
from cuda.core._memory._buffer cimport Buffer, MemoryResource
2826
from cuda.core._resource_handles cimport (
2927
ContextHandle,
30-
DevResourceDescHandle,
3128
GreenCtxHandle,
3229
create_context_handle_ref,
33-
create_dev_resource_desc_handle,
3430
create_green_ctx_handle,
3531
get_primary_context,
3632
get_last_error,
@@ -1308,7 +1304,6 @@ class Device:
13081304
cdef SMResource sm_res
13091305
cdef WorkqueueResource wq_res
13101306
cdef cydriver.CUdevResource* c_resources = NULL
1311-
cdef DevResourceDescHandle h_desc
13121307
cdef GreenCtxHandle h_green
13131308

13141309
if options is None:
@@ -1346,13 +1341,9 @@ class Device:
13461341
else:
13471342
raise TypeError(f"Unsupported context resource type: {type(res)}")
13481343

1349-
h_desc = create_dev_resource_desc_handle(c_resources, <unsigned int>n_resources)
1350-
if h_desc.get() == NULL:
1351-
HANDLE_RETURN(get_last_error())
1352-
raise RuntimeError("Failed to generate CUDA device resource descriptor")
1353-
13541344
h_green = create_green_ctx_handle(
1355-
as_cu(h_desc),
1345+
c_resources,
1346+
<unsigned int>n_resources,
13561347
<cydriver.CUdevice>self._device_id,
13571348
<unsigned int>cydriver.CUgreenCtxCreate_flags.CU_GREEN_CTX_DEFAULT_STREAM,
13581349
)

cuda_core/cuda/core/_device_resources.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,22 @@ cdef class DeviceResources:
460460
"""Query workqueue resources from this device."""
461461
_check_green_ctx_support()
462462
_check_workqueue_support()
463-
cdef cydriver.CUdevResource wq_config
464-
cdef cydriver.CUdevResource wq
463+
cdef cydriver.CUdevResource _wq_config
464+
cdef cydriver.CUdevResource _wq
465465

466466
IF CUDA_CORE_BUILD_MAJOR >= 13:
467467
with nogil:
468468
HANDLE_RETURN(cydriver.cuDeviceGetDevResource(
469469
<cydriver.CUdevice>self._device_id,
470-
&wq_config,
470+
&_wq_config,
471471
cydriver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_WORKQUEUE_CONFIG,
472472
))
473473
HANDLE_RETURN(cydriver.cuDeviceGetDevResource(
474474
<cydriver.CUdevice>self._device_id,
475-
&wq,
475+
&_wq,
476476
cydriver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_WORKQUEUE,
477477
))
478-
return WorkqueueResource._from_dev_resources(wq_config, wq)
478+
return WorkqueueResource._from_dev_resources(_wq_config, _wq)
479479
ELSE:
480480
raise NotImplementedError(
481481
"WorkqueueResource requires cuda.core to be built with CUDA 13.x bindings"

cuda_core/cuda/core/_resource_handles.pxd

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
2121
# Handle types
2222
ctypedef shared_ptr[const cydriver.CUcontext] ContextHandle
2323
ctypedef shared_ptr[const cydriver.CUgreenCtx] GreenCtxHandle
24-
ctypedef shared_ptr[const cydriver.CUdevResourceDesc] DevResourceDescHandle
2524
ctypedef shared_ptr[const cydriver.CUstream] StreamHandle
2625
ctypedef shared_ptr[const cydriver.CUevent] EventHandle
2726
ctypedef shared_ptr[const cydriver.CUmemoryPool] MemoryPoolHandle
@@ -48,7 +47,6 @@ cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
4847
# as_cu() - extract the raw CUDA handle (inline C++)
4948
cydriver.CUcontext as_cu(ContextHandle h) noexcept nogil
5049
cydriver.CUgreenCtx as_cu(GreenCtxHandle h) noexcept nogil
51-
cydriver.CUdevResourceDesc as_cu(DevResourceDescHandle h) noexcept nogil
5250
cydriver.CUstream as_cu(StreamHandle h) noexcept nogil
5351
cydriver.CUevent as_cu(EventHandle h) noexcept nogil
5452
cydriver.CUmemoryPool as_cu(MemoryPoolHandle h) noexcept nogil
@@ -66,7 +64,6 @@ cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
6664
# as_intptr() - extract handle as intptr_t for Python interop (inline C++)
6765
intptr_t as_intptr(ContextHandle h) noexcept nogil
6866
intptr_t as_intptr(GreenCtxHandle h) noexcept nogil
69-
intptr_t as_intptr(DevResourceDescHandle h) noexcept nogil
7067
intptr_t as_intptr(StreamHandle h) noexcept nogil
7168
intptr_t as_intptr(EventHandle h) noexcept nogil
7269
intptr_t as_intptr(MemoryPoolHandle h) noexcept nogil
@@ -85,7 +82,6 @@ cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
8582
# as_py() - convert handle to Python wrapper object (inline C++; requires GIL)
8683
object as_py(ContextHandle h)
8784
object as_py(GreenCtxHandle h)
88-
object as_py(DevResourceDescHandle h)
8985
object as_py(StreamHandle h)
9086
object as_py(EventHandle h)
9187
object as_py(MemoryPoolHandle h)
@@ -118,10 +114,9 @@ cdef ContextHandle create_context_handle_ref(cydriver.CUcontext ctx) except+ nog
118114
cdef ContextHandle create_context_handle_from_green_ctx(const GreenCtxHandle& h_green_ctx) except+ nogil
119115
cdef GreenCtxHandle get_context_green_ctx(const ContextHandle& h) noexcept nogil
120116
cdef GreenCtxHandle create_green_ctx_handle(
121-
cydriver.CUdevResourceDesc desc, cydriver.CUdevice dev, unsigned int flags) except+ nogil
117+
cydriver.CUdevResource* resources, unsigned int nbResources,
118+
cydriver.CUdevice dev, unsigned int flags) except+ nogil
122119
cdef GreenCtxHandle create_green_ctx_handle_ref(cydriver.CUgreenCtx ctx) except+ nogil
123-
cdef DevResourceDescHandle create_dev_resource_desc_handle(
124-
cydriver.CUdevResource* resources, unsigned int nbResources) except+ nogil
125120
cdef ContextHandle get_primary_context(int device_id) except+ nogil
126121
cdef ContextHandle get_current_context() except+ nogil
127122

cuda_core/cuda/core/_resource_handles.pyx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ from cuda.bindings cimport cynvjitlink
2121
from ._resource_handles cimport (
2222
ContextHandle,
2323
GreenCtxHandle,
24-
DevResourceDescHandle,
2524
StreamHandle,
2625
EventHandle,
2726
MemoryPoolHandle,
@@ -62,11 +61,10 @@ cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
6261
GreenCtxHandle get_context_green_ctx "cuda_core::get_context_green_ctx" (
6362
const ContextHandle& h) noexcept nogil
6463
GreenCtxHandle create_green_ctx_handle "cuda_core::create_green_ctx_handle" (
65-
cydriver.CUdevResourceDesc desc, cydriver.CUdevice dev, unsigned int flags) except+ nogil
64+
cydriver.CUdevResource* resources, unsigned int nbResources,
65+
cydriver.CUdevice dev, unsigned int flags) except+ nogil
6666
GreenCtxHandle create_green_ctx_handle_ref "cuda_core::create_green_ctx_handle_ref" (
6767
cydriver.CUgreenCtx ctx) except+ nogil
68-
DevResourceDescHandle create_dev_resource_desc_handle "cuda_core::create_dev_resource_desc_handle" (
69-
cydriver.CUdevResource* resources, unsigned int nbResources) except+ nogil
7068
ContextHandle get_primary_context "cuda_core::get_primary_context" (
7169
int device_id) except+ nogil
7270
ContextHandle get_current_context "cuda_core::get_current_context" () except+ nogil

0 commit comments

Comments
 (0)