Coverage for cuda/bindings/_internal/cudla.pyx: 25.00%

200 statements  

« 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# SPDX-License-Identifier: Apache-2.0 

3  

4# This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly. 

5# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=82e74b806368f149cc9ff54763fc00a4e51e01f0379ce9a8ea920880e557c091 

6  

7  

8# <<<< PREAMBLE CONTENT >>>> 

9  

10cdef extern from * nogil: 

11 """ 

12 #if defined(_MSC_VER) && !defined(__clang__) 

13 #include <intrin.h> 

14 static __forceinline int atomic_int_load(int *p) { 

15 int v = *(int volatile *)p; _ReadBarrier(); return v; 

16 } 

17 static __forceinline void atomic_int_store(int *p, int v) { 

18 _WriteBarrier(); *(int volatile *)p = v; 

19 } 

20 #elif defined(__cplusplus) 

21 /* GCC/Clang __atomic builtins work in any C++ standard without headers */ 

22 static inline int atomic_int_load(int *p) { 

23 return __atomic_load_n(p, __ATOMIC_ACQUIRE); 

24 } 

25 static inline void atomic_int_store(int *p, int v) { 

26 __atomic_store_n(p, v, __ATOMIC_RELEASE); 

27 } 

28 #else 

29 #include <stdatomic.h> 

30 static inline int atomic_int_load(int *p) { 

31 return (int)atomic_load_explicit((atomic_int *)p, memory_order_acquire); 

32 } 

33 static inline void atomic_int_store(int *p, int v) { 

34 atomic_store_explicit((atomic_int *)p, v, memory_order_release); 

35 } 

36 #endif 

37  

38 """ 

39 cdef int _cyb_atomic_int_load "atomic_int_load"(int *p) nogil 

40 cdef void _cyb_atomic_int_store "atomic_int_store"(int *p, int v) nogil 

41  

42cdef extern from "<dlfcn.h>": 

43 void* _cyb_dlsym "dlsym"(void*, const char*) nogil 

44 const void * _cyb_RTLD_DEFAULT "RTLD_DEFAULT" 

45  

46from libc.stdint cimport ( 

47 intptr_t, 

48 uint32_t, 

49 uint64_t, 

50 uint8_t, 

51) 

52  

53import threading as _cyb_threading 

54  

55cdef int _cyb___py_cudla_init = 0 

56cdef dict _cyb_func_ptrs = None 

57cdef object _cyb_symbol_lock = _cyb_threading.Lock() 

58  

59# <<<< END OF PREAMBLE CONTENT >>>> 

60  

61from libc.stdint cimport uintptr_t 

62  

63from .utils import FunctionNotFoundError, NotSupportedError 

64from cuda.pathfinder import load_nvidia_dynamic_lib 

65  

66  

67############################################################################### 

68# Wrapper init 

69############################################################################### 

70  

71cdef void* __cudlaGetVersion = NULL 

72cdef void* __cudlaDeviceGetCount = NULL 

73cdef void* __cudlaCreateDevice = NULL 

74cdef void* __cudlaMemRegister = NULL 

75cdef void* __cudlaModuleLoadFromMemory = NULL 

76cdef void* __cudlaModuleGetAttributes = NULL 

77cdef void* __cudlaModuleUnload = NULL 

78cdef void* __cudlaSubmitTask = NULL 

79cdef void* __cudlaDeviceGetAttribute = NULL 

80cdef void* __cudlaMemUnregister = NULL 

81cdef void* __cudlaGetLastError = NULL 

82cdef void* __cudlaDestroyDevice = NULL 

83cdef void* __cudlaSetTaskTimeoutInMs = NULL 

84  

85cdef int _init_cudla() except -1 nogil: 

86 global _cyb___py_cudla_init 

87 cdef void* handle = NULL 

88 with gil, _cyb_symbol_lock: 

89 if _cyb___py_cudla_init: return 0 

90  

91 global __cudlaGetVersion 

92 __cudlaGetVersion = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaGetVersion') 

93 if __cudlaGetVersion == NULL: 

94 if handle == NULL: 

95 handle = load_library() 

96 __cudlaGetVersion = _cyb_dlsym(handle, 'cudlaGetVersion') 

97  

98 global __cudlaDeviceGetCount 

99 __cudlaDeviceGetCount = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDeviceGetCount') 

100 if __cudlaDeviceGetCount == NULL: 

101 if handle == NULL: 

102 handle = load_library() 

103 __cudlaDeviceGetCount = _cyb_dlsym(handle, 'cudlaDeviceGetCount') 

104  

105 global __cudlaCreateDevice 

106 __cudlaCreateDevice = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaCreateDevice') 

107 if __cudlaCreateDevice == NULL: 

108 if handle == NULL: 

109 handle = load_library() 

110 __cudlaCreateDevice = _cyb_dlsym(handle, 'cudlaCreateDevice') 

111  

112 global __cudlaMemRegister 

113 __cudlaMemRegister = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaMemRegister') 

114 if __cudlaMemRegister == NULL: 

115 if handle == NULL: 

116 handle = load_library() 

117 __cudlaMemRegister = _cyb_dlsym(handle, 'cudlaMemRegister') 

118  

119 global __cudlaModuleLoadFromMemory 

120 __cudlaModuleLoadFromMemory = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleLoadFromMemory') 

121 if __cudlaModuleLoadFromMemory == NULL: 

122 if handle == NULL: 

123 handle = load_library() 

124 __cudlaModuleLoadFromMemory = _cyb_dlsym(handle, 'cudlaModuleLoadFromMemory') 

125  

126 global __cudlaModuleGetAttributes 

127 __cudlaModuleGetAttributes = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleGetAttributes') 

128 if __cudlaModuleGetAttributes == NULL: 

129 if handle == NULL: 

130 handle = load_library() 

131 __cudlaModuleGetAttributes = _cyb_dlsym(handle, 'cudlaModuleGetAttributes') 

132  

133 global __cudlaModuleUnload 

134 __cudlaModuleUnload = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleUnload') 

135 if __cudlaModuleUnload == NULL: 

136 if handle == NULL: 

137 handle = load_library() 

138 __cudlaModuleUnload = _cyb_dlsym(handle, 'cudlaModuleUnload') 

139  

140 global __cudlaSubmitTask 

141 __cudlaSubmitTask = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaSubmitTask') 

142 if __cudlaSubmitTask == NULL: 

143 if handle == NULL: 

144 handle = load_library() 

145 __cudlaSubmitTask = _cyb_dlsym(handle, 'cudlaSubmitTask') 

146  

147 global __cudlaDeviceGetAttribute 

148 __cudlaDeviceGetAttribute = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDeviceGetAttribute') 

149 if __cudlaDeviceGetAttribute == NULL: 

150 if handle == NULL: 

151 handle = load_library() 

152 __cudlaDeviceGetAttribute = _cyb_dlsym(handle, 'cudlaDeviceGetAttribute') 

153  

154 global __cudlaMemUnregister 

155 __cudlaMemUnregister = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaMemUnregister') 

156 if __cudlaMemUnregister == NULL: 

157 if handle == NULL: 

158 handle = load_library() 

159 __cudlaMemUnregister = _cyb_dlsym(handle, 'cudlaMemUnregister') 

160  

161 global __cudlaGetLastError 

162 __cudlaGetLastError = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaGetLastError') 

163 if __cudlaGetLastError == NULL: 

164 if handle == NULL: 

165 handle = load_library() 

166 __cudlaGetLastError = _cyb_dlsym(handle, 'cudlaGetLastError') 

167  

168 global __cudlaDestroyDevice 

169 __cudlaDestroyDevice = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDestroyDevice') 

170 if __cudlaDestroyDevice == NULL: 

171 if handle == NULL: 

172 handle = load_library() 

173 __cudlaDestroyDevice = _cyb_dlsym(handle, 'cudlaDestroyDevice') 

174  

175 global __cudlaSetTaskTimeoutInMs 

176 __cudlaSetTaskTimeoutInMs = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaSetTaskTimeoutInMs') 

177 if __cudlaSetTaskTimeoutInMs == NULL: 

178 if handle == NULL: 

179 handle = load_library() 

180 __cudlaSetTaskTimeoutInMs = _cyb_dlsym(handle, 'cudlaSetTaskTimeoutInMs') 

181  

182 _cyb_atomic_int_store(<int *>&_cyb___py_cudla_init, 1) 

183 return 0 

184  

185cdef inline int _check_or_init_cudla() except -1 nogil: 

186 if _cyb_atomic_int_load(<int *>&_cyb___py_cudla_init): 

187 return 0 

188  

189 return _init_cudla() 

190  

191  

192cpdef dict _inspect_function_pointers(): 

193 global _cyb_func_ptrs 

194 if _cyb_func_ptrs is not None: 

195 return _cyb_func_ptrs 

196  

197 _check_or_init_cudla() 

198 cdef dict data = {} 

199 global __cudlaGetVersion 

200 data["__cudlaGetVersion"] = <intptr_t>__cudlaGetVersion 

201  

202 global __cudlaDeviceGetCount 

203 data["__cudlaDeviceGetCount"] = <intptr_t>__cudlaDeviceGetCount 

204  

205 global __cudlaCreateDevice 

206 data["__cudlaCreateDevice"] = <intptr_t>__cudlaCreateDevice 

207  

208 global __cudlaMemRegister 

209 data["__cudlaMemRegister"] = <intptr_t>__cudlaMemRegister 

210  

211 global __cudlaModuleLoadFromMemory 

212 data["__cudlaModuleLoadFromMemory"] = <intptr_t>__cudlaModuleLoadFromMemory 

213  

214 global __cudlaModuleGetAttributes 

215 data["__cudlaModuleGetAttributes"] = <intptr_t>__cudlaModuleGetAttributes 

216  

217 global __cudlaModuleUnload 

218 data["__cudlaModuleUnload"] = <intptr_t>__cudlaModuleUnload 

219  

220 global __cudlaSubmitTask 

221 data["__cudlaSubmitTask"] = <intptr_t>__cudlaSubmitTask 

222  

223 global __cudlaDeviceGetAttribute 

224 data["__cudlaDeviceGetAttribute"] = <intptr_t>__cudlaDeviceGetAttribute 

225  

226 global __cudlaMemUnregister 

227 data["__cudlaMemUnregister"] = <intptr_t>__cudlaMemUnregister 

228  

229 global __cudlaGetLastError 

230 data["__cudlaGetLastError"] = <intptr_t>__cudlaGetLastError 

231  

232 global __cudlaDestroyDevice 

233 data["__cudlaDestroyDevice"] = <intptr_t>__cudlaDestroyDevice 

234  

235 global __cudlaSetTaskTimeoutInMs 

236 data["__cudlaSetTaskTimeoutInMs"] = <intptr_t>__cudlaSetTaskTimeoutInMs 

237 _cyb_func_ptrs = data 

238 return data 

239  

240  

241cpdef _inspect_function_pointer(str name): 

242 global _cyb_func_ptrs 

243 if _cyb_func_ptrs is None: 

244 _cyb_func_ptrs = _inspect_function_pointers() 

245 return _cyb_func_ptrs[name] 

246  

247  

248  

249  

250cdef void* load_library() except* with gil: 

251 cdef uintptr_t handle = load_nvidia_dynamic_lib("cudla")._handle_uint 

252 return <void*>handle 

253  

254  

255############################################################################### 

256# Wrapper functions 

257############################################################################### 

258  

259cdef cudlaStatus _cudlaGetVersion(uint64_t* const version) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

260 global __cudlaGetVersion 

261 _check_or_init_cudla() 

262 if __cudlaGetVersion == NULL: 

263 with gil: 

264 raise FunctionNotFoundError("function cudlaGetVersion is not found") 

265 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaGetVersion)( 

266 version) 

267  

268  

269cdef cudlaStatus _cudlaDeviceGetCount(uint64_t* const pNumDevices) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

270 global __cudlaDeviceGetCount 

271 _check_or_init_cudla() 

272 if __cudlaDeviceGetCount == NULL: 

273 with gil: 

274 raise FunctionNotFoundError("function cudlaDeviceGetCount is not found") 

275 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaDeviceGetCount)( 

276 pNumDevices) 

277  

278  

279cdef cudlaStatus _cudlaCreateDevice(const uint64_t device, cudlaDevHandle* const devHandle, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

280 global __cudlaCreateDevice 

281 _check_or_init_cudla() 

282 if __cudlaCreateDevice == NULL: 

283 with gil: 

284 raise FunctionNotFoundError("function cudlaCreateDevice is not found") 

285 return (<cudlaStatus (*)(const uint64_t, cudlaDevHandle* const, const uint32_t) noexcept nogil>__cudlaCreateDevice)( 

286 device, devHandle, flags) 

287  

288  

289cdef cudlaStatus _cudlaMemRegister(const cudlaDevHandle devHandle, const uint64_t* const ptr, const size_t size, uint64_t** const devPtr, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

290 global __cudlaMemRegister 

291 _check_or_init_cudla() 

292 if __cudlaMemRegister == NULL: 

293 with gil: 

294 raise FunctionNotFoundError("function cudlaMemRegister is not found") 

295 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const, const size_t, uint64_t** const, const uint32_t) noexcept nogil>__cudlaMemRegister)( 

296 devHandle, ptr, size, devPtr, flags) 

297  

298  

299cdef cudlaStatus _cudlaModuleLoadFromMemory(const cudlaDevHandle devHandle, const uint8_t* const pModule, const size_t moduleSize, cudlaModule* const hModule, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

300 global __cudlaModuleLoadFromMemory 

301 _check_or_init_cudla() 

302 if __cudlaModuleLoadFromMemory == NULL: 

303 with gil: 

304 raise FunctionNotFoundError("function cudlaModuleLoadFromMemory is not found") 

305 return (<cudlaStatus (*)(const cudlaDevHandle, const uint8_t* const, const size_t, cudlaModule* const, const uint32_t) noexcept nogil>__cudlaModuleLoadFromMemory)( 

306 devHandle, pModule, moduleSize, hModule, flags) 

307  

308  

309cdef cudlaStatus _cudlaModuleGetAttributes(const cudlaModule hModule, const cudlaModuleAttributeType attrType, cudlaModuleAttribute* const attribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

310 global __cudlaModuleGetAttributes 

311 _check_or_init_cudla() 

312 if __cudlaModuleGetAttributes == NULL: 

313 with gil: 

314 raise FunctionNotFoundError("function cudlaModuleGetAttributes is not found") 

315 return (<cudlaStatus (*)(const cudlaModule, const cudlaModuleAttributeType, cudlaModuleAttribute* const) noexcept nogil>__cudlaModuleGetAttributes)( 

316 hModule, attrType, attribute) 

317  

318  

319cdef cudlaStatus _cudlaModuleUnload(const cudlaModule hModule, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

320 global __cudlaModuleUnload 

321 _check_or_init_cudla() 

322 if __cudlaModuleUnload == NULL: 

323 with gil: 

324 raise FunctionNotFoundError("function cudlaModuleUnload is not found") 

325 return (<cudlaStatus (*)(const cudlaModule, const uint32_t) noexcept nogil>__cudlaModuleUnload)( 

326 hModule, flags) 

327  

328  

329cdef cudlaStatus _cudlaSubmitTask(const cudlaDevHandle devHandle, const cudlaTask* const ptrToTasks, const uint32_t numTasks, void* const stream, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

330 global __cudlaSubmitTask 

331 _check_or_init_cudla() 

332 if __cudlaSubmitTask == NULL: 

333 with gil: 

334 raise FunctionNotFoundError("function cudlaSubmitTask is not found") 

335 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaTask* const, const uint32_t, void* const, const uint32_t) noexcept nogil>__cudlaSubmitTask)( 

336 devHandle, ptrToTasks, numTasks, stream, flags) 

337  

338  

339cdef cudlaStatus _cudlaDeviceGetAttribute(const cudlaDevHandle devHandle, const cudlaDevAttributeType attrib, cudlaDevAttribute* const pAttribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

340 global __cudlaDeviceGetAttribute 

341 _check_or_init_cudla() 

342 if __cudlaDeviceGetAttribute == NULL: 

343 with gil: 

344 raise FunctionNotFoundError("function cudlaDeviceGetAttribute is not found") 

345 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaDevAttributeType, cudlaDevAttribute* const) noexcept nogil>__cudlaDeviceGetAttribute)( 

346 devHandle, attrib, pAttribute) 

347  

348  

349cdef cudlaStatus _cudlaMemUnregister(const cudlaDevHandle devHandle, const uint64_t* const devPtr) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

350 global __cudlaMemUnregister 

351 _check_or_init_cudla() 

352 if __cudlaMemUnregister == NULL: 

353 with gil: 

354 raise FunctionNotFoundError("function cudlaMemUnregister is not found") 

355 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const) noexcept nogil>__cudlaMemUnregister)( 

356 devHandle, devPtr) 

357  

358  

359cdef cudlaStatus _cudlaGetLastError(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

360 global __cudlaGetLastError 

361 _check_or_init_cudla() 

362 if __cudlaGetLastError == NULL: 

363 with gil: 

364 raise FunctionNotFoundError("function cudlaGetLastError is not found") 

365 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaGetLastError)( 

366 devHandle) 

367  

368  

369cdef cudlaStatus _cudlaDestroyDevice(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

370 global __cudlaDestroyDevice 

371 _check_or_init_cudla() 

372 if __cudlaDestroyDevice == NULL: 

373 with gil: 

374 raise FunctionNotFoundError("function cudlaDestroyDevice is not found") 

375 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaDestroyDevice)( 

376 devHandle) 

377  

378  

379cdef cudlaStatus _cudlaSetTaskTimeoutInMs(const cudlaDevHandle devHandle, const uint32_t timeout) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil: 

380 global __cudlaSetTaskTimeoutInMs 

381 _check_or_init_cudla() 

382 if __cudlaSetTaskTimeoutInMs == NULL: 

383 with gil: 

384 raise FunctionNotFoundError("function cudlaSetTaskTimeoutInMs is not found") 

385 return (<cudlaStatus (*)(const cudlaDevHandle, const uint32_t) noexcept nogil>__cudlaSetTaskTimeoutInMs)( 

386 devHandle, timeout)