Skip to content

Commit 6f02532

Browse files
Use BLAS.trsm! instead of LAPACK.trtrs! in left-triangular solves (#1194)
Co-authored-by: Alexis Montoison <[email protected]>
1 parent e7da19f commit 6f02532

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

Diff for: src/blas.jl

+12-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export
8484
trsm!,
8585
trsm
8686

87-
using ..LinearAlgebra: libblastrampoline, BlasReal, BlasComplex, BlasFloat, BlasInt, DimensionMismatch, checksquare, chkstride1
87+
using ..LinearAlgebra: libblastrampoline, BlasReal, BlasComplex, BlasFloat, BlasInt,
88+
DimensionMismatch, checksquare, chkstride1, SingularException
8889

8990
include("lbt.jl")
9091

@@ -1369,6 +1370,11 @@ for (fname, elty) in ((:dtrsv_,:Float64),
13691370
throw(DimensionMismatch(lazy"size of A is $n != length(x) = $(length(x))"))
13701371
end
13711372
chkstride1(A)
1373+
if diag == 'N'
1374+
for i in 1:n
1375+
iszero(A[i,i]) && throw(SingularException(i))
1376+
end
1377+
end
13721378
px, stx = vec_pointer_stride(x, ArgumentError("input vector with 0 stride is not allowed"))
13731379
GC.@preserve x ccall((@blasfunc($fname), libblastrampoline), Cvoid,
13741380
(Ref{UInt8}, Ref{UInt8}, Ref{UInt8}, Ref{BlasInt},
@@ -2217,6 +2223,11 @@ for (mmname, smname, elty) in
22172223
end
22182224
chkstride1(A)
22192225
chkstride1(B)
2226+
if diag == 'N'
2227+
for i in 1:k
2228+
iszero(A[i,i]) && throw(SingularException(i))
2229+
end
2230+
end
22202231
ccall((@blasfunc($smname), libblastrampoline), Cvoid,
22212232
(Ref{UInt8}, Ref{UInt8}, Ref{UInt8}, Ref{UInt8},
22222233
Ref{BlasInt}, Ref{BlasInt}, Ref{$elty}, Ptr{$elty},

Diff for: src/triangular.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,13 @@ function generic_mattrimul!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function,
12231223
end
12241224
end
12251225
# division
1226-
function generic_trimatdiv!(C::StridedVecOrMat{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractVecOrMat{T}) where {T<:BlasFloat}
1226+
generic_trimatdiv!(C::StridedVector{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractVector{T}) where {T<:BlasFloat} =
1227+
BLAS.trsv!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : copyto!(C, B))
1228+
function generic_trimatdiv!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::StridedMatrix{T}, B::AbstractMatrix{T}) where {T<:BlasFloat}
12271229
if stride(C,1) == stride(A,1) == 1
1228-
LAPACK.trtrs!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, A, C === B ? C : copyto!(C, B))
1230+
BLAS.trsm!('L', uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', isunitc, one(T), A, C === B ? C : copyto!(C, B))
12291231
else # incompatible with LAPACK
1230-
@invoke generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, tfun::Function, A::AbstractMatrix, B::AbstractVecOrMat)
1232+
@invoke generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, tfun::Function, A::AbstractMatrix, B::AbstractMatrix)
12311233
end
12321234
end
12331235
function generic_mattridiv!(C::StridedMatrix{T}, uploc, isunitc, tfun::Function, A::AbstractMatrix{T}, B::StridedMatrix{T}) where {T<:BlasFloat}

Diff for: test/testtriag.jl

+2
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ function test_triangular(elty1_types)
493493
@test_throws DimensionMismatch transpose(Ann) \ bm
494494
if t1 == UpperTriangular || t1 == LowerTriangular
495495
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n))
496+
@test_throws SingularException ldiv!(t1(zeros(elty1, n, n)), fill(eltyB(1), n, 2))
497+
@test_throws SingularException rdiv!(fill(eltyB(1), n, n), t1(zeros(elty1, n, n)))
496498
end
497499
@test B / A1 B / M1
498500
@test B / transpose(A1) B / transpose(M1)

Diff for: test/triangular.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,13 @@ end
886886
end
887887
end
888888

889-
@testset "(l/r)mul! and (l/r)div! for non-contiguous matrices" begin
889+
@testset "(l/r)mul! and (l/r)div! for non-contiguous arrays" begin
890890
U = UpperTriangular(reshape(collect(3:27.0),5,5))
891+
b = float.(1:10)
892+
b2 = copy(b); b2v = view(b2, 1:2:9); b2vc = copy(b2v)
893+
@test lmul!(U, b2v) == lmul!(U, b2vc)
894+
b2 = copy(b); b2v = view(b2, 1:2:9); b2vc = copy(b2v)
895+
@test ldiv!(U, b2v) ldiv!(U, b2vc)
891896
B = float.(collect(reshape(1:100, 10,10)))
892897
B2 = copy(B); B2v = view(B2, 1:2:9, 1:5); B2vc = copy(B2v)
893898
@test lmul!(U, B2v) == lmul!(U, B2vc)

0 commit comments

Comments
 (0)