66
77from libc.stdint cimport intptr_t
88
9+ from .utils cimport get_nvvm_dso_version_suffix
10+
911from .utils import FunctionNotFoundError, NotSupportedError
1012
11- from cuda.bindings import path_finder
13+ import os
14+ import site
1215
1316import win32api
1417
@@ -37,9 +40,52 @@ cdef void* __nvvmGetProgramLogSize = NULL
3740cdef void * __nvvmGetProgramLog = NULL
3841
3942
40- cdef void * load_library(int driver_ver) except * with gil:
41- cdef intptr_t handle = path_finder.load_nvidia_dynamic_library(" nvvm" ).handle
42- return < void * > handle
43+ cdef inline list get_site_packages():
44+ return [site.getusersitepackages()] + site.getsitepackages() + [" conda" ]
45+
46+
47+ cdef load_library(const int driver_ver):
48+ handle = 0
49+
50+ for suffix in get_nvvm_dso_version_suffix(driver_ver):
51+ if len (suffix) == 0 :
52+ continue
53+ dll_name = " nvvm64_40_0.dll"
54+
55+ # First check if the DLL has been loaded by 3rd parties
56+ try :
57+ return win32api.GetModuleHandle(dll_name)
58+ except :
59+ pass
60+
61+ # Next, check if DLLs are installed via pip or conda
62+ for sp in get_site_packages():
63+ if sp == " conda" :
64+ # nvvm is not under $CONDA_PREFIX/lib, so it's not in the default search path
65+ conda_prefix = os.environ.get(" CONDA_PREFIX" )
66+ if conda_prefix is None :
67+ continue
68+ mod_path = os.path.join(conda_prefix, " Library" , " nvvm" , " bin" )
69+ else :
70+ mod_path = os.path.join(sp, " nvidia" , " cuda_nvcc" , " nvvm" , " bin" )
71+ if os.path.isdir(mod_path):
72+ os.add_dll_directory(mod_path)
73+ try :
74+ return win32api.LoadLibraryEx(
75+ # Note: LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR needs an abs path...
76+ os.path.join(mod_path, dll_name),
77+ 0 , LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)
78+ except :
79+ pass
80+
81+ # Finally, try default search
82+ # Only reached if DLL wasn't found in any site-package path
83+ try :
84+ return win32api.LoadLibrary(dll_name)
85+ except :
86+ pass
87+
88+ raise RuntimeError (' Failed to load nvvm' )
4389
4490
4591cdef int _check_or_init_nvvm() except - 1 nogil:
@@ -48,24 +94,23 @@ cdef int _check_or_init_nvvm() except -1 nogil:
4894 return 0
4995
5096 cdef int err, driver_ver
51- cdef intptr_t handle
5297 with gil:
5398 # Load driver to check version
5499 try :
55- nvcuda_handle = win32api.LoadLibraryEx(" nvcuda.dll" , 0 , LOAD_LIBRARY_SEARCH_SYSTEM32)
100+ handle = win32api.LoadLibraryEx(" nvcuda.dll" , 0 , LOAD_LIBRARY_SEARCH_SYSTEM32)
56101 except Exception as e:
57102 raise NotSupportedError(f' CUDA driver is not found ({e})' )
58103 global __cuDriverGetVersion
59104 if __cuDriverGetVersion == NULL :
60- __cuDriverGetVersion = < void * >< intptr_t> win32api.GetProcAddress(nvcuda_handle , ' cuDriverGetVersion' )
105+ __cuDriverGetVersion = < void * >< intptr_t> win32api.GetProcAddress(handle , ' cuDriverGetVersion' )
61106 if __cuDriverGetVersion == NULL :
62107 raise RuntimeError (' something went wrong' )
63108 err = (< int (* )(int * ) noexcept nogil> __cuDriverGetVersion)(& driver_ver)
64109 if err != 0 :
65110 raise RuntimeError (' something went wrong' )
66111
67112 # Load library
68- handle = < intptr_t > load_library(driver_ver)
113+ handle = load_library(driver_ver)
69114
70115 # Load function
71116 global __nvvmVersion
0 commit comments