Check 'info' output of getrf/getrs in lu_factor/lu_solve and warn or raise exception accordingly
+from warnings import warn
+
import numpy
import cupy
@@ -78,6 +80,13 @@ def lu_factor(a, overwrite_a=False, check_finite=True):
getrf(cusolver_handle, m, m, a.data.ptr, m, workspace.data.ptr,
ipiv.data.ptr, dev_info.data.ptr)
+ if dev_info[0] < 0:
+ raise ValueError('illegal value in %d-th argument of '
+ 'internal getrf (lu_factor)' % -dev_info[0])
+ elif dev_info[0] > 0:
+ warn("Diagonal number %d is exactly zero. Singular matrix."
+ % dev_info[0], RuntimeWarning, stacklevel=2)
+
# cuSolver uses 1-origin while SciPy uses 0-origin
ipiv -= 1
@@ -168,4 +177,8 @@ def lu_solve(lu_and_piv, b, trans=0, overwrite_b=False, check_finite=True):
m, n, lu.data.ptr, m, ipiv.data.ptr, b.data.ptr,
m, dev_info.data.ptr)
+ if dev_info[0] < 0:
+ raise ValueError('illegal value in %d-th argument of '
+ 'internal getrs (lu_solve)' % -dev_info[0])
+
return b
