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

Overload cholesky method in order to prevent scalar indexing for Diagonal #384

Merged
merged 10 commits into from
Dec 31, 2021
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
14 changes: 14 additions & 0 deletions src/host/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ end

Base.copy(D::Diagonal{T, <:AbstractGPUArray{T, N}}) where {T, N} = Diagonal(copy(D.diag))

# prevent scalar indexing
function LinearAlgebra.cholesky!(D::Diagonal{T, <:AbstractGPUArray{T, N}},
::Val{false} = Val(false); check::Bool = true
) where {T, N}
info = 0
if mapreduce(x -> isreal(x) && isposdef(x), &, D.diag)
D.diag .= sqrt.(D.diag)
else
info = findfirst(x -> !isreal(x) || !isposdef(x), collect(D.diag))
check && throw(PosDefException(info))
end
Cholesky(D, 'U', convert(LinearAlgebra.BlasInt, info))
end


## matrix multiplication

Expand Down
14 changes: 14 additions & 0 deletions test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@test compare(x -> permutedims(x, [2,1,4,3]), AT, randn(ComplexF32,3,4,5,1))
end


@testset "symmetric" begin
@testset "Hermitian" begin
A = rand(Float32,2,2)
Expand Down Expand Up @@ -99,6 +100,19 @@
@test collect(D) == collect(C)
end

@testset "cholesky + Diagonal" begin
n = 128
d = AT(rand(Float32, n))
D = Diagonal(d)
F = collect(D)
Copy link
Contributor Author

@simsurace simsurace Dec 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modification avoids calling a dense cholesky method, which is not available for oneArrays

@test collect(cholesky(D).U) ≈ collect(cholesky(F).U)
@test collect(cholesky(D).L) ≈ collect(cholesky(F).L)

d = AT([1f0, 2f0, -1f0, 0f0])
D = Diagonal(d)
@test cholesky(D, check = false).info == 3
end

@testset "$f! with diagonal $d" for (f, f!) in ((triu, triu!), (tril, tril!)),
d in -2:2
A = randn(Float32, 10, 10)
Expand Down