lebedov / scikit-cuda

Python interface to GPU-powered libraries
http://scikit-cuda.readthedocs.org/
Other
975 stars 179 forks source link

_get_cublas_version: avoid CUBLAS context creation #302

Closed pierrepaleo closed 4 years ago

pierrepaleo commented 4 years ago

The function _get_cublas_version() of cublas.py sometimes creates a CUBLAS context to get the version number. This can sometimes be avoided.

The relevant part of the code finding the cublas library version is:

    try:
        major, minor = re.search(r'[\D\.]+\.+(\d+)\.(\d+)',
                                 utils.get_soname(cublas_path)).groups()
    except:
        # Create a temporary context to run cublasGetVersion():
        warnings.warn('creating CUBLAS context to get version number')

The regexp search succeeds with names like libcublas.so.9.1.85, but fails where there is only the "major version number", ex. libcublas.so.10. However, the library filename cublas_path is often actually a symbolik link (at least on Debian-based distribution, I don't know for the other Linux distributions/OS). So following the symbolic link with os.path.realpath(cublas_path) gives a file name with the correct major.minor.patch name that can be matched with the regexp.

One possible drawback is that the objdump command stills returns the "incomplete" file name, ex.

objdump -p /usr/lib/powerpc64le-linux-gnu/libcublas.so.10.2.0.168 | grep "SONAME"
  SONAME               libcublas.so.10

Would it make sense to modify _get_cublas_version() to avoid creating a CUBLAS context in this case ?

lebedov commented 4 years ago

I would still keep the call to cublasGetVersion() as a means of final resort (it's evidently necessary on MacOS anyway), but using os.path.realpath() to follow library symlinks and thereby reduce the likelihood of cublasGetVersion() having to be called seems like a good idea - skcuda/utils.py updated.