Skip to content

Commit 681816c

Browse files
authored
[WIP] Specialize chklapackerror to improve error messages (#51645)
This is an attempt at resolving https://github.com/JuliaLang/julia/issues/46636. Since each LAPACK function generally has a specific meaning attached to a positive error code, this PR tries to specialize `chklapackerror` for the caller to throw a more informative error instead of a `LAPACKException`. For example: ```julia julia> U = UpperTriangular([1 2; 0 0]) 2×2 UpperTriangular{Int64, Matrix{Int64}}: 1 2 ⋅ 0 julia> inv(U) ERROR: SingularException(2) [...] ``` Currently, I've only specialized it for `trtrs!`, but if this seems reasonable, I'll add others. Functions to be implemented: - [ ] `gbtrf` - [ ] `gbtrs` - [ ] `gebal!` - [ ] `gebak!` - [ ] `gebrd!` - [ ] `gelqf!` - [ ] `geqlf!` - [ ] `geqp3!` - [ ] `geqrt!` - [ ] `geqrt3!` - [ ] `geqrf!` - [ ] `gerqf!` - [ ] `getrf!` - [ ] `tzrzf!` - [ ] `ormrz!` - [ ] `gels!` - [ ] `gesv!` - [ ] `getrs!` - [ ] `getri!` - [ ] `gesvx!` - [ ] `gelsd!` - [ ] `gelsy!` - [ ] `gglse!` - [ ] `geev!` - [ ] `gesdd!` - [ ] `gesvd!` - [ ] `ggsvd!` - [ ] `ggsvd3!` - [ ] `geevx!` - [ ] `ggev!` - [ ] `ggev3!` - [ ] `gtsv!` - [ ] `gttrf!` - [ ] `gttrs!` - [ ] `orglq!` - [ ] `orgqr!` - [ ] `orgql!` - [ ] `orgrq!` - [ ] `ormlq!` - [ ] `ormqr!` - [ ] `ormql!` - [ ] `ormrq!` - [ ] `gemqrt!` - [ ] `potrs!` - [ ] `ptsv!` - [ ] `pttrf!` - [ ] `pttrs!` - [ ] `trtri!` - [x] `trtrs!` - [ ] `trcon!` - [ ] `trevc!` - [ ] `trrfs!` - [ ] `stev!` - [ ] `stebz!` - [ ] `stegr!` - [ ] `stein!` - [ ] `syconv!` - [ ] `sytrs!` - [ ] `sytrs_rook!` - [ ] `syconvf_rook!` - [ ] `hesv!` - [ ] `hetri!` - [ ] `hetrs!` - [ ] `hesv_rook!` - [ ] `hetri_rook!` - [ ] `hetrs_rook!` - [ ] `sytri!` - [ ] `sytri_rook!` - [ ] `syconvf_rook!` - [ ] `syev!` - [ ] `syevr!` - [ ] `syevd!` - [ ] `bdsqr!` - [ ] `bdsdc!` - [ ] `gecon!` - [ ] `gehrd!` - [ ] `orghr!` - [ ] `ormhr!` - [ ] `hseqr!` - [ ] `hetrd!` - [ ] `ormtr!` - [ ] `gees!` - [ ] `gges!` - [ ] `gges3!` - [ ] `trexc!` - [ ] `trsen!` - [ ] `tgsen!` - [ ] `trsyl!`
1 parent eadec43 commit 681816c

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

stdlib/LinearAlgebra/src/lapack.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ function chkargsok(ret::BlasInt)
3131
end
3232

3333
"Handle all nonzero info codes"
34-
function chklapackerror(ret::BlasInt)
34+
function chklapackerror(ret::BlasInt, f...)
3535
if ret == 0
3636
return
3737
elseif ret < 0
3838
throw(ArgumentError("invalid argument #$(-ret) to LAPACK call"))
3939
else # ret > 0
40-
throw(LAPACKException(ret))
40+
chklapackerror_positive(ret, f...)
4141
end
4242
end
4343

44+
chklapackerror_positive(ret, f...) = throw(LAPACKException(ret))
45+
4446
function chknonsingular(ret::BlasInt)
4547
if ret > 0
4648
throw(SingularException(ret))
@@ -3571,11 +3573,12 @@ for (trtri, trtrs, elty) in
35713573
uplo, trans, diag, n, size(B,2), A, max(1,stride(A,2)),
35723574
B, max(1,stride(B,2)), info,
35733575
1, 1, 1)
3574-
chklapackerror(info[])
3576+
chklapackerror(info[], trtrs!)
35753577
B
35763578
end
35773579
end
35783580
end
3581+
chklapackerror_positive(ret, ::typeof(trtrs!)) = chknonsingular(ret)
35793582

35803583
"""
35813584
trtri!(uplo, diag, A)

stdlib/LinearAlgebra/test/triangular.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,7 @@ for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFlo
510510
@test_throws DimensionMismatch Ann'\bm
511511
@test_throws DimensionMismatch transpose(Ann)\bm
512512
if t1 == UpperTriangular || t1 == LowerTriangular
513-
if elty1 === eltyB <: BlasFloat
514-
@test_throws LAPACKException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n))
515-
else
516-
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n))
517-
end
513+
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n))
518514
end
519515
@test B/A1 B/Matrix(A1)
520516
@test B/transpose(A1) B/transpose(Matrix(A1))

0 commit comments

Comments
 (0)