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
8 changes: 8 additions & 0 deletions src/host/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ end

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

# copy only the diagonal; the generic `AnyGPUArray` method would launch a kernel over all
# n^2 entries, calling `setindex!(::Diagonal, ...)` whose error path isn't GPU-compatible
function Base.copyto!(D1::Diagonal{T, <:AbstractGPUArray{T, N}},
D2::Diagonal{S, <:AbstractGPUArray{S, M}}) where {T, N, S, M}
copyto!(D1.diag, D2.diag)
return D1
end

_isrealandpositive(x) = isreal(x) && real(x) > 0

function LinearAlgebra.cholesky!(D::Diagonal{<:Any, <:AbstractGPUArray},
Expand Down
12 changes: 12 additions & 0 deletions test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ end
@test collect(D) == collect(C)
end

@testset "copyto! diagonal" begin
D = Diagonal(AT(rand(Float32, 10)))
C = Diagonal(AT(zeros(Float32, 10)))
@test copyto!(C, D) === C
@test C.diag isa AT
@test collect(C) == collect(D)

D = Diagonal(AT(Int32.(1:10)))
copyto!(C, D)
@test collect(C) == Diagonal(Float32.(1:10))
end

@testset "cholesky + Diagonal" begin
n = 128
# Add one in order prevent failures due to random numbers being zero
Expand Down
Loading