Skip to content

Commit 4e54ca6

Browse files
committed
refactor: rename native() to cu() for extracting raw CUDA handles
Shorter name that clearly indicates the return type is a CU* driver handle. Updates all definitions, declarations, cimports, and call sites across 15 files.
1 parent d5325a6 commit 4e54ca6

15 files changed

Lines changed: 77 additions & 77 deletions

cuda_core/cuda/core/_cpp/DESIGN.md

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

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

@@ -85,7 +85,7 @@ Example usage from Cython:
8585

8686
```cython
8787
# Get raw handle for CUDA API calls
88-
cdef CUstream raw_stream = native(h_stream) # cuda.bindings.cydriver.CUstream
88+
cdef CUstream raw_stream = cu(h_stream) # cuda.bindings.cydriver.CUstream
8989
9090
# Get as integer for other use cases
9191
return hash(intptr(h_stream))
@@ -250,7 +250,7 @@ Related functions:
250250
from cuda.core._resource_handles cimport (
251251
StreamHandle,
252252
create_stream_handle,
253-
native,
253+
cu,
254254
intptr,
255255
get_last_error,
256256
_init_handles_table,
@@ -264,7 +264,7 @@ if not h_stream:
264264
HANDLE_RETURN(get_last_error())
265265
266266
# Use in CUDA API
267-
cuStreamSynchronize(native(h_stream))
267+
cuStreamSynchronize(cu(h_stream))
268268
269269
# Return to Python
270270
return py(h_stream)
@@ -279,7 +279,7 @@ The resource handle design:
279279
3. **Uses capsules** to solve two distinct problems:
280280
- Sharing C++ code across Cython modules without duplicate statics.
281281
- Resolving CUDA driver symbols dynamically through cuda-bindings.
282-
4. **Provides overloaded accessors** (`native`, `intptr`, `py`) since handles cannot
282+
4. **Provides overloaded accessors** (`cu`, `intptr`, `py`) since handles cannot
283283
have attributes without unnecessary Python object wrappers.
284284

285285
This architecture ensures CUDA resources are managed correctly regardless of Python

cuda_core/cuda/core/_cpp/resource_handles.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,15 @@ DevicePtrHandle deviceptr_alloc_from_pool(size_t size, MemoryPoolHandle h_pool,
587587
}
588588
GILReleaseGuard gil;
589589
CUdeviceptr ptr;
590-
if (CUDA_SUCCESS != (err = p_cuMemAllocFromPoolAsync(&ptr, size, *h_pool, native(h_stream)))) {
590+
if (CUDA_SUCCESS != (err = p_cuMemAllocFromPoolAsync(&ptr, size, *h_pool, cu(h_stream)))) {
591591
return {};
592592
}
593593

594594
auto box = std::shared_ptr<DevicePtrBox>(
595595
new DevicePtrBox{ptr, h_stream},
596596
[h_pool](DevicePtrBox* b) {
597597
GILReleaseGuard gil;
598-
p_cuMemFreeAsync(b->resource, native(b->h_stream));
598+
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
599599
delete b;
600600
}
601601
);
@@ -609,15 +609,15 @@ DevicePtrHandle deviceptr_alloc_async(size_t size, StreamHandle h_stream) {
609609
}
610610
GILReleaseGuard gil;
611611
CUdeviceptr ptr;
612-
if (CUDA_SUCCESS != (err = p_cuMemAllocAsync(&ptr, size, native(h_stream)))) {
612+
if (CUDA_SUCCESS != (err = p_cuMemAllocAsync(&ptr, size, cu(h_stream)))) {
613613
return {};
614614
}
615615

616616
auto box = std::shared_ptr<DevicePtrBox>(
617617
new DevicePtrBox{ptr, h_stream},
618618
[](DevicePtrBox* b) {
619619
GILReleaseGuard gil;
620-
p_cuMemFreeAsync(b->resource, native(b->h_stream));
620+
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
621621
delete b;
622622
}
623623
);
@@ -791,7 +791,7 @@ DevicePtrHandle deviceptr_import_ipc(MemoryPoolHandle h_pool, const void* export
791791
ipc_ptr_cache.erase(it);
792792
}
793793
}
794-
p_cuMemFreeAsync(b->resource, native(b->h_stream));
794+
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
795795
delete b;
796796
}
797797
);
@@ -810,7 +810,7 @@ DevicePtrHandle deviceptr_import_ipc(MemoryPoolHandle h_pool, const void* export
810810
new DevicePtrBox{ptr, h_stream},
811811
[h_pool](DevicePtrBox* b) {
812812
GILReleaseGuard gil;
813-
p_cuMemFreeAsync(b->resource, native(b->h_stream));
813+
p_cuMemFreeAsync(b->resource, cu(b->h_stream));
814814
delete b;
815815
}
816816
);

cuda_core/cuda/core/_cpp/resource_handles.hpp

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

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

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

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

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

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

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

215215
inline std::intptr_t intptr(const StreamHandle& h) noexcept {
216-
return reinterpret_cast<std::intptr_t>(h ? *h : nullptr);
216+
return reinterpret_cast<std::intptr_t>(cu(h));
217217
}
218218

219219
inline std::intptr_t intptr(const EventHandle& h) noexcept {
220-
return reinterpret_cast<std::intptr_t>(h ? *h : nullptr);
220+
return reinterpret_cast<std::intptr_t>(cu(h));
221221
}
222222

223223
inline std::intptr_t intptr(const MemoryPoolHandle& h) noexcept {
224-
return reinterpret_cast<std::intptr_t>(h ? *h : nullptr);
224+
return reinterpret_cast<std::intptr_t>(cu(h));
225225
}
226226

227227
inline std::intptr_t intptr(const DevicePtrHandle& h) noexcept {
228-
return h ? static_cast<std::intptr_t>(*h) : 0;
228+
return static_cast<std::intptr_t>(cu(h));
229229
}
230230

231231
// py() - convert handle to Python driver wrapper object (returns new reference)

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-
native,
23+
cu,
2424
)
2525

2626
_init_handles_table()
@@ -1242,7 +1242,7 @@ class Device:
12421242
f" id={ctx._device_id}, which is different from the target id={self._device_id}"
12431243
)
12441244
# prev_ctx is the previous context
1245-
curr_ctx = native(ctx._h_context)
1245+
curr_ctx = cu(ctx._h_context)
12461246
prev_ctx = NULL
12471247
with nogil:
12481248
HANDLE_RETURN(cydriver.cuCtxPopCurrent(&prev_ctx))
@@ -1257,7 +1257,7 @@ class Device:
12571257
if h_context.get() == NULL:
12581258
raise ValueError("Cannot set NULL context as current")
12591259
with nogil:
1260-
HANDLE_RETURN(cydriver.cuCtxSetCurrent(native(h_context)))
1260+
HANDLE_RETURN(cydriver.cuCtxSetCurrent(cu(h_context)))
12611261
self._has_inited = True
12621262
self._context = Context._from_handle(Context, h_context, self._device_id) # Store owning context
12631263

cuda_core/cuda/core/_event.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from cuda.core._resource_handles cimport (
1515
create_event_handle,
1616
create_event_handle_ipc,
1717
intptr,
18-
native,
18+
cu,
1919
py,
2020
)
2121

@@ -148,7 +148,7 @@ cdef class Event:
148148
# return self - other (in milliseconds)
149149
cdef float timing
150150
with nogil:
151-
err = cydriver.cuEventElapsedTime(&timing, native((<Event>other)._h_event), native(self._h_event))
151+
err = cydriver.cuEventElapsedTime(&timing, cu((<Event>other)._h_event), cu(self._h_event))
152152
if err == 0:
153153
return timing
154154
else:
@@ -191,7 +191,7 @@ cdef class Event:
191191
raise RuntimeError("Event is not IPC-enabled")
192192
cdef cydriver.CUipcEventHandle data
193193
with nogil:
194-
HANDLE_RETURN(cydriver.cuIpcGetEventHandle(&data, native(self._h_event)))
194+
HANDLE_RETURN(cydriver.cuIpcGetEventHandle(&data, cu(self._h_event)))
195195
cdef bytes data_b = cpython.PyBytes_FromStringAndSize(<char*>(data.reserved), sizeof(data.reserved))
196196
self._ipc_descriptor = IPCEventDescriptor._init(data_b, self._busy_waited)
197197
return self._ipc_descriptor
@@ -241,13 +241,13 @@ cdef class Event:
241241
242242
"""
243243
with nogil:
244-
HANDLE_RETURN(cydriver.cuEventSynchronize(native(self._h_event)))
244+
HANDLE_RETURN(cydriver.cuEventSynchronize(cu(self._h_event)))
245245

246246
@property
247247
def is_done(self) -> bool:
248248
"""Return True if all captured works have been completed, otherwise False."""
249249
with nogil:
250-
result = cydriver.cuEventQuery(native(self._h_event))
250+
result = cydriver.cuEventQuery(cu(self._h_event))
251251
if result == cydriver.CUresult.CUDA_SUCCESS:
252252
return True
253253
if result == cydriver.CUresult.CUDA_ERROR_NOT_READY:

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 native
11+
from cuda.core._resource_handles cimport 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 = native(s._h_stream)
91+
drv_cfg.hStream = 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, native(s._h_stream), args_ptr, NULL
103+
conf.shmem_size, cu(s._h_stream), args_ptr, NULL
104104
)
105105
)
106106

cuda_core/cuda/core/_memory/_buffer.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from cuda.core._resource_handles cimport (
1919
_init_handles_table,
2020
deviceptr_create_with_owner,
2121
intptr,
22-
native,
22+
cu,
2323
set_deallocation_stream,
2424
)
2525

@@ -187,7 +187,7 @@ cdef class Buffer:
187187
)
188188
with nogil:
189189
HANDLE_RETURN(cydriver.cuMemcpyAsync(
190-
native(dst._h_ptr), native(self._h_ptr), src_size, native(s._h_stream)))
190+
cu(dst._h_ptr), cu(self._h_ptr), src_size, cu(s._h_stream)))
191191
return dst
192192

193193
def copy_from(self, src: Buffer, *, stream: Stream | GraphBuilder):
@@ -212,7 +212,7 @@ cdef class Buffer:
212212
)
213213
with nogil:
214214
HANDLE_RETURN(cydriver.cuMemcpyAsync(
215-
native(self._h_ptr), native(src._h_ptr), dst_size, native(s._h_stream)))
215+
cu(self._h_ptr), cu(src._h_ptr), dst_size, cu(s._h_stream)))
216216

217217
def fill(self, value: int | BufferProtocol, *, stream: Stream | GraphBuilder):
218218
"""Fill this buffer with a repeating byte pattern.
@@ -369,7 +369,7 @@ cdef class Buffer:
369369
cdef inline void _init_mem_attrs(Buffer self):
370370
"""Initialize memory attributes by querying the pointer."""
371371
if not self._mem_attrs_inited:
372-
_query_memory_attrs(self._mem_attrs, native(self._h_ptr))
372+
_query_memory_attrs(self._mem_attrs, cu(self._h_ptr))
373373
self._mem_attrs_inited = True
374374

375375

@@ -524,8 +524,8 @@ cdef inline void Buffer_close(Buffer self, object stream):
524524

525525

526526
cdef inline int Buffer_fill_uint8(Buffer self, uint8_t value, StreamHandle h_stream) except? -1:
527-
cdef cydriver.CUdeviceptr ptr = native(self._h_ptr)
528-
cdef cydriver.CUstream s = native(h_stream)
527+
cdef cydriver.CUdeviceptr ptr = cu(self._h_ptr)
528+
cdef cydriver.CUstream s = cu(h_stream)
529529
with nogil:
530530
HANDLE_RETURN(cydriver.cuMemsetD8Async(ptr, value, self._size, s))
531531
return 0
@@ -535,8 +535,8 @@ cdef inline int Buffer_fill_from_ptr(
535535
Buffer self, const char* ptr, size_t width, StreamHandle h_stream
536536
) except? -1:
537537
cdef size_t buffer_size = self._size
538-
cdef cydriver.CUdeviceptr dst = native(self._h_ptr)
539-
cdef cydriver.CUstream s = native(h_stream)
538+
cdef cydriver.CUdeviceptr dst = cu(self._h_ptr)
539+
cdef cydriver.CUstream s = cu(h_stream)
540540

541541
if width == 1:
542542
with nogil:

cuda_core/cuda/core/_memory/_device_memory_resource.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import platform # no-cython-lint
2020
import uuid
2121

2222
from cuda.core._utils.cuda_utils import check_multiprocessing_start_method
23-
from cuda.core._resource_handles cimport native
23+
from cuda.core._resource_handles cimport cu
2424

2525
if TYPE_CHECKING:
2626
from .._device import Device
@@ -255,7 +255,7 @@ cpdef DMR_mempool_get_access(DeviceMemoryResource dmr, int device_id):
255255
location.id = dev_id
256256

257257
with nogil:
258-
HANDLE_RETURN(cydriver.cuMemPoolGetAccess(&flags, native(dmr._h_pool), &location))
258+
HANDLE_RETURN(cydriver.cuMemPoolGetAccess(&flags, cu(dmr._h_pool), &location))
259259

260260
if flags == cydriver.CUmemAccess_flags.CU_MEM_ACCESS_FLAGS_PROT_READWRITE:
261261
return "rw"

cuda_core/cuda/core/_memory/_graph_memory_resource.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from cuda.core._resource_handles cimport (
1212
DevicePtrHandle,
1313
_init_handles_table,
1414
deviceptr_alloc_async,
15-
native,
15+
cu,
1616
)
1717

1818
_init_handles_table()
@@ -194,7 +194,7 @@ cdef inline int check_capturing(cydriver.CUstream s) except?-1 nogil:
194194

195195

196196
cdef inline Buffer GMR_allocate(cyGraphMemoryResource self, size_t size, Stream stream):
197-
cdef cydriver.CUstream s = native(stream._h_stream)
197+
cdef cydriver.CUstream s = cu(stream._h_stream)
198198
cdef DevicePtrHandle h_ptr
199199
with nogil:
200200
check_capturing(s)
@@ -205,7 +205,7 @@ cdef inline Buffer GMR_allocate(cyGraphMemoryResource self, size_t size, Stream
205205

206206

207207
cdef inline void GMR_deallocate(intptr_t ptr, size_t size, Stream stream) noexcept:
208-
cdef cydriver.CUstream s = native(stream._h_stream)
208+
cdef cydriver.CUstream s = cu(stream._h_stream)
209209
cdef cydriver.CUdeviceptr devptr = <cydriver.CUdeviceptr>ptr
210210
with nogil:
211211
HANDLE_RETURN(cydriver.cuMemFreeAsync(devptr, s))

cuda_core/cuda/core/_memory/_ipc.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from cuda.core._resource_handles cimport (
1414
create_mempool_handle_ipc,
1515
deviceptr_import_ipc,
1616
get_last_error,
17-
native,
17+
cu,
1818
)
1919

2020
_init_handles_table()
@@ -164,7 +164,7 @@ cdef IPCBufferDescriptor Buffer_get_ipc_descriptor(Buffer self):
164164
cdef cydriver.CUmemPoolPtrExportData data
165165
with nogil:
166166
HANDLE_RETURN(
167-
cydriver.cuMemPoolExportPointer(&data, native(self._h_ptr))
167+
cydriver.cuMemPoolExportPointer(&data, cu(self._h_ptr))
168168
)
169169
cdef bytes data_b = cpython.PyBytes_FromStringAndSize(
170170
<char*>(data.reserved), sizeof(data.reserved)
@@ -250,7 +250,7 @@ cdef IPCAllocationHandle MP_export_mempool(_MemPool self):
250250
cdef int fd
251251
with nogil:
252252
HANDLE_RETURN(cydriver.cuMemPoolExportToShareableHandle(
253-
&fd, native(self._h_pool), IPC_HANDLE_TYPE, 0)
253+
&fd, cu(self._h_pool), IPC_HANDLE_TYPE, 0)
254254
)
255255
try:
256256
return IPCAllocationHandle._init(fd, uuid.uuid4())

0 commit comments

Comments
 (0)