Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 36 additions & 4 deletions src/host/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ function LinearAlgebra.mul!(C::Diagonal{<:Any, <:AbstractGPUArray},
return C
end

function LinearAlgebra.mul!(C::Diagonal{<:Any, <:AbstractGPUArray},
A::Diagonal{<:Any, <:AbstractGPUArray},
B::Diagonal{<:Any, <:AbstractGPUArray},
α::Number,
β::Number)
dc = C.diag
da = A.diag
db = B.diag
d = length(dc)
length(da) == d || throw(DimensionMismatch("right hand side has $(length(da)) rows but output is $d by $d"))
length(db) == d || throw(DimensionMismatch("left hand side has $(length(db)) rows but output is $d by $d"))
@. dc = α * da * db + β * dc

return C
end
function LinearAlgebra.mul!(C::Diagonal{<:Any, <:AbstractGPUArray},
A::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}},
B::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}}) where {T}
Expand All @@ -324,6 +339,23 @@ function LinearAlgebra.mul!(C::Diagonal{<:Any, <:AbstractGPUArray},
return C
end

function LinearAlgebra.mul!(C::Diagonal{<:Any, <:AbstractGPUArray},
A::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}},
B::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}},
α::Number,
β::Number) where {T}
dc = C.diag
d = length(dc)
m, n = size(A, 1), size(A, 2)
m′, n′ = size(B, 1), size(B, 2)
m == d || throw(DimensionMismatch("left hand side has $m rows but output is $d by $d"))
n′ == d || throw(DimensionMismatch("right hand side has $n′ cols but output is $d by $d"))
C_ = @. α * A * B + β * C
isdiag(C_) || throw(ErrorException("output matrix must be diagonal"))
dc .= diag(C_)
return C
end

function LinearAlgebra.mul!(B::AbstractGPUVecOrMat,
D::Diagonal{<:Any, <:AbstractGPUArray},
A::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}}) where {T}
Expand Down Expand Up @@ -354,8 +386,8 @@ function LinearAlgebra.mul!(B::AbstractGPUVecOrMat,
end

function LinearAlgebra.mul!(B::AbstractGPUVecOrMat,
A::AbstractGPUVecOrMat,
D::Diagonal{<:Any, <:AbstractGPUArray})
A::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}},
D::Diagonal{<:Any, <:AbstractGPUArray}) where {T}
dd = D.diag
d = length(dd)
m, n = size(A, 1), size(A, 2)
Expand All @@ -369,10 +401,10 @@ function LinearAlgebra.mul!(B::AbstractGPUVecOrMat,
end

function LinearAlgebra.mul!(B::AbstractGPUVecOrMat,
A::AbstractGPUVecOrMat,
A::Union{AbstractGPUArray, Adjoint{T,<:AbstractGPUArray{T}}, Transpose{T,<:AbstractGPUArray{T}}},
D::Diagonal{<:Any, <:AbstractGPUArray},
α::Number,
β::Number)
β::Number) where {T}
dd = D.diag
d = length(dd)
m, n = size(A, 1), size(A, 2)
Expand Down
10 changes: 10 additions & 0 deletions test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ end
@test_throws SingularException D \ B
end

#TODO: Refactor
@testset "mul! + Diagonal" begin
@testset "$elty" for elty in (Float32, ComplexF32)
if !(elty in eltypes)
Expand Down Expand Up @@ -380,16 +381,25 @@ end
A = Diagonal(a)
mul!(C, A, B)
@test collect(C.diag) ≈ collect(A.diag) .* collect(B.diag)
C_diag = collect(C.diag)
mul!(C, A, B, α, β)
@test collect(C.diag) ≈ α * collect(A.diag) .* collect(B.diag) .+ β * C_diag
a = AT(diagm(rand(elty, n)))
b = AT(diagm(rand(elty, n)))
C = Diagonal(d)
mul!(C, a, b)
@test collect(C) ≈ Diagonal(collect(a) * collect(b))
C_coll = collect(C)
mul!(C, a, b, α, β)
@test collect(C) ≈ Diagonal(α * collect(a) * collect(b) + β * C_coll)
a = transpose(AT(diagm(rand(elty, n))))
b = adjoint(AT(diagm(rand(elty, n))))
C = Diagonal(d)
mul!(C, a, b)
@test collect(C) ≈ Diagonal(collect(a) * collect(b))
C_coll = collect(C)
mul!(C, a, b, α, β)
@test collect(C) ≈ Diagonal(α * collect(a) * collect(b)) + β * C_coll
end
end

Expand Down
Loading