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

81 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-03 01:38 +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 ContextHandle get_stream_context "cuda_core::get_stream_context" ( 

65 const StreamHandle& h) noexcept nogil 

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

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

68  

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

70 EventHandle create_event_handle "cuda_core::create_event_handle" ( 

71 const ContextHandle& h_ctx, unsigned int flags, 

72 bint timing_enabled, bint is_blocking_sync, 

73 bint ipc_enabled, int device_id) except+ nogil 

74 EventHandle create_event_handle_noctx "cuda_core::create_event_handle_noctx" ( 

75 unsigned int flags) except+ nogil 

76 EventHandle create_event_handle_ref "cuda_core::create_event_handle_ref" ( 

77 cydriver.CUevent event) except+ nogil 

78 EventHandle create_event_handle_ipc "cuda_core::create_event_handle_ipc" ( 

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

80  

81 # Event metadata getters 

82 bint get_event_timing_enabled "cuda_core::get_event_timing_enabled" ( 

83 const EventHandle& h) noexcept nogil 

84 bint get_event_is_blocking_sync "cuda_core::get_event_is_blocking_sync" ( 

85 const EventHandle& h) noexcept nogil 

86 bint get_event_ipc_enabled "cuda_core::get_event_ipc_enabled" ( 

87 const EventHandle& h) noexcept nogil 

88 int get_event_device_id "cuda_core::get_event_device_id" ( 

89 const EventHandle& h) noexcept nogil 

90 ContextHandle get_event_context "cuda_core::get_event_context" ( 

91 const EventHandle& h) noexcept nogil 

92  

93 # Memory pool handles 

94 MemoryPoolHandle create_mempool_handle "cuda_core::create_mempool_handle" ( 

95 const cydriver.CUmemPoolProps& props) except+ nogil 

96 MemoryPoolHandle create_mempool_handle_ref "cuda_core::create_mempool_handle_ref" ( 

97 cydriver.CUmemoryPool pool) except+ nogil 

98 MemoryPoolHandle get_device_mempool "cuda_core::get_device_mempool" ( 

99 int device_id) except+ nogil 

100 MemoryPoolHandle create_mempool_handle_ipc "cuda_core::create_mempool_handle_ipc" ( 

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

102  

103 # Device pointer handles 

104 DevicePtrHandle deviceptr_alloc_from_pool "cuda_core::deviceptr_alloc_from_pool" ( 

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

106 DevicePtrHandle deviceptr_alloc_async "cuda_core::deviceptr_alloc_async" ( 

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

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

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

110 DevicePtrHandle deviceptr_create_ref "cuda_core::deviceptr_create_ref" ( 

111 cydriver.CUdeviceptr ptr) except+ nogil 

112 DevicePtrHandle deviceptr_create_with_owner "cuda_core::deviceptr_create_with_owner" ( 

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

114 DevicePtrHandle deviceptr_create_mapped_graphics "cuda_core::deviceptr_create_mapped_graphics" ( 

115 cydriver.CUdeviceptr ptr, 

116 const GraphicsResourceHandle& h_resource, 

117 const StreamHandle& h_stream) except+ nogil 

118  

119 # MR deallocation callback 

120 void register_mr_dealloc_callback "cuda_core::register_mr_dealloc_callback" ( 

121 MRDeallocCallback cb) noexcept 

122 DevicePtrHandle deviceptr_create_with_mr "cuda_core::deviceptr_create_with_mr" ( 

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

124  

125 DevicePtrHandle deviceptr_import_ipc "cuda_core::deviceptr_import_ipc" ( 

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

127 StreamHandle deallocation_stream "cuda_core::deallocation_stream" ( 

128 const DevicePtrHandle& h) noexcept nogil 

129 void set_deallocation_stream "cuda_core::set_deallocation_stream" ( 

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

131  

132 # Library handles 

133 LibraryHandle create_library_handle_from_file "cuda_core::create_library_handle_from_file" ( 

134 const char* path) except+ nogil 

135 LibraryHandle create_library_handle_from_data "cuda_core::create_library_handle_from_data" ( 

136 const void* data) except+ nogil 

137 LibraryHandle create_library_handle_ref "cuda_core::create_library_handle_ref" ( 

138 cydriver.CUlibrary library) except+ nogil 

139  

140 # Kernel handles 

141 KernelHandle create_kernel_handle "cuda_core::create_kernel_handle" ( 

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

143 KernelHandle create_kernel_handle_ref "cuda_core::create_kernel_handle_ref" ( 

144 cydriver.CUkernel kernel) except+ nogil 

145 LibraryHandle get_kernel_library "cuda_core::get_kernel_library" ( 

146 const KernelHandle& h) noexcept nogil 

147  

148 # Graph handles 

149 GraphHandle create_graph_handle "cuda_core::create_graph_handle" ( 

150 cydriver.CUgraph graph) except+ nogil 

151 GraphHandle create_graph_handle_ref "cuda_core::create_graph_handle_ref" ( 

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

153  

154 # Graph exec handles 

155 GraphExecHandle create_graph_exec_handle "cuda_core::create_graph_exec_handle" ( 

156 cydriver.CUgraphExec graph_exec) except+ nogil 

157  

158 # Graph node handles 

159 GraphNodeHandle create_graph_node_handle "cuda_core::create_graph_node_handle" ( 

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

161 GraphHandle graph_node_get_graph "cuda_core::graph_node_get_graph" ( 

162 const GraphNodeHandle& h) noexcept nogil 

163 void invalidate_graph_node "cuda_core::invalidate_graph_node" ( 

164 const GraphNodeHandle& h) noexcept nogil 

165  

166 # Graphics resource handles 

167 GraphicsResourceHandle create_graphics_resource_handle "cuda_core::create_graphics_resource_handle" ( 

168 cydriver.CUgraphicsResource resource) except+ nogil 

169  

170 # NVRTC Program handles 

171 NvrtcProgramHandle create_nvrtc_program_handle "cuda_core::create_nvrtc_program_handle" ( 

172 cynvrtc.nvrtcProgram prog) except+ nogil 

173 NvrtcProgramHandle create_nvrtc_program_handle_ref "cuda_core::create_nvrtc_program_handle_ref" ( 

174 cynvrtc.nvrtcProgram prog) except+ nogil 

175  

176 # NVVM Program handles 

177 NvvmProgramHandle create_nvvm_program_handle "cuda_core::create_nvvm_program_handle" ( 

178 cynvvm.nvvmProgram prog) except+ nogil 

179 NvvmProgramHandle create_nvvm_program_handle_ref "cuda_core::create_nvvm_program_handle_ref" ( 

180 cynvvm.nvvmProgram prog) except+ nogil 

181  

182 # nvJitLink handles 

183 NvJitLinkHandle create_nvjitlink_handle "cuda_core::create_nvjitlink_handle" ( 

184 cynvjitlink.nvJitLinkHandle handle) except+ nogil 

185 NvJitLinkHandle create_nvjitlink_handle_ref "cuda_core::create_nvjitlink_handle_ref" ( 

186 cynvjitlink.nvJitLinkHandle handle) except+ nogil 

187  

188 # cuLink handles 

189 CuLinkHandle create_culink_handle "cuda_core::create_culink_handle" ( 

190 cydriver.CUlinkState state) except+ nogil 

191 CuLinkHandle create_culink_handle_ref "cuda_core::create_culink_handle_ref" ( 

192 cydriver.CUlinkState state) except+ nogil 

193  

194 # File descriptor handles 

195 FileDescriptorHandle create_fd_handle "cuda_core::create_fd_handle" ( 

196 int fd) except+ nogil 

197 FileDescriptorHandle create_fd_handle_ref "cuda_core::create_fd_handle_ref" ( 

198 int fd) except+ nogil 

199  

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

201 # groupParams is void* to avoid referencing CU_DEV_SM_RESOURCE_GROUP_PARAMS 

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

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

204 cydriver.CUdevResource* result, unsigned int nbGroups, 

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

206 unsigned int flags, void* groupParams) nogil 

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

208  

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

210 OpaqueArrayHandle create_array_handle "cuda_core::create_array_handle" ( 

211 const cydriver.CUDA_ARRAY3D_DESCRIPTOR& desc) except+ nogil 

212 OpaqueArrayHandle create_array_handle_ref "cuda_core::create_array_handle_ref" ( 

213 cydriver.CUarray arr) except+ nogil 

214 OpaqueArrayHandle create_array_handle_owning "cuda_core::create_array_handle_owning" ( 

215 cydriver.CUarray arr) except+ nogil 

216 OpaqueArrayHandle create_array_level_handle "cuda_core::create_array_level_handle" ( 

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

218 MipmappedArrayHandle create_mipmapped_array_handle "cuda_core::create_mipmapped_array_handle" ( 

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

220 TexObjectHandle create_tex_object_handle_array "cuda_core::create_tex_object_handle_array" ( 

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

222 const OpaqueArrayHandle& h_backing) except+ nogil 

223 TexObjectHandle create_tex_object_handle_mipmap "cuda_core::create_tex_object_handle_mipmap" ( 

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

225 const MipmappedArrayHandle& h_backing) except+ nogil 

226 TexObjectHandle create_tex_object_handle_linear "cuda_core::create_tex_object_handle_linear" ( 

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

228 const DevicePtrHandle& h_backing) except+ nogil 

229 SurfObjectHandle create_surf_object_handle "cuda_core::create_surf_object_handle" ( 

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

231  

232  

233# ============================================================================= 

234# CUDA Driver API capsule 

235# 

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

237# ============================================================================= 

238  

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

240  

241  

242# ============================================================================= 

243# CUDA driver function pointer initialization 

244# 

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

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

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

248# 

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

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

251# ============================================================================= 

252  

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

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

255 # Context 

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

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

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

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

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

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

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

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

264  

265 # Stream 

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

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

268  

269 # Event 

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

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

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

273  

274 # Device 

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

276  

277 # Memory pool 

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

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

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

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

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

283  

284 # Memory allocation 

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

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

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

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

289  

290 # Memory deallocation 

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

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

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

294  

295 # IPC 

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

297  

298 # Library 

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

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

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

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

303  

304 # Graph 

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

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

307  

308 # Linker 

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

310  

311 # Graphics interop 

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

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

314  

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

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

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

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

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

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

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

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

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

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

325  

326 # SM resource split (13.1+) 

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

328  

329 # NVRTC 

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

331  

332 # NVVM 

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

334  

335 # nvJitLink 

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

337  

338  

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

340cdef void* _get_driver_fn(str name): 

341 capsule = cydriver.__pyx_capi__[name] 

342 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

343  

344  

345cdef void* _get_optional_driver_fn(str name): 

346 try: 

347 capsule = cydriver.__pyx_capi__[name] 

348 except KeyError: 

349 return NULL 

350 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

351  

352  

353cdef void _init_driver_fn_pointers() noexcept: 

354 global p_cuDevicePrimaryCtxRetain, p_cuDevicePrimaryCtxRelease, p_cuCtxGetCurrent 

355 global p_cuGreenCtxCreate, p_cuGreenCtxDestroy, p_cuCtxFromGreenCtx 

356 global p_cuDevResourceGenerateDesc, p_cuGreenCtxStreamCreate 

357 global p_cuStreamCreateWithPriority, p_cuStreamDestroy 

358 global p_cuEventCreate, p_cuEventDestroy, p_cuIpcOpenEventHandle 

359 global p_cuDeviceGetCount 

360 global p_cuMemPoolSetAccess, p_cuMemPoolDestroy, p_cuMemPoolCreate 

361 global p_cuDeviceGetMemPool, p_cuMemPoolImportFromShareableHandle 

362 global p_cuMemAllocFromPoolAsync, p_cuMemAllocAsync, p_cuMemAlloc, p_cuMemAllocHost 

363 global p_cuMemFreeAsync, p_cuMemFree, p_cuMemFreeHost 

364 global p_cuMemPoolImportPointer 

365 global p_cuLibraryLoadFromFile, p_cuLibraryLoadData, p_cuLibraryUnload, p_cuLibraryGetKernel 

366 global p_cuGraphDestroy, p_cuGraphExecDestroy 

367 global p_cuLinkDestroy 

368 global p_cuGraphicsUnmapResources, p_cuGraphicsUnregisterResource 

369 global p_cuDevSmResourceSplit 

370 global p_cuArray3DCreate, p_cuArrayDestroy 

371 global p_cuMipmappedArrayCreate, p_cuMipmappedArrayDestroy, p_cuMipmappedArrayGetLevel 

372 global p_cuTexObjectCreate, p_cuTexObjectDestroy 

373 global p_cuSurfObjectCreate, p_cuSurfObjectDestroy 

374  

375 # Context 

376 p_cuDevicePrimaryCtxRetain = _get_driver_fn("cuDevicePrimaryCtxRetain") 

377 p_cuDevicePrimaryCtxRelease = _get_driver_fn("cuDevicePrimaryCtxRelease") 

378 p_cuCtxGetCurrent = _get_driver_fn("cuCtxGetCurrent") 

379 p_cuGreenCtxCreate = _get_optional_driver_fn("cuGreenCtxCreate") 

380 p_cuGreenCtxDestroy = _get_optional_driver_fn("cuGreenCtxDestroy") 

381 p_cuCtxFromGreenCtx = _get_optional_driver_fn("cuCtxFromGreenCtx") 

382 p_cuDevResourceGenerateDesc = _get_optional_driver_fn("cuDevResourceGenerateDesc") 

383 p_cuGreenCtxStreamCreate = _get_optional_driver_fn("cuGreenCtxStreamCreate") 

384  

385 # Stream 

386 p_cuStreamCreateWithPriority = _get_driver_fn("cuStreamCreateWithPriority") 

387 p_cuStreamDestroy = _get_driver_fn("cuStreamDestroy") 

388  

389 # Event 

390 p_cuEventCreate = _get_driver_fn("cuEventCreate") 

391 p_cuEventDestroy = _get_driver_fn("cuEventDestroy") 

392 p_cuIpcOpenEventHandle = _get_driver_fn("cuIpcOpenEventHandle") 

393  

394 # Device 

395 p_cuDeviceGetCount = _get_driver_fn("cuDeviceGetCount") 

396  

397 # Memory pool 

398 p_cuMemPoolSetAccess = _get_driver_fn("cuMemPoolSetAccess") 

399 p_cuMemPoolDestroy = _get_driver_fn("cuMemPoolDestroy") 

400 p_cuMemPoolCreate = _get_driver_fn("cuMemPoolCreate") 

401 p_cuDeviceGetMemPool = _get_driver_fn("cuDeviceGetMemPool") 

402 p_cuMemPoolImportFromShareableHandle = _get_driver_fn("cuMemPoolImportFromShareableHandle") 

403  

404 # Memory allocation 

405 p_cuMemAllocFromPoolAsync = _get_driver_fn("cuMemAllocFromPoolAsync") 

406 p_cuMemAllocAsync = _get_driver_fn("cuMemAllocAsync") 

407 p_cuMemAlloc = _get_driver_fn("cuMemAlloc") 

408 p_cuMemAllocHost = _get_driver_fn("cuMemAllocHost") 

409  

410 # Memory deallocation 

411 p_cuMemFreeAsync = _get_driver_fn("cuMemFreeAsync") 

412 p_cuMemFree = _get_driver_fn("cuMemFree") 

413 p_cuMemFreeHost = _get_driver_fn("cuMemFreeHost") 

414  

415 # IPC 

416 p_cuMemPoolImportPointer = _get_driver_fn("cuMemPoolImportPointer") 

417  

418 # Library 

419 p_cuLibraryLoadFromFile = _get_driver_fn("cuLibraryLoadFromFile") 

420 p_cuLibraryLoadData = _get_driver_fn("cuLibraryLoadData") 

421 p_cuLibraryUnload = _get_driver_fn("cuLibraryUnload") 

422 p_cuLibraryGetKernel = _get_driver_fn("cuLibraryGetKernel") 

423  

424 # Graph 

425 p_cuGraphDestroy = _get_driver_fn("cuGraphDestroy") 

426 p_cuGraphExecDestroy = _get_driver_fn("cuGraphExecDestroy") 

427  

428 # Linker 

429 p_cuLinkDestroy = _get_driver_fn("cuLinkDestroy") 

430  

431 # Graphics interop 

432 p_cuGraphicsUnmapResources = _get_driver_fn("cuGraphicsUnmapResources") 

433 p_cuGraphicsUnregisterResource = _get_driver_fn("cuGraphicsUnregisterResource") 

434  

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

436 p_cuArray3DCreate = _get_driver_fn("cuArray3DCreate") 

437 p_cuArrayDestroy = _get_driver_fn("cuArrayDestroy") 

438 p_cuMipmappedArrayCreate = _get_driver_fn("cuMipmappedArrayCreate") 

439 p_cuMipmappedArrayDestroy = _get_driver_fn("cuMipmappedArrayDestroy") 

440 p_cuMipmappedArrayGetLevel = _get_driver_fn("cuMipmappedArrayGetLevel") 

441 p_cuTexObjectCreate = _get_driver_fn("cuTexObjectCreate") 

442 p_cuTexObjectDestroy = _get_driver_fn("cuTexObjectDestroy") 

443 p_cuSurfObjectCreate = _get_driver_fn("cuSurfObjectCreate") 

444 p_cuSurfObjectDestroy = _get_driver_fn("cuSurfObjectDestroy") 

445  

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

447 p_cuDevSmResourceSplit = _get_optional_driver_fn("cuDevSmResourceSplit") 

448  

449_init_driver_fn_pointers() 

450  

451# ============================================================================= 

452# NVRTC function pointer initialization 

453# ============================================================================= 

454  

455cdef void* _get_nvrtc_fn(str name): 

456 capsule = cynvrtc.__pyx_capi__[name] 

457 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

458  

459cdef void _init_nvrtc_fn_pointers() noexcept: 

460 global p_nvrtcDestroyProgram 

461 p_nvrtcDestroyProgram = _get_nvrtc_fn("nvrtcDestroyProgram") 

462  

463_init_nvrtc_fn_pointers() 

464  

465# ============================================================================= 

466# NVVM function pointer initialization 

467# 

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

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

470# ============================================================================= 

471  

472cdef void* _get_nvvm_fn(str name): 

473 capsule = cynvvm.__pyx_capi__[name] 

474 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

475  

476cdef void _init_nvvm_fn_pointers() noexcept: 

477 global p_nvvmDestroyProgram 

478 p_nvvmDestroyProgram = _get_nvvm_fn("nvvmDestroyProgram") 

479  

480_init_nvvm_fn_pointers() 

481  

482# ============================================================================= 

483# nvJitLink function pointer initialization 

484# 

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

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

487# ============================================================================= 

488  

489cdef void* _get_nvjitlink_fn(str name): 

490 capsule = cynvjitlink.__pyx_capi__[name] 

491 return PyCapsule_GetPointer(capsule, PyCapsule_GetName(capsule)) 

492  

493cdef void _init_nvjitlink_fn_pointers() noexcept: 

494 global p_nvJitLinkDestroy 

495 p_nvJitLinkDestroy = _get_nvjitlink_fn("nvJitLinkDestroy") 

496  

497_init_nvjitlink_fn_pointers()