Coverage for cuda/core/_rt.pyx: 100.00%
2 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
5# cython: show_performance_hints=False
6#
7# Performance hints are off for this module on purpose. The handle factories
8# below are declared ``except+ nogil``, and Cython reports each one as an
9# exception check that "will always require the GIL". For ``except+`` the
10# GIL is taken only inside the C++ catch handler, so the success path pays
11# nothing and the hint is noise here. Functions that only return a status
12# code are declared ``noexcept nogil`` instead, which is what the hint is
13# really about: without it, every call from a nogil block acquires the GIL
14# to run PyErr_Occurred().
16# This module compiles the C++ under _cpp/rt/ into one shared library.
17# Consumer modules cimport the functions declared in _rt.pxd. Since there is
18# only one copy of the C++ code (in this .so), all static and thread-local
19# state is shared correctly across all consumer modules.
20#
21# "rt" is short for runtime: this is cuda.core's runtime support layer
22# (resource handles, the driver function-pointer table, error reporting and
23# deferred cleanup). It is unrelated to the CUDA Runtime API (cudart).
24#
25# The cdef extern from declarations below satisfy the .pxd declarations directly,
26# without needing separate wrapper functions.
28from cpython.object cimport PyObject
29from libc.stddef cimport size_t
30from libcpp cimport bool as cpp_bool
32from cuda.bindings cimport cydriver
33from cuda.bindings cimport cynvrtc
34from cuda.bindings cimport cynvvm
35from cuda.bindings cimport cynvjitlink
38# =============================================================================
39# C++ function declarations (non-inline, implemented under _cpp/rt/)
40#
41# These declarations satisfy the cdef function declarations in _rt.pxd.
42# Consumer modules cimport these functions and calls go through this .so.
43# =============================================================================
45cdef extern from "_cpp/rt/rt.hpp" namespace "cuda_core::rt":
46 # Thread-local error handling
47 cydriver.CUresult get_last_error "cuda_core::rt::get_last_error" () noexcept nogil
48 cydriver.CUresult peek_last_error "cuda_core::rt::peek_last_error" () noexcept nogil
49 void clear_last_error "cuda_core::rt::clear_last_error" () noexcept nogil
51 # Non-propagating error reporting
52 void register_warning_category "cuda_core::rt::register_warning_category" (
53 PyObject* category) noexcept
54 void report_cuda_error "cuda_core::rt::report_cuda_error" (
55 const char* operation, cydriver.CUresult status, const char* detail) noexcept nogil
56 void report_message "cuda_core::rt::report_message" (const char* message) noexcept nogil
57 void report_status_code "cuda_core::rt::report_status_code" (
58 const char* operation, long code) noexcept nogil
59 void attach_rollback_failure "cuda_core::rt::attach_rollback_failure" (
60 const char* operation, cydriver.CUresult status, const char* detail) noexcept nogil
61 const char* take_last_error_detail "cuda_core::rt::take_last_error_detail" (
62 cydriver.CUresult status) noexcept nogil
63 void clear_last_error_detail "cuda_core::rt::clear_last_error_detail" () noexcept nogil
65 # Context handles
66 ContextHandle create_context_handle_ref "cuda_core::rt::create_context_handle_ref" (
67 cydriver.CUcontext ctx) except+ nogil
68 ContextHandle create_context_handle_from_green_ctx "cuda_core::rt::create_context_handle_from_green_ctx" (
69 const GreenCtxHandle& h_green_ctx) except+ nogil
70 GreenCtxHandle get_context_green_ctx "cuda_core::rt::get_context_green_ctx" (
71 const ContextHandle& h) noexcept nogil
72 GreenCtxHandle create_green_ctx_handle "cuda_core::rt::create_green_ctx_handle" (
73 cydriver.CUdevResource* resources, unsigned int nbResources,
74 cydriver.CUdevice dev, unsigned int flags) except+ nogil
75 GreenCtxHandle create_green_ctx_handle_ref "cuda_core::rt::create_green_ctx_handle_ref" (
76 cydriver.CUgreenCtx ctx) except+ nogil
77 ContextHandle get_primary_context "cuda_core::rt::get_primary_context" (
78 int device_id) except+ nogil
79 ContextHandle get_current_context "cuda_core::rt::get_current_context" () except+ nogil
80 cydriver.CUresult context_synchronize "cuda_core::rt::context_synchronize" (
81 const ContextHandle& h_context) noexcept nogil
82 cydriver.CUresult context_get_stream_priority_range "cuda_core::rt::context_get_stream_priority_range" (
83 const ContextHandle& h_context,
84 int* least_priority,
85 int* greatest_priority) noexcept nogil
86 cydriver.CUresult context_get_device "cuda_core::rt::context_get_device" (
87 const ContextHandle& h_context, cydriver.CUdevice* device) noexcept nogil
88 cydriver.CUresult graph_node_set_params "cuda_core::rt::graph_node_set_params" (
89 cydriver.CUgraphNode node, cydriver.CUgraphNodeParams* params,
90 const ContextHandle& h_context, cydriver.CUresult* restore_status) noexcept nogil
92 # Stream handles
93 StreamHandle create_stream_handle "cuda_core::rt::create_stream_handle" (
94 const ContextHandle& h_ctx, unsigned int flags, int priority) except+ nogil
95 StreamHandle create_stream_handle_ref "cuda_core::rt::create_stream_handle_ref" (
96 cydriver.CUstream stream) except+ nogil
97 StreamHandle create_stream_handle_with_owner "cuda_core::rt::create_stream_handle_with_owner" (
98 cydriver.CUstream stream, object owner) except+ nogil
99 void initialize_deferred_cleanup "cuda_core::rt::initialize_deferred_cleanup" () except+
100 void retry_deferred_cleanup "cuda_core::rt::retry_deferred_cleanup" () noexcept
101 cydriver.CUresult launch_host_function "cuda_core::rt::launch_host_function" (
102 cydriver.CUstream stream, cydriver.CUhostFn fn, void* user_data,
103 OpaqueHandle fn_owner, OpaqueHandle data_owner) noexcept nogil
104 ContextHandle get_stream_context "cuda_core::rt::get_stream_context" (
105 const StreamHandle& h) noexcept nogil
106 StreamHandle get_legacy_stream "cuda_core::rt::get_legacy_stream" () except+ nogil
107 StreamHandle get_per_thread_stream "cuda_core::rt::get_per_thread_stream" () except+ nogil
108 StreamHandle create_context_bound_legacy_stream "cuda_core::rt::create_context_bound_legacy_stream" (
109 const ContextHandle& h_context) except+ nogil
111 # Event handles (note: _create_event_handle* are internal due to C++ overloading)
112 EventHandle create_event_handle "cuda_core::rt::create_event_handle" (
113 const ContextHandle& h_ctx, unsigned int flags,
114 bint timing_enabled, bint is_blocking_sync,
115 bint ipc_enabled, int device_id) except+ nogil
116 EventHandle create_event_handle_for_stream "cuda_core::rt::create_event_handle_for_stream" (
117 cydriver.CUstream stream, unsigned int flags) except+ nogil
118 EventHandle create_event_handle_ref "cuda_core::rt::create_event_handle_ref" (
119 cydriver.CUevent event) except+ nogil
120 EventHandle create_event_handle_ipc "cuda_core::rt::create_event_handle_ipc" (
121 const cydriver.CUipcEventHandle& ipc_handle, bint is_blocking_sync) except+ nogil
123 # Event metadata getters
124 cpp_bool get_event_timing_enabled "cuda_core::rt::get_event_timing_enabled" (
125 const EventHandle& h) noexcept nogil
126 cpp_bool get_event_is_blocking_sync "cuda_core::rt::get_event_is_blocking_sync" (
127 const EventHandle& h) noexcept nogil
128 cpp_bool get_event_ipc_enabled "cuda_core::rt::get_event_ipc_enabled" (
129 const EventHandle& h) noexcept nogil
130 int get_event_device_id "cuda_core::rt::get_event_device_id" (
131 const EventHandle& h) noexcept nogil
132 ContextHandle get_event_context "cuda_core::rt::get_event_context" (
133 const EventHandle& h) noexcept nogil
135 # Memory pool handles
136 MemoryPoolHandle create_mempool_handle "cuda_core::rt::create_mempool_handle" (
137 const cydriver.CUmemPoolProps& props) except+ nogil
138 MemoryPoolHandle create_mempool_handle_ref "cuda_core::rt::create_mempool_handle_ref" (
139 cydriver.CUmemoryPool pool) except+ nogil
140 MemoryPoolHandle get_device_mempool "cuda_core::rt::get_device_mempool" (
141 int device_id) except+ nogil
142 MemoryPoolHandle create_mempool_handle_ipc "cuda_core::rt::create_mempool_handle_ipc" (
143 int fd, cydriver.CUmemAllocationHandleType handle_type) except+ nogil
145 # Device pointer handles
146 DevicePtrHandle deviceptr_alloc_from_pool "cuda_core::rt::deviceptr_alloc_from_pool" (
147 size_t size, const MemoryPoolHandle& h_pool, const StreamHandle& h_stream) except+ nogil
148 DevicePtrHandle deviceptr_alloc_async "cuda_core::rt::deviceptr_alloc_async" (
149 size_t size, const StreamHandle& h_stream) except+ nogil
150 cydriver.CUresult deviceptr_alloc_raw "cuda_core::rt::deviceptr_alloc_raw" (
151 cydriver.CUdeviceptr* ptr, size_t size, const ContextHandle& h_context) noexcept nogil
152 DevicePtrHandle deviceptr_alloc_host "cuda_core::rt::deviceptr_alloc_host" (size_t size) except+ nogil
153 DevicePtrHandle deviceptr_create_ref "cuda_core::rt::deviceptr_create_ref" (
154 cydriver.CUdeviceptr ptr) except+ nogil
155 DevicePtrHandle deviceptr_create_with_owner "cuda_core::rt::deviceptr_create_with_owner" (
156 cydriver.CUdeviceptr ptr, object owner) except+ nogil
157 DevicePtrHandle deviceptr_create_mapped_graphics "cuda_core::rt::deviceptr_create_mapped_graphics" (
158 cydriver.CUdeviceptr ptr,
159 const GraphicsResourceHandle& h_resource,
160 const StreamHandle& h_stream) except+ nogil
162 # MR deallocation callback
163 void register_mr_dealloc_callback "cuda_core::rt::register_mr_dealloc_callback" (
164 MRDeallocCallback cb) noexcept
165 DevicePtrHandle deviceptr_create_with_mr "cuda_core::rt::deviceptr_create_with_mr" (
166 cydriver.CUdeviceptr ptr, size_t size, object mr) except+ nogil
168 DevicePtrHandle deviceptr_import_ipc "cuda_core::rt::deviceptr_import_ipc" (
169 const MemoryPoolHandle& h_pool, const void* export_data, const StreamHandle& h_stream) except+ nogil
170 StreamHandle deallocation_stream "cuda_core::rt::deallocation_stream" (
171 const DevicePtrHandle& h) noexcept nogil
172 cydriver.CUresult set_deallocation_stream "cuda_core::rt::set_deallocation_stream" (
173 const DevicePtrHandle& h, const StreamHandle& h_stream) except+ nogil
175 # Virtual memory management (VMM_DESIGN.md)
176 MemAllocationHandle create_mem_allocation_handle "cuda_core::rt::create_mem_allocation_handle" (
177 size_t size, const cydriver.CUmemAllocationProp& prop,
178 const cydriver.CUmemAccessDesc* descs, size_t count) except+ nogil
179 size_t mem_allocation_size "cuda_core::rt::mem_allocation_size" (
180 const MemAllocationHandle& h) noexcept nogil
181 cpp_bool mem_allocation_is_imported "cuda_core::rt::mem_allocation_is_imported" (
182 const MemAllocationHandle& h) noexcept nogil
183 MemAllocationHandle import_mem_allocation_handle "cuda_core::rt::import_mem_allocation_handle" (
184 void* os_handle, cydriver.CUmemAllocationHandleType handle_type, size_t size,
185 const cydriver.CUmemAccessDesc* descs, size_t count) except+ nogil
186 VaReservationHandle create_va_reservation_handle "cuda_core::rt::create_va_reservation_handle" (
187 size_t size, size_t alignment, cydriver.CUdeviceptr hint) except+ nogil
188 size_t va_reservation_size "cuda_core::rt::va_reservation_size" (
189 const VaReservationHandle& h) noexcept nogil
190 long long live_owned_mempool_count "cuda_core::rt::live_owned_mempool_count" () noexcept nogil
191 long long live_va_reservation_count "cuda_core::rt::live_va_reservation_count" () noexcept nogil
192 VaMappingHandle create_va_mapping_handle "cuda_core::rt::create_va_mapping_handle" (
193 cydriver.CUdeviceptr ptr, const MemAllocationHandle& h_alloc,
194 const VaReservationHandle& h_res) except+ nogil
195 size_t va_mapping_size "cuda_core::rt::va_mapping_size" (const VaMappingHandle& h) noexcept nogil
196 MemAllocationHandle va_mapping_allocation "cuda_core::rt::va_mapping_allocation" (
197 const VaMappingHandle& h) noexcept nogil
198 VmmRangeHandle create_vmm_range "cuda_core::rt::create_vmm_range" (
199 const vector[VaMappingHandle]& mappings) except+ nogil
200 VmmRangeHandle vmm_range "cuda_core::rt::vmm_range" (const DevicePtrHandle& h) noexcept nogil
201 vector[VaMappingHandle] vmm_range_mappings "cuda_core::rt::vmm_range_mappings" (
202 const VmmRangeHandle& range) except+ nogil
203 size_t vmm_range_total "cuda_core::rt::vmm_range_total" (const VmmRangeHandle& range) noexcept nogil
204 DevicePtrHandle deviceptr_create_vmm "cuda_core::rt::deviceptr_create_vmm" (
205 cydriver.CUdeviceptr base, const VmmRangeHandle& range) except+ nogil
207 # Library handles
208 LibraryHandle create_library_handle_from_file "cuda_core::rt::create_library_handle_from_file" (
209 const char* path) except+ nogil
210 LibraryHandle create_library_handle_from_data "cuda_core::rt::create_library_handle_from_data" (
211 const void* data) except+ nogil
212 LibraryHandle create_library_handle_ref "cuda_core::rt::create_library_handle_ref" (
213 cydriver.CUlibrary library) except+ nogil
215 # Kernel handles
216 KernelHandle create_kernel_handle "cuda_core::rt::create_kernel_handle" (
217 const LibraryHandle& h_library, const char* name) except+ nogil
218 KernelHandle create_kernel_handle_ref "cuda_core::rt::create_kernel_handle_ref" (
219 cydriver.CUkernel kernel) except+ nogil
220 LibraryHandle get_kernel_library "cuda_core::rt::get_kernel_library" (
221 const KernelHandle& h) noexcept nogil
223 # Graph handles
224 GraphHandle create_graph_handle "cuda_core::rt::create_graph_handle" (
225 cydriver.CUgraph graph) except+ nogil
226 GraphHandle create_child_graph_handle "cuda_core::rt::create_child_graph_handle" (
227 cydriver.CUgraph child_graph, const GraphHandle& h_parent,
228 cydriver.CUgraphNode owner_node) except+ nogil
230 # Graph node attachments
231 OpaqueHandle make_opaque_py "cuda_core::rt::make_opaque_py" (object obj) except+
232 OpaqueHandle make_opaque_malloc "cuda_core::rt::make_opaque_malloc" (void* buf) except+
233 cydriver.CUresult graph_get_attachment "cuda_core::rt::graph_get_attachment" (
234 const GraphHandle& h_graph, cydriver.CUgraphNode node,
235 OpaqueHandle* owner0, OpaqueHandle* owner1) except+
236 cydriver.CUresult graph_prepare_attachment "cuda_core::rt::graph_prepare_attachment" (
237 const GraphHandle& h_graph, OpaqueHandle owner0, OpaqueHandle owner1,
238 PreparedAttachment* out_prepared) except+
239 cydriver.CUresult graph_commit_attachment "cuda_core::rt::graph_commit_attachment" (
240 PreparedAttachment& prepared, cydriver.CUgraphNode node) except+
241 cydriver.CUresult graph_clone_attachments "cuda_core::rt::graph_clone_attachments" (
242 const GraphHandle& h_clone, const GraphHandle& h_source) except+
243 cydriver.CUresult graph_prepare_child_graph_update "cuda_core::rt::graph_prepare_child_graph_update" (
244 const GraphHandle& h_parent, const GraphHandle& h_old_child,
245 cydriver.CUgraphNode owner_node, const GraphHandle& h_source,
246 PreparedChildGraphUpdate* out_prepared) except+
247 cydriver.CUresult graph_commit_child_graph_update "cuda_core::rt::graph_commit_child_graph_update" (
248 PreparedChildGraphUpdate& prepared, GraphHandle* out_child) except+
249 void invalidate_child_graph_state "cuda_core::rt::invalidate_child_graph_state" (
250 const GraphHandle& h_parent, cydriver.CUgraphNode owner_node) noexcept
251 void invalidate_root_graph_state "cuda_core::rt::invalidate_root_graph_state" (
252 const GraphHandle& h_root) noexcept
254 # Graph exec handles
255 GraphExecHandle create_graph_exec_handle "cuda_core::rt::create_graph_exec_handle" (
256 const GraphHandle& h_source,
257 cydriver.CUDA_GRAPH_INSTANTIATE_PARAMS* params) except+
258 cydriver.CUresult graph_exec_update "cuda_core::rt::graph_exec_update" (
259 const GraphExecHandle& h_exec,
260 const GraphHandle& h_source,
261 cydriver.CUgraphExecUpdateResultInfo* result_info) except+
262 cydriver.CUresult graph_prepare_exec_attachment "cuda_core::rt::graph_prepare_exec_attachment" (
263 const GraphExecHandle& h_exec,
264 OpaqueHandle owner0,
265 OpaqueHandle owner1,
266 PreparedExecAttachment* out_prepared) except+
267 void graph_commit_exec_attachment "cuda_core::rt::graph_commit_exec_attachment" (
268 PreparedExecAttachment& prepared) noexcept
270 # Graph node handles
271 GraphNodeHandle create_graph_node_handle "cuda_core::rt::create_graph_node_handle" (
272 cydriver.CUgraphNode node, const GraphHandle& h_graph) except+ nogil
273 GraphHandle graph_node_get_graph "cuda_core::rt::graph_node_get_graph" (
274 const GraphNodeHandle& h) noexcept nogil
275 void invalidate_graph_node "cuda_core::rt::invalidate_graph_node" (
276 const GraphNodeHandle& h) noexcept nogil
278 # Graphics resource handles
279 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::rt::create_graphics_resource_handle" (
280 cydriver.CUgraphicsResource resource) except+ nogil
282 # NVRTC Program handles
283 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::rt::create_nvrtc_program_handle" (
284 cynvrtc.nvrtcProgram prog) except+ nogil
285 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::rt::create_nvrtc_program_handle_ref" (
286 cynvrtc.nvrtcProgram prog) except+ nogil
288 # NVVM Program handles
289 NvvmProgramHandle create_nvvm_program_handle "cuda_core::rt::create_nvvm_program_handle" (
290 cynvvm.nvvmProgram prog) except+ nogil
291 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::rt::create_nvvm_program_handle_ref" (
292 cynvvm.nvvmProgram prog) except+ nogil
294 # nvJitLink handles
295 NvJitLinkHandle create_nvjitlink_handle "cuda_core::rt::create_nvjitlink_handle" (
296 cynvjitlink.nvJitLinkHandle handle) except+ nogil
297 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::rt::create_nvjitlink_handle_ref" (
298 cynvjitlink.nvJitLinkHandle handle) except+ nogil
300 # cuLink handles
301 CuLinkHandle create_culink_handle "cuda_core::rt::create_culink_handle" (
302 cydriver.CUlinkState state) except+ nogil
303 CuLinkHandle create_culink_handle_ref "cuda_core::rt::create_culink_handle_ref" (
304 cydriver.CUlinkState state) except+ nogil
306 # File descriptor handles
307 FileDescriptorHandle create_fd_handle "cuda_core::rt::create_fd_handle" (
308 int fd) except+ nogil
309 FileDescriptorHandle create_fd_handle_ref "cuda_core::rt::create_fd_handle_ref" (
310 int fd) except+ nogil
312 # Array / mipmapped-array / texture / surface handles (PR #467)
313 OpaqueArrayHandle create_array_handle "cuda_core::rt::create_array_handle" (
314 const ContextHandle& h_context, const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil
315 OpaqueArrayHandle create_array_handle_ref "cuda_core::rt::create_array_handle_ref" (
316 cydriver.CUarray arr) except+ nogil
317 OpaqueArrayHandle create_array_handle_owning "cuda_core::rt::create_array_handle_owning" (
318 cydriver.CUarray arr) except+ nogil
319 ContextHandle get_array_context "cuda_core::rt::get_array_context" (
320 const OpaqueArrayHandle& h) noexcept nogil
321 OpaqueArrayHandle create_array_level_handle "cuda_core::rt::create_array_level_handle" (
322 const MipmappedArrayHandle& h_mip, unsigned int level) except+ nogil
323 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::rt::create_mipmapped_array_handle" (
324 const ContextHandle& h_context, const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc,
325 unsigned int num_levels) except+ nogil
326 ContextHandle get_mipmapped_array_context "cuda_core::rt::get_mipmapped_array_context" (
327 const MipmappedArrayHandle& h) noexcept nogil
328 TexObjectHandle create_tex_object_handle_array "cuda_core::rt::create_tex_object_handle_array" (
329 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
330 const cydriver.CUDA_TEXTURE_DESC& tex, const OpaqueArrayHandle& h_backing) except+ nogil
331 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::rt::create_tex_object_handle_mipmap" (
332 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
333 const cydriver.CUDA_TEXTURE_DESC& tex, const MipmappedArrayHandle& h_backing) except+ nogil
334 TexObjectHandle create_tex_object_handle_linear "cuda_core::rt::create_tex_object_handle_linear" (
335 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
336 const cydriver.CUDA_TEXTURE_DESC& tex, const DevicePtrHandle& h_backing) except+ nogil
337 SurfObjectHandle create_surf_object_handle "cuda_core::rt::create_surf_object_handle" (
338 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
339 const OpaqueArrayHandle& h_backing) except+ nogil
342initialize_deferred_cleanup()