Coverage for cuda / bindings / _internal / cudla.pyx: 18.14%
215 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-22 01:37 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-22 01:37 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4# This code was automatically generated with version 1.5.0, generator version 0.3.1.dev1465+gc5c5c8652. Do not modify it directly.
6from libc.stdint cimport intptr_t, uintptr_t
8import threading
9from .utils import FunctionNotFoundError, NotSupportedError
11from cuda.pathfinder import load_nvidia_dynamic_lib
14###############################################################################
15# Extern
16###############################################################################
18# You must 'from .utils import NotSupportedError' before using this template
20cdef extern from "<dlfcn.h>" nogil:
21 void* dlopen(const char*, int)
22 char* dlerror()
23 void* dlsym(void*, const char*)
24 int dlclose(void*)
26 enum:
27 RTLD_LAZY
28 RTLD_NOW
29 RTLD_GLOBAL
30 RTLD_LOCAL
32 const void* RTLD_DEFAULT 'RTLD_DEFAULT'
34cdef int get_cuda_version():
35 cdef void* handle = NULL
36 cdef int err, driver_ver = 0
38 # Load driver to check version
39 handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)
40 if handle == NULL:
41 err_msg = dlerror()
42 raise NotSupportedError(f'CUDA driver is not found ({err_msg.decode()})')
43 cuDriverGetVersion = dlsym(handle, "cuDriverGetVersion")
44 if cuDriverGetVersion == NULL:
45 raise RuntimeError('Did not find cuDriverGetVersion symbol in libcuda.so.1')
46 err = (<int (*)(int*) noexcept nogil>cuDriverGetVersion)(&driver_ver)
47 if err != 0:
48 raise RuntimeError(f'cuDriverGetVersion returned error code {err}')
50 return driver_ver
54###############################################################################
55# Wrapper init
56###############################################################################
58cdef object __symbol_lock = threading.Lock()
59cdef bint __py_cudla_init = False
61cdef void* __cudlaGetVersion = NULL
62cdef void* __cudlaDeviceGetCount = NULL
63cdef void* __cudlaCreateDevice = NULL
64cdef void* __cudlaMemRegister = NULL
65cdef void* __cudlaModuleLoadFromMemory = NULL
66cdef void* __cudlaModuleGetAttributes = NULL
67cdef void* __cudlaModuleUnload = NULL
68cdef void* __cudlaSubmitTask = NULL
69cdef void* __cudlaDeviceGetAttribute = NULL
70cdef void* __cudlaMemUnregister = NULL
71cdef void* __cudlaGetLastError = NULL
72cdef void* __cudlaDestroyDevice = NULL
73cdef void* __cudlaSetTaskTimeoutInMs = NULL
76cdef void* load_library() except* with gil:
77 cdef uintptr_t handle = load_nvidia_dynamic_lib("cudla")._handle_uint
78 return <void*>handle
81cdef int _init_cudla() except -1 nogil:
82 global __py_cudla_init
83 cdef void* handle = NULL
85 with gil, __symbol_lock:
86 # Recheck the flag after obtaining the locks
87 if __py_cudla_init:
88 return 0
90 # Load function
91 global __cudlaGetVersion
92 __cudlaGetVersion = dlsym(RTLD_DEFAULT, 'cudlaGetVersion')
93 if __cudlaGetVersion == NULL:
94 if handle == NULL:
95 handle = load_library()
96 __cudlaGetVersion = dlsym(handle, 'cudlaGetVersion')
98 global __cudlaDeviceGetCount
99 __cudlaDeviceGetCount = dlsym(RTLD_DEFAULT, 'cudlaDeviceGetCount')
100 if __cudlaDeviceGetCount == NULL:
101 if handle == NULL:
102 handle = load_library()
103 __cudlaDeviceGetCount = dlsym(handle, 'cudlaDeviceGetCount')
105 global __cudlaCreateDevice
106 __cudlaCreateDevice = dlsym(RTLD_DEFAULT, 'cudlaCreateDevice')
107 if __cudlaCreateDevice == NULL:
108 if handle == NULL:
109 handle = load_library()
110 __cudlaCreateDevice = dlsym(handle, 'cudlaCreateDevice')
112 global __cudlaMemRegister
113 __cudlaMemRegister = dlsym(RTLD_DEFAULT, 'cudlaMemRegister')
114 if __cudlaMemRegister == NULL:
115 if handle == NULL:
116 handle = load_library()
117 __cudlaMemRegister = dlsym(handle, 'cudlaMemRegister')
119 global __cudlaModuleLoadFromMemory
120 __cudlaModuleLoadFromMemory = dlsym(RTLD_DEFAULT, 'cudlaModuleLoadFromMemory')
121 if __cudlaModuleLoadFromMemory == NULL:
122 if handle == NULL:
123 handle = load_library()
124 __cudlaModuleLoadFromMemory = dlsym(handle, 'cudlaModuleLoadFromMemory')
126 global __cudlaModuleGetAttributes
127 __cudlaModuleGetAttributes = dlsym(RTLD_DEFAULT, 'cudlaModuleGetAttributes')
128 if __cudlaModuleGetAttributes == NULL:
129 if handle == NULL:
130 handle = load_library()
131 __cudlaModuleGetAttributes = dlsym(handle, 'cudlaModuleGetAttributes')
133 global __cudlaModuleUnload
134 __cudlaModuleUnload = dlsym(RTLD_DEFAULT, 'cudlaModuleUnload')
135 if __cudlaModuleUnload == NULL:
136 if handle == NULL:
137 handle = load_library()
138 __cudlaModuleUnload = dlsym(handle, 'cudlaModuleUnload')
140 global __cudlaSubmitTask
141 __cudlaSubmitTask = dlsym(RTLD_DEFAULT, 'cudlaSubmitTask')
142 if __cudlaSubmitTask == NULL:
143 if handle == NULL:
144 handle = load_library()
145 __cudlaSubmitTask = dlsym(handle, 'cudlaSubmitTask')
147 global __cudlaDeviceGetAttribute
148 __cudlaDeviceGetAttribute = dlsym(RTLD_DEFAULT, 'cudlaDeviceGetAttribute')
149 if __cudlaDeviceGetAttribute == NULL:
150 if handle == NULL:
151 handle = load_library()
152 __cudlaDeviceGetAttribute = dlsym(handle, 'cudlaDeviceGetAttribute')
154 global __cudlaMemUnregister
155 __cudlaMemUnregister = dlsym(RTLD_DEFAULT, 'cudlaMemUnregister')
156 if __cudlaMemUnregister == NULL:
157 if handle == NULL:
158 handle = load_library()
159 __cudlaMemUnregister = dlsym(handle, 'cudlaMemUnregister')
161 global __cudlaGetLastError
162 __cudlaGetLastError = dlsym(RTLD_DEFAULT, 'cudlaGetLastError')
163 if __cudlaGetLastError == NULL:
164 if handle == NULL:
165 handle = load_library()
166 __cudlaGetLastError = dlsym(handle, 'cudlaGetLastError')
168 global __cudlaDestroyDevice
169 __cudlaDestroyDevice = dlsym(RTLD_DEFAULT, 'cudlaDestroyDevice')
170 if __cudlaDestroyDevice == NULL:
171 if handle == NULL:
172 handle = load_library()
173 __cudlaDestroyDevice = dlsym(handle, 'cudlaDestroyDevice')
175 global __cudlaSetTaskTimeoutInMs
176 __cudlaSetTaskTimeoutInMs = dlsym(RTLD_DEFAULT, 'cudlaSetTaskTimeoutInMs')
177 if __cudlaSetTaskTimeoutInMs == NULL:
178 if handle == NULL:
179 handle = load_library()
180 __cudlaSetTaskTimeoutInMs = dlsym(handle, 'cudlaSetTaskTimeoutInMs')
182 __py_cudla_init = True
183 return 0
186cdef inline int _check_or_init_cudla() except -1 nogil:
187 if __py_cudla_init:
188 return 0
190 return _init_cudla()
193cdef dict func_ptrs = None
196cpdef dict _inspect_function_pointers():
197 global func_ptrs
198 if func_ptrs is not None:
199 return func_ptrs
201 _check_or_init_cudla()
202 cdef dict data = {}
204 global __cudlaGetVersion
205 data["__cudlaGetVersion"] = <intptr_t>__cudlaGetVersion
207 global __cudlaDeviceGetCount
208 data["__cudlaDeviceGetCount"] = <intptr_t>__cudlaDeviceGetCount
210 global __cudlaCreateDevice
211 data["__cudlaCreateDevice"] = <intptr_t>__cudlaCreateDevice
213 global __cudlaMemRegister
214 data["__cudlaMemRegister"] = <intptr_t>__cudlaMemRegister
216 global __cudlaModuleLoadFromMemory
217 data["__cudlaModuleLoadFromMemory"] = <intptr_t>__cudlaModuleLoadFromMemory
219 global __cudlaModuleGetAttributes
220 data["__cudlaModuleGetAttributes"] = <intptr_t>__cudlaModuleGetAttributes
222 global __cudlaModuleUnload
223 data["__cudlaModuleUnload"] = <intptr_t>__cudlaModuleUnload
225 global __cudlaSubmitTask
226 data["__cudlaSubmitTask"] = <intptr_t>__cudlaSubmitTask
228 global __cudlaDeviceGetAttribute
229 data["__cudlaDeviceGetAttribute"] = <intptr_t>__cudlaDeviceGetAttribute
231 global __cudlaMemUnregister
232 data["__cudlaMemUnregister"] = <intptr_t>__cudlaMemUnregister
234 global __cudlaGetLastError
235 data["__cudlaGetLastError"] = <intptr_t>__cudlaGetLastError
237 global __cudlaDestroyDevice
238 data["__cudlaDestroyDevice"] = <intptr_t>__cudlaDestroyDevice
240 global __cudlaSetTaskTimeoutInMs
241 data["__cudlaSetTaskTimeoutInMs"] = <intptr_t>__cudlaSetTaskTimeoutInMs
243 func_ptrs = data
244 return data
247cpdef _inspect_function_pointer(str name):
248 global func_ptrs
249 if func_ptrs is None:
250 func_ptrs = _inspect_function_pointers()
251 return func_ptrs[name]
254###############################################################################
255# Wrapper functions
256###############################################################################
258cdef cudlaStatus _cudlaGetVersion(uint64_t* const version) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
259 global __cudlaGetVersion
260 _check_or_init_cudla()
261 if __cudlaGetVersion == NULL:
262 with gil:
263 raise FunctionNotFoundError("function cudlaGetVersion is not found")
264 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaGetVersion)(
265 version)
268cdef cudlaStatus _cudlaDeviceGetCount(uint64_t* const pNumDevices) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
269 global __cudlaDeviceGetCount
270 _check_or_init_cudla()
271 if __cudlaDeviceGetCount == NULL:
272 with gil:
273 raise FunctionNotFoundError("function cudlaDeviceGetCount is not found")
274 return (<cudlaStatus (*)(uint64_t* const) noexcept nogil>__cudlaDeviceGetCount)(
275 pNumDevices)
278cdef cudlaStatus _cudlaCreateDevice(const uint64_t device, cudlaDevHandle* const devHandle, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
279 global __cudlaCreateDevice
280 _check_or_init_cudla()
281 if __cudlaCreateDevice == NULL:
282 with gil:
283 raise FunctionNotFoundError("function cudlaCreateDevice is not found")
284 return (<cudlaStatus (*)(const uint64_t, cudlaDevHandle* const, const uint32_t) noexcept nogil>__cudlaCreateDevice)(
285 device, devHandle, flags)
288cdef 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:
289 global __cudlaMemRegister
290 _check_or_init_cudla()
291 if __cudlaMemRegister == NULL:
292 with gil:
293 raise FunctionNotFoundError("function cudlaMemRegister is not found")
294 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const, const size_t, uint64_t** const, const uint32_t) noexcept nogil>__cudlaMemRegister)(
295 devHandle, ptr, size, devPtr, flags)
298cdef 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:
299 global __cudlaModuleLoadFromMemory
300 _check_or_init_cudla()
301 if __cudlaModuleLoadFromMemory == NULL:
302 with gil:
303 raise FunctionNotFoundError("function cudlaModuleLoadFromMemory is not found")
304 return (<cudlaStatus (*)(const cudlaDevHandle, const uint8_t* const, const size_t, cudlaModule* const, const uint32_t) noexcept nogil>__cudlaModuleLoadFromMemory)(
305 devHandle, pModule, moduleSize, hModule, flags)
308cdef cudlaStatus _cudlaModuleGetAttributes(const cudlaModule hModule, const cudlaModuleAttributeType attrType, cudlaModuleAttribute* const attribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
309 global __cudlaModuleGetAttributes
310 _check_or_init_cudla()
311 if __cudlaModuleGetAttributes == NULL:
312 with gil:
313 raise FunctionNotFoundError("function cudlaModuleGetAttributes is not found")
314 return (<cudlaStatus (*)(const cudlaModule, const cudlaModuleAttributeType, cudlaModuleAttribute* const) noexcept nogil>__cudlaModuleGetAttributes)(
315 hModule, attrType, attribute)
318cdef cudlaStatus _cudlaModuleUnload(const cudlaModule hModule, const uint32_t flags) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
319 global __cudlaModuleUnload
320 _check_or_init_cudla()
321 if __cudlaModuleUnload == NULL:
322 with gil:
323 raise FunctionNotFoundError("function cudlaModuleUnload is not found")
324 return (<cudlaStatus (*)(const cudlaModule, const uint32_t) noexcept nogil>__cudlaModuleUnload)(
325 hModule, flags)
328cdef 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:
329 global __cudlaSubmitTask
330 _check_or_init_cudla()
331 if __cudlaSubmitTask == NULL:
332 with gil:
333 raise FunctionNotFoundError("function cudlaSubmitTask is not found")
334 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaTask* const, const uint32_t, void* const, const uint32_t) noexcept nogil>__cudlaSubmitTask)(
335 devHandle, ptrToTasks, numTasks, stream, flags)
338cdef cudlaStatus _cudlaDeviceGetAttribute(const cudlaDevHandle devHandle, const cudlaDevAttributeType attrib, cudlaDevAttribute* const pAttribute) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
339 global __cudlaDeviceGetAttribute
340 _check_or_init_cudla()
341 if __cudlaDeviceGetAttribute == NULL:
342 with gil:
343 raise FunctionNotFoundError("function cudlaDeviceGetAttribute is not found")
344 return (<cudlaStatus (*)(const cudlaDevHandle, const cudlaDevAttributeType, cudlaDevAttribute* const) noexcept nogil>__cudlaDeviceGetAttribute)(
345 devHandle, attrib, pAttribute)
348cdef cudlaStatus _cudlaMemUnregister(const cudlaDevHandle devHandle, const uint64_t* const devPtr) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
349 global __cudlaMemUnregister
350 _check_or_init_cudla()
351 if __cudlaMemUnregister == NULL:
352 with gil:
353 raise FunctionNotFoundError("function cudlaMemUnregister is not found")
354 return (<cudlaStatus (*)(const cudlaDevHandle, const uint64_t* const) noexcept nogil>__cudlaMemUnregister)(
355 devHandle, devPtr)
358cdef cudlaStatus _cudlaGetLastError(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
359 global __cudlaGetLastError
360 _check_or_init_cudla()
361 if __cudlaGetLastError == NULL:
362 with gil:
363 raise FunctionNotFoundError("function cudlaGetLastError is not found")
364 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaGetLastError)(
365 devHandle)
368cdef cudlaStatus _cudlaDestroyDevice(const cudlaDevHandle devHandle) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
369 global __cudlaDestroyDevice
370 _check_or_init_cudla()
371 if __cudlaDestroyDevice == NULL:
372 with gil:
373 raise FunctionNotFoundError("function cudlaDestroyDevice is not found")
374 return (<cudlaStatus (*)(const cudlaDevHandle) noexcept nogil>__cudlaDestroyDevice)(
375 devHandle)
378cdef cudlaStatus _cudlaSetTaskTimeoutInMs(const cudlaDevHandle devHandle, const uint32_t timeout) except?_CUDLASTATUS_INTERNAL_LOADING_ERROR nogil:
379 global __cudlaSetTaskTimeoutInMs
380 _check_or_init_cudla()
381 if __cudlaSetTaskTimeoutInMs == NULL:
382 with gil:
383 raise FunctionNotFoundError("function cudlaSetTaskTimeoutInMs is not found")
384 return (<cudlaStatus (*)(const cudlaDevHandle, const uint32_t) noexcept nogil>__cudlaSetTaskTimeoutInMs)(
385 devHandle, timeout)