Coverage for cuda/core/_resource_handles.pyx: 97.94%
97 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-10 02:27 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-10 02:27 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
5# This module compiles _cpp/resource_handles.cpp into a shared library.
6# Consumer modules cimport the functions declared in _resource_handles.pxd.
7# Since there is only one copy of the C++ code (in this .so), all static and
8# thread-local state is shared correctly across all consumer modules.
9#
10# The cdef extern from declarations below satisfy the .pxd declarations directly,
11# without needing separate wrapper functions.
13from cpython.pycapsule cimport PyCapsule_GetName, PyCapsule_GetPointer
14from libc.stddef cimport size_t
16from cuda.bindings cimport cydriver
17from cuda.bindings cimport cynvrtc
18from cuda.bindings cimport cynvvm
19from cuda.bindings cimport cynvjitlink
21import cuda.bindings.cydriver as cydriver
22import cuda.bindings.cynvrtc as cynvrtc
23import cuda.bindings.cynvvm as cynvvm
24import cuda.bindings.cynvjitlink as cynvjitlink
26# =============================================================================
27# C++ function declarations (non-inline, implemented in resource_handles.cpp)
28#
29# These declarations satisfy the cdef function declarations in _resource_handles.pxd.
30# Consumer modules cimport these functions and calls go through this .so.
31# =============================================================================
33cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
34 # Thread-local error handling
35 cydriver.CUresult get_last_error "cuda_core::get_last_error" () noexcept nogil
36 cydriver.CUresult peek_last_error "cuda_core::peek_last_error" () noexcept nogil
37 void clear_last_error "cuda_core::clear_last_error" () noexcept nogil
39 # Context handles
40 ContextHandle create_context_handle_ref "cuda_core::create_context_handle_ref" (
41 cydriver.CUcontext ctx) except+ nogil
42 ContextHandle create_context_handle_from_green_ctx "cuda_core::create_context_handle_from_green_ctx" (
43 const GreenCtxHandle& h_green_ctx) except+ nogil
44 GreenCtxHandle get_context_green_ctx "cuda_core::get_context_green_ctx" (
45 const ContextHandle& h) noexcept nogil
46 GreenCtxHandle create_green_ctx_handle "cuda_core::create_green_ctx_handle" (
47 cydriver.CUdevResource* resources, unsigned int nbResources,
48 cydriver.CUdevice dev, unsigned int flags) except+ nogil
49 GreenCtxHandle create_green_ctx_handle_ref "cuda_core::create_green_ctx_handle_ref" (
50 cydriver.CUgreenCtx ctx) except+ nogil
51 ContextHandle get_primary_context "cuda_core::get_primary_context" (
52 int device_id) except+ nogil
53 ContextHandle get_current_context "cuda_core::get_current_context" () except+ nogil
54 cydriver.CUresult context_synchronize "cuda_core::context_synchronize" (
55 const ContextHandle& h_context) noexcept nogil
56 cydriver.CUresult context_get_stream_priority_range "cuda_core::context_get_stream_priority_range" (
57 const ContextHandle& h_context,
58 int* least_priority,
59 int* greatest_priority) noexcept nogil
61 # Stream handles
62 StreamHandle create_stream_handle "cuda_core::create_stream_handle" (
63 const ContextHandle& h_ctx, unsigned int flags, int priority) except+ nogil
64 StreamHandle create_stream_handle_ref "cuda_core::create_stream_handle_ref" (
65 cydriver.CUstream stream) except+ nogil
66 StreamHandle create_stream_handle_with_owner "cuda_core::create_stream_handle_with_owner" (
67 cydriver.CUstream stream, object owner) except+ nogil
68 void py_object_user_object_destroy "cuda_core::py_object_user_object_destroy" (
69 void* py_object) noexcept nogil
70 void initialize_deferred_cleanup "cuda_core::initialize_deferred_cleanup" () except+
71 void retry_deferred_cleanup "cuda_core::retry_deferred_cleanup" () noexcept
72 ContextHandle get_stream_context "cuda_core::get_stream_context" (
73 const StreamHandle& h) noexcept nogil
74 StreamHandle get_legacy_stream "cuda_core::get_legacy_stream" () except+ nogil
75 StreamHandle get_per_thread_stream "cuda_core::get_per_thread_stream" () except+ nogil
76 StreamHandle create_context_bound_legacy_stream "cuda_core::create_context_bound_legacy_stream" (
77 const ContextHandle& h_context) except+ nogil
79 # Event handles (note: _create_event_handle* are internal due to C++ overloading)
80 EventHandle create_event_handle "cuda_core::create_event_handle" (
81 const ContextHandle& h_ctx, unsigned int flags,
82 bint timing_enabled, bint is_blocking_sync,
83 bint ipc_enabled, int device_id) except+ nogil
84 EventHandle create_event_handle_for_stream "cuda_core::create_event_handle_for_stream" (
85 cydriver.CUstream stream, unsigned int flags) except+ nogil
86 EventHandle create_event_handle_ref "cuda_core::create_event_handle_ref" (
87 cydriver.CUevent event) except+ nogil
88 EventHandle create_event_handle_ipc "cuda_core::create_event_handle_ipc" (
89 const cydriver.CUipcEventHandle& ipc_handle, bint is_blocking_sync) except+ nogil
91 # Event metadata getters
92 bint get_event_timing_enabled "cuda_core::get_event_timing_enabled" (
93 const EventHandle& h) noexcept nogil
94 bint get_event_is_blocking_sync "cuda_core::get_event_is_blocking_sync" (
95 const EventHandle& h) noexcept nogil
96 bint get_event_ipc_enabled "cuda_core::get_event_ipc_enabled" (
97 const EventHandle& h) noexcept nogil
98 int get_event_device_id "cuda_core::get_event_device_id" (
99 const EventHandle& h) noexcept nogil
100 ContextHandle get_event_context "cuda_core::get_event_context" (
101 const EventHandle& h) noexcept nogil
103 # Memory pool handles
104 MemoryPoolHandle create_mempool_handle "cuda_core::create_mempool_handle" (
105 const cydriver.CUmemPoolProps& props) except+ nogil
106 MemoryPoolHandle create_mempool_handle_ref "cuda_core::create_mempool_handle_ref" (
107 cydriver.CUmemoryPool pool) except+ nogil
108 MemoryPoolHandle get_device_mempool "cuda_core::get_device_mempool" (
109 int device_id) except+ nogil
110 MemoryPoolHandle create_mempool_handle_ipc "cuda_core::create_mempool_handle_ipc" (
111 int fd, cydriver.CUmemAllocationHandleType handle_type) except+ nogil
113 # Device pointer handles
114 DevicePtrHandle deviceptr_alloc_from_pool "cuda_core::deviceptr_alloc_from_pool" (
115 size_t size, const MemoryPoolHandle& h_pool, const StreamHandle& h_stream) except+ nogil
116 DevicePtrHandle deviceptr_alloc_async "cuda_core::deviceptr_alloc_async" (
117 size_t size, const StreamHandle& h_stream) except+ nogil
118 cydriver.CUresult deviceptr_alloc_raw "cuda_core::deviceptr_alloc_raw" (
119 cydriver.CUdeviceptr* ptr, size_t size, const ContextHandle& h_context) noexcept nogil
120 DevicePtrHandle deviceptr_alloc_host "cuda_core::deviceptr_alloc_host" (size_t size) except+ nogil
121 DevicePtrHandle deviceptr_create_ref "cuda_core::deviceptr_create_ref" (
122 cydriver.CUdeviceptr ptr) except+ nogil
123 DevicePtrHandle deviceptr_create_with_owner "cuda_core::deviceptr_create_with_owner" (
124 cydriver.CUdeviceptr ptr, object owner) except+ nogil
125 DevicePtrHandle deviceptr_create_mapped_graphics "cuda_core::deviceptr_create_mapped_graphics" (
126 cydriver.CUdeviceptr ptr,
127 const GraphicsResourceHandle& h_resource,
128 const StreamHandle& h_stream) except+ nogil
130 # MR deallocation callback
131 void register_mr_dealloc_callback "cuda_core::register_mr_dealloc_callback" (
132 MRDeallocCallback cb) noexcept
133 DevicePtrHandle deviceptr_create_with_mr "cuda_core::deviceptr_create_with_mr" (
134 cydriver.CUdeviceptr ptr, size_t size, object mr) except+ nogil
136 DevicePtrHandle deviceptr_import_ipc "cuda_core::deviceptr_import_ipc" (
137 const MemoryPoolHandle& h_pool, const void* export_data, const StreamHandle& h_stream) except+ nogil
138 StreamHandle deallocation_stream "cuda_core::deallocation_stream" (
139 const DevicePtrHandle& h) noexcept nogil
140 cydriver.CUresult set_deallocation_stream "cuda_core::set_deallocation_stream" (
141 const DevicePtrHandle& h, const StreamHandle& h_stream) noexcept nogil
143 # Library handles
144 LibraryHandle create_library_handle_from_file "cuda_core::create_library_handle_from_file" (
145 const char* path) except+ nogil
146 LibraryHandle create_library_handle_from_data "cuda_core::create_library_handle_from_data" (
147 const void* data) except+ nogil
148 LibraryHandle create_library_handle_ref "cuda_core::create_library_handle_ref" (
149 cydriver.CUlibrary library) except+ nogil
151 # Kernel handles
152 KernelHandle create_kernel_handle "cuda_core::create_kernel_handle" (
153 const LibraryHandle& h_library, const char* name) except+ nogil
154 KernelHandle create_kernel_handle_ref "cuda_core::create_kernel_handle_ref" (
155 cydriver.CUkernel kernel) except+ nogil
156 LibraryHandle get_kernel_library "cuda_core::get_kernel_library" (
157 const KernelHandle& h) noexcept nogil
159 # Graph handles
160 GraphHandle create_graph_handle "cuda_core::create_graph_handle" (
161 cydriver.CUgraph graph) except+ nogil
162 GraphHandle create_child_graph_handle "cuda_core::create_child_graph_handle" (
163 cydriver.CUgraph child_graph, const GraphHandle& h_parent,
164 cydriver.CUgraphNode owner_node) except+ nogil
166 # Graph node attachments
167 OpaqueHandle make_opaque_py "cuda_core::make_opaque_py" (object obj) except+
168 OpaqueHandle make_opaque_malloc "cuda_core::make_opaque_malloc" (void* buf) except+
169 cydriver.CUresult graph_get_attachment "cuda_core::graph_get_attachment" (
170 const GraphHandle& h_graph, cydriver.CUgraphNode node,
171 OpaqueHandle* owner0, OpaqueHandle* owner1) except+
172 cydriver.CUresult graph_prepare_attachment "cuda_core::graph_prepare_attachment" (
173 const GraphHandle& h_graph, OpaqueHandle owner0, OpaqueHandle owner1,
174 PreparedAttachment* out_prepared) except+
175 cydriver.CUresult graph_commit_attachment "cuda_core::graph_commit_attachment" (
176 PreparedAttachment& prepared, cydriver.CUgraphNode node) except+
177 cydriver.CUresult graph_clone_attachments "cuda_core::graph_clone_attachments" (
178 const GraphHandle& h_clone, const GraphHandle& h_source) except+
179 cydriver.CUresult graph_prepare_child_graph_update "cuda_core::graph_prepare_child_graph_update" (
180 const GraphHandle& h_parent, const GraphHandle& h_old_child,
181 cydriver.CUgraphNode owner_node, const GraphHandle& h_source,
182 PreparedChildGraphUpdate* out_prepared) except+
183 cydriver.CUresult graph_commit_child_graph_update "cuda_core::graph_commit_child_graph_update" (
184 PreparedChildGraphUpdate& prepared, GraphHandle* out_child) except+
185 void invalidate_child_graph_state "cuda_core::invalidate_child_graph_state" (
186 const GraphHandle& h_parent, cydriver.CUgraphNode owner_node) noexcept
188 # Graph exec handles
189 GraphExecHandle create_graph_exec_handle "cuda_core::create_graph_exec_handle" (
190 const GraphHandle& h_source,
191 cydriver.CUDA_GRAPH_INSTANTIATE_PARAMS* params) except+
192 cydriver.CUresult graph_exec_update "cuda_core::graph_exec_update" (
193 const GraphExecHandle& h_exec,
194 const GraphHandle& h_source,
195 cydriver.CUgraphExecUpdateResultInfo* result_info) except+
196 cydriver.CUresult graph_prepare_exec_attachment "cuda_core::graph_prepare_exec_attachment" (
197 const GraphExecHandle& h_exec,
198 OpaqueHandle owner0,
199 OpaqueHandle owner1,
200 PreparedExecAttachment* out_prepared) except+
201 void graph_commit_exec_attachment "cuda_core::graph_commit_exec_attachment" (
202 PreparedExecAttachment& prepared) noexcept
204 # Graph node handles
205 GraphNodeHandle create_graph_node_handle "cuda_core::create_graph_node_handle" (
206 cydriver.CUgraphNode node, const GraphHandle& h_graph) except+ nogil
207 GraphHandle graph_node_get_graph "cuda_core::graph_node_get_graph" (
208 const GraphNodeHandle& h) noexcept nogil
209 void invalidate_graph_node "cuda_core::invalidate_graph_node" (
210 const GraphNodeHandle& h) noexcept nogil
212 # Graphics resource handles
213 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::create_graphics_resource_handle" (
214 cydriver.CUgraphicsResource resource) except+ nogil
216 # NVRTC Program handles
217 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::create_nvrtc_program_handle" (
218 cynvrtc.nvrtcProgram prog) except+ nogil
219 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::create_nvrtc_program_handle_ref" (
220 cynvrtc.nvrtcProgram prog) except+ nogil
222 # NVVM Program handles
223 NvvmProgramHandle create_nvvm_program_handle "cuda_core::create_nvvm_program_handle" (
224 cynvvm.nvvmProgram prog) except+ nogil
225 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::create_nvvm_program_handle_ref" (
226 cynvvm.nvvmProgram prog) except+ nogil
228 # nvJitLink handles
229 NvJitLinkHandle create_nvjitlink_handle "cuda_core::create_nvjitlink_handle" (
230 cynvjitlink.nvJitLinkHandle handle) except+ nogil
231 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::create_nvjitlink_handle_ref" (
232 cynvjitlink.nvJitLinkHandle handle) except+ nogil
234 # cuLink handles
235 CuLinkHandle create_culink_handle "cuda_core::create_culink_handle" (
236 cydriver.CUlinkState state) except+ nogil
237 CuLinkHandle create_culink_handle_ref "cuda_core::create_culink_handle_ref" (
238 cydriver.CUlinkState state) except+ nogil
240 # File descriptor handles
241 FileDescriptorHandle create_fd_handle "cuda_core::create_fd_handle" (
242 int fd) except+ nogil
243 FileDescriptorHandle create_fd_handle_ref "cuda_core::create_fd_handle_ref" (
244 int fd) except+ nogil
246 # SM resource split (13.1+ wrapper — avoids direct cydriver cimport)
247 # groupParams is void* to avoid referencing CU_DEV_SM_RESOURCE_GROUP_PARAMS
248 # (which doesn't exist in cuda-bindings 13.0 .pxd). The C++ side casts it.
249 cydriver.CUresult sm_resource_split "cuda_core::sm_resource_split" (
250 cydriver.CUdevResource* result, unsigned int nbGroups,
251 const cydriver.CUdevResource* input, cydriver.CUdevResource* remainder,
252 unsigned int flags, void* groupParams) nogil
253 bint has_sm_resource_split "cuda_core::has_sm_resource_split" () noexcept nogil
255 # cuMemcpyWithAttributesAsync (13.2+ wrapper — avoids direct cydriver cimport)
256 # attr is void* to avoid referencing CUmemcpyAttributes (absent from
257 # cuda-bindings built against CUDA < 12.8). The C++ side casts it.
258 cydriver.CUresult memcpy_with_attributes_async "cuda_core::memcpy_with_attributes_async" (
259 cydriver.CUdeviceptr dst, cydriver.CUdeviceptr src, size_t size,
260 void* attr, cydriver.CUstream hStream) nogil
261 bint has_memcpy_with_attributes_async "cuda_core::has_memcpy_with_attributes_async" () noexcept nogil
263 # Array / mipmapped-array / texture / surface handles (PR #467)
264 OpaqueArrayHandle create_array_handle "cuda_core::create_array_handle" (
265 const ContextHandle& h_context, const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil
266 OpaqueArrayHandle create_array_handle_ref "cuda_core::create_array_handle_ref" (
267 cydriver.CUarray arr) except+ nogil
268 OpaqueArrayHandle create_array_handle_owning "cuda_core::create_array_handle_owning" (
269 cydriver.CUarray arr) except+ nogil
270 ContextHandle get_array_context "cuda_core::get_array_context" (
271 const OpaqueArrayHandle& h) noexcept nogil
272 OpaqueArrayHandle create_array_level_handle "cuda_core::create_array_level_handle" (
273 const MipmappedArrayHandle& h_mip, unsigned int level) except+ nogil
274 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::create_mipmapped_array_handle" (
275 const ContextHandle& h_context, const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc,
276 unsigned int num_levels) except+ nogil
277 ContextHandle get_mipmapped_array_context "cuda_core::get_mipmapped_array_context" (
278 const MipmappedArrayHandle& h) noexcept nogil
279 TexObjectHandle create_tex_object_handle_array "cuda_core::create_tex_object_handle_array" (
280 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
281 const cydriver.CUDA_TEXTURE_DESC& tex, const OpaqueArrayHandle& h_backing) except+ nogil
282 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::create_tex_object_handle_mipmap" (
283 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
284 const cydriver.CUDA_TEXTURE_DESC& tex, const MipmappedArrayHandle& h_backing) except+ nogil
285 TexObjectHandle create_tex_object_handle_linear "cuda_core::create_tex_object_handle_linear" (
286 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
287 const cydriver.CUDA_TEXTURE_DESC& tex, const DevicePtrHandle& h_backing) except+ nogil
288 SurfObjectHandle create_surf_object_handle "cuda_core::create_surf_object_handle" (
289 const ContextHandle& h_context, const cydriver.CUDA_RESOURCE_DESC& res,
290 const OpaqueArrayHandle& h_backing) except+ nogil
293# =============================================================================
294# CUDA Driver API capsule
295#
296# This provides resolved CUDA driver function pointers to the C++ code.
297# =============================================================================
299cdef const char* _CUDA_DRIVER_API_V1_NAME = b"cuda.core._resource_handles._CUDA_DRIVER_API_V1"
302# =============================================================================
303# CUDA driver function pointer initialization
304#
305# The C++ code declares extern function pointers (p_cuXxx) that need to be
306# populated before any handle creation functions are called. We extract these
307# from cuda.bindings.cydriver.__pyx_capi__ at module import time.
308#
309# The Cython string substitution (e.g., "reinterpret_cast<void*&>(...)")
310# allows us to assign void* values to typed function pointer variables.
311# =============================================================================
313# Declare extern variables with reinterpret_cast to allow void* assignment
314cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
315 # Error formatting
316 void* p_cuGetErrorName "reinterpret_cast<void*&>(cuda_core::p_cuGetErrorName)"
317 void* p_cuGetErrorString "reinterpret_cast<void*&>(cuda_core::p_cuGetErrorString)"
319 # Context
320 void* p_cuDevicePrimaryCtxRetain "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRetain)"
321 void* p_cuDevicePrimaryCtxRelease "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRelease)"
322 void* p_cuCtxGetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxGetCurrent)"
323 void* p_cuCtxSetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxSetCurrent)"
324 void* p_cuCtxSynchronize "reinterpret_cast<void*&>(cuda_core::p_cuCtxSynchronize)"
325 void* p_cuCtxGetStreamPriorityRange "reinterpret_cast<void*&>(cuda_core::p_cuCtxGetStreamPriorityRange)"
326 void* p_cuGreenCtxCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxCreate)"
327 void* p_cuGreenCtxDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxDestroy)"
328 void* p_cuCtxFromGreenCtx "reinterpret_cast<void*&>(cuda_core::p_cuCtxFromGreenCtx)"
329 void* p_cuDevResourceGenerateDesc "reinterpret_cast<void*&>(cuda_core::p_cuDevResourceGenerateDesc)"
330 void* p_cuGreenCtxStreamCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxStreamCreate)"
332 # Stream
333 void* p_cuStreamCreateWithPriority "reinterpret_cast<void*&>(cuda_core::p_cuStreamCreateWithPriority)"
334 void* p_cuStreamDestroy "reinterpret_cast<void*&>(cuda_core::p_cuStreamDestroy)"
335 void* p_cuStreamGetCtx "reinterpret_cast<void*&>(cuda_core::p_cuStreamGetCtx)"
337 # Event
338 void* p_cuEventCreate "reinterpret_cast<void*&>(cuda_core::p_cuEventCreate)"
339 void* p_cuEventDestroy "reinterpret_cast<void*&>(cuda_core::p_cuEventDestroy)"
340 void* p_cuIpcOpenEventHandle "reinterpret_cast<void*&>(cuda_core::p_cuIpcOpenEventHandle)"
342 # Device
343 void* p_cuDeviceGetCount "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetCount)"
345 # Memory pool
346 void* p_cuMemPoolSetAccess "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolSetAccess)"
347 void* p_cuMemPoolDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolDestroy)"
348 void* p_cuMemPoolCreate "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolCreate)"
349 void* p_cuDeviceGetMemPool "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetMemPool)"
350 void* p_cuMemPoolImportFromShareableHandle "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportFromShareableHandle)"
352 # Memory allocation
353 void* p_cuMemAllocFromPoolAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocFromPoolAsync)"
354 void* p_cuMemAllocAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocAsync)"
355 void* p_cuMemAlloc "reinterpret_cast<void*&>(cuda_core::p_cuMemAlloc)"
356 void* p_cuMemAllocHost "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocHost)"
358 # Memory deallocation
359 void* p_cuMemFreeAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeAsync)"
360 void* p_cuMemFree "reinterpret_cast<void*&>(cuda_core::p_cuMemFree)"
361 void* p_cuMemFreeHost "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeHost)"
363 # IPC
364 void* p_cuMemPoolImportPointer "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportPointer)"
366 # Library
367 void* p_cuLibraryLoadFromFile "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadFromFile)"
368 void* p_cuLibraryLoadData "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadData)"
369 void* p_cuLibraryUnload "reinterpret_cast<void*&>(cuda_core::p_cuLibraryUnload)"
370 void* p_cuLibraryGetKernel "reinterpret_cast<void*&>(cuda_core::p_cuLibraryGetKernel)"
372 # Graph
373 void* p_cuGraphDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphDestroy)"
374 void* p_cuGraphInstantiateWithParams "reinterpret_cast<void*&>(cuda_core::p_cuGraphInstantiateWithParams)"
375 void* p_cuGraphExecUpdate "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecUpdate)"
376 void* p_cuGraphExecDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecDestroy)"
377 void* p_cuUserObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectCreate)"
378 void* p_cuUserObjectRelease "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectRelease)"
379 void* p_cuGraphRetainUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphRetainUserObject)"
380 void* p_cuGraphReleaseUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphReleaseUserObject)"
381 void* p_cuGraphNodeFindInClone "reinterpret_cast<void*&>(cuda_core::p_cuGraphNodeFindInClone)"
382 void* p_cuGraphChildGraphNodeGetGraph "reinterpret_cast<void*&>(cuda_core::p_cuGraphChildGraphNodeGetGraph)"
384 # Linker
385 void* p_cuLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_cuLinkDestroy)"
387 # Graphics interop
388 void* p_cuGraphicsUnmapResources "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnmapResources)"
389 void* p_cuGraphicsUnregisterResource "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnregisterResource)"
391 # Texture / surface / array (PR #467)
392 void* p_cuArray3DCreate "reinterpret_cast<void*&>(cuda_core::p_cuArray3DCreate)"
393 void* p_cuArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuArrayDestroy)"
394 void* p_cuMipmappedArrayCreate "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayCreate)"
395 void* p_cuMipmappedArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayDestroy)"
396 void* p_cuMipmappedArrayGetLevel "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayGetLevel)"
397 void* p_cuTexObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectCreate)"
398 void* p_cuTexObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectDestroy)"
399 void* p_cuSurfObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectCreate)"
400 void* p_cuSurfObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectDestroy)"
402 # SM resource split (13.1+)
403 void* p_cuDevSmResourceSplit "reinterpret_cast<void*&>(cuda_core::p_cuDevSmResourceSplit)"
405 # cuMemcpyWithAttributesAsync (13.2+)
406 void* p_cuMemcpyWithAttributesAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemcpyWithAttributesAsync)"
408 # NVRTC
409 void* p_nvrtcDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvrtcDestroyProgram)"
411 # NVVM
412 void* p_nvvmDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvvmDestroyProgram)"
414 # nvJitLink
415 void* p_nvJitLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_nvJitLinkDestroy)"
418# Initialize driver function pointers from cydriver.__pyx_capi__ at module load
419cdef void* _get_driver_fn(str name):
420 capsule = cydriver.__pyx_capi__[name]
421 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
424cdef void* _get_optional_driver_fn(str name):
425 try:
426 capsule = cydriver.__pyx_capi__[name]
427 except KeyError:
428 return NULL
429 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
432cdef void _init_driver_fn_pointers() noexcept:
433 global p_cuGetErrorName, p_cuGetErrorString
434 global p_cuDevicePrimaryCtxRetain, p_cuDevicePrimaryCtxRelease, p_cuCtxGetCurrent
435 global p_cuCtxSetCurrent, p_cuCtxSynchronize, p_cuCtxGetStreamPriorityRange
436 global p_cuGreenCtxCreate, p_cuGreenCtxDestroy, p_cuCtxFromGreenCtx
437 global p_cuDevResourceGenerateDesc, p_cuGreenCtxStreamCreate
438 global p_cuStreamCreateWithPriority, p_cuStreamDestroy, p_cuStreamGetCtx
439 global p_cuEventCreate, p_cuEventDestroy, p_cuIpcOpenEventHandle
440 global p_cuDeviceGetCount
441 global p_cuMemPoolSetAccess, p_cuMemPoolDestroy, p_cuMemPoolCreate
442 global p_cuDeviceGetMemPool, p_cuMemPoolImportFromShareableHandle
443 global p_cuMemAllocFromPoolAsync, p_cuMemAllocAsync, p_cuMemAlloc, p_cuMemAllocHost
444 global p_cuMemFreeAsync, p_cuMemFree, p_cuMemFreeHost
445 global p_cuMemPoolImportPointer
446 global p_cuLibraryLoadFromFile, p_cuLibraryLoadData, p_cuLibraryUnload, p_cuLibraryGetKernel
447 global p_cuGraphDestroy, p_cuGraphInstantiateWithParams
448 global p_cuGraphExecUpdate, p_cuGraphExecDestroy
449 global p_cuUserObjectCreate, p_cuUserObjectRelease
450 global p_cuGraphRetainUserObject, p_cuGraphReleaseUserObject
451 global p_cuGraphNodeFindInClone, p_cuGraphChildGraphNodeGetGraph
452 global p_cuLinkDestroy
453 global p_cuGraphicsUnmapResources, p_cuGraphicsUnregisterResource
454 global p_cuDevSmResourceSplit
455 global p_cuMemcpyWithAttributesAsync
456 global p_cuArray3DCreate, p_cuArrayDestroy
457 global p_cuMipmappedArrayCreate, p_cuMipmappedArrayDestroy, p_cuMipmappedArrayGetLevel
458 global p_cuTexObjectCreate, p_cuTexObjectDestroy
459 global p_cuSurfObjectCreate, p_cuSurfObjectDestroy
461 # Error formatting
462 p_cuGetErrorName = _get_driver_fn("cuGetErrorName")
463 p_cuGetErrorString = _get_driver_fn("cuGetErrorString")
465 # Context
466 p_cuDevicePrimaryCtxRetain = _get_driver_fn("cuDevicePrimaryCtxRetain")
467 p_cuDevicePrimaryCtxRelease = _get_driver_fn("cuDevicePrimaryCtxRelease")
468 p_cuCtxGetCurrent = _get_driver_fn("cuCtxGetCurrent")
469 p_cuCtxSetCurrent = _get_driver_fn("cuCtxSetCurrent")
470 p_cuCtxSynchronize = _get_driver_fn("cuCtxSynchronize")
471 p_cuCtxGetStreamPriorityRange = _get_driver_fn("cuCtxGetStreamPriorityRange")
472 p_cuGreenCtxCreate = _get_optional_driver_fn("cuGreenCtxCreate")
473 p_cuGreenCtxDestroy = _get_optional_driver_fn("cuGreenCtxDestroy")
474 p_cuCtxFromGreenCtx = _get_optional_driver_fn("cuCtxFromGreenCtx")
475 p_cuDevResourceGenerateDesc = _get_optional_driver_fn("cuDevResourceGenerateDesc")
476 p_cuGreenCtxStreamCreate = _get_optional_driver_fn("cuGreenCtxStreamCreate")
478 # Stream
479 p_cuStreamCreateWithPriority = _get_driver_fn("cuStreamCreateWithPriority")
480 p_cuStreamDestroy = _get_driver_fn("cuStreamDestroy")
481 p_cuStreamGetCtx = _get_driver_fn("cuStreamGetCtx")
483 # Event
484 p_cuEventCreate = _get_driver_fn("cuEventCreate")
485 p_cuEventDestroy = _get_driver_fn("cuEventDestroy")
486 p_cuIpcOpenEventHandle = _get_driver_fn("cuIpcOpenEventHandle")
488 # Device
489 p_cuDeviceGetCount = _get_driver_fn("cuDeviceGetCount")
491 # Memory pool
492 p_cuMemPoolSetAccess = _get_driver_fn("cuMemPoolSetAccess")
493 p_cuMemPoolDestroy = _get_driver_fn("cuMemPoolDestroy")
494 p_cuMemPoolCreate = _get_driver_fn("cuMemPoolCreate")
495 p_cuDeviceGetMemPool = _get_driver_fn("cuDeviceGetMemPool")
496 p_cuMemPoolImportFromShareableHandle = _get_driver_fn("cuMemPoolImportFromShareableHandle")
498 # Memory allocation
499 p_cuMemAllocFromPoolAsync = _get_driver_fn("cuMemAllocFromPoolAsync")
500 p_cuMemAllocAsync = _get_driver_fn("cuMemAllocAsync")
501 p_cuMemAlloc = _get_driver_fn("cuMemAlloc")
502 p_cuMemAllocHost = _get_driver_fn("cuMemAllocHost")
504 # Memory deallocation
505 p_cuMemFreeAsync = _get_driver_fn("cuMemFreeAsync")
506 p_cuMemFree = _get_driver_fn("cuMemFree")
507 p_cuMemFreeHost = _get_driver_fn("cuMemFreeHost")
509 # IPC
510 p_cuMemPoolImportPointer = _get_driver_fn("cuMemPoolImportPointer")
512 # Library
513 p_cuLibraryLoadFromFile = _get_driver_fn("cuLibraryLoadFromFile")
514 p_cuLibraryLoadData = _get_driver_fn("cuLibraryLoadData")
515 p_cuLibraryUnload = _get_driver_fn("cuLibraryUnload")
516 p_cuLibraryGetKernel = _get_driver_fn("cuLibraryGetKernel")
518 # Graph
519 p_cuGraphDestroy = _get_driver_fn("cuGraphDestroy")
520 p_cuGraphInstantiateWithParams = _get_driver_fn("cuGraphInstantiateWithParams")
521 p_cuGraphExecUpdate = _get_driver_fn("cuGraphExecUpdate")
522 p_cuGraphExecDestroy = _get_driver_fn("cuGraphExecDestroy")
523 p_cuUserObjectCreate = _get_driver_fn("cuUserObjectCreate")
524 p_cuUserObjectRelease = _get_driver_fn("cuUserObjectRelease")
525 p_cuGraphRetainUserObject = _get_driver_fn("cuGraphRetainUserObject")
526 p_cuGraphReleaseUserObject = _get_driver_fn("cuGraphReleaseUserObject")
527 p_cuGraphNodeFindInClone = _get_driver_fn("cuGraphNodeFindInClone")
528 p_cuGraphChildGraphNodeGetGraph = _get_driver_fn("cuGraphChildGraphNodeGetGraph")
530 # Linker
531 p_cuLinkDestroy = _get_driver_fn("cuLinkDestroy")
533 # Graphics interop
534 p_cuGraphicsUnmapResources = _get_driver_fn("cuGraphicsUnmapResources")
535 p_cuGraphicsUnregisterResource = _get_driver_fn("cuGraphicsUnregisterResource")
537 # Texture / surface / array (PR #467)
538 p_cuArray3DCreate = _get_driver_fn("cuArray3DCreate")
539 p_cuArrayDestroy = _get_driver_fn("cuArrayDestroy")
540 p_cuMipmappedArrayCreate = _get_driver_fn("cuMipmappedArrayCreate")
541 p_cuMipmappedArrayDestroy = _get_driver_fn("cuMipmappedArrayDestroy")
542 p_cuMipmappedArrayGetLevel = _get_driver_fn("cuMipmappedArrayGetLevel")
543 p_cuTexObjectCreate = _get_driver_fn("cuTexObjectCreate")
544 p_cuTexObjectDestroy = _get_driver_fn("cuTexObjectDestroy")
545 p_cuSurfObjectCreate = _get_driver_fn("cuSurfObjectCreate")
546 p_cuSurfObjectDestroy = _get_driver_fn("cuSurfObjectDestroy")
548 # SM resource split (13.1+ — may not exist in older cuda-bindings)
549 p_cuDevSmResourceSplit = _get_optional_driver_fn("cuDevSmResourceSplit")
551 # cuMemcpyWithAttributesAsync (13.2+ — may not exist in older cuda-bindings)
552 p_cuMemcpyWithAttributesAsync = _get_optional_driver_fn("cuMemcpyWithAttributesAsync")
554_init_driver_fn_pointers()
555initialize_deferred_cleanup()
557# =============================================================================
558# NVRTC function pointer initialization
559# =============================================================================
561cdef void* _get_nvrtc_fn(str name):
562 capsule = cynvrtc.__pyx_capi__[name]
563 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
565cdef void _init_nvrtc_fn_pointers() noexcept:
566 global p_nvrtcDestroyProgram
567 p_nvrtcDestroyProgram = _get_nvrtc_fn("nvrtcDestroyProgram")
569_init_nvrtc_fn_pointers()
571# =============================================================================
572# NVVM function pointer initialization
573#
574# NVVM may not be available at runtime, so we handle missing function pointers
575# gracefully. The C++ deleter checks for null before calling.
576# =============================================================================
578cdef void* _get_nvvm_fn(str name):
579 capsule = cynvvm.__pyx_capi__[name]
580 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
582cdef void _init_nvvm_fn_pointers() noexcept:
583 global p_nvvmDestroyProgram
584 p_nvvmDestroyProgram = _get_nvvm_fn("nvvmDestroyProgram")
586_init_nvvm_fn_pointers()
588# =============================================================================
589# nvJitLink function pointer initialization
590#
591# nvJitLink may not be available at runtime, so we handle missing function
592# pointers gracefully. The C++ deleter checks for null before calling.
593# =============================================================================
595cdef void* _get_nvjitlink_fn(str name):
596 capsule = cynvjitlink.__pyx_capi__[name]
597 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
599cdef void _init_nvjitlink_fn_pointers() noexcept:
600 global p_nvJitLinkDestroy
601 p_nvJitLinkDestroy = _get_nvjitlink_fn("nvJitLinkDestroy")
603_init_nvjitlink_fn_pointers()