Coverage for cuda/core/_resource_handles.pyx: 97.65%

85 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-19 01:12 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4  

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. 

12  

13from cpython.pycapsule cimport PyCapsule_GetName, PyCapsule_GetPointer 

14from libc.stddef cimport size_t 

15  

16from cuda.bindings cimport cydriver 

17from cuda.bindings cimport cynvrtc 

18from cuda.bindings cimport cynvvm 

19from cuda.bindings cimport cynvjitlink 

20  

21import cuda.bindings.cydriver as cydriver 

22import cuda.bindings.cynvrtc as cynvrtc 

23import cuda.bindings.cynvvm as cynvvm 

24import cuda.bindings.cynvjitlink as cynvjitlink 

25  

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# ============================================================================= 

32  

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 

38  

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  

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 ContextHandle get_stream_context "cuda_core::get_stream_context" ( 

66 const StreamHandle& h) noexcept nogil 

67 StreamHandle get_legacy_stream "cuda_core::get_legacy_stream" () except+ nogil 

68 StreamHandle get_per_thread_stream "cuda_core::get_per_thread_stream" () except+ nogil 

69  

70 # Event handles (note: _create_event_handle* are internal due to C++ overloading) 

71 EventHandle create_event_handle "cuda_core::create_event_handle" ( 

72 const ContextHandle& h_ctx, unsigned int flags, 

73 bint timing_enabled, bint is_blocking_sync, 

74 bint ipc_enabled, int device_id) except+ nogil 

75 EventHandle create_event_handle_noctx "cuda_core::create_event_handle_noctx" ( 

76 unsigned int flags) except+ nogil 

77 EventHandle create_event_handle_ref "cuda_core::create_event_handle_ref" ( 

78 cydriver.CUevent event) except+ nogil 

79 EventHandle create_event_handle_ipc "cuda_core::create_event_handle_ipc" ( 

80 const cydriver.CUipcEventHandle& ipc_handle, bint is_blocking_sync) except+ nogil 

81  

82 # Event metadata getters 

83 bint get_event_timing_enabled "cuda_core::get_event_timing_enabled" ( 

84 const EventHandle& h) noexcept nogil 

85 bint get_event_is_blocking_sync "cuda_core::get_event_is_blocking_sync" ( 

86 const EventHandle& h) noexcept nogil 

87 bint get_event_ipc_enabled "cuda_core::get_event_ipc_enabled" ( 

88 const EventHandle& h) noexcept nogil 

89 int get_event_device_id "cuda_core::get_event_device_id" ( 

90 const EventHandle& h) noexcept nogil 

91 ContextHandle get_event_context "cuda_core::get_event_context" ( 

92 const EventHandle& h) noexcept nogil 

93  

94 # Memory pool handles 

95 MemoryPoolHandle create_mempool_handle "cuda_core::create_mempool_handle" ( 

96 const cydriver.CUmemPoolProps& props) except+ nogil 

97 MemoryPoolHandle create_mempool_handle_ref "cuda_core::create_mempool_handle_ref" ( 

98 cydriver.CUmemoryPool pool) except+ nogil 

99 MemoryPoolHandle get_device_mempool "cuda_core::get_device_mempool" ( 

100 int device_id) except+ nogil 

101 MemoryPoolHandle create_mempool_handle_ipc "cuda_core::create_mempool_handle_ipc" ( 

102 int fd, cydriver.CUmemAllocationHandleType handle_type) except+ nogil 

103  

104 # Device pointer handles 

105 DevicePtrHandle deviceptr_alloc_from_pool "cuda_core::deviceptr_alloc_from_pool" ( 

106 size_t size, const MemoryPoolHandle& h_pool, const StreamHandle& h_stream) except+ nogil 

107 DevicePtrHandle deviceptr_alloc_async "cuda_core::deviceptr_alloc_async" ( 

108 size_t size, const StreamHandle& h_stream) except+ nogil 

109 DevicePtrHandle deviceptr_alloc "cuda_core::deviceptr_alloc" (size_t size) except+ nogil 

110 DevicePtrHandle deviceptr_alloc_host "cuda_core::deviceptr_alloc_host" (size_t size) except+ nogil 

111 DevicePtrHandle deviceptr_create_ref "cuda_core::deviceptr_create_ref" ( 

112 cydriver.CUdeviceptr ptr) except+ nogil 

113 DevicePtrHandle deviceptr_create_with_owner "cuda_core::deviceptr_create_with_owner" ( 

114 cydriver.CUdeviceptr ptr, object owner) except+ nogil 

115 DevicePtrHandle deviceptr_create_mapped_graphics "cuda_core::deviceptr_create_mapped_graphics" ( 

116 cydriver.CUdeviceptr ptr, 

117 const GraphicsResourceHandle& h_resource, 

118 const StreamHandle& h_stream) except+ nogil 

119  

120 # MR deallocation callback 

121 void register_mr_dealloc_callback "cuda_core::register_mr_dealloc_callback" ( 

122 MRDeallocCallback cb) noexcept 

123 DevicePtrHandle deviceptr_create_with_mr "cuda_core::deviceptr_create_with_mr" ( 

124 cydriver.CUdeviceptr ptr, size_t size, object mr) except+ nogil 

125  

126 DevicePtrHandle deviceptr_import_ipc "cuda_core::deviceptr_import_ipc" ( 

127 const MemoryPoolHandle& h_pool, const void* export_data, const StreamHandle& h_stream) except+ nogil 

128 StreamHandle deallocation_stream "cuda_core::deallocation_stream" ( 

129 const DevicePtrHandle& h) noexcept nogil 

130 void set_deallocation_stream "cuda_core::set_deallocation_stream" ( 

131 const DevicePtrHandle& h, const StreamHandle& h_stream) noexcept nogil 

132  

133 # Library handles 

134 LibraryHandle create_library_handle_from_file "cuda_core::create_library_handle_from_file" ( 

135 const char* path) except+ nogil 

136 LibraryHandle create_library_handle_from_data "cuda_core::create_library_handle_from_data" ( 

137 const void* data) except+ nogil 

138 LibraryHandle create_library_handle_ref "cuda_core::create_library_handle_ref" ( 

139 cydriver.CUlibrary library) except+ nogil 

140  

141 # Kernel handles 

142 KernelHandle create_kernel_handle "cuda_core::create_kernel_handle" ( 

143 const LibraryHandle& h_library, const char* name) except+ nogil 

144 KernelHandle create_kernel_handle_ref "cuda_core::create_kernel_handle_ref" ( 

145 cydriver.CUkernel kernel) except+ nogil 

146 LibraryHandle get_kernel_library "cuda_core::get_kernel_library" ( 

147 const KernelHandle& h) noexcept nogil 

148  

149 # Graph handles 

150 GraphHandle create_graph_handle "cuda_core::create_graph_handle" ( 

151 cydriver.CUgraph graph) except+ nogil 

152 GraphHandle create_graph_handle_ref "cuda_core::create_graph_handle_ref" ( 

153 cydriver.CUgraph graph, const GraphHandle& h_parent) except+ nogil 

154  

155 # Graph slot attachments 

156 OpaqueHandle make_opaque_py "cuda_core::make_opaque_py" (object obj) except+ 

157 OpaqueHandle make_opaque_malloc "cuda_core::make_opaque_malloc" (void* buf) except+ 

158 cydriver.CUresult graph_set_slot "cuda_core::graph_set_slot" ( 

159 const GraphHandle& h_graph, cydriver.CUgraphNode node, 

160 unsigned int slot, OpaqueHandle owner) except+ 

161  

162 # Graph exec handles 

163 GraphExecHandle create_graph_exec_handle "cuda_core::create_graph_exec_handle" ( 

164 cydriver.CUgraphExec graph_exec) except+ nogil 

165  

166 # Graph node handles 

167 GraphNodeHandle create_graph_node_handle "cuda_core::create_graph_node_handle" ( 

168 cydriver.CUgraphNode node, const GraphHandle& h_graph) except+ nogil 

169 GraphHandle graph_node_get_graph "cuda_core::graph_node_get_graph" ( 

170 const GraphNodeHandle& h) noexcept nogil 

171 void invalidate_graph_node "cuda_core::invalidate_graph_node" ( 

172 const GraphNodeHandle& h) noexcept nogil 

173  

174 # Graphics resource handles 

175 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::create_graphics_resource_handle" ( 

176 cydriver.CUgraphicsResource resource) except+ nogil 

177  

178 # NVRTC Program handles 

179 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::create_nvrtc_program_handle" ( 

180 cynvrtc.nvrtcProgram prog) except+ nogil 

181 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::create_nvrtc_program_handle_ref" ( 

182 cynvrtc.nvrtcProgram prog) except+ nogil 

183  

184 # NVVM Program handles 

185 NvvmProgramHandle create_nvvm_program_handle "cuda_core::create_nvvm_program_handle" ( 

186 cynvvm.nvvmProgram prog) except+ nogil 

187 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::create_nvvm_program_handle_ref" ( 

188 cynvvm.nvvmProgram prog) except+ nogil 

189  

190 # nvJitLink handles 

191 NvJitLinkHandle create_nvjitlink_handle "cuda_core::create_nvjitlink_handle" ( 

192 cynvjitlink.nvJitLinkHandle handle) except+ nogil 

193 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::create_nvjitlink_handle_ref" ( 

194 cynvjitlink.nvJitLinkHandle handle) except+ nogil 

195  

196 # cuLink handles 

197 CuLinkHandle create_culink_handle "cuda_core::create_culink_handle" ( 

198 cydriver.CUlinkState state) except+ nogil 

199 CuLinkHandle create_culink_handle_ref "cuda_core::create_culink_handle_ref" ( 

200 cydriver.CUlinkState state) except+ nogil 

201  

202 # File descriptor handles 

203 FileDescriptorHandle create_fd_handle "cuda_core::create_fd_handle" ( 

204 int fd) except+ nogil 

205 FileDescriptorHandle create_fd_handle_ref "cuda_core::create_fd_handle_ref" ( 

206 int fd) except+ nogil 

207  

208 # SM resource split (13.1+ wrapper — avoids direct cydriver cimport) 

209 # groupParams is void* to avoid referencing CU_DEV_SM_RESOURCE_GROUP_PARAMS 

210 # (which doesn't exist in cuda-bindings 13.0 .pxd). The C++ side casts it. 

211 cydriver.CUresult sm_resource_split "cuda_core::sm_resource_split" ( 

212 cydriver.CUdevResource* result, unsigned int nbGroups, 

213 const cydriver.CUdevResource* input, cydriver.CUdevResource* remainder, 

214 unsigned int flags, void* groupParams) nogil 

215 bint has_sm_resource_split "cuda_core::has_sm_resource_split" () noexcept nogil 

216  

217 # Array / mipmapped-array / texture / surface handles (PR #467) 

218 OpaqueArrayHandle create_array_handle "cuda_core::create_array_handle" ( 

219 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil 

220 OpaqueArrayHandle create_array_handle_ref "cuda_core::create_array_handle_ref" ( 

221 cydriver.CUarray arr) except+ nogil 

222 OpaqueArrayHandle create_array_handle_owning "cuda_core::create_array_handle_owning" ( 

223 cydriver.CUarray arr) except+ nogil 

224 OpaqueArrayHandle create_array_level_handle "cuda_core::create_array_level_handle" ( 

225 const MipmappedArrayHandle& h_mip, unsigned int level) except+ nogil 

226 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::create_mipmapped_array_handle" ( 

227 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc, unsigned int num_levels) except+ nogil 

228 TexObjectHandle create_tex_object_handle_array "cuda_core::create_tex_object_handle_array" ( 

229 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex, 

230 const OpaqueArrayHandle& h_backing) except+ nogil 

231 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::create_tex_object_handle_mipmap" ( 

232 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex, 

233 const MipmappedArrayHandle& h_backing) except+ nogil 

234 TexObjectHandle create_tex_object_handle_linear "cuda_core::create_tex_object_handle_linear" ( 

235 const cydriver.CUDA_RESOURCE_DESC& res, const cydriver.CUDA_TEXTURE_DESC& tex, 

236 const DevicePtrHandle& h_backing) except+ nogil 

237 SurfObjectHandle create_surf_object_handle "cuda_core::create_surf_object_handle" ( 

238 const cydriver.CUDA_RESOURCE_DESC& res, const OpaqueArrayHandle& h_backing) except+ nogil 

239  

240  

241# ============================================================================= 

242# CUDA Driver API capsule 

243# 

244# This provides resolved CUDA driver function pointers to the C++ code. 

245# ============================================================================= 

246  

247cdef const char* _CUDA_DRIVER_API_V1_NAME = b"cuda.core._resource_handles._CUDA_DRIVER_API_V1" 

248  

249  

250# ============================================================================= 

251# CUDA driver function pointer initialization 

252# 

253# The C++ code declares extern function pointers (p_cuXxx) that need to be 

254# populated before any handle creation functions are called. We extract these 

255# from cuda.bindings.cydriver.__pyx_capi__ at module import time. 

256# 

257# The Cython string substitution (e.g., "reinterpret_cast<void*&>(...)") 

258# allows us to assign void* values to typed function pointer variables. 

259# ============================================================================= 

260  

261# Declare extern variables with reinterpret_cast to allow void* assignment 

262cdef extern from "_cpp/resource_handles.hpp" namespace "cuda_core": 

263 # Context 

264 void* p_cuDevicePrimaryCtxRetain "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRetain)" 

265 void* p_cuDevicePrimaryCtxRelease "reinterpret_cast<void*&>(cuda_core::p_cuDevicePrimaryCtxRelease)" 

266 void* p_cuCtxGetCurrent "reinterpret_cast<void*&>(cuda_core::p_cuCtxGetCurrent)" 

267 void* p_cuGreenCtxCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxCreate)" 

268 void* p_cuGreenCtxDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxDestroy)" 

269 void* p_cuCtxFromGreenCtx "reinterpret_cast<void*&>(cuda_core::p_cuCtxFromGreenCtx)" 

270 void* p_cuDevResourceGenerateDesc "reinterpret_cast<void*&>(cuda_core::p_cuDevResourceGenerateDesc)" 

271 void* p_cuGreenCtxStreamCreate "reinterpret_cast<void*&>(cuda_core::p_cuGreenCtxStreamCreate)" 

272  

273 # Stream 

274 void* p_cuStreamCreateWithPriority "reinterpret_cast<void*&>(cuda_core::p_cuStreamCreateWithPriority)" 

275 void* p_cuStreamDestroy "reinterpret_cast<void*&>(cuda_core::p_cuStreamDestroy)" 

276  

277 # Event 

278 void* p_cuEventCreate "reinterpret_cast<void*&>(cuda_core::p_cuEventCreate)" 

279 void* p_cuEventDestroy "reinterpret_cast<void*&>(cuda_core::p_cuEventDestroy)" 

280 void* p_cuIpcOpenEventHandle "reinterpret_cast<void*&>(cuda_core::p_cuIpcOpenEventHandle)" 

281  

282 # Device 

283 void* p_cuDeviceGetCount "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetCount)" 

284  

285 # Memory pool 

286 void* p_cuMemPoolSetAccess "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolSetAccess)" 

287 void* p_cuMemPoolDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolDestroy)" 

288 void* p_cuMemPoolCreate "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolCreate)" 

289 void* p_cuDeviceGetMemPool "reinterpret_cast<void*&>(cuda_core::p_cuDeviceGetMemPool)" 

290 void* p_cuMemPoolImportFromShareableHandle "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportFromShareableHandle)" 

291  

292 # Memory allocation 

293 void* p_cuMemAllocFromPoolAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocFromPoolAsync)" 

294 void* p_cuMemAllocAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocAsync)" 

295 void* p_cuMemAlloc "reinterpret_cast<void*&>(cuda_core::p_cuMemAlloc)" 

296 void* p_cuMemAllocHost "reinterpret_cast<void*&>(cuda_core::p_cuMemAllocHost)" 

297  

298 # Memory deallocation 

299 void* p_cuMemFreeAsync "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeAsync)" 

300 void* p_cuMemFree "reinterpret_cast<void*&>(cuda_core::p_cuMemFree)" 

301 void* p_cuMemFreeHost "reinterpret_cast<void*&>(cuda_core::p_cuMemFreeHost)" 

302  

303 # IPC 

304 void* p_cuMemPoolImportPointer "reinterpret_cast<void*&>(cuda_core::p_cuMemPoolImportPointer)" 

305  

306 # Library 

307 void* p_cuLibraryLoadFromFile "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadFromFile)" 

308 void* p_cuLibraryLoadData "reinterpret_cast<void*&>(cuda_core::p_cuLibraryLoadData)" 

309 void* p_cuLibraryUnload "reinterpret_cast<void*&>(cuda_core::p_cuLibraryUnload)" 

310 void* p_cuLibraryGetKernel "reinterpret_cast<void*&>(cuda_core::p_cuLibraryGetKernel)" 

311  

312 # Graph 

313 void* p_cuGraphDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphDestroy)" 

314 void* p_cuGraphExecDestroy "reinterpret_cast<void*&>(cuda_core::p_cuGraphExecDestroy)" 

315 void* p_cuUserObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectCreate)" 

316 void* p_cuUserObjectRelease "reinterpret_cast<void*&>(cuda_core::p_cuUserObjectRelease)" 

317 void* p_cuGraphRetainUserObject "reinterpret_cast<void*&>(cuda_core::p_cuGraphRetainUserObject)" 

318  

319 # Linker 

320 void* p_cuLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_cuLinkDestroy)" 

321  

322 # Graphics interop 

323 void* p_cuGraphicsUnmapResources "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnmapResources)" 

324 void* p_cuGraphicsUnregisterResource "reinterpret_cast<void*&>(cuda_core::p_cuGraphicsUnregisterResource)" 

325  

326 # Texture / surface / array (PR #467) 

327 void* p_cuArray3DCreate "reinterpret_cast<void*&>(cuda_core::p_cuArray3DCreate)" 

328 void* p_cuArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuArrayDestroy)" 

329 void* p_cuMipmappedArrayCreate "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayCreate)" 

330 void* p_cuMipmappedArrayDestroy "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayDestroy)" 

331 void* p_cuMipmappedArrayGetLevel "reinterpret_cast<void*&>(cuda_core::p_cuMipmappedArrayGetLevel)" 

332 void* p_cuTexObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectCreate)" 

333 void* p_cuTexObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuTexObjectDestroy)" 

334 void* p_cuSurfObjectCreate "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectCreate)" 

335 void* p_cuSurfObjectDestroy "reinterpret_cast<void*&>(cuda_core::p_cuSurfObjectDestroy)" 

336  

337 # SM resource split (13.1+) 

338 void* p_cuDevSmResourceSplit "reinterpret_cast<void*&>(cuda_core::p_cuDevSmResourceSplit)" 

339  

340 # NVRTC 

341 void* p_nvrtcDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvrtcDestroyProgram)" 

342  

343 # NVVM 

344 void* p_nvvmDestroyProgram "reinterpret_cast<void*&>(cuda_core::p_nvvmDestroyProgram)" 

345  

346 # nvJitLink 

347 void* p_nvJitLinkDestroy "reinterpret_cast<void*&>(cuda_core::p_nvJitLinkDestroy)" 

348  

349  

350# Initialize driver function pointers from cydriver.__pyx_capi__ at module load 

351cdef void* _get_driver_fn(str name): 

352 capsule = cydriver.__pyx_capi__[name] 

353 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

354  

355  

356cdef void* _get_optional_driver_fn(str name): 

357 try: 

358 capsule = cydriver.__pyx_capi__[name] 

359 except KeyError: 

360 return NULL 

361 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

362  

363  

364cdef void _init_driver_fn_pointers() noexcept: 

365 global p_cuDevicePrimaryCtxRetain, p_cuDevicePrimaryCtxRelease, p_cuCtxGetCurrent 

366 global p_cuGreenCtxCreate, p_cuGreenCtxDestroy, p_cuCtxFromGreenCtx 

367 global p_cuDevResourceGenerateDesc, p_cuGreenCtxStreamCreate 

368 global p_cuStreamCreateWithPriority, p_cuStreamDestroy 

369 global p_cuEventCreate, p_cuEventDestroy, p_cuIpcOpenEventHandle 

370 global p_cuDeviceGetCount 

371 global p_cuMemPoolSetAccess, p_cuMemPoolDestroy, p_cuMemPoolCreate 

372 global p_cuDeviceGetMemPool, p_cuMemPoolImportFromShareableHandle 

373 global p_cuMemAllocFromPoolAsync, p_cuMemAllocAsync, p_cuMemAlloc, p_cuMemAllocHost 

374 global p_cuMemFreeAsync, p_cuMemFree, p_cuMemFreeHost 

375 global p_cuMemPoolImportPointer 

376 global p_cuLibraryLoadFromFile, p_cuLibraryLoadData, p_cuLibraryUnload, p_cuLibraryGetKernel 

377 global p_cuGraphDestroy, p_cuGraphExecDestroy 

378 global p_cuUserObjectCreate, p_cuUserObjectRelease, p_cuGraphRetainUserObject 

379 global p_cuLinkDestroy 

380 global p_cuGraphicsUnmapResources, p_cuGraphicsUnregisterResource 

381 global p_cuDevSmResourceSplit 

382 global p_cuArray3DCreate, p_cuArrayDestroy 

383 global p_cuMipmappedArrayCreate, p_cuMipmappedArrayDestroy, p_cuMipmappedArrayGetLevel 

384 global p_cuTexObjectCreate, p_cuTexObjectDestroy 

385 global p_cuSurfObjectCreate, p_cuSurfObjectDestroy 

386  

387 # Context 

388 p_cuDevicePrimaryCtxRetain = _get_driver_fn("cuDevicePrimaryCtxRetain") 

389 p_cuDevicePrimaryCtxRelease = _get_driver_fn("cuDevicePrimaryCtxRelease") 

390 p_cuCtxGetCurrent = _get_driver_fn("cuCtxGetCurrent") 

391 p_cuGreenCtxCreate = _get_optional_driver_fn("cuGreenCtxCreate") 

392 p_cuGreenCtxDestroy = _get_optional_driver_fn("cuGreenCtxDestroy") 

393 p_cuCtxFromGreenCtx = _get_optional_driver_fn("cuCtxFromGreenCtx") 

394 p_cuDevResourceGenerateDesc = _get_optional_driver_fn("cuDevResourceGenerateDesc") 

395 p_cuGreenCtxStreamCreate = _get_optional_driver_fn("cuGreenCtxStreamCreate") 

396  

397 # Stream 

398 p_cuStreamCreateWithPriority = _get_driver_fn("cuStreamCreateWithPriority") 

399 p_cuStreamDestroy = _get_driver_fn("cuStreamDestroy") 

400  

401 # Event 

402 p_cuEventCreate = _get_driver_fn("cuEventCreate") 

403 p_cuEventDestroy = _get_driver_fn("cuEventDestroy") 

404 p_cuIpcOpenEventHandle = _get_driver_fn("cuIpcOpenEventHandle") 

405  

406 # Device 

407 p_cuDeviceGetCount = _get_driver_fn("cuDeviceGetCount") 

408  

409 # Memory pool 

410 p_cuMemPoolSetAccess = _get_driver_fn("cuMemPoolSetAccess") 

411 p_cuMemPoolDestroy = _get_driver_fn("cuMemPoolDestroy") 

412 p_cuMemPoolCreate = _get_driver_fn("cuMemPoolCreate") 

413 p_cuDeviceGetMemPool = _get_driver_fn("cuDeviceGetMemPool") 

414 p_cuMemPoolImportFromShareableHandle = _get_driver_fn("cuMemPoolImportFromShareableHandle") 

415  

416 # Memory allocation 

417 p_cuMemAllocFromPoolAsync = _get_driver_fn("cuMemAllocFromPoolAsync") 

418 p_cuMemAllocAsync = _get_driver_fn("cuMemAllocAsync") 

419 p_cuMemAlloc = _get_driver_fn("cuMemAlloc") 

420 p_cuMemAllocHost = _get_driver_fn("cuMemAllocHost") 

421  

422 # Memory deallocation 

423 p_cuMemFreeAsync = _get_driver_fn("cuMemFreeAsync") 

424 p_cuMemFree = _get_driver_fn("cuMemFree") 

425 p_cuMemFreeHost = _get_driver_fn("cuMemFreeHost") 

426  

427 # IPC 

428 p_cuMemPoolImportPointer = _get_driver_fn("cuMemPoolImportPointer") 

429  

430 # Library 

431 p_cuLibraryLoadFromFile = _get_driver_fn("cuLibraryLoadFromFile") 

432 p_cuLibraryLoadData = _get_driver_fn("cuLibraryLoadData") 

433 p_cuLibraryUnload = _get_driver_fn("cuLibraryUnload") 

434 p_cuLibraryGetKernel = _get_driver_fn("cuLibraryGetKernel") 

435  

436 # Graph 

437 p_cuGraphDestroy = _get_driver_fn("cuGraphDestroy") 

438 p_cuGraphExecDestroy = _get_driver_fn("cuGraphExecDestroy") 

439 p_cuUserObjectCreate = _get_driver_fn("cuUserObjectCreate") 

440 p_cuUserObjectRelease = _get_driver_fn("cuUserObjectRelease") 

441 p_cuGraphRetainUserObject = _get_driver_fn("cuGraphRetainUserObject") 

442  

443 # Linker 

444 p_cuLinkDestroy = _get_driver_fn("cuLinkDestroy") 

445  

446 # Graphics interop 

447 p_cuGraphicsUnmapResources = _get_driver_fn("cuGraphicsUnmapResources") 

448 p_cuGraphicsUnregisterResource = _get_driver_fn("cuGraphicsUnregisterResource") 

449  

450 # Texture / surface / array (PR #467) 

451 p_cuArray3DCreate = _get_driver_fn("cuArray3DCreate") 

452 p_cuArrayDestroy = _get_driver_fn("cuArrayDestroy") 

453 p_cuMipmappedArrayCreate = _get_driver_fn("cuMipmappedArrayCreate") 

454 p_cuMipmappedArrayDestroy = _get_driver_fn("cuMipmappedArrayDestroy") 

455 p_cuMipmappedArrayGetLevel = _get_driver_fn("cuMipmappedArrayGetLevel") 

456 p_cuTexObjectCreate = _get_driver_fn("cuTexObjectCreate") 

457 p_cuTexObjectDestroy = _get_driver_fn("cuTexObjectDestroy") 

458 p_cuSurfObjectCreate = _get_driver_fn("cuSurfObjectCreate") 

459 p_cuSurfObjectDestroy = _get_driver_fn("cuSurfObjectDestroy") 

460  

461 # SM resource split (13.1+ — may not exist in older cuda-bindings) 

462 p_cuDevSmResourceSplit = _get_optional_driver_fn("cuDevSmResourceSplit") 

463  

464_init_driver_fn_pointers() 

465initialize_deferred_cleanup() 

466  

467# ============================================================================= 

468# NVRTC function pointer initialization 

469# ============================================================================= 

470  

471cdef void* _get_nvrtc_fn(str name): 

472 capsule = cynvrtc.__pyx_capi__[name] 

473 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

474  

475cdef void _init_nvrtc_fn_pointers() noexcept: 

476 global p_nvrtcDestroyProgram 

477 p_nvrtcDestroyProgram = _get_nvrtc_fn("nvrtcDestroyProgram") 

478  

479_init_nvrtc_fn_pointers() 

480  

481# ============================================================================= 

482# NVVM function pointer initialization 

483# 

484# NVVM may not be available at runtime, so we handle missing function pointers 

485# gracefully. The C++ deleter checks for null before calling. 

486# ============================================================================= 

487  

488cdef void* _get_nvvm_fn(str name): 

489 capsule = cynvvm.__pyx_capi__[name] 

490 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

491  

492cdef void _init_nvvm_fn_pointers() noexcept: 

493 global p_nvvmDestroyProgram 

494 p_nvvmDestroyProgram = _get_nvvm_fn("nvvmDestroyProgram") 

495  

496_init_nvvm_fn_pointers() 

497  

498# ============================================================================= 

499# nvJitLink function pointer initialization 

500# 

501# nvJitLink may not be available at runtime, so we handle missing function 

502# pointers gracefully. The C++ deleter checks for null before calling. 

503# ============================================================================= 

504  

505cdef void* _get_nvjitlink_fn(str name): 

506 capsule = cynvjitlink.__pyx_capi__[name] 

507 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

508  

509cdef void _init_nvjitlink_fn_pointers() noexcept: 

510 global p_nvJitLinkDestroy 

511 p_nvJitLinkDestroy = _get_nvjitlink_fn("nvJitLinkDestroy") 

512  

513_init_nvjitlink_fn_pointers()