Coverage for cuda/bindings/_internal/cudla.pyx: 26.50%
200 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# SPDX-License-Identifier: Apache-2.0
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=052aecd587e459179f49158c93f43cde9e327476eedfb5be12e98520f7401d94
8# <<<< PREAMBLE CONTENT >>>>
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
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
42cdef extern from "<dlfcn.h>":
43 void* _cyb_dlsym "dlsym"(void*, const char*) nogil
44 const void * _cyb_RTLD_DEFAULT "RTLD_DEFAULT"
46from libc.stdint cimport intptr_t as _cyb_intptr_t
48import threading as _cyb_threading
50cdef int _cyb___py_cudla_init = 0
51cdef dict _cyb_func_ptrs = None
52cdef object _cyb_symbol_lock = _cyb_threading.Lock()
54# <<<< END OF PREAMBLE CONTENT >>>>
56from libc.stdint cimport uintptr_t
58from .utils import FunctionNotFoundError, NotSupportedError
59from cuda.pathfinder import load_nvidia_dynamic_lib
62###############################################################################
63# Wrapper init
64###############################################################################
66cdef void* __cudlaGetVersion = NULL
67cdef void* __cudlaDeviceGetCount = NULL
68cdef void* __cudlaCreateDevice = NULL
69cdef void* __cudlaMemRegister = NULL
70cdef void* __cudlaModuleLoadFromMemory = NULL
71cdef void* __cudlaModuleGetAttributes = NULL
72cdef void* __cudlaModuleUnload = NULL
73cdef void* __cudlaSubmitTask = NULL
74cdef void* __cudlaDeviceGetAttribute = NULL
75cdef void* __cudlaMemUnregister = NULL
76cdef void* __cudlaGetLastError = NULL
77cdef void* __cudlaDestroyDevice = NULL
78cdef void* __cudlaSetTaskTimeoutInMs = NULL
80cdef int _init_cudla() except -1 nogil:
81 global _cyb___py_cudla_init
82 cdef void* handle = NULL
83 with gil, _cyb_symbol_lock:
84 if _cyb___py_cudla_init: return 0
86 global __cudlaGetVersion
87 __cudlaGetVersion = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaGetVersion')
88 if __cudlaGetVersion == NULL:
89 if handle == NULL:
90 handle = load_library()
91 __cudlaGetVersion = _cyb_dlsym(handle, 'cudlaGetVersion')
93 global __cudlaDeviceGetCount
94 __cudlaDeviceGetCount = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDeviceGetCount')
95 if __cudlaDeviceGetCount == NULL:
96 if handle == NULL:
97 handle = load_library()
98 __cudlaDeviceGetCount = _cyb_dlsym(handle, 'cudlaDeviceGetCount')
100 global __cudlaCreateDevice
101 __cudlaCreateDevice = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaCreateDevice')
102 if __cudlaCreateDevice == NULL:
103 if handle == NULL:
104 handle = load_library()
105 __cudlaCreateDevice = _cyb_dlsym(handle, 'cudlaCreateDevice')
107 global __cudlaMemRegister
108 __cudlaMemRegister = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaMemRegister')
109 if __cudlaMemRegister == NULL:
110 if handle == NULL:
111 handle = load_library()
112 __cudlaMemRegister = _cyb_dlsym(handle, 'cudlaMemRegister')
114 global __cudlaModuleLoadFromMemory
115 __cudlaModuleLoadFromMemory = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleLoadFromMemory')
116 if __cudlaModuleLoadFromMemory == NULL:
117 if handle == NULL:
118 handle = load_library()
119 __cudlaModuleLoadFromMemory = _cyb_dlsym(handle, 'cudlaModuleLoadFromMemory')
121 global __cudlaModuleGetAttributes
122 __cudlaModuleGetAttributes = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleGetAttributes')
123 if __cudlaModuleGetAttributes == NULL:
124 if handle == NULL:
125 handle = load_library()
126 __cudlaModuleGetAttributes = _cyb_dlsym(handle, 'cudlaModuleGetAttributes')
128 global __cudlaModuleUnload
129 __cudlaModuleUnload = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaModuleUnload')
130 if __cudlaModuleUnload == NULL:
131 if handle == NULL:
132 handle = load_library()
133 __cudlaModuleUnload = _cyb_dlsym(handle, 'cudlaModuleUnload')
135 global __cudlaSubmitTask
136 __cudlaSubmitTask = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaSubmitTask')
137 if __cudlaSubmitTask == NULL:
138 if handle == NULL:
139 handle = load_library()
140 __cudlaSubmitTask = _cyb_dlsym(handle, 'cudlaSubmitTask')
142 global __cudlaDeviceGetAttribute
143 __cudlaDeviceGetAttribute = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDeviceGetAttribute')
144 if __cudlaDeviceGetAttribute == NULL:
145 if handle == NULL:
146 handle = load_library()
147 __cudlaDeviceGetAttribute = _cyb_dlsym(handle, 'cudlaDeviceGetAttribute')
149 global __cudlaMemUnregister
150 __cudlaMemUnregister = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaMemUnregister')
151 if __cudlaMemUnregister == NULL:
152 if handle == NULL:
153 handle = load_library()
154 __cudlaMemUnregister = _cyb_dlsym(handle, 'cudlaMemUnregister')
156 global __cudlaGetLastError
157 __cudlaGetLastError = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaGetLastError')
158 if __cudlaGetLastError == NULL:
159 if handle == NULL:
160 handle = load_library()
161 __cudlaGetLastError = _cyb_dlsym(handle, 'cudlaGetLastError')
163 global __cudlaDestroyDevice
164 __cudlaDestroyDevice = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaDestroyDevice')
165 if __cudlaDestroyDevice == NULL:
166 if handle == NULL:
167 handle = load_library()
168 __cudlaDestroyDevice = _cyb_dlsym(handle, 'cudlaDestroyDevice')
170 global __cudlaSetTaskTimeoutInMs
171 __cudlaSetTaskTimeoutInMs = _cyb_dlsym(_cyb_RTLD_DEFAULT, 'cudlaSetTaskTimeoutInMs')
172 if __cudlaSetTaskTimeoutInMs == NULL:
173 if handle == NULL:
174 handle = load_library()
175 __cudlaSetTaskTimeoutInMs = _cyb_dlsym(handle, 'cudlaSetTaskTimeoutInMs')
177 _cyb_atomic_int_store(<int *>&_cyb___py_cudla_init, 1)
178 return 0
180cdef inline int _check_or_init_cudla() except -1 nogil:
181 if _cyb_atomic_int_load(<int *>&_cyb___py_cudla_init):
182 return 0
184 return _init_cudla()
187cpdef dict _inspect_function_pointers():
188 global _cyb_func_ptrs
189 if _cyb_func_ptrs is not None:
190 return _cyb_func_ptrs
192 _check_or_init_cudla()
193 cdef dict data = {}
194 global __cudlaGetVersion
195 data["__cudlaGetVersion"] = <_cyb_intptr_t>__cudlaGetVersion
197 global __cudlaDeviceGetCount
198 data["__cudlaDeviceGetCount"] = <_cyb_intptr_t>__cudlaDeviceGetCount
200 global __cudlaCreateDevice
201 data["__cudlaCreateDevice"] = <_cyb_intptr_t>__cudlaCreateDevice
203 global __cudlaMemRegister
204 data["__cudlaMemRegister"] = <_cyb_intptr_t>__cudlaMemRegister
206 global __cudlaModuleLoadFromMemory
207 data["__cudlaModuleLoadFromMemory"] = <_cyb_intptr_t>__cudlaModuleLoadFromMemory
209 global __cudlaModuleGetAttributes
210 data["__cudlaModuleGetAttributes"] = <_cyb_intptr_t>__cudlaModuleGetAttributes
212 global __cudlaModuleUnload
213 data["__cudlaModuleUnload"] = <_cyb_intptr_t>__cudlaModuleUnload
215 global __cudlaSubmitTask
216 data["__cudlaSubmitTask"] = <_cyb_intptr_t>__cudlaSubmitTask
218 global __cudlaDeviceGetAttribute
219 data["__cudlaDeviceGetAttribute"] = <_cyb_intptr_t>__cudlaDeviceGetAttribute
221 global __cudlaMemUnregister
222 data["__cudlaMemUnregister"] = <_cyb_intptr_t>__cudlaMemUnregister
224 global __cudlaGetLastError
225 data["__cudlaGetLastError"] = <_cyb_intptr_t>__cudlaGetLastError
227 global __cudlaDestroyDevice
228 data["__cudlaDestroyDevice"] = <_cyb_intptr_t>__cudlaDestroyDevice
230 global __cudlaSetTaskTimeoutInMs
231 data["__cudlaSetTaskTimeoutInMs"] = <_cyb_intptr_t>__cudlaSetTaskTimeoutInMs
232 _cyb_func_ptrs = data
233 return data
236cpdef _inspect_function_pointer(str name):
237 global _cyb_func_ptrs
238 if _cyb_func_ptrs is None:
239 _cyb_func_ptrs = _inspect_function_pointers()
240 return _cyb_func_ptrs[name]
245cdef void* load_library() except* with gil:
246 cdef uintptr_t handle = load_nvidia_dynamic_lib("cudla")._handle_uint
247 return <void*>handle
250###############################################################################
251# Wrapper functions
252###############################################################################
254cdef cudlaStatus _cudlaGetVersion(uint64_t* const version) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
255 global __cudlaGetVersion
256 _check_or_init_cudla()
257 if __cudlaGetVersion == NULL:
258 with gil:
259 raise FunctionNotFoundError("function cudlaGetVersion is not found")
260 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaGetVersion)(
261 version)
264cdef cudlaStatus _cudlaDeviceGetCount(uint64_t* const pNumDevices) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
265 global __cudlaDeviceGetCount
266 _check_or_init_cudla()
267 if __cudlaDeviceGetCount == NULL:
268 with gil:
269 raise FunctionNotFoundError("function cudlaDeviceGetCount is not found")
270 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaDeviceGetCount)(
271 pNumDevices)
274cdef cudlaStatus _cudlaCreateDevice(const uint64_t device, cudlaDevHandle* const devHandle, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
275 global __cudlaCreateDevice
276 _check_or_init_cudla()
277 if __cudlaCreateDevice == NULL:
278 with gil:
279 raise FunctionNotFoundError("function cudlaCreateDevice is not found")
280 return (<cudlaStatus (*)(const uint64_t, cudlaDevHandle* const, const uint32_t) noexcept nogil>__cudlaCreateDevice)(
281 device, devHandle, flags)
284cdef 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:
285 global __cudlaMemRegister
286 _check_or_init_cudla()
287 if __cudlaMemRegister == NULL:
288 with gil:
289 raise FunctionNotFoundError("function cudlaMemRegister is not found")
290 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const, const size_t, uint64_t** const, const uint32_t) noexcept nogil>__cudlaMemRegister)(
291 devHandle, ptr, size, devPtr, flags)
294cdef 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:
295 global __cudlaModuleLoadFromMemory
296 _check_or_init_cudla()
297 if __cudlaModuleLoadFromMemory == NULL:
298 with gil:
299 raise FunctionNotFoundError("function cudlaModuleLoadFromMemory is not found")
300 return (<cudlaStatus (*)(const cudlaDevHandle, const uint8_t* const, const size_t, cudlaModule* const, const uint32_t) noexcept nogil>__cudlaModuleLoadFromMemory)(
301 devHandle, pModule, moduleSize, hModule, flags)
304cdef cudlaStatus _cudlaModuleGetAttributes(const cudlaModule hModule, const cudlaModuleAttributeType attrType, cudlaModuleAttribute* const attribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
305 global __cudlaModuleGetAttributes
306 _check_or_init_cudla()
307 if __cudlaModuleGetAttributes == NULL:
308 with gil:
309 raise FunctionNotFoundError("function cudlaModuleGetAttributes is not found")
310 return (<cudlaStatus (*)(const cudlaModule, const cudlaModuleAttributeType, cudlaModuleAttribute* const) noexcept nogil>__cudlaModuleGetAttributes)(
311 hModule, attrType, attribute)
314cdef cudlaStatus _cudlaModuleUnload(const cudlaModule hModule, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
315 global __cudlaModuleUnload
316 _check_or_init_cudla()
317 if __cudlaModuleUnload == NULL:
318 with gil:
319 raise FunctionNotFoundError("function cudlaModuleUnload is not found")
320 return (<cudlaStatus (*)(const cudlaModule, const uint32_t) noexcept nogil>__cudlaModuleUnload)(
321 hModule, flags)
324cdef 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:
325 global __cudlaSubmitTask
326 _check_or_init_cudla()
327 if __cudlaSubmitTask == NULL:
328 with gil:
329 raise FunctionNotFoundError("function cudlaSubmitTask is not found")
330 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaTask* const, const uint32_t, void* const, const uint32_t) noexcept nogil>__cudlaSubmitTask)(
331 devHandle, ptrToTasks, numTasks, stream, flags)
334cdef cudlaStatus _cudlaDeviceGetAttribute(const cudlaDevHandle devHandle, const cudlaDevAttributeType attrib, cudlaDevAttribute* const pAttribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
335 global __cudlaDeviceGetAttribute
336 _check_or_init_cudla()
337 if __cudlaDeviceGetAttribute == NULL:
338 with gil:
339 raise FunctionNotFoundError("function cudlaDeviceGetAttribute is not found")
340 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaDevAttributeType, cudlaDevAttribute* const) noexcept nogil>__cudlaDeviceGetAttribute)(
341 devHandle, attrib, pAttribute)
344cdef cudlaStatus _cudlaMemUnregister(const cudlaDevHandle devHandle, const uint64_t* const devPtr) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
345 global __cudlaMemUnregister
346 _check_or_init_cudla()
347 if __cudlaMemUnregister == NULL:
348 with gil:
349 raise FunctionNotFoundError("function cudlaMemUnregister is not found")
350 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const) noexcept nogil>__cudlaMemUnregister)(
351 devHandle, devPtr)
354cdef cudlaStatus _cudlaGetLastError(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
355 global __cudlaGetLastError
356 _check_or_init_cudla()
357 if __cudlaGetLastError == NULL:
358 with gil:
359 raise FunctionNotFoundError("function cudlaGetLastError is not found")
360 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaGetLastError)(
361 devHandle)
364cdef cudlaStatus _cudlaDestroyDevice(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
365 global __cudlaDestroyDevice
366 _check_or_init_cudla()
367 if __cudlaDestroyDevice == NULL:
368 with gil:
369 raise FunctionNotFoundError("function cudlaDestroyDevice is not found")
370 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaDestroyDevice)(
371 devHandle)
374cdef cudlaStatus _cudlaSetTaskTimeoutInMs(const cudlaDevHandle devHandle, const uint32_t timeout) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
375 global __cudlaSetTaskTimeoutInMs
376 _check_or_init_cudla()
377 if __cudlaSetTaskTimeoutInMs == NULL:
378 with gil:
379 raise FunctionNotFoundError("function cudlaSetTaskTimeoutInMs is not found")
380 return (<cudlaStatus (*)(const cudlaDevHandle, const uint32_t) noexcept nogil>__cudlaSetTaskTimeoutInMs)(
381 devHandle, timeout)