Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete size checks in BLAS.[sy/he]mm! #45605

Merged
merged 3 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
split size checks in left- and right-multiplication
  • Loading branch information
dkarrasch committed Jun 9, 2022
commit 581a2b7f55f5aa7e0df98fb863684d0319bb06e7
58 changes: 42 additions & 16 deletions stdlib/LinearAlgebra/src/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1539,14 +1539,27 @@ for (mfname, elty) in ((:dsymm_,:Float64),
require_one_based_indexing(A, B, C)
m, n = size(C)
j = checksquare(A)
if j != (side == 'L' ? m : n)
throw(DimensionMismatch(lazy"A has size $(size(A)), C has size ($m,$n)"))
end
if size(B,2) != (side == 'L' ? n : j)
throw(DimensionMismatch(lazy"B has second dimension $(size(B,2)) but needs to match second dimension of C, $n"))
end
if size(B,1) != (side == 'L' ? j : m)
throw(DimensionMismatch(lazy"A has second dimension $j but needs to match first dimension of B, $(size(B,1))"))
M, N = size(B)
if side == 'L'
if j != m
throw(DimensionMismatch(lazy"A has first dimension $j but needs to match first dimension of C, $m"))
end
if N != n
throw(DimensionMismatch(lazy"B has second dimension $N but needs to match second dimension of C, $n"))
end
if j != M
throw(DimensionMismatch(lazy"A has second dimension $j but needs to match first dimension of B, $M"))
end
else
if j != n
throw(DimensionMismatch(lazy"B has second dimension $j but needs to match second dimension of C, $n"))
end
if N != j
throw(DimensionMismatch(lazy"A has second dimension $N but needs to match first dimension of B, $j"))
end
if M != m
throw(DimensionMismatch(lazy"A has first dimension $M but needs to match first dimension of C, $m"))
end
end
chkstride1(A)
chkstride1(B)
Expand Down Expand Up @@ -1616,14 +1629,27 @@ for (mfname, elty) in ((:zhemm_,:ComplexF64),
require_one_based_indexing(A, B, C)
m, n = size(C)
j = checksquare(A)
if j != (side == 'L' ? m : n)
throw(DimensionMismatch(lazy"A has size $(size(A)), C has size ($m,$n)"))
end
if size(B,2) != (side == 'L' ? n : j)
throw(DimensionMismatch(lazy"B has second dimension $(size(B,2)) but needs to match second dimension of C, $n"))
end
if size(B,1) != (side == 'L' ? j : m)
throw(DimensionMismatch(lazy"A has second dimension $j but needs to match first dimension of B, $(size(B,1))"))
M, N = size(B)
if side == 'L'
if j != m
throw(DimensionMismatch(lazy"A has first dimension $j but needs to match first dimension of C, $m"))
end
if N != n
throw(DimensionMismatch(lazy"B has second dimension $N but needs to match second dimension of C, $n"))
end
if j != M
throw(DimensionMismatch(lazy"A has second dimension $j but needs to match first dimension of B, $M"))
end
else
if j != n
throw(DimensionMismatch(lazy"B has second dimension $j but needs to match second dimension of C, $n"))
end
if N != j
throw(DimensionMismatch(lazy"A has second dimension $N but needs to match first dimension of B, $j"))
end
if M != m
throw(DimensionMismatch(lazy"A has first dimension $M but needs to match first dimension of C, $m"))
end
end
chkstride1(A)
chkstride1(B)
Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,18 @@ Random.seed!(100)
@test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,Cnn,one(elty),Cmn)
@test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,Cnn,one(elty),Cnm)
@test_throws DimensionMismatch BLAS.symm!('L','U',one(elty),Asymm,Cmn,one(elty),Cnn)
@test_throws DimensionMismatch BLAS.symm!('R','U',one(elty),Asymm,Cnm,one(elty),Cmn)
@test_throws DimensionMismatch BLAS.symm!('R','U',one(elty),Asymm,Cnn,one(elty),Cnm)
@test_throws DimensionMismatch BLAS.symm!('R','U',one(elty),Asymm,Cmn,one(elty),Cnn)
if elty <: BlasComplex
@test_throws DimensionMismatch BLAS.hemm('L','U',Cnm,Cnn)
@test_throws DimensionMismatch BLAS.hemm('R','U',Cmn,Cnn)
@test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,Cnn,one(elty),Cmn)
@test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,Cnn,one(elty),Cnm)
@test_throws DimensionMismatch BLAS.hemm!('L','U',one(elty),Aherm,Cmn,one(elty),Cnn)
@test_throws DimensionMismatch BLAS.hemm!('R','U',one(elty),Aherm,Cnm,one(elty),Cmn)
@test_throws DimensionMismatch BLAS.hemm!('R','U',one(elty),Aherm,Cnn,one(elty),Cnm)
@test_throws DimensionMismatch BLAS.hemm!('R','U',one(elty),Aherm,Cmn,one(elty),Cnn)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ end
C = zeros(eltya,n,n)
@test Hermitian(aherm) * a ≈ aherm * a
@test a * Hermitian(aherm) ≈ a * aherm
# rectangular multiplication
@test [a; a] * Hermitian(aherm) ≈ [a; a] * aherm
@test Hermitian(aherm) * [a a] ≈ aherm * [a a]
@test Hermitian(aherm) * Hermitian(aherm) ≈ aherm*aherm
@test_throws DimensionMismatch Hermitian(aherm) * Vector{eltya}(undef, n+1)
LinearAlgebra.mul!(C,a,Hermitian(aherm))
Expand All @@ -360,6 +363,9 @@ end
@test Symmetric(asym) * Symmetric(asym) ≈ asym*asym
@test Symmetric(asym) * a ≈ asym * a
@test a * Symmetric(asym) ≈ a * asym
# rectangular multiplication
@test Symmetric(asym) * [a a] ≈ asym * [a a]
@test [a; a] * Symmetric(asym) ≈ [a; a] * asym
@test_throws DimensionMismatch Symmetric(asym) * Vector{eltya}(undef, n+1)
LinearAlgebra.mul!(C,a,Symmetric(asym))
@test C ≈ a*asym
Expand Down