Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3749929

Browse files
authored
Fix #702: Update cyruntime.getLocalRuntimeVersion to use pathfinder (#929)
* Fix #702: Update cyruntime.getLocalRuntimeVersion to use pathfinder * Try to fix Windows build * Fix Windows * More realistic minimum version * Simplify gil handling * Unload module when done with it * Don't need try/except on Windows * Don't need gil * Fix message * Fix test * DynamicLibrary -> DynamicLib * Update to not use pywin32 * Add cast * Try to fix cast again * Try to fix cast again * Address comments in PR * Fix lib -> loaded_dl * Add changelog
1 parent 4bc9941 commit 3749929

File tree

5 files changed

+41
-11
lines changed

5 files changed

+41
-11
lines changed

cuda_bindings/cuda/bindings/_lib/windll.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cdef extern from "windows.h" nogil:
1212
ctypedef unsigned long DWORD
1313
ctypedef const wchar_t *LPCWSTR
1414
ctypedef const char *LPCSTR
15+
ctypedef int BOOL
1516

1617
cdef DWORD LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
1718

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

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

27+
BOOL _FreeLibrary "FreeLibrary"(HMODULE hLibModule)
28+
2629
cdef inline uintptr_t LoadLibraryExW(str path, HANDLE hFile, DWORD dwFlags):
2730
cdef uintptr_t result
2831
cdef wchar_t* wpath = PyUnicode_AsWideCharString(path, NULL)
@@ -37,3 +40,6 @@ cdef inline uintptr_t LoadLibraryExW(str path, HANDLE hFile, DWORD dwFlags):
3740

3841
cdef inline FARPROC GetProcAddress(uintptr_t hModule, const char* lpProcName) nogil:
3942
return _GetProcAddress(<HMODULE>hModule, lpProcName)
43+
44+
cdef inline BOOL FreeLibrary(uintptr_t hLibModule) nogil:
45+
return _FreeLibrary(<HMODULE>hLibModule)

cuda_bindings/cuda/bindings/cyruntime.pyx.in

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,35 +1885,46 @@ cdef cudaError_t cudaGraphicsVDPAURegisterOutputSurface(cudaGraphicsResource** r
18851885

18861886
{{if True}}
18871887

1888-
{{if 'Windows' != platform.system()}}
1888+
from libc.stdint cimport uintptr_t
1889+
from cuda.pathfinder import load_nvidia_dynamic_lib
1890+
{{if 'Windows' == platform.system()}}
1891+
cimport cuda.bindings._lib.windll as windll
1892+
{{else}}
18891893
cimport cuda.bindings._lib.dlfcn as dlfcn
18901894
{{endif}}
18911895

18921896
cdef cudaError_t getLocalRuntimeVersion(int* runtimeVersion) except ?cudaErrorCallRequiresNewerDriver nogil:
1893-
{{if 'Windows' == platform.system()}}
1894-
with gil:
1895-
raise NotImplementedError('"getLocalRuntimeVersion" is unsupported on Windows')
1896-
{{else}}
1897+
18971898
# Load
1898-
handle = dlfcn.dlopen('libcudart.so.13', dlfcn.RTLD_NOW)
1899-
if handle == NULL:
1900-
with gil:
1901-
raise RuntimeError(f'Failed to dlopen libcudart.so.13')
1899+
with gil:
1900+
loaded_dl = load_nvidia_dynamic_lib("cudart")
1901+
{{if 'Windows' == platform.system()}}
1902+
handle = <uintptr_t>loaded_dl._handle_uint
1903+
{{else}}
1904+
handle = <void *><uintptr_t>loaded_dl._handle_uint
1905+
{{endif}}
19021906

1907+
{{if 'Windows' == platform.system()}}
1908+
__cudaRuntimeGetVersion = windll.GetProcAddress(handle, b'cudaRuntimeGetVersion')
1909+
{{else}}
19031910
__cudaRuntimeGetVersion = dlfcn.dlsym(handle, 'cudaRuntimeGetVersion')
1911+
{{endif}}
19041912

19051913
if __cudaRuntimeGetVersion == NULL:
19061914
with gil:
1907-
raise RuntimeError(f'Function "cudaRuntimeGetVersion" not found in libcudart.so.13')
1915+
raise RuntimeError(f'Function "cudaRuntimeGetVersion" not found in {loaded_dl.abs_path}')
19081916

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

19131921
# Unload
1922+
{{if 'Windows' == platform.system()}}
1923+
windll.FreeLibrary(handle)
1924+
{{else}}
19141925
dlfcn.dlclose(handle)
1926+
{{endif}}
19151927

19161928
# Return
19171929
return err
1918-
{{endif}}
19191930
{{endif}}

cuda_bindings/docs/source/release/12.9.X-notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Highlights
1515
* Automatic CUDA library path detection based on ``CUDA_HOME``, eliminating the need to manually set ``LIBRARY_PATH`` environment variables for installation.
1616
* The Python overhead of calling functions in CUDA bindings in ``driver``, ``runtime`` and ``nvrtc`` has been reduced by approximately 30%.
1717
* Updated the ``cuda.bindings.runtime`` module to statically link against the CUDA Runtime library from CUDA Toolkit 12.9.1.
18+
* ``cyruntime.getLocalRuntimeVersion`` now uses pathfinder to find the CUDA runtime.
1819

1920

2021
Known issues

cuda_bindings/docs/source/release/13.X.Y-notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Highlights
1818
* The Python overhead of calling functions in CUDA bindings in ``driver``, ``runtime`` and ``nvrtc`` has been reduced by approximately 30%.
1919
* On Windows, the ``pywin32`` dependency has been removed. The necessary Windows API functions are now accessed directly.
2020
* Updated the ``cuda.bindings.runtime`` module to statically link against the CUDA Runtime library from CUDA Toolkit 13.0.1.
21+
* ``cyruntime.getLocalRuntimeVersion`` now uses pathfinder to find the CUDA runtime.
2122

2223

2324
Known issues

cuda_bindings/tests/test_cudart.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import cuda.bindings.driver as cuda
1111
import cuda.bindings.runtime as cudart
12+
from cuda import pathfinder
1213
from cuda.bindings import runtime
1314

1415

@@ -1400,3 +1401,13 @@ def test_struct_pointer_comparison(target):
14001401
c = target(456)
14011402
assert a != c
14021403
assert hash(a) != hash(c)
1404+
1405+
1406+
def test_getLocalRuntimeVersion():
1407+
try:
1408+
err, version = cudart.getLocalRuntimeVersion()
1409+
except pathfinder.DynamicLibNotFoundError:
1410+
pytest.skip("cudart dynamic lib not available")
1411+
else:
1412+
assertSuccess(err)
1413+
assert version >= 12000 # CUDA 12.0

0 commit comments

Comments
 (0)