@@ -18,6 +18,8 @@ import cython
1818# Extern
1919# ##############################################################################
2020
21+ from .utils import NotSupportedError
22+
2123cdef extern from " <dlfcn.h>" nogil:
2224 void * dlopen(const char * , int )
2325 char * dlerror()
@@ -32,14 +34,31 @@ cdef extern from "<dlfcn.h>" nogil:
3234
3335 const void * RTLD_DEFAULT ' RTLD_DEFAULT'
3436
37+ cdef int get_cuda_version():
38+ cdef void * handle = NULL
39+ cdef int err, driver_ver = 0
40+
41+ # Load driver to check version
42+ handle = dlopen(' libcuda.so.1' , RTLD_NOW | RTLD_GLOBAL)
43+ if handle == NULL :
44+ err_msg = dlerror()
45+ raise NotSupportedError(f' CUDA driver is not found ({err_msg.decode()})' )
46+ cuDriverGetVersion = dlsym(handle, " cuDriverGetVersion" )
47+ if cuDriverGetVersion == NULL :
48+ raise RuntimeError (' something went wrong' )
49+ err = (< int (* )(int * ) noexcept nogil> cuDriverGetVersion)(& driver_ver)
50+ if err != 0 :
51+ raise RuntimeError (' something went wrong' )
52+
53+ return driver_ver
54+
3555
3656# ##############################################################################
3757# Wrapper init
3858# ##############################################################################
3959
4060cdef object __symbol_lock = threading.Lock()
4161cdef bint __py_cufile_init = False
42- cdef void * __cuDriverGetVersion = NULL
4362
4463cdef void * __cuFileHandleRegister = NULL
4564cdef void * __cuFileHandleDeregister = NULL
@@ -97,24 +116,9 @@ cdef int _check_or_init_cufile() except -1 nogil:
97116 return 0
98117
99118 cdef void * handle = NULL
100- cdef int err, driver_ver = 0
101119
102120 with gil, __symbol_lock:
103- # Load driver to check version
104- handle = dlopen(' libcuda.so.1' , RTLD_NOW | RTLD_GLOBAL)
105- if handle == NULL :
106- err_msg = dlerror()
107- raise NotSupportedError(f' CUDA driver is not found ({err_msg.decode()})' )
108- global __cuDriverGetVersion
109- if __cuDriverGetVersion == NULL :
110- __cuDriverGetVersion = dlsym(handle, " cuDriverGetVersion" )
111- if __cuDriverGetVersion == NULL :
112- raise RuntimeError (' something went wrong' )
113- err = (< int (* )(int * ) noexcept nogil> __cuDriverGetVersion)(& driver_ver)
114- if err != 0 :
115- raise RuntimeError (' something went wrong' )
116- # dlclose(handle)
117- handle = NULL
121+ driver_ver = get_cuda_version()
118122
119123 # Load function
120124 global __cuFileHandleRegister
0 commit comments