Coverage for cuda/core/_resource_handles.pyx: 97.73%
88 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-29 01:38 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-29 01:38 +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 void 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 void invalidate_child_graph_state "cuda_core::invalidate_child_graph_state" (
171 const GraphHandle& h_parent, cydriver.CUgraphNode owner_node) noexcept
173 # Graph exec handles
174 GraphExecHandle create_graph_exec_handle "cuda_core::create_graph_exec_handle" (
175 cydriver.CUgraphExec graph_exec) except+ nogil
177 # Graph node handles
178 GraphNodeHandle create_graph_node_handle "cuda_core::create_graph_node_handle" (
179 cydriver.CUgraphNode node, const GraphHandle& h_graph) except+ nogil
180 GraphHandle graph_node_get_graph "cuda_core::graph_node_get_graph" (
181 const GraphNodeHandle& h) noexcept nogil
182 void invalidate_graph_node "cuda_core::invalidate_graph_node" (
183 const GraphNodeHandle& h) noexcept nogil
185 # Graphics resource handles
186 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::create_graphics_resource_handle" (
187 cydriver.CUgraphicsResource resource) except+ nogil
189 # NVRTC Program handles
190 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::create_nvrtc_program_handle" (
191 cynvrtc.nvrtcProgram prog) except+ nogil
192 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::create_nvrtc_program_handle_ref" (
193 cynvrtc.nvrtcProgram prog) except+ nogil
195 # NVVM Program handles
196 NvvmProgramHandle create_nvvm_program_handle "cuda_core::create_nvvm_program_handle" (
197 cynvvm.nvvmProgram prog) except+ nogil
198 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::create_nvvm_program_handle_ref" (
199 cynvvm.nvvmProgram prog) except+ nogil
201 # nvJitLink handles
202 NvJitLinkHandle create_nvjitlink_handle "cuda_core::create_nvjitlink_handle" (
203 cynvjitlink.nvJitLinkHandle handle) except+ nogil
204 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::create_nvjitlink_handle_ref" (
205 cynvjitlink.nvJitLinkHandle handle) except+ nogil
207 # cuLink handles
208 CuLinkHandle create_culink_handle "cuda_core::create_culink_handle" (
209 cydriver.CUlinkState state) except+ nogil
210 CuLinkHandle create_culink_handle_ref "cuda_core::create_culink_handle_ref" (
211 cydriver.CUlinkState state) except+ nogil
213 # File descriptor handles
214 FileDescriptorHandle create_fd_handle "cuda_core::create_fd_handle" (
215 int fd) except+ nogil
216 FileDescriptorHandle create_fd_handle_ref "cuda_core::create_fd_handle_ref" (
217 int fd) except+ nogil
219 # SM resource split (13.1+ wrapper — avoids direct cydriver cimport)
220 # groupParams is void* to avoid referencing CU_DEV_SM_RESOURCE_GROUP_PARAMS
221 # (which doesn't exist in cuda-bindings 13.0 .pxd). The C++ side casts it.
222 cydriver.CUresult sm_resource_split "cuda_core::sm_resource_split" (
223 cydriver.CUdevResource* result, unsigned int nbGroups,
224 const cydriver.CUdevResource* input, cydriver.CUdevResource* remainder,
225 unsigned int flags, void* groupParams) nogil
226 bint has_sm_resource_split "cuda_core::has_sm_resource_split" () noexcept nogil
228 # Array / mipmapped-array / texture / surface handles (PR #467)
229 OpaqueArrayHandle create_array_handle "cuda_core::create_array_handle" (
230 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil
231 OpaqueArrayHandle create_array_handle_ref "cuda_core::create_array_handle_ref" (
232 cydriver.CUarray arr) except+ nogil
233 OpaqueArrayHandle create_array_handle_owning "cuda_core::create_array_handle_owning" (
234 cydriver.CUarray arr) except+ nogil
235 OpaqueArrayHandle create_array_level_handle "cuda_core::create_array_level_handle" (
236 const MipmappedArrayHandle& h_mip, unsigned int level) except+ nogil
237 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::create_mipmapped_array_handle" (
238 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc, unsigned int num_levels) except+ nogil
239 TexObjectHandle create_tex_object_handle_array "cuda_core::create_tex_object_handle_array" (
240 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
241 const OpaqueArrayHandle& h_backing) except+ nogil
242 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::create_tex_object_handle_mipmap" (
243 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
244 const MipmappedArrayHandle& h_backing) except+ nogil
245 TexObjectHandle create_tex_object_handle_linear "cuda_core::create_tex_object_handle_linear" (
246 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex,
247 const DevicePtrHandle& h_backing) except+ nogil
248 SurfObjectHandle create_surf_object_handle "cuda_core::create_surf_object_handle" (
249 const cydriver.CUDA_RESOURCE_DESC& res, const OpaqueArrayHandle& h_backing) except+ nogil
252# =============================================================================
253# CUDA Driver API capsule
254#
255# This provides resolved CUDA driver function pointers to the C++ code.
256# =============================================================================
258cdef const char* _CUDA_DRIVER_API_V1_NAME = b"cuda.core._resource_handles._CUDA_DRIVER_API_V1"
261# =============================================================================
262# CUDA driver function pointer initialization
263#
264# The C++ code declares extern function pointers (p_cuXxx) that need to be
265# populated before any handle creation functions are called. We extract these
266# from cuda.bindings.cydriver.__pyx_capi__ at module import time.
267#
268# The Cython string substitution (e.g., "reinterpret_cast<void*&>(...)")
269# allows us to assign void* values to typed function pointer variables.
270# =============================================================================
272# Declare extern variables with reinterpret_cast to allow void* assignment
273cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core":
274 # Context
275 void* p_cuDevicePrimaryCtxRetain "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRetain)"
276 void* p_cuDevicePrimaryCtxRelease "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRelease)"
277 void* p_cuCtxGetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxGetCurrent)"
278 void* p_cuGreenCtxCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxCreate)"
279 void* p_cuGreenCtxDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxDestroy)"
280 void* p_cuCtxFromGreenCtx "reinterpret_cast<void*&>(cuda_core::p_cuCtxFromGreenCtx)"
281 void* p_cuDevResourceGenerateDesc "reinterpret_cast<void*&>(cuda_core::p_cuDevResourceGenerateDesc)"
282 void* p_cuGreenCtxStreamCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxStreamCreate)"
284 # Stream
285 void* p_cuStreamCreateWithPriority "reinterpret_cast<void*&>(cuda_core::p_cuStreamCreateWithPriority)"
286 void* p_cuStreamDestroy "reinterpret_cast<void*&>(cuda_core::p_cuStreamDestroy)"
288 # Event
289 void* p_cuEventCreate "reinterpret_cast<void*&>(cuda_core::p_cuEventCreate)"
290 void* p_cuEventDestroy "reinterpret_cast<void*&>(cuda_core::p_cuEventDestroy)"
291 void* p_cuIpcOpenEventHandle "reinterpret_cast<void*&>(cuda_core::p_cuIpcOpenEventHandle)"
293 # Device
294 void* p_cuDeviceGetCount "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetCount)"
296 # Memory pool
297 void* p_cuMemPoolSetAccess "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolSetAccess)"
298 void* p_cuMemPoolDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolDestroy)"
299 void* p_cuMemPoolCreate "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolCreate)"
300 void* p_cuDeviceGetMemPool "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetMemPool)"
301 void* p_cuMemPoolImportFromShareableHandle "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportFromShareableHandle)"
303 # Memory allocation
304 void* p_cuMemAllocFromPoolAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocFromPoolAsync)"
305 void* p_cuMemAllocAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocAsync)"
306 void* p_cuMemAlloc "reinterpret_cast<void*&>(cuda_core::p_cuMemAlloc)"
307 void* p_cuMemAllocHost "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocHost)"
309 # Memory deallocation
310 void* p_cuMemFreeAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeAsync)"
311 void* p_cuMemFree "reinterpret_cast<void*&>(cuda_core::p_cuMemFree)"
312 void* p_cuMemFreeHost "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeHost)"
314 # IPC
315 void* p_cuMemPoolImportPointer "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportPointer)"
317 # Library
318 void* p_cuLibraryLoadFromFile "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadFromFile)"
319 void* p_cuLibraryLoadData "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadData)"
320 void* p_cuLibraryUnload "reinterpret_cast<void*&>(cuda_core::p_cuLibraryUnload)"
321 void* p_cuLibraryGetKernel "reinterpret_cast<void*&>(cuda_core::p_cuLibraryGetKernel)"
323 # Graph
324 void* p_cuGraphDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphDestroy)"
325 void* p_cuGraphExecDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecDestroy)"
326 void* p_cuUserObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectCreate)"
327 void* p_cuUserObjectRelease "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectRelease)"
328 void* p_cuGraphRetainUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphRetainUserObject)"
329 void* p_cuGraphReleaseUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphReleaseUserObject)"
330 void* p_cuGraphNodeFindInClone "reinterpret_cast<void*&>(cuda_core::p_cuGraphNodeFindInClone)"
331 void* p_cuGraphChildGraphNodeGetGraph "reinterpret_cast<void*&>(cuda_core::p_cuGraphChildGraphNodeGetGraph)"
333 # Linker
334 void* p_cuLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_cuLinkDestroy)"
336 # Graphics interop
337 void* p_cuGraphicsUnmapResources "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnmapResources)"
338 void* p_cuGraphicsUnregisterResource "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnregisterResource)"
340 # Texture / surface / array (PR #467)
341 void* p_cuArray3DCreate "reinterpret_cast<void*&>(cuda_core::p_cuArray3DCreate)"
342 void* p_cuArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuArrayDestroy)"
343 void* p_cuMipmappedArrayCreate "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayCreate)"
344 void* p_cuMipmappedArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayDestroy)"
345 void* p_cuMipmappedArrayGetLevel "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayGetLevel)"
346 void* p_cuTexObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectCreate)"
347 void* p_cuTexObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectDestroy)"
348 void* p_cuSurfObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectCreate)"
349 void* p_cuSurfObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectDestroy)"
351 # SM resource split (13.1+)
352 void* p_cuDevSmResourceSplit "reinterpret_cast<void*&>(cuda_core::p_cuDevSmResourceSplit)"
354 # NVRTC
355 void* p_nvrtcDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvrtcDestroyProgram)"
357 # NVVM
358 void* p_nvvmDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvvmDestroyProgram)"
360 # nvJitLink
361 void* p_nvJitLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_nvJitLinkDestroy)"
364# Initialize driver function pointers from cydriver.__pyx_capi__ at module load
365cdef void* _get_driver_fn(str name):
366 capsule = cydriver.__pyx_capi__[name]
367 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
370cdef void* _get_optional_driver_fn(str name):
371 try:
372 capsule = cydriver.__pyx_capi__[name]
373 except KeyError:
374 return NULL
375 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
378cdef void _init_driver_fn_pointers() noexcept:
379 global p_cuDevicePrimaryCtxRetain, p_cuDevicePrimaryCtxRelease, p_cuCtxGetCurrent
380 global p_cuGreenCtxCreate, p_cuGreenCtxDestroy, p_cuCtxFromGreenCtx
381 global p_cuDevResourceGenerateDesc, p_cuGreenCtxStreamCreate
382 global p_cuStreamCreateWithPriority, p_cuStreamDestroy
383 global p_cuEventCreate, p_cuEventDestroy, p_cuIpcOpenEventHandle
384 global p_cuDeviceGetCount
385 global p_cuMemPoolSetAccess, p_cuMemPoolDestroy, p_cuMemPoolCreate
386 global p_cuDeviceGetMemPool, p_cuMemPoolImportFromShareableHandle
387 global p_cuMemAllocFromPoolAsync, p_cuMemAllocAsync, p_cuMemAlloc, p_cuMemAllocHost
388 global p_cuMemFreeAsync, p_cuMemFree, p_cuMemFreeHost
389 global p_cuMemPoolImportPointer
390 global p_cuLibraryLoadFromFile, p_cuLibraryLoadData, p_cuLibraryUnload, p_cuLibraryGetKernel
391 global p_cuGraphDestroy, p_cuGraphExecDestroy
392 global p_cuUserObjectCreate, p_cuUserObjectRelease
393 global p_cuGraphRetainUserObject, p_cuGraphReleaseUserObject
394 global p_cuGraphNodeFindInClone, p_cuGraphChildGraphNodeGetGraph
395 global p_cuLinkDestroy
396 global p_cuGraphicsUnmapResources, p_cuGraphicsUnregisterResource
397 global p_cuDevSmResourceSplit
398 global p_cuArray3DCreate, p_cuArrayDestroy
399 global p_cuMipmappedArrayCreate, p_cuMipmappedArrayDestroy, p_cuMipmappedArrayGetLevel
400 global p_cuTexObjectCreate, p_cuTexObjectDestroy
401 global p_cuSurfObjectCreate, p_cuSurfObjectDestroy
403 # Context
404 p_cuDevicePrimaryCtxRetain = _get_driver_fn("cuDevicePrimaryCtxRetain")
405 p_cuDevicePrimaryCtxRelease = _get_driver_fn("cuDevicePrimaryCtxRelease")
406 p_cuCtxGetCurrent = _get_driver_fn("cuCtxGetCurrent")
407 p_cuGreenCtxCreate = _get_optional_driver_fn("cuGreenCtxCreate")
408 p_cuGreenCtxDestroy = _get_optional_driver_fn("cuGreenCtxDestroy")
409 p_cuCtxFromGreenCtx = _get_optional_driver_fn("cuCtxFromGreenCtx")
410 p_cuDevResourceGenerateDesc = _get_optional_driver_fn("cuDevResourceGenerateDesc")
411 p_cuGreenCtxStreamCreate = _get_optional_driver_fn("cuGreenCtxStreamCreate")
413 # Stream
414 p_cuStreamCreateWithPriority = _get_driver_fn("cuStreamCreateWithPriority")
415 p_cuStreamDestroy = _get_driver_fn("cuStreamDestroy")
417 # Event
418 p_cuEventCreate = _get_driver_fn("cuEventCreate")
419 p_cuEventDestroy = _get_driver_fn("cuEventDestroy")
420 p_cuIpcOpenEventHandle = _get_driver_fn("cuIpcOpenEventHandle")
422 # Device
423 p_cuDeviceGetCount = _get_driver_fn("cuDeviceGetCount")
425 # Memory pool
426 p_cuMemPoolSetAccess = _get_driver_fn("cuMemPoolSetAccess")
427 p_cuMemPoolDestroy = _get_driver_fn("cuMemPoolDestroy")
428 p_cuMemPoolCreate = _get_driver_fn("cuMemPoolCreate")
429 p_cuDeviceGetMemPool = _get_driver_fn("cuDeviceGetMemPool")
430 p_cuMemPoolImportFromShareableHandle = _get_driver_fn("cuMemPoolImportFromShareableHandle")
432 # Memory allocation
433 p_cuMemAllocFromPoolAsync = _get_driver_fn("cuMemAllocFromPoolAsync")
434 p_cuMemAllocAsync = _get_driver_fn("cuMemAllocAsync")
435 p_cuMemAlloc = _get_driver_fn("cuMemAlloc")
436 p_cuMemAllocHost = _get_driver_fn("cuMemAllocHost")
438 # Memory deallocation
439 p_cuMemFreeAsync = _get_driver_fn("cuMemFreeAsync")
440 p_cuMemFree = _get_driver_fn("cuMemFree")
441 p_cuMemFreeHost = _get_driver_fn("cuMemFreeHost")
443 # IPC
444 p_cuMemPoolImportPointer = _get_driver_fn("cuMemPoolImportPointer")
446 # Library
447 p_cuLibraryLoadFromFile = _get_driver_fn("cuLibraryLoadFromFile")
448 p_cuLibraryLoadData = _get_driver_fn("cuLibraryLoadData")
449 p_cuLibraryUnload = _get_driver_fn("cuLibraryUnload")
450 p_cuLibraryGetKernel = _get_driver_fn("cuLibraryGetKernel")
452 # Graph
453 p_cuGraphDestroy = _get_driver_fn("cuGraphDestroy")
454 p_cuGraphExecDestroy = _get_driver_fn("cuGraphExecDestroy")
455 p_cuUserObjectCreate = _get_driver_fn("cuUserObjectCreate")
456 p_cuUserObjectRelease = _get_driver_fn("cuUserObjectRelease")
457 p_cuGraphRetainUserObject = _get_driver_fn("cuGraphRetainUserObject")
458 p_cuGraphReleaseUserObject = _get_driver_fn("cuGraphReleaseUserObject")
459 p_cuGraphNodeFindInClone = _get_driver_fn("cuGraphNodeFindInClone")
460 p_cuGraphChildGraphNodeGetGraph = _get_driver_fn("cuGraphChildGraphNodeGetGraph")
462 # Linker
463 p_cuLinkDestroy = _get_driver_fn("cuLinkDestroy")
465 # Graphics interop
466 p_cuGraphicsUnmapResources = _get_driver_fn("cuGraphicsUnmapResources")
467 p_cuGraphicsUnregisterResource = _get_driver_fn("cuGraphicsUnregisterResource")
469 # Texture / surface / array (PR #467)
470 p_cuArray3DCreate = _get_driver_fn("cuArray3DCreate")
471 p_cuArrayDestroy = _get_driver_fn("cuArrayDestroy")
472 p_cuMipmappedArrayCreate = _get_driver_fn("cuMipmappedArrayCreate")
473 p_cuMipmappedArrayDestroy = _get_driver_fn("cuMipmappedArrayDestroy")
474 p_cuMipmappedArrayGetLevel = _get_driver_fn("cuMipmappedArrayGetLevel")
475 p_cuTexObjectCreate = _get_driver_fn("cuTexObjectCreate")
476 p_cuTexObjectDestroy = _get_driver_fn("cuTexObjectDestroy")
477 p_cuSurfObjectCreate = _get_driver_fn("cuSurfObjectCreate")
478 p_cuSurfObjectDestroy = _get_driver_fn("cuSurfObjectDestroy")
480 # SM resource split (13.1+ — may not exist in older cuda-bindings)
481 p_cuDevSmResourceSplit = _get_optional_driver_fn("cuDevSmResourceSplit")
483_init_driver_fn_pointers()
484initialize_deferred_cleanup()
486# =============================================================================
487# NVRTC function pointer initialization
488# =============================================================================
490cdef void* _get_nvrtc_fn(str name):
491 capsule = cynvrtc.__pyx_capi__[name]
492 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
494cdef void _init_nvrtc_fn_pointers() noexcept:
495 global p_nvrtcDestroyProgram
496 p_nvrtcDestroyProgram = _get_nvrtc_fn("nvrtcDestroyProgram")
498_init_nvrtc_fn_pointers()
500# =============================================================================
501# NVVM function pointer initialization
502#
503# NVVM may not be available at runtime, so we handle missing function pointers
504# gracefully. The C++ deleter checks for null before calling.
505# =============================================================================
507cdef void* _get_nvvm_fn(str name):
508 capsule = cynvvm.__pyx_capi__[name]
509 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
511cdef void _init_nvvm_fn_pointers() noexcept:
512 global p_nvvmDestroyProgram
513 p_nvvmDestroyProgram = _get_nvvm_fn("nvvmDestroyProgram")
515_init_nvvm_fn_pointers()
517# =============================================================================
518# nvJitLink function pointer initialization
519#
520# nvJitLink may not be available at runtime, so we handle missing function
521# pointers gracefully. The C++ deleter checks for null before calling.
522# =============================================================================
524cdef void* _get_nvjitlink_fn(str name):
525 capsule = cynvjitlink.__pyx_capi__[name]
526 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule))
528cdef void _init_nvjitlink_fn_pointers() noexcept:
529 global p_nvJitLinkDestroy
530 p_nvJitLinkDestroy = _get_nvjitlink_fn("nvJitLinkDestroy")
532_init_nvjitlink_fn_pointers()