Skip to content

Commit

Permalink
Merge pull request #7977 from kmaehashi/softlink-debug
Browse files Browse the repository at this point in the history
Add debug feature to preloading and softlink
  • Loading branch information
emcastillo committed Nov 14, 2023
2 parents 0706808 + ccc6937 commit 37a2df2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
15 changes: 6 additions & 9 deletions cupy/_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
'cutensor': None,
}

_preload_logs = []
_debug = os.environ.get('CUPY_DEBUG_LIBRARY_LOAD', '0') == '1'


def _log(msg):
# TODO(kmaehashi): replace with the standard logging
_preload_logs.append(msg)
def _log(msg: str) -> None:
if _debug:
sys.stderr.write(f'[CUPY_DEBUG_LIBRARY_LOAD] {msg}\n')
sys.stderr.flush()


def get_cuda_path():
Expand Down Expand Up @@ -289,7 +290,7 @@ def _can_attempt_preload(lib: str) -> bool:
# Conda-Forge. See here for the configuration files used in
# Conda-Forge distributions.
# https://github.com/conda-forge/cupy-feedstock/blob/master/recipe/preload_config/
_log(f'Cannot preload {lib} as this is not a wheel installation')
_log(f'Not preloading {lib} as this is not a pip wheel installation')
return False

if lib not in _preload_libs:
Expand Down Expand Up @@ -372,10 +373,6 @@ def _preload_library(lib):
_log(f'Library {lib} could not be preloaded: {e}')


def _get_preload_logs():
return '\n'.join(_preload_logs)


def _preload_warning(lib, exc):
config = get_preload_config()
if config is not None and lib in config:
Expand Down
1 change: 1 addition & 0 deletions cupy_backends/cuda/_softlink.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ cdef class SoftLink:
cdef:
object error
str prefix
str _libname
object _cdll
func_ptr get(self, str name)
12 changes: 11 additions & 1 deletion cupy_backends/cuda/_softlink.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ from libc.stdint cimport intptr_t
cimport cython


def _log(msg: str) -> None:
import cupy
cupy._environment._log(msg)


cdef class SoftLink:
def __init__(self, object libname, str prefix, *, bint mandatory=False):
self.error = None
self.prefix = prefix
self._libname = libname
self._cdll = None
if libname is None:
# Stub build or CUDA/HIP only library.
Expand All @@ -17,6 +23,7 @@ cdef class SoftLink:
else:
try:
self._cdll = ctypes.CDLL(libname)
_log(f'Library "{libname}" loaded')
except Exception as e:
self.error = e
msg = (
Expand All @@ -29,13 +36,16 @@ cdef class SoftLink:
"""
Returns a function pointer for the API.
"""
cdef str funcname = f'{self.prefix}{name}'
if self._cdll is None:
_log(f'[NOTICE] {self._libname} ({funcname}): library not loaded')
return <func_ptr>_fail_unsupported
cdef str funcname = f'{self.prefix}{name}'
cdef object func = getattr(self._cdll, funcname, None)
if func is None:
_log(f'[NOTICE] {self._libname} ({funcname}): function not found')
return <func_ptr>_fail_not_found
cdef intptr_t ptr = ctypes.addressof(func)
_log(f'{self._libname} ({funcname}): function loaded')
return cython.operator.dereference(<func_ptr*>ptr)


Expand Down

0 comments on commit 37a2df2

Please sign in to comment.