Coverage for cuda/bindings/_internal/cudla.pyx: 38.46%
143 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-12-03 01:06 +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.5.0. Do not modify it directly.
5# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=cb3b6b9aa1163e7e39f1bd8728002ff326c7893c6f53adc17810a4ab7ac856f2
8# <<<< PREAMBLE CONTENT >>>>
10from libc.stdint cimport (
11 intptr_t,
12 uint32_t,
13 uint64_t,
14 uint8_t,
15)
17cdef extern from * nogil:
18 """
19 #if defined(_MSC_VER) && !defined(__clang__)
20 #include <intrin.h>
21 static __forceinline int atomic_int_load(int *p) {
22 int v = *(int volatile *)p; _ReadBarrier(); return v;
23 }
24 static __forceinline void atomic_int_store(int *p, int v) {
25 _WriteBarrier(); *(int volatile *)p = v;
26 }
27 #elif defined(__cplusplus)
28 /* GCC/Clang __atomic builtins work in any C++ standard without headers */
29 static inline int atomic_int_load(int *p) {
30 return __atomic_load_n(p, __ATOMIC_ACQUIRE);
31 }
32 static inline void atomic_int_store(int *p, int v) {
33 __atomic_store_n(p, v, __ATOMIC_RELEASE);
34 }
35 #else
36 #include <stdatomic.h>
37 static inline int atomic_int_load(int *p) {
38 return (int)atomic_load_explicit((atomic_int *)p, memory_order_acquire);
39 }
40 static inline void atomic_int_store(int *p, int v) {
41 atomic_store_explicit((atomic_int *)p, v, memory_order_release);
42 }
43 #endif
45 """
46 cdef int _cyb_atomic_int_load "atomic_int_load"(int *p) nogil
47 cdef void _cyb_atomic_int_store "atomic_int_store"(int *p, int v) nogil
49cdef extern from "<dlfcn.h>":
50 void* _cyb_dlsym "dlsym"(void*, const char*) nogil
51 const void * _cyb_RTLD_DEFAULT "RTLD_DEFAULT"
53import threading as _cyb_threading
55cdef int _cyb___py_cudla_init = 0
56cdef dict _cyb_func_ptrs = None
57cdef object _cyb_symbol_lock = _cyb_threading.Lock()
59# <<<< END OF PREAMBLE CONTENT >>>>
61from libc.stdint cimport uintptr_t
63from .utils import FunctionNotFoundError, NotSupportedError
64from cuda.pathfinder import load_nvidia_dynamic_lib
67###############################################################################
68# Wrapper init
69###############################################################################
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
85cdef enum: _cyb_cudla_n_functions = 13
86cdef void** _cyb_cudla_slots[_cyb_cudla_n_functions]
87cdef const char* _cyb_cudla_names[_cyb_cudla_n_functions]
88_cyb_cudla_slots[0] = &__cudlaGetVersion; _cyb_cudla_names[0] = b'cudlaGetVersion'
89_cyb_cudla_slots[1] = &__cudlaDeviceGetCount; _cyb_cudla_names[1] = b'cudlaDeviceGetCount'
90_cyb_cudla_slots[2] = &__cudlaCreateDevice; _cyb_cudla_names[2] = b'cudlaCreateDevice'
91_cyb_cudla_slots[3] = &__cudlaMemRegister; _cyb_cudla_names[3] = b'cudlaMemRegister'
92_cyb_cudla_slots[4] = &__cudlaModuleLoadFromMemory; _cyb_cudla_names[4] = b'cudlaModuleLoadFromMemory'
93_cyb_cudla_slots[5] = &__cudlaModuleGetAttributes; _cyb_cudla_names[5] = b'cudlaModuleGetAttributes'
94_cyb_cudla_slots[6] = &__cudlaModuleUnload; _cyb_cudla_names[6] = b'cudlaModuleUnload'
95_cyb_cudla_slots[7] = &__cudlaSubmitTask; _cyb_cudla_names[7] = b'cudlaSubmitTask'
96_cyb_cudla_slots[8] = &__cudlaDeviceGetAttribute; _cyb_cudla_names[8] = b'cudlaDeviceGetAttribute'
97_cyb_cudla_slots[9] = &__cudlaMemUnregister; _cyb_cudla_names[9] = b'cudlaMemUnregister'
98_cyb_cudla_slots[10] = &__cudlaGetLastError; _cyb_cudla_names[10] = b'cudlaGetLastError'
99_cyb_cudla_slots[11] = &__cudlaDestroyDevice; _cyb_cudla_names[11] = b'cudlaDestroyDevice'
100_cyb_cudla_slots[12] = &__cudlaSetTaskTimeoutInMs; _cyb_cudla_names[12] = b'cudlaSetTaskTimeoutInMs'
102cdef int _init_cudla() except -1 nogil:
103 global _cyb___py_cudla_init
104 cdef void* handle = NULL
105 cdef Py_ssize_t i
106 with gil, _cyb_symbol_lock:
107 if _cyb___py_cudla_init: return 0
109 for i in range(_cyb_cudla_n_functions):
110 _cyb_cudla_slots[i][0] = _cyb_dlsym(_cyb_RTLD_DEFAULT, _cyb_cudla_names[i])
111 if _cyb_cudla_slots[i][0] == NULL:
112 if handle == NULL:
113 handle = load_library()
114 _cyb_cudla_slots[i][0] = _cyb_dlsym(handle, _cyb_cudla_names[i])
116 _cyb_atomic_int_store(<int *>&_cyb___py_cudla_init, 1)
117 return 0
119cdef inline int _check_or_init_cudla() except -1 nogil:
120 if _cyb_atomic_int_load(<int *>&_cyb___py_cudla_init):
121 return 0
123 return _init_cudla()
126cpdef dict _inspect_function_pointers():
127 global _cyb_func_ptrs
128 if _cyb_func_ptrs is not None:
129 return _cyb_func_ptrs
131 _check_or_init_cudla()
132 cdef dict data = {}
133 cdef Py_ssize_t i
134 for i in range(_cyb_cudla_n_functions):
135 data['__' + _cyb_cudla_names[i].decode()] = <intptr_t>_cyb_cudla_slots[i][0]
136 _cyb_func_ptrs = data
137 return data
140cpdef _inspect_function_pointer(str name):
141 global _cyb_func_ptrs
142 if _cyb_func_ptrs is None:
143 _cyb_func_ptrs = _inspect_function_pointers()
144 return _cyb_func_ptrs[name]
149cdef void* load_library() except* with gil:
150 cdef uintptr_t handle = load_nvidia_dynamic_lib("cudla")._handle_uint
151 return <void*>handle
154###############################################################################
155# Wrapper functions
156###############################################################################
158cdef cudlaStatus _cudlaGetVersion(uint64_t* const version) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
159 global __cudlaGetVersion
160 _check_or_init_cudla()
161 if __cudlaGetVersion == NULL:
162 with gil:
163 raise FunctionNotFoundError("function cudlaGetVersion is not found")
164 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaGetVersion)(
165 version)
168cdef cudlaStatus _cudlaDeviceGetCount(uint64_t* const pNumDevices) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
169 global __cudlaDeviceGetCount
170 _check_or_init_cudla()
171 if __cudlaDeviceGetCount == NULL:
172 with gil:
173 raise FunctionNotFoundError("function cudlaDeviceGetCount is not found")
174 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaDeviceGetCount)(
175 pNumDevices)
178cdef cudlaStatus _cudlaCreateDevice(const uint64_t device, cudlaDevHandle* const devHandle, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
179 global __cudlaCreateDevice
180 _check_or_init_cudla()
181 if __cudlaCreateDevice == NULL:
182 with gil:
183 raise FunctionNotFoundError("function cudlaCreateDevice is not found")
184 return (<cudlaStatus (*)(const uint64_t, cudlaDevHandle* const, const uint32_t) noexcept nogil>__cudlaCreateDevice)(
185 device, devHandle, flags)
188cdef 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:
189 global __cudlaMemRegister
190 _check_or_init_cudla()
191 if __cudlaMemRegister == NULL:
192 with gil:
193 raise FunctionNotFoundError("function cudlaMemRegister is not found")
194 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const, const size_t, uint64_t** const, const uint32_t) noexcept nogil>__cudlaMemRegister)(
195 devHandle, ptr, size, devPtr, flags)
198cdef 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:
199 global __cudlaModuleLoadFromMemory
200 _check_or_init_cudla()
201 if __cudlaModuleLoadFromMemory == NULL:
202 with gil:
203 raise FunctionNotFoundError("function cudlaModuleLoadFromMemory is not found")
204 return (<cudlaStatus (*)(const cudlaDevHandle, const uint8_t* const, const size_t, cudlaModule* const, const uint32_t) noexcept nogil>__cudlaModuleLoadFromMemory)(
205 devHandle, pModule, moduleSize, hModule, flags)
208cdef cudlaStatus _cudlaModuleGetAttributes(const cudlaModule hModule, const cudlaModuleAttributeType attrType, cudlaModuleAttribute* const attribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
209 global __cudlaModuleGetAttributes
210 _check_or_init_cudla()
211 if __cudlaModuleGetAttributes == NULL:
212 with gil:
213 raise FunctionNotFoundError("function cudlaModuleGetAttributes is not found")
214 return (<cudlaStatus (*)(const cudlaModule, const cudlaModuleAttributeType, cudlaModuleAttribute* const) noexcept nogil>__cudlaModuleGetAttributes)(
215 hModule, attrType, attribute)
218cdef cudlaStatus _cudlaModuleUnload(const cudlaModule hModule, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
219 global __cudlaModuleUnload
220 _check_or_init_cudla()
221 if __cudlaModuleUnload == NULL:
222 with gil:
223 raise FunctionNotFoundError("function cudlaModuleUnload is not found")
224 return (<cudlaStatus (*)(const cudlaModule, const uint32_t) noexcept nogil>__cudlaModuleUnload)(
225 hModule, flags)
228cdef 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:
229 global __cudlaSubmitTask
230 _check_or_init_cudla()
231 if __cudlaSubmitTask == NULL:
232 with gil:
233 raise FunctionNotFoundError("function cudlaSubmitTask is not found")
234 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaTask* const, const uint32_t, void* const, const uint32_t) noexcept nogil>__cudlaSubmitTask)(
235 devHandle, ptrToTasks, numTasks, stream, flags)
238cdef cudlaStatus _cudlaDeviceGetAttribute(const cudlaDevHandle devHandle, const cudlaDevAttributeType attrib, cudlaDevAttribute* const pAttribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
239 global __cudlaDeviceGetAttribute
240 _check_or_init_cudla()
241 if __cudlaDeviceGetAttribute == NULL:
242 with gil:
243 raise FunctionNotFoundError("function cudlaDeviceGetAttribute is not found")
244 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaDevAttributeType, cudlaDevAttribute* const) noexcept nogil>__cudlaDeviceGetAttribute)(
245 devHandle, attrib, pAttribute)
248cdef cudlaStatus _cudlaMemUnregister(const cudlaDevHandle devHandle, const uint64_t* const devPtr) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
249 global __cudlaMemUnregister
250 _check_or_init_cudla()
251 if __cudlaMemUnregister == NULL:
252 with gil:
253 raise FunctionNotFoundError("function cudlaMemUnregister is not found")
254 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const) noexcept nogil>__cudlaMemUnregister)(
255 devHandle, devPtr)
258cdef cudlaStatus _cudlaGetLastError(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
259 global __cudlaGetLastError
260 _check_or_init_cudla()
261 if __cudlaGetLastError == NULL:
262 with gil:
263 raise FunctionNotFoundError("function cudlaGetLastError is not found")
264 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaGetLastError)(
265 devHandle)
268cdef cudlaStatus _cudlaDestroyDevice(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
269 global __cudlaDestroyDevice
270 _check_or_init_cudla()
271 if __cudlaDestroyDevice == NULL:
272 with gil:
273 raise FunctionNotFoundError("function cudlaDestroyDevice is not found")
274 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaDestroyDevice)(
275 devHandle)
278cdef cudlaStatus _cudlaSetTaskTimeoutInMs(const cudlaDevHandle devHandle, const uint32_t timeout) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
279 global __cudlaSetTaskTimeoutInMs
280 _check_or_init_cudla()
281 if __cudlaSetTaskTimeoutInMs == NULL:
282 with gil:
283 raise FunctionNotFoundError("function cudlaSetTaskTimeoutInMs is not found")
284 return (<cudlaStatus (*)(const cudlaDevHandle, const uint32_t) noexcept nogil>__cudlaSetTaskTimeoutInMs)(
285 devHandle, timeout)