|
6 | 6 | # this software and related documentation outside the terms of the EULA
|
7 | 7 | # is strictly prohibited.
|
8 | 8 | {{if 'Windows' == platform.system()}}
|
9 |
| -import win32api |
| 9 | +import os |
| 10 | +import site |
10 | 11 | import struct
|
| 12 | +import win32api |
11 | 13 | from pywintypes import error
|
12 | 14 | {{else}}
|
13 | 15 | cimport cuda.bindings._lib.dlfcn as dlfcn
|
@@ -40,18 +42,70 @@ cdef int cuPythonInit() except -1 nogil:
|
40 | 42 |
|
41 | 43 | # Load library
|
42 | 44 | {{if 'Windows' == platform.system()}}
|
43 |
| - LOAD_LIBRARY_SAFE_CURRENT_DIRS = 0x00002000 |
44 | 45 | with gil:
|
| 46 | + # First check if the DLL has been loaded by 3rd parties |
45 | 47 | try:
|
46 |
| - handle = win32api.LoadLibraryEx("nvrtc64_112_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 48 | + handle = win32api.GetModuleHandle("nvrtc64_112_0.dll") |
47 | 49 | except:
|
48 | 50 | try:
|
49 |
| - handle = win32api.LoadLibraryEx("nvrtc64_111_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 51 | + handle = win32api.GetModuleHandle("nvrtc64_111_0.dll") |
| 52 | + except: |
| 53 | + try: |
| 54 | + handle = win32api.GetModuleHandle("nvrtc64_110_0.dll") |
| 55 | + except: |
| 56 | + handle = None |
| 57 | + |
| 58 | + # Else try default search |
| 59 | + if not handle: |
| 60 | + LOAD_LIBRARY_SAFE_CURRENT_DIRS = 0x00002000 |
| 61 | + try: |
| 62 | + handle = win32api.LoadLibraryEx("nvrtc64_112_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 63 | + except: |
| 64 | + try: |
| 65 | + handle = win32api.LoadLibraryEx("nvrtc64_111_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 66 | + except: |
| 67 | + try: |
| 68 | + handle = win32api.LoadLibraryEx("nvrtc64_110_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 69 | + except: |
| 70 | + pass |
| 71 | + |
| 72 | + # Final check if DLLs can be found within pip installations |
| 73 | + if not handle: |
| 74 | + site_packages = [site.getusersitepackages()] + site.getsitepackages() |
| 75 | + for sp in site_packages: |
| 76 | + mod_path = os.path.join(sp, "nvidia", "cuda_nvrtc", "bin") |
| 77 | + if not os.path.isdir(mod_path): |
| 78 | + continue |
| 79 | + os.add_dll_directory(mod_path) |
| 80 | + LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000 |
| 81 | + LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100 |
| 82 | + try: |
| 83 | + handle = win32api.LoadLibraryEx( |
| 84 | + # Note: LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR needs an abs path... |
| 85 | + os.path.join(mod_path, "nvrtc64_112_0.dll"), |
| 86 | + 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) |
| 87 | + |
| 88 | + # Note: nvrtc64_120_0.dll calls into nvrtc-builtins64_*.dll which is |
| 89 | + # located in the same mod_path. |
| 90 | + # Update PATH environ so that the two dlls can find each other |
| 91 | + os.environ["PATH"] = os.pathsep.join((os.environ.get("PATH", ""), mod_path)) |
50 | 92 | except:
|
51 | 93 | try:
|
52 |
| - handle = win32api.LoadLibraryEx("nvrtc64_110_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) |
| 94 | + handle = win32api.LoadLibraryEx( |
| 95 | + os.path.join(mod_path, "nvrtc64_111_0.dll"), |
| 96 | + 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) |
| 97 | + os.environ["PATH"] = os.pathsep.join((os.environ.get("PATH", ""), mod_path)) |
53 | 98 | except:
|
54 |
| - raise RuntimeError('Failed to LoadLibraryEx nvrtc64_112_0.dll, or nvrtc64_111_0.dll, or nvrtc64_110_0.dll') |
| 99 | + try: |
| 100 | + handle = win32api.LoadLibraryEx( |
| 101 | + os.path.join(mod_path, "nvrtc64_110_0.dll"), |
| 102 | + 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR) |
| 103 | + os.environ["PATH"] = os.pathsep.join((os.environ.get("PATH", ""), mod_path)) |
| 104 | + except: |
| 105 | + pass |
| 106 | + |
| 107 | + if not handle: |
| 108 | + raise RuntimeError('Failed to LoadLibraryEx nvrtc64_112_0.dll, or nvrtc64_111_0.dll, or nvrtc64_110_0.dll') |
55 | 109 | {{else}}
|
56 | 110 | handle = NULL
|
57 | 111 | if handle == NULL:
|
|
0 commit comments