Coverage for cuda/core/_resource_handles.pyx: 97.83%
92 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-03 02:41 +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
55 # Stream handles
56 StreamHandle create_stream_handle "cuda_core::create_stream_handle" (
57 const ContextHandle& h_ctx, unsigned int flags, int priority) except+ nogil
58 StreamHandle create_stream_handle_ref "cuda_core::create_stream_handle_ref" (
59 cydriver.CUstream stream) except+ nogil
60 StreamHandle create_stream_handle_with_owner "cuda_core::create_stream_handle_with_owner" (
61 cydriver.CUstream stream, object owner) except+ nogil
62 void py_object_user_object_destroy "cuda_core::py_object_user_object_destroy" (
63 void* py_object) noexcept nogil
64 void initialize_deferred_cleanup "cuda_core::initialize_deferred_cleanup" () except+
65 void retry_deferred_cleanup "cuda_core::retry_deferred_cleanup" () noexcept
66 ContextHandle get_stream_context "cuda_core::get_stream_context" (
67 const StreamHandle& h) noexcept nogil
68 StreamHandle get_legacy_stream "cuda_core::get_legacy_stream" () except+ nogil
69 StreamHandle get_per_thread_stream "cuda_core::get_per_thread_stream" () except+ nogil
71 # Event handles (note: _create_event_handle* are internal due to C++ overloading)
72 EventHandle create_event_handle "cuda_core::create_event_handle" (
73 const ContextHandle& h_ctx, unsigned int flags,
74 bint timing_enabled, bint is_blocking_sync,
75 bint ipc_enabled, int device_id) except+ nogil
76 EventHandle create_event_handle_noctx "cuda_core::create_event_handle_noctx" (
77 unsigned int flags) except+ nogil
78 EventHandle create_event_handle_ref "cuda_core::create_event_handle_ref" (
79 cydriver.CUevent event) except+ nogil
80 EventHandle create_event_handle_ipc "cuda_core::create_event_handle_ipc" (
81 const cydriver.CUipcEventHandle& ipc_handle, bint is_blocking_sync) except+ nogil
83 # Event metadata getters
84 bint get_event_timing_enabled "cuda_core::get_event_timing_enabled" (
85 const EventHandle& h) noexcept nogil
86 bint get_event_is_blocking_sync "cuda_core::get_event_is_blocking_sync" (
87 const EventHandle& h) noexcept nogil
88 bint get_event_ipc_enabled "cuda_core::get_event_ipc_enabled" (
89 const EventHandle& h) noexcept nogil
90 int get_event_device_id "cuda_core::get_event_device_id" (
91 const EventHandle& h) noexcept nogil
92 ContextHandle get_event_context "cuda_core::get_event_context" (
93 const EventHandle& h) noexcept nogil
95 # Memory pool handles
96 MemoryPoolHandle create_mempool_handle "cuda_core::create_mempool_handle" (
97 const cydriver.CUmemPoolProps& props) except+ nogil
98 MemoryPoolHandle create_mempool_handle_ref "cuda_core::create_mempool_handle_ref" (
99 cydriver.CUmemoryPool pool) except+ nogil
100 MemoryPoolHandle get_device_mempool "cuda_core::get_device_mempool" (
101 int device_id) except+ nogil
102 MemoryPoolHandle create_mempool_handle_ipc "cuda_core::create_mempool_handle_ipc" (
103 int fd, cydriver.CUmemAllocationHandleType handle_type) except+ nogil
105 # Device pointer handles
106 DevicePtrHandle deviceptr_alloc_from_pool "cuda_core::deviceptr_alloc_from_pool" (
107 size_t size, const MemoryPoolHandle& h_pool, const StreamHandle& h_stream) except+ nogil
108 DevicePtrHandle deviceptr_alloc_async "cuda_core::deviceptr_alloc_async" (
109 size_t size, const StreamHandle& h_stream) except+ nogil
110 DevicePtrHandle deviceptr_alloc "cuda_core::deviceptr_alloc" (size_t size) except+ nogil
111 DevicePtrHandle deviceptr_alloc_host "cuda_core::deviceptr_alloc_host" (size_t size) except+ nogil
112 DevicePtrHandle deviceptr_create_ref "cuda_core::deviceptr_create_ref" (
113 cydriver.CUdeviceptr ptr) except+ nogil
114 DevicePtrHandle deviceptr_create_with_owner "cuda_core::deviceptr_create_with_owner" (
115 cydriver.CUdeviceptr ptr, object owner) except+ nogil
116 DevicePtrHandle deviceptr_create_mapped_graphics "cuda_core::deviceptr_create_mapped_graphics" (
117 cydriver.CUdeviceptr ptr,
118 const GraphicsResourceHandle& h_resource,
119 const StreamHandle& h_stream) except+ nogil
121 # MR deallocation callback
122 void register_mr_dealloc_callback "cuda_core::register_mr_dealloc_callback" (
123 MRDeallocCallback cb) noexcept
124 DevicePtrHandle deviceptr_create_with_mr "cuda_core::deviceptr_create_with_mr" (
125 cydriver.CUdeviceptr ptr, size_t size, object mr) except+ nogil
127 DevicePtrHandle deviceptr_import_ipc "cuda_core::deviceptr_import_ipc" (
128 const MemoryPoolHandle& h_pool, const void* export_data, const StreamHandle& h_stream) except+ nogil
129 StreamHandle deallocation_stream "cuda_core::deallocation_stream" (
130 const DevicePtrHandle& h) noexcept nogil
131 cydriver.CUresult set_deallocation_stream "cuda_core::set_deallocation_stream" (
132 const DevicePtrHandle& h, const StreamHandle& h_stream) noexcept nogil
134 # Library handles
135 LibraryHandle create_library_handle_from_file "cuda_core::create_library_handle_from_file" (
136 const char* path) except+ nogil
137 LibraryHandle create_library_handle_from_data "cuda_core::create_library_handle_from_data" (
138 const void* data) except+ nogil
139 LibraryHandle create_library_handle_ref "cuda_core::create_library_handle_ref" (
140 cydriver.CUlibrary library) except+ nogil
142 # Kernel handles
143 KernelHandle create_kernel_handle "cuda_core::create_kernel_handle" (
144 const LibraryHandle& h_library, const char* name) except+ nogil
145 KernelHandle create_kernel_handle_ref "cuda_core::create_kernel_handle_ref" (
146 cydriver.CUkernel kernel) except+ nogil
147 LibraryHandle get_kernel_library "cuda_core::get_kernel_library" (
148 const KernelHandle& h) noexcept nogil
150 # Graph handles
151 GraphHandle create_graph_handle "cuda_core::create_graph_handle" (
152 cydriver.CUgraph graph) except+ nogil
153 GraphHandle create_child_graph_handle "cuda_core::create_child_graph_handle" (
154 cydriver.CUgraph child_graph, const GraphHandle& h_parent,
155 cydriver.CUgraphNode owner_node) except+ nogil
157 # Graph node attachments
158 OpaqueHandle make_opaque_py "cuda_core::make_opaque_py" (object obj) except+
159 OpaqueHandle make_opaque_malloc "cuda_core::make_opaque_malloc" (void* buf) except+
160 cydriver.CUresult graph_get_attachment "cuda_core::graph_get_attachment" (
161 const GraphHandle& h_graph, cydriver.CUgraphNode node,
162 OpaqueHandle* owner0, OpaqueHandle* owner1) except+
163 cydriver.CUresult graph_prepare_attachment "cuda_core::graph_prepare_attachment" (
164 const GraphHandle& h_graph, OpaqueHandle owner0, OpaqueHandle owner1,
165 PreparedAttachment* out_prepared) except+
166 cydriver.CUresult graph_commit_attachment "cuda_core::graph_commit_attachment" (
167 PreparedAttachment& prepared, cydriver.CUgraphNode node) except+
168 cydriver.CUresult graph_clone_attachments "cuda_core::graph_clone_attachments" (
169 const GraphHandle& h_clone, const GraphHandle& h_source) except+
170 cydriver.CUresult graph_prepare_child_graph_update "cuda_core::graph_prepare_child_graph_update" (
171 const GraphHandle& h_parent, const GraphHandle& h_old_child,
172 cydriver.CUgraphNode owner_node, const GraphHandle& h_source,
173 PreparedChildGraphUpdate* out_prepared) except+
174 cydriver.CUresult graph_commit_child_graph_update "cuda_core::graph_commit_child_graph_update" (
175 PreparedChildGraphUpdate& prepared, GraphHandle* out_child) except+
176 void invalidate_child_graph_state "cuda_core::invalidate_child_graph_state" (
177 const GraphHandle& h_parent, cydriver.CUgraphNode owner_node) noexcept
179 # Graph exec handles
180 GraphExecHandle create_graph_exec_handle "cuda_core::create_graph_exec_handle" (
181 const GraphHandle& h_source,
182 cydriver.CUDA_GRAPH_INSTANTIATE_PARAMS* params) except+
183 cydriver.CUresult graph_exec_update "cuda_core::graph_exec_update" (
184 const GraphExecHandle& h_exec,
185 const GraphHandle& h_source,
186 cydriver.CUgraphExecUpdateResultInfo* result_info) except+
187 cydriver.CUresult graph_prepare_exec_attachment "cuda_core::graph_prepare_exec_attachment" (
188 const GraphExecHandle& h_exec,
189 OpaqueHandle owner0,
190 OpaqueHandle owner1,
191 PreparedExecAttachment* out_prepared) except+
192 void graph_commit_exec_attachment "cuda_core::graph_commit_exec_attachment" (
193 PreparedExecAttachment& prepared) noexcept
195 # Graph node handles
196 GraphNodeHandle create_graph_node_handle "cuda_core::create_graph_node_handle" (
197 cydriver.CUgraphNode node, const GraphHandle& h_graph) except+ nogil
198 GraphHandle graph_node_get_graph "cuda_core::graph_node_get_graph" (
199 const GraphNodeHandle& h) noexcept nogil
200 void invalidate_graph_node "cuda_core::invalidate_graph_node" (
201 const GraphNodeHandle& h) noexcept nogil
203 # Graphics resource handles
204 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::create_graphics_resource_handle" (
205 cydriver.CUgraphicsResource resource) except+ nogil
207 # NVRTC Program handles
208 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::create_nvrtc_program_handle" (
209 cynvrtc.nvrtcProgram prog) except+ nogil
210 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::create_nvrtc_program_handle_ref" (
211 cynvrtc.nvrtcProgram prog) except+ nogil
213 # NVVM Program handles
214 NvvmProgramHandle create_nvvm_program_handle "cuda_core::create_nvvm_program_handle" (
215 cynvvm.nvvmProgram prog) except+ nogil
216 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::create_nvvm_program_handle_ref" (
217 cynvvm.nvvmProgram prog) except+ nogil
219 # nvJitLink handles
220 NvJitLinkHandle create_nvjitlink_handle "cuda_core::create_nvjitlink_handle" (
221 cynvjitlink.nvJitLinkHandle handle) except+ nogil
222 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::create_nvjitlink_handle_ref" (
223 cynvjitlink.nvJitLinkHandle handle) except+ nogil
225 # cuLink handles
226 CuLinkHandle create_culink_handle "cuda_core::create_culink_handle" (
227 cydriver.CUlinkState state) except+ nogil
228 CuLinkHandle create_culink_handle_ref "cuda_core::create_culink_handle_ref" (
229 cydriver.CUlinkState state) except+ nogil
231 # File descriptor handles
232 FileDescriptorHandle create_fd_handle "cuda_core::create_fd_handle" (
233 int fd) except+ nogil
234 FileDescriptorHandle create_fd_handle_ref "cuda_core::create_fd_handle_ref" (
235 int fd) except+ nogil
237 # SM resource split (13.1+ wrapper — avoids direct cydriver cimport)
238 # groupParams is void* to avoid referencing CU_DEV_SM_RESOURCE_GROUP_PARAMS
239 # (which doesn't exist in cuda-bindings 13.0 .pxd). The C++ side casts it.
240 cydriver.CUresult sm_resource_split "cuda_core::sm_resource_split" (
241 cydriver.CUdevResource* result, unsigned int nbGroups,
242 const cydriver.CUdevResource* input, cydriver.CUdevResource* remainder,
243 unsigned int flags, void* groupParams) nogil
244 bint has_sm_resource_split "cuda_core::has_sm_resource_split" () noexcept nogil
246 # cuMemcpyWithAttributesAsync (13.2+ wrapper — avoids direct cydriver cimport)
247 # attr is void* to avoid referencing CUmemcpyAttributes (absent from
248 # cuda-bindings built against CUDA < 12.8). The C++ side casts it.
249 cydriver.CUresult memcpy_with_attributes_async "cuda_core::memcpy_with_attributes_async" (
250 cydriver.CUdeviceptr dst, cydriver.CUdeviceptr src, size_t size,
251 void* attr, cydriver.CUstream hStream) nogil
252 bint has_memcpy_with_attributes_async "cuda_core::has_memcpy_with_attributes_async" () noexcept nogil
254 # Array / mipmapped-array / texture / surface handles (PR #467)
255 OpaqueArrayHandle create_array_handle "cuda_core::create_array_handle" (
256 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil
257 OpaqueArrayHandle create_array_handle_ref "cuda_core::create_array_handle_ref" (
258 cydriver.CUarray arr) except+ nogil
259 OpaqueArrayHandle create_array_handle_owning "cuda_core::create_array_handle_owning" (
260 cydriver.CUarray arr) except+ nogil
261 OpaqueArrayHandle create_array_level_handle "cuda_core::create_array_level_handle" (
262 const MipmappedArrayHandle& h_mip, unsigned int level) except+ nogil
263 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::create_mipmapped_array_handle" (
264 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc, unsigned int num_levels) except+ nogil
265 TexObjectHandle create_tex_object_handle_array "cuda_core::create_tex_object_handle_array" (
266 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
267 const OpaqueArrayHandle& h_backing) except+ nogil
268 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::create_tex_object_handle_mipmap" (
269 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
270 const MipmappedArrayHandle& h_backing) except+ nogil
271 TexObjectHandle create_tex_object_handle_linear "cuda_core::create_tex_object_handle_linear" (
272 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
273 const DevicePtrHandle& h_backing) except+ nogil
274 SurfObjectHandle create_surf_object_handle "cuda_core::create_surf_object_handle" (
275 const cydriver.CUDA_RESOURCE_DESC& res, const OpaqueArrayHandle& h_backing) except+ nogil
278# =============================================================================
279# CUDA Driver API capsule
280#
281# This provides resolved CUDA driver function pointers to the C++ code.
282# =============================================================================
284cdef const char* _CUDA_DRIVER_API_V1_NAME = b"cuda.core._resource_handles._CUDA_DRIVER_API_V1"
287# =============================================================================
288# CUDA driver function pointer initialization
289#
290# The C++ code declares extern function pointers (p_cuXxx) that need to be
291# populated before any handle creation functions are called. We extract these
292# from cuda.bindings.cydriver.__pyx_capi__ at module import time.
293#
294# The Cython string substitution (e.g., "reinterpret_cast<void*&>(...)")
295# allows us to assign void* values to typed function pointer variables.
296# =============================================================================
298# Declare extern variables with reinterpret_cast to allow void* assignment
299cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
300 # Context
301 void* p_cuDevicePrimaryCtxRetain "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRetain)"
302 void* p_cuDevicePrimaryCtxRelease "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRelease)"
303 void* p_cuCtxGetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxGetCurrent)"
304 void* p_cuCtxSetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxSetCurrent)"
305 void* p_cuGreenCtxCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxCreate)"
306 void* p_cuGreenCtxDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxDestroy)"
307 void* p_cuCtxFromGreenCtx "reinterpret_cast<void*&>(cuda_core::p_cuCtxFromGreenCtx)"
308 void* p_cuDevResourceGenerateDesc "reinterpret_cast<void*&>(cuda_core::p_cuDevResourceGenerateDesc)"
309 void* p_cuGreenCtxStreamCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxStreamCreate)"
311 # Stream
312 void* p_cuStreamCreateWithPriority "reinterpret_cast<void*&>(cuda_core::p_cuStreamCreateWithPriority)"
313 void* p_cuStreamDestroy "reinterpret_cast<void*&>(cuda_core::p_cuStreamDestroy)"
315 # Event
316 void* p_cuEventCreate "reinterpret_cast<void*&>(cuda_core::p_cuEventCreate)"
317 void* p_cuEventDestroy "reinterpret_cast<void*&>(cuda_core::p_cuEventDestroy)"
318 void* p_cuIpcOpenEventHandle "reinterpret_cast<void*&>(cuda_core::p_cuIpcOpenEventHandle)"
320 # Device
321 void* p_cuDeviceGetCount "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetCount)"
323 # Memory pool
324 void* p_cuMemPoolSetAccess "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolSetAccess)"
325 void* p_cuMemPoolDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolDestroy)"
326 void* p_cuMemPoolCreate "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolCreate)"
327 void* p_cuDeviceGetMemPool "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetMemPool)"
328 void* p_cuMemPoolImportFromShareableHandle "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportFromShareableHandle)"
330 # Memory allocation
331 void* p_cuMemAllocFromPoolAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocFromPoolAsync)"
332 void* p_cuMemAllocAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocAsync)"
333 void* p_cuMemAlloc "reinterpret_cast<void*&>(cuda_core::p_cuMemAlloc)"
334 void* p_cuMemAllocHost "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocHost)"
336 # Memory deallocation
337 void* p_cuMemFreeAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeAsync)"
338 void* p_cuMemFree "reinterpret_cast<void*&>(cuda_core::p_cuMemFree)"
339 void* p_cuMemFreeHost "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeHost)"
341 # IPC
342 void* p_cuMemPoolImportPointer "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportPointer)"
344 # Library
345 void* p_cuLibraryLoadFromFile "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadFromFile)"
346 void* p_cuLibraryLoadData "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadData)"
347 void* p_cuLibraryUnload "reinterpret_cast<void*&>(cuda_core::p_cuLibraryUnload)"
348 void* p_cuLibraryGetKernel "reinterpret_cast<void*&>(cuda_core::p_cuLibraryGetKernel)"
350 # Graph
351 void* p_cuGraphDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphDestroy)"
352 void* p_cuGraphInstantiateWithParams "reinterpret_cast<void*&>(cuda_core::p_cuGraphInstantiateWithParams)"
353 void* p_cuGraphExecUpdate "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecUpdate)"
354 void* p_cuGraphExecDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecDestroy)"
355 void* p_cuUserObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectCreate)"
356 void* p_cuUserObjectRelease "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectRelease)"
357 void* p_cuGraphRetainUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphRetainUserObject)"
358 void* p_cuGraphReleaseUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphReleaseUserObject)"
359 void* p_cuGraphNodeFindInClone "reinterpret_cast<void*&>(cuda_core::p_cuGraphNodeFindInClone)"
360 void* p_cuGraphChildGraphNodeGetGraph "reinterpret_cast<void*&>(cuda_core::p_cuGraphChildGraphNodeGetGraph)"
362 # Linker
363 void* p_cuLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_cuLinkDestroy)"
365 # Graphics interop
366 void* p_cuGraphicsUnmapResources "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnmapResources)"
367 void* p_cuGraphicsUnregisterResource "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnregisterResource)"
369 # Texture / surface / array (PR #467)
370 void* p_cuArray3DCreate "reinterpret_cast<void*&>(cuda_core::p_cuArray3DCreate)"
371 void* p_cuArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuArrayDestroy)"
372 void* p_cuMipmappedArrayCreate "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayCreate)"
373 void* p_cuMipmappedArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayDestroy)"
374 void* p_cuMipmappedArrayGetLevel "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayGetLevel)"
375 void* p_cuTexObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectCreate)"
376 void* p_cuTexObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectDestroy)"
377 void* p_cuSurfObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectCreate)"
378 void* p_cuSurfObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectDestroy)"
380 # SM resource split (13.1+)
381 void* p_cuDevSmResourceSplit "reinterpret_cast<void*&>(cuda_core::p_cuDevSmResourceSplit)"
383 # cuMemcpyWithAttributesAsync (13.2+)
384 void* p_cuMemcpyWithAttributesAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemcpyWithAttributesAsync)"
386 # NVRTC
387 void* p_nvrtcDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvrtcDestroyProgram)"
389 # NVVM
390 void* p_nvvmDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvvmDestroyProgram)"
392 # nvJitLink
393 void* p_nvJitLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_nvJitLinkDestroy)"
396# Initialize driver function pointers from cydriver.__pyx_capi__ at module load
397cdef void* _get_driver_fn(str name):
398 capsule = cydriver.__pyx_capi__[name]
399 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
402cdef void* _get_optional_driver_fn(str name):
403 try:
404 capsule = cydriver.__pyx_capi__[name]
405 except KeyError:
406 return NULL
407 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
410cdef void _init_driver_fn_pointers() noexcept:
411 global p_cuDevicePrimaryCtxRetain, p_cuDevicePrimaryCtxRelease, p_cuCtxGetCurrent
412 global p_cuCtxSetCurrent
413 global p_cuGreenCtxCreate, p_cuGreenCtxDestroy, p_cuCtxFromGreenCtx
414 global p_cuDevResourceGenerateDesc, p_cuGreenCtxStreamCreate
415 global p_cuStreamCreateWithPriority, p_cuStreamDestroy
416 global p_cuEventCreate, p_cuEventDestroy, p_cuIpcOpenEventHandle
417 global p_cuDeviceGetCount
418 global p_cuMemPoolSetAccess, p_cuMemPoolDestroy, p_cuMemPoolCreate
419 global p_cuDeviceGetMemPool, p_cuMemPoolImportFromShareableHandle
420 global p_cuMemAllocFromPoolAsync, p_cuMemAllocAsync, p_cuMemAlloc, p_cuMemAllocHost
421 global p_cuMemFreeAsync, p_cuMemFree, p_cuMemFreeHost
422 global p_cuMemPoolImportPointer
423 global p_cuLibraryLoadFromFile, p_cuLibraryLoadData, p_cuLibraryUnload, p_cuLibraryGetKernel
424 global p_cuGraphDestroy, p_cuGraphInstantiateWithParams
425 global p_cuGraphExecUpdate, p_cuGraphExecDestroy
426 global p_cuUserObjectCreate, p_cuUserObjectRelease
427 global p_cuGraphRetainUserObject, p_cuGraphReleaseUserObject
428 global p_cuGraphNodeFindInClone, p_cuGraphChildGraphNodeGetGraph
429 global p_cuLinkDestroy
430 global p_cuGraphicsUnmapResources, p_cuGraphicsUnregisterResource
431 global p_cuDevSmResourceSplit
432 global p_cuMemcpyWithAttributesAsync
433 global p_cuArray3DCreate, p_cuArrayDestroy
434 global p_cuMipmappedArrayCreate, p_cuMipmappedArrayDestroy, p_cuMipmappedArrayGetLevel
435 global p_cuTexObjectCreate, p_cuTexObjectDestroy
436 global p_cuSurfObjectCreate, p_cuSurfObjectDestroy
438 # Context
439 p_cuDevicePrimaryCtxRetain = _get_driver_fn("cuDevicePrimaryCtxRetain")
440 p_cuDevicePrimaryCtxRelease = _get_driver_fn("cuDevicePrimaryCtxRelease")
441 p_cuCtxGetCurrent = _get_driver_fn("cuCtxGetCurrent")
442 p_cuCtxSetCurrent = _get_driver_fn("cuCtxSetCurrent")
443 p_cuGreenCtxCreate = _get_optional_driver_fn("cuGreenCtxCreate")
444 p_cuGreenCtxDestroy = _get_optional_driver_fn("cuGreenCtxDestroy")
445 p_cuCtxFromGreenCtx = _get_optional_driver_fn("cuCtxFromGreenCtx")
446 p_cuDevResourceGenerateDesc = _get_optional_driver_fn("cuDevResourceGenerateDesc")
447 p_cuGreenCtxStreamCreate = _get_optional_driver_fn("cuGreenCtxStreamCreate")
449 # Stream
450 p_cuStreamCreateWithPriority = _get_driver_fn("cuStreamCreateWithPriority")
451 p_cuStreamDestroy = _get_driver_fn("cuStreamDestroy")
453 # Event
454 p_cuEventCreate = _get_driver_fn("cuEventCreate")
455 p_cuEventDestroy = _get_driver_fn("cuEventDestroy")
456 p_cuIpcOpenEventHandle = _get_driver_fn("cuIpcOpenEventHandle")
458 # Device
459 p_cuDeviceGetCount = _get_driver_fn("cuDeviceGetCount")
461 # Memory pool
462 p_cuMemPoolSetAccess = _get_driver_fn("cuMemPoolSetAccess")
463 p_cuMemPoolDestroy = _get_driver_fn("cuMemPoolDestroy")
464 p_cuMemPoolCreate = _get_driver_fn("cuMemPoolCreate")
465 p_cuDeviceGetMemPool = _get_driver_fn("cuDeviceGetMemPool")
466 p_cuMemPoolImportFromShareableHandle = _get_driver_fn("cuMemPoolImportFromShareableHandle")
468 # Memory allocation
469 p_cuMemAllocFromPoolAsync = _get_driver_fn("cuMemAllocFromPoolAsync")
470 p_cuMemAllocAsync = _get_driver_fn("cuMemAllocAsync")
471 p_cuMemAlloc = _get_driver_fn("cuMemAlloc")
472 p_cuMemAllocHost = _get_driver_fn("cuMemAllocHost")
474 # Memory deallocation
475 p_cuMemFreeAsync = _get_driver_fn("cuMemFreeAsync")
476 p_cuMemFree = _get_driver_fn("cuMemFree")
477 p_cuMemFreeHost = _get_driver_fn("cuMemFreeHost")
479 # IPC
480 p_cuMemPoolImportPointer = _get_driver_fn("cuMemPoolImportPointer")
482 # Library
483 p_cuLibraryLoadFromFile = _get_driver_fn("cuLibraryLoadFromFile")
484 p_cuLibraryLoadData = _get_driver_fn("cuLibraryLoadData")
485 p_cuLibraryUnload = _get_driver_fn("cuLibraryUnload")
486 p_cuLibraryGetKernel = _get_driver_fn("cuLibraryGetKernel")
488 # Graph
489 p_cuGraphDestroy = _get_driver_fn("cuGraphDestroy")
490 p_cuGraphInstantiateWithParams = _get_driver_fn("cuGraphInstantiateWithParams")
491 p_cuGraphExecUpdate = _get_driver_fn("cuGraphExecUpdate")
492 p_cuGraphExecDestroy = _get_driver_fn("cuGraphExecDestroy")
493 p_cuUserObjectCreate = _get_driver_fn("cuUserObjectCreate")
494 p_cuUserObjectRelease = _get_driver_fn("cuUserObjectRelease")
495 p_cuGraphRetainUserObject = _get_driver_fn("cuGraphRetainUserObject")
496 p_cuGraphReleaseUserObject = _get_driver_fn("cuGraphReleaseUserObject")
497 p_cuGraphNodeFindInClone = _get_driver_fn("cuGraphNodeFindInClone")
498 p_cuGraphChildGraphNodeGetGraph = _get_driver_fn("cuGraphChildGraphNodeGetGraph")
500 # Linker
501 p_cuLinkDestroy = _get_driver_fn("cuLinkDestroy")
503 # Graphics interop
504 p_cuGraphicsUnmapResources = _get_driver_fn("cuGraphicsUnmapResources")
505 p_cuGraphicsUnregisterResource = _get_driver_fn("cuGraphicsUnregisterResource")
507 # Texture / surface / array (PR #467)
508 p_cuArray3DCreate = _get_driver_fn("cuArray3DCreate")
509 p_cuArrayDestroy = _get_driver_fn("cuArrayDestroy")
510 p_cuMipmappedArrayCreate = _get_driver_fn("cuMipmappedArrayCreate")
511 p_cuMipmappedArrayDestroy = _get_driver_fn("cuMipmappedArrayDestroy")
512 p_cuMipmappedArrayGetLevel = _get_driver_fn("cuMipmappedArrayGetLevel")
513 p_cuTexObjectCreate = _get_driver_fn("cuTexObjectCreate")
514 p_cuTexObjectDestroy = _get_driver_fn("cuTexObjectDestroy")
515 p_cuSurfObjectCreate = _get_driver_fn("cuSurfObjectCreate")
516 p_cuSurfObjectDestroy = _get_driver_fn("cuSurfObjectDestroy")
518 # SM resource split (13.1+ — may not exist in older cuda-bindings)
519 p_cuDevSmResourceSplit = _get_optional_driver_fn("cuDevSmResourceSplit")
521 # cuMemcpyWithAttributesAsync (13.2+ — may not exist in older cuda-bindings)
522 p_cuMemcpyWithAttributesAsync = _get_optional_driver_fn("cuMemcpyWithAttributesAsync")
524_init_driver_fn_pointers()
525initialize_deferred_cleanup()
527# =============================================================================
528# NVRTC function pointer initialization
529# =============================================================================
531cdef void* _get_nvrtc_fn(str name):
532 capsule = cynvrtc.__pyx_capi__[name]
533 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
535cdef void _init_nvrtc_fn_pointers() noexcept:
536 global p_nvrtcDestroyProgram
537 p_nvrtcDestroyProgram = _get_nvrtc_fn("nvrtcDestroyProgram")
539_init_nvrtc_fn_pointers()
541# =============================================================================
542# NVVM function pointer initialization
543#
544# NVVM may not be available at runtime, so we handle missing function pointers
545# gracefully. The C++ deleter checks for null before calling.
546# =============================================================================
548cdef void* _get_nvvm_fn(str name):
549 capsule = cynvvm.__pyx_capi__[name]
550 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
552cdef void _init_nvvm_fn_pointers() noexcept:
553 global p_nvvmDestroyProgram
554 p_nvvmDestroyProgram = _get_nvvm_fn("nvvmDestroyProgram")
556_init_nvvm_fn_pointers()
558# =============================================================================
559# nvJitLink function pointer initialization
560#
561# nvJitLink may not be available at runtime, so we handle missing function
562# pointers gracefully. The C++ deleter checks for null before calling.
563# =============================================================================
565cdef void* _get_nvjitlink_fn(str name):
566 capsule = cynvjitlink.__pyx_capi__[name]
567 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
569cdef void _init_nvjitlink_fn_pointers() noexcept:
570 global p_nvJitLinkDestroy
571 p_nvJitLinkDestroy = _get_nvjitlink_fn("nvJitLinkDestroy")
573_init_nvjitlink_fn_pointers()