Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cuda_bindings/cuda/bindings/_lib/windll.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cdef extern from "windows.h" nogil:
ctypedef unsigned long DWORD
ctypedef const wchar_t *LPCWSTR
ctypedef const char *LPCSTR
ctypedef int BOOL

cdef DWORD LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800

Expand All @@ -23,6 +24,8 @@ cdef extern from "windows.h" nogil:

FARPROC _GetProcAddress "GetProcAddress"(HMODULE hModule, LPCSTR lpProcName)

BOOL _FreeLibrary "FreeLibrary"(HMODULE hLibModule)

cdef inline uintptr_t LoadLibraryExW(str path, HANDLE hFile, DWORD dwFlags):
cdef uintptr_t result
cdef wchar_t* wpath = PyUnicode_AsWideCharString(path, NULL)
Expand All @@ -37,3 +40,6 @@ cdef inline uintptr_t LoadLibraryExW(str path, HANDLE hFile, DWORD dwFlags):

cdef inline FARPROC GetProcAddress(uintptr_t hModule, const char* lpProcName) nogil:
return _GetProcAddress(<HMODULE>hModule, lpProcName)

cdef inline BOOL FreeLibrary(uintptr_t hLibModule) nogil:
return _FreeLibrary(<HMODULE>hLibModule)
33 changes: 22 additions & 11 deletions cuda_bindings/cuda/bindings/cyruntime.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -1885,35 +1885,46 @@ cdef cudaError_t cudaGraphicsVDPAURegisterOutputSurface(cudaGraphicsResource** r

{{if True}}

{{if 'Windows' != platform.system()}}
from libc.stdint cimport uintptr_t
from cuda.pathfinder import load_nvidia_dynamic_lib
{{if 'Windows' == platform.system()}}
cimport cuda.bindings._lib.windll as windll
{{else}}
cimport cuda.bindings._lib.dlfcn as dlfcn
{{endif}}

cdef cudaError_t getLocalRuntimeVersion(int* runtimeVersion) except ?cudaErrorCallRequiresNewerDriver nogil:
{{if 'Windows' == platform.system()}}
with gil:
raise NotImplementedError('"getLocalRuntimeVersion" is unsupported on Windows')
{{else}}

# Load
handle = dlfcn.dlopen('libcudart.so.13', dlfcn.RTLD_NOW)
if handle == NULL:
with gil:
raise RuntimeError(f'Failed to dlopen libcudart.so.13')
with gil:
loaded_dl = load_nvidia_dynamic_lib("cudart")
{{if 'Windows' == platform.system()}}
handle = <uintptr_t>loaded_dl._handle_uint
{{else}}
handle = <void *><uintptr_t>loaded_dl._handle_uint
{{endif}}

{{if 'Windows' == platform.system()}}
__cudaRuntimeGetVersion = windll.GetProcAddress(handle, b'cudaRuntimeGetVersion')
{{else}}
__cudaRuntimeGetVersion = dlfcn.dlsym(handle, 'cudaRuntimeGetVersion')
{{endif}}

if __cudaRuntimeGetVersion == NULL:
with gil:
raise RuntimeError(f'Function "cudaRuntimeGetVersion" not found in libcudart.so.13')
raise RuntimeError(f'Function "cudaRuntimeGetVersion" not found in {loaded_dl.abs_path}')

# Call
cdef cudaError_t err = cudaSuccess
err = (<cudaError_t (*)(int*) except ?cudaErrorCallRequiresNewerDriver nogil> __cudaRuntimeGetVersion)(runtimeVersion)

# Unload
{{if 'Windows' == platform.system()}}
windll.FreeLibrary(handle)
{{else}}
dlfcn.dlclose(handle)
{{endif}}

# Return
return err
{{endif}}
{{endif}}
1 change: 1 addition & 0 deletions cuda_bindings/docs/source/release/12.9.X-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Highlights
* Automatic CUDA library path detection based on ``CUDA_HOME``, eliminating the need to manually set ``LIBRARY_PATH`` environment variables for installation.
* The Python overhead of calling functions in CUDA bindings in ``driver``, ``runtime`` and ``nvrtc`` has been reduced by approximately 30%.
* Updated the ``cuda.bindings.runtime`` module to statically link against the CUDA Runtime library from CUDA Toolkit 12.9.1.
* ``cyruntime.getLocalRuntimeVersion`` now uses pathfinder to find the CUDA runtime.


Known issues
Expand Down
1 change: 1 addition & 0 deletions cuda_bindings/docs/source/release/13.X.Y-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Highlights
* The Python overhead of calling functions in CUDA bindings in ``driver``, ``runtime`` and ``nvrtc`` has been reduced by approximately 30%.
* On Windows, the ``pywin32`` dependency has been removed. The necessary Windows API functions are now accessed directly.
* Updated the ``cuda.bindings.runtime`` module to statically link against the CUDA Runtime library from CUDA Toolkit 13.0.1.
* ``cyruntime.getLocalRuntimeVersion`` now uses pathfinder to find the CUDA runtime.


Known issues
Expand Down
11 changes: 11 additions & 0 deletions cuda_bindings/tests/test_cudart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import cuda.bindings.driver as cuda
import cuda.bindings.runtime as cudart
from cuda import pathfinder
from cuda.bindings import runtime


Expand Down Expand Up @@ -1400,3 +1401,13 @@ def test_struct_pointer_comparison(target):
c = target(456)
assert a != c
assert hash(a) != hash(c)


def test_getLocalRuntimeVersion():
try:
err, version = cudart.getLocalRuntimeVersion()
except pathfinder.DynamicLibNotFoundError:
pytest.skip("cudart dynamic lib not available")
else:
assertSuccess(err)
assert version >= 12000 # CUDA 12.0
Loading