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
13 changes: 7 additions & 6 deletions src/host/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ function transpose_f!(f, B::AnyGPUMatrix{T}, A::AnyGPUMatrix{T}) where T
B
end

function Base.copyto!(A::AbstractGPUArray{T,N}, B::Adjoint{T, <: AbstractGPUArray{T,N}}) where {T,N}
adjoint!(A, B.parent)
end

function Base.copyto!(A::AbstractGPUArray{T,N}, B::Transpose{T, <: AbstractGPUArray{T,N}}) where {T,N}
transpose!(A, B.parent)
# `copyto!` copies all of the source in linear order into the leading elements of a
# destination of any shape. Only when the shapes match can the wrapper be applied in place.
for (W, f!) in ((:Adjoint, :adjoint!), (:Transpose, :transpose!))
@eval function Base.copyto!(A::AbstractGPUArray{T,N}, B::$W{T, <:AbstractGPUArray{T,N}}) where {T,N}
axes(A) == axes(B) && return $f!(A, parent(B))
copyto!(A, 1, Base.unalias(A, B), 1, length(B))
end
end

function Base.copyto!(A::Array{T,N}, B::Adjoint{T, <:AbstractGPUArray{T,N}}) where {T,N}
Expand Down
16 changes: 16 additions & 0 deletions test/testsuite/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
if ComplexF32 in eltypes
@test compare(adjoint!, AT, rand(ComplexF32, 70, 45), rand(ComplexF32, 45, 70))
end

# like Base, `copyto!` copies all of the wrapped source in linear order into the
# leading elements of a destination of any shape
@testset "copyto! from $f of a differently-shaped source" for f in (adjoint, transpose)
copy_wrapped!(x, y) = copyto!(x, f(y))
@test compare(copy_wrapped!, AT, rand(Float32, 0), rand(Float32, 0))
@test compare(copy_wrapped!, AT, rand(Float32, 40), rand(Float32, 32))
@test compare(copy_wrapped!, AT, rand(Float32, 2, 3), rand(Float32, 2, 2))
@test compare(copy_wrapped!, AT, rand(Float32, 70, 50), rand(Float32, 45, 70))
if ComplexF32 in eltypes
@test compare(copy_wrapped!, AT, rand(ComplexF32, 32), rand(ComplexF32, 32))
@test compare(copy_wrapped!, AT, rand(ComplexF32, 3, 3), rand(ComplexF32, 2, 3))
end
@test_throws BoundsError copyto!(AT(rand(Float32, 2, 1)), f(AT(rand(Float32, 2, 2))))
@test compare(x -> copyto!(x, f(x)), AT, rand(Float32, 2, 3))
end
end

@testset "tr" begin
Expand Down
Loading