Skip to content

Commit 10623ee

Browse files
committed
Rename handle accessor functions for clarity
Rename cu() -> as_cu(), py() -> as_py(), intptr() -> as_intptr() to avoid overly short two-letter function names per review feedback.
1 parent b3c6c64 commit 10623ee

15 files changed

Lines changed: 131 additions & 131 deletions

‎cuda_core/cuda/core/_context.pyx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ from dataclasses import dataclass
66

77
from cuda.core._resource_handles cimport (
88
ContextHandle,
9-
intptr,
10-
py,
9+
as_intptr,
10+
as_py,
1111
)
1212

1313

@@ -37,16 +37,16 @@ cdef class Context:
3737
"""Return the underlying CUcontext handle."""
3838
if self._h_context.get() == NULL:
3939
return None
40-
return py(self._h_context)
40+
return as_py(self._h_context)
4141

4242
def __eq__(self, other):
4343
if not isinstance(other, Context):
4444
return NotImplemented
4545
cdef Context _other = <Context>other
46-
return intptr(self._h_context) == intptr(_other._h_context)
46+
return as_intptr(self._h_context) == as_intptr(_other._h_context)
4747

4848
def __hash__(self) -> int:
49-
return hash((type(self), intptr(self._h_context)))
49+
return hash((type(self), as_intptr(self._h_context)))
5050

5151

5252
@dataclass

‎cuda_core/cuda/core/_cpp/DESIGN.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ Handles can be accessed in three ways via overloaded helper functions:
7373

7474
| Function | Returns | Use Case | Notes
7575
|----------|---------|----------|-------|
76-
| `cu(h)` | Raw CUDA type (e.g., `CUstream`) | Passing to CUDA APIs | An attribute of `cuda.bindings.cydriver` |
77-
| `intptr(h)` | `intptr_t` | Python interop, foreign code | |
78-
| `py(h)` | Python wrapper object | Returning to Python callers | An attribute of `cure.bindings.driver`
76+
| `as_cu(h)` | Raw CUDA type (e.g., `CUstream`) | Passing to CUDA APIs | An attribute of `cuda.bindings.cydriver` |
77+
| `as_intptr(h)` | `intptr_t` | Python interop, foreign code | |
78+
| `as_py(h)` | Python wrapper object | Returning to Python callers | An attribute of `cuda.bindings.driver`
7979

8080
These overloads exist because `std::shared_ptr` cannot have additional attributes.
8181
Wrapping handles in Python objects would be superfluous overhead for internal use,
@@ -85,13 +85,13 @@ Example usage from Cython:
8585

8686
```cython
8787
# Get raw handle for CUDA API calls
88-
cdef CUstream raw_stream = cu(h_stream) # cuda.bindings.cydriver.CUstream
88+
cdef CUstream raw_stream = as_cu(h_stream) # cuda.bindings.cydriver.CUstream
8989
9090
# Get as integer for other use cases
91-
return hash(intptr(h_stream))
91+
return hash(as_intptr(h_stream))
9292
9393
# Get Python wrapper for returning to user
94-
return py(h_stream) # cuda.bindings.driver.CUstream
94+
return as_py(h_stream) # cuda.bindings.driver.CUstream
9595
```
9696

9797
## Code Structure
@@ -290,10 +290,10 @@ if not h_stream:
290290
HANDLE_RETURN(get_last_error())
291291
292292
# Use in CUDA API
293-
cuStreamSynchronize(cu(h_stream))
293+
cuStreamSynchronize(as_cu(h_stream))
294294
295295
# Return to Python
296-
return py(h_stream)
296+
return as_py(h_stream)
297297
```
298298

299299
## Summary

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,15 +622,15 @@ DevicePtrHandle deviceptr_alloc_from_pool(size_t size, MemoryPoolHandle h_pool,
622622
}
623623
GILReleaseGuard gil;
624624
CUdeviceptr ptr;
625-
if (CUDA_SUCCESS != (err = p_cuMemAllocFromPoolAsync(&ptr, size, *h_pool, cu(h_stream)))) {
625+
if (CUDA_SUCCESS != (err = p_cuMemAllocFromPoolAsync(&ptr, size, *h_pool, as_cu(h_stream)))) {
626626
return {};
627627
}
628628

629629
auto box = std::shared_ptr<DevicePtrBox>(
630630
new DevicePtrBox{ptr, h_stream},
631631
[h_pool](DevicePtrBox* b) {
632632
GILReleaseGuard gil;
633-
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
633+
p_cuMemFreeAsync(b->resource, as_cu(b->h_stream));
634634
delete b;
635635
}
636636
);
@@ -644,15 +644,15 @@ DevicePtrHandle deviceptr_alloc_async(size_t size, StreamHandle h_stream) {
644644
}
645645
GILReleaseGuard gil;
646646
CUdeviceptr ptr;
647-
if (CUDA_SUCCESS != (err = p_cuMemAllocAsync(&ptr, size, cu(h_stream)))) {
647+
if (CUDA_SUCCESS != (err = p_cuMemAllocAsync(&ptr, size, as_cu(h_stream)))) {
648648
return {};
649649
}
650650

651651
auto box = std::shared_ptr<DevicePtrBox>(
652652
new DevicePtrBox{ptr, h_stream},
653653
[](DevicePtrBox* b) {
654654
GILReleaseGuard gil;
655-
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
655+
p_cuMemFreeAsync(b->resource, as_cu(b->h_stream));
656656
delete b;
657657
}
658658
);
@@ -830,7 +830,7 @@ DevicePtrHandle deviceptr_import_ipc(MemoryPoolHandle h_pool, const void* export
830830
ipc_ptr_cache.erase(it);
831831
}
832832
}
833-
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
833+
p_cuMemFreeAsync(b->resource, as_cu(b->h_stream));
834834
delete b;
835835
}
836836
);
@@ -849,7 +849,7 @@ DevicePtrHandle deviceptr_import_ipc(MemoryPoolHandle h_pool, const void* export
849849
new DevicePtrBox{ptr, h_stream},
850850
[h_pool](DevicePtrBox* b) {
851851
GILReleaseGuard gil;
852-
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
852+
p_cuMemFreeAsync(b->resource, as_cu(b->h_stream));
853853
delete b;
854854
}
855855
);

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,50 +185,50 @@ void set_deallocation_stream(const DevicePtrHandle& h, StreamHandle h_stream);
185185
// Overloaded helper functions to extract raw resources from handles
186186
// ============================================================================
187187

188-
// cu() - extract the raw CUDA handle
189-
inline CUcontext cu(const ContextHandle& h) noexcept {
188+
// as_cu() - extract the raw CUDA handle
189+
inline CUcontext as_cu(const ContextHandle& h) noexcept {
190190
return h ? *h : nullptr;
191191
}
192192

193-
inline CUstream cu(const StreamHandle& h) noexcept {
193+
inline CUstream as_cu(const StreamHandle& h) noexcept {
194194
return h ? *h : nullptr;
195195
}
196196

197-
inline CUevent cu(const EventHandle& h) noexcept {
197+
inline CUevent as_cu(const EventHandle& h) noexcept {
198198
return h ? *h : nullptr;
199199
}
200200

201-
inline CUmemoryPool cu(const MemoryPoolHandle& h) noexcept {
201+
inline CUmemoryPool as_cu(const MemoryPoolHandle& h) noexcept {
202202
return h ? *h : nullptr;
203203
}
204204

205-
inline CUdeviceptr cu(const DevicePtrHandle& h) noexcept {
205+
inline CUdeviceptr as_cu(const DevicePtrHandle& h) noexcept {
206206
return h ? *h : 0;
207207
}
208208

209-
// intptr() - extract handle as intptr_t for Python interop
209+
// as_intptr() - extract handle as intptr_t for Python interop
210210
// Using signed intptr_t per C standard convention and issue #1342
211-
inline std::intptr_t intptr(const ContextHandle& h) noexcept {
212-
return reinterpret_cast<std::intptr_t>(cu(h));
211+
inline std::intptr_t as_intptr(const ContextHandle& h) noexcept {
212+
return reinterpret_cast<std::intptr_t>(as_cu(h));
213213
}
214214

215-
inline std::intptr_t intptr(const StreamHandle& h) noexcept {
216-
return reinterpret_cast<std::intptr_t>(cu(h));
215+
inline std::intptr_t as_intptr(const StreamHandle& h) noexcept {
216+
return reinterpret_cast<std::intptr_t>(as_cu(h));
217217
}
218218

219-
inline std::intptr_t intptr(const EventHandle& h) noexcept {
220-
return reinterpret_cast<std::intptr_t>(cu(h));
219+
inline std::intptr_t as_intptr(const EventHandle& h) noexcept {
220+
return reinterpret_cast<std::intptr_t>(as_cu(h));
221221
}
222222

223-
inline std::intptr_t intptr(const MemoryPoolHandle& h) noexcept {
224-
return reinterpret_cast<std::intptr_t>(cu(h));
223+
inline std::intptr_t as_intptr(const MemoryPoolHandle& h) noexcept {
224+
return reinterpret_cast<std::intptr_t>(as_cu(h));
225225
}
226226

227-
inline std::intptr_t intptr(const DevicePtrHandle& h) noexcept {
228-
return static_cast<std::intptr_t>(cu(h));
227+
inline std::intptr_t as_intptr(const DevicePtrHandle& h) noexcept {
228+
return static_cast<std::intptr_t>(as_cu(h));
229229
}
230230

231-
// py() - convert handle to Python driver wrapper object (returns new reference)
231+
// as_py() - convert handle to Python driver wrapper object (returns new reference)
232232
namespace detail {
233233
// n.b. class lookup is not cached to avoid deadlock hazard, see DESIGN.md
234234
inline PyObject* make_py(const char* class_name, std::intptr_t value) {
@@ -243,24 +243,24 @@ inline PyObject* make_py(const char* class_name, std::intptr_t value) {
243243
}
244244
} // namespace detail
245245

246-
inline PyObject* py(const ContextHandle& h) {
247-
return detail::make_py("CUcontext", intptr(h));
246+
inline PyObject* as_py(const ContextHandle& h) {
247+
return detail::make_py("CUcontext", as_intptr(h));
248248
}
249249

250-
inline PyObject* py(const StreamHandle& h) {
251-
return detail::make_py("CUstream", intptr(h));
250+
inline PyObject* as_py(const StreamHandle& h) {
251+
return detail::make_py("CUstream", as_intptr(h));
252252
}
253253

254-
inline PyObject* py(const EventHandle& h) {
255-
return detail::make_py("CUevent", intptr(h));
254+
inline PyObject* as_py(const EventHandle& h) {
255+
return detail::make_py("CUevent", as_intptr(h));
256256
}
257257

258-
inline PyObject* py(const MemoryPoolHandle& h) {
259-
return detail::make_py("CUmemoryPool", intptr(h));
258+
inline PyObject* as_py(const MemoryPoolHandle& h) {
259+
return detail::make_py("CUmemoryPool", as_intptr(h));
260260
}
261261

262-
inline PyObject* py(const DevicePtrHandle& h) {
263-
return detail::make_py("CUdeviceptr", intptr(h));
262+
inline PyObject* as_py(const DevicePtrHandle& h) {
263+
return detail::make_py("CUdeviceptr", as_intptr(h));
264264
}
265265

266266
} // namespace cuda_core

‎cuda_core/cuda/core/_device.pyx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ from cuda.core._resource_handles cimport (
2020
_init_handles_table,
2121
create_context_handle_ref,
2222
get_primary_context,
23-
cu,
23+
as_cu,
2424
)
2525

2626
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
@@ -1254,7 +1254,7 @@ class Device:
12541254
f" id={ctx._device_id}, which is different from the target id={self._device_id}"
12551255
)
12561256
# prev_ctx is the previous context
1257-
curr_ctx = cu(ctx._h_context)
1257+
curr_ctx = as_cu(ctx._h_context)
12581258
prev_ctx = NULL
12591259
with nogil:
12601260
HANDLE_RETURN(cydriver.cuCtxPopCurrent(&prev_ctx))
@@ -1269,7 +1269,7 @@ class Device:
12691269
if h_context.get() == NULL:
12701270
raise ValueError("Cannot set NULL context as current")
12711271
with nogil:
1272-
HANDLE_RETURN(cydriver.cuCtxSetCurrent(cu(h_context)))
1272+
HANDLE_RETURN(cydriver.cuCtxSetCurrent(as_cu(h_context)))
12731273
self._has_inited = True
12741274
self._context = Context._from_handle(Context, h_context, self._device_id) # Store owning context
12751275

‎cuda_core/cuda/core/_event.pyx‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ from cuda.core._resource_handles cimport (
1414
_init_handles_table,
1515
create_event_handle,
1616
create_event_handle_ipc,
17-
intptr,
18-
cu,
19-
py,
17+
as_intptr,
18+
as_cu,
19+
as_py,
2020
)
2121

2222
# Prerequisite before calling handle API functions (see _cpp/DESIGN.md)
@@ -150,7 +150,7 @@ cdef class Event:
150150
# return self - other (in milliseconds)
151151
cdef float timing
152152
with nogil:
153-
err = cydriver.cuEventElapsedTime(&timing, cu((<Event>other)._h_event), cu(self._h_event))
153+
err = cydriver.cuEventElapsedTime(&timing, as_cu((<Event>other)._h_event), as_cu(self._h_event))
154154
if err == 0:
155155
return timing
156156
else:
@@ -176,14 +176,14 @@ cdef class Event:
176176
raise RuntimeError(explanation)
177177

178178
def __hash__(self) -> int:
179-
return hash((type(self), intptr(self._h_context), intptr(self._h_event)))
179+
return hash((type(self), as_intptr(self._h_context), as_intptr(self._h_event)))
180180

181181
def __eq__(self, other) -> bool:
182182
# Note: using isinstance because `Event` can be subclassed.
183183
if not isinstance(other, Event):
184184
return NotImplemented
185185
cdef Event _other = <Event>other
186-
return intptr(self._h_event) == intptr(_other._h_event)
186+
return as_intptr(self._h_event) == as_intptr(_other._h_event)
187187

188188
def get_ipc_descriptor(self) -> IPCEventDescriptor:
189189
"""Export an event allocated for sharing between processes."""
@@ -193,7 +193,7 @@ cdef class Event:
193193
raise RuntimeError("Event is not IPC-enabled")
194194
cdef cydriver.CUipcEventHandle data
195195
with nogil:
196-
HANDLE_RETURN(cydriver.cuIpcGetEventHandle(&data, cu(self._h_event)))
196+
HANDLE_RETURN(cydriver.cuIpcGetEventHandle(&data, as_cu(self._h_event)))
197197
cdef bytes data_b = cpython.PyBytes_FromStringAndSize(<char*>(data.reserved), sizeof(data.reserved))
198198
self._ipc_descriptor = IPCEventDescriptor._init(data_b, self._busy_waited)
199199
return self._ipc_descriptor
@@ -243,13 +243,13 @@ cdef class Event:
243243
244244
"""
245245
with nogil:
246-
HANDLE_RETURN(cydriver.cuEventSynchronize(cu(self._h_event)))
246+
HANDLE_RETURN(cydriver.cuEventSynchronize(as_cu(self._h_event)))
247247

248248
@property
249249
def is_done(self) -> bool:
250250
"""Return True if all captured works have been completed, otherwise False."""
251251
with nogil:
252-
result = cydriver.cuEventQuery(cu(self._h_event))
252+
result = cydriver.cuEventQuery(as_cu(self._h_event))
253253
if result == cydriver.CUresult.CUDA_SUCCESS:
254254
return True
255255
if result == cydriver.CUresult.CUDA_ERROR_NOT_READY:
@@ -265,7 +265,7 @@ cdef class Event:
265265
This handle is a Python object. To get the memory address of the underlying C
266266
handle, call ``int(Event.handle)``.
267267
"""
268-
return py(self._h_event)
268+
return as_py(self._h_event)
269269

270270
@property
271271
def device(self) -> Device:

‎cuda_core/cuda/core/_launcher.pyx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from cuda.bindings cimport cydriver
88

99
from cuda.core._launch_config cimport LaunchConfig
1010
from cuda.core._kernel_arg_handler cimport ParamHolder
11-
from cuda.core._resource_handles cimport cu
11+
from cuda.core._resource_handles cimport as_cu
1212
from cuda.core._stream cimport Stream_accept, Stream
1313
from cuda.core._utils.cuda_utils cimport (
1414
check_or_create_options,
@@ -88,7 +88,7 @@ def launch(stream: Stream | GraphBuilder | IsStreamT, config: LaunchConfig, kern
8888
# rich.
8989
if _use_ex:
9090
drv_cfg = conf._to_native_launch_config()
91-
drv_cfg.hStream = cu(s._h_stream)
91+
drv_cfg.hStream = as_cu(s._h_stream)
9292
if conf.cooperative_launch:
9393
_check_cooperative_launch(kernel, conf, s)
9494
with nogil:
@@ -100,7 +100,7 @@ def launch(stream: Stream | GraphBuilder | IsStreamT, config: LaunchConfig, kern
100100
func_handle,
101101
conf.grid[0], conf.grid[1], conf.grid[2],
102102
conf.block[0], conf.block[1], conf.block[2],
103-
conf.shmem_size, cu(s._h_stream), args_ptr, NULL
103+
conf.shmem_size, as_cu(s._h_stream), args_ptr, NULL
104104
)
105105
)
106106

0 commit comments

Comments
 (0)