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
23 changes: 23 additions & 0 deletions src/host/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,29 @@ function Base.copyto!(dest::AnyGPUArray{T}, dstart::Integer,
return dest
end

# `BitArray`s pack their elements into bits, so stage them through a `Vector` on the CPU
function Base.copyto!(dest::AnyGPUArray{T}, dstart::Integer,
src::BitArray, sstart::Integer, n::Integer) where {T}
n == 0 && return dest
temp = Vector{T}(undef, n)
copyto!(temp, 1, src, sstart, n)
copyto!(dest, dstart, temp, 1, n)
return dest
end

Base.copyto!(dest::AnyGPUArray, src::BitArray) = copyto!(dest, 1, src, 1, length(src))

function Base.copyto!(dest::BitArray, dstart::Integer,
src::AnyGPUArray{U}, sstart::Integer, n::Integer) where {U}
n == 0 && return dest
temp = Vector{U}(undef, n)
copyto!(temp, 1, src, sstart, n)
copyto!(dest, dstart, temp, 1, n)
return dest
end

Base.copyto!(dest::BitArray, src::AnyGPUArray) = copyto!(dest, 1, src, 1, length(src))

## generalized blocks of heterogeneous memory

# mirrors the checks of Base's `copyto!(dest, Rdest, src, Rsrc)`
Expand Down
2 changes: 1 addition & 1 deletion src/host/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ ToGPU(A::WrappedArray) = ToGPU(parent(A))
ToGPU(A::Bidiagonal) = ToGPU(A.dv)
ToGPU(A::Tridiagonal) = ToGPU(A.d)
ToGPU(A::Base.LogicalIndex) = ToGPU(A.mask)
function Adapt.adapt_storage(to::ToGPU, xs::Array)
function Adapt.adapt_storage(to::ToGPU, xs::Union{Array,BitArray})
arr = similar(to.array, eltype(xs), size(xs))
copyto!(arr, xs)
arr
Expand Down
46 changes: 46 additions & 0 deletions test/testsuite/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,52 @@ end
@test Array(src) == dst
end
end

# from a `BitArray`, without scalar indexing (#836)
let src = BitVector([true, false, true, true, false])
dst = AT{Bool}(undef, length(src))
copyto!(dst, src)
@test Array(dst) == src

dst = AT(fill(false, 7))
copyto!(dst, 2, src, 3, 2)
@test Array(dst) == [false, true, true, false, false, false, false]

src = BitMatrix([true false; false true])
dst = AT{Bool}(undef, 2, 2)
copyto!(dst, src)
@test Array(dst) == src
end
if Float32 in eltypes
let src = BitVector([true, false, true])
dst = AT{Float32}(undef, 3)
copyto!(dst, src)
@test Array(dst) == Float32[1, 0, 1]
end
end

# to a `BitArray`
let src = AT([true, false, true, true, false])
dst = falses(5)
copyto!(dst, src)
@test dst == Array(src)

dst = falses(7)
copyto!(dst, 2, src, 3, 2)
@test dst == [false, true, true, false, false, false, false]

src = AT([true false; false true])
dst = falses(2, 2)
copyto!(dst, src)
@test dst == Array(src)
end
if Float32 in eltypes
let src = AT(Float32[1, 0, 1])
dst = falses(3)
copyto!(dst, src)
@test dst == [true, false, true]
end
end
end

@testset "copyto! of blocks" begin
Expand Down
9 changes: 9 additions & 0 deletions test/testsuite/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ end
end
end

@testset "sliced setindex, BitArray source" begin
x = AT(zeros(Bool, (2, 3)))
y = BitVector([true, false])
x[:, 2] = y
@test Array(x) == [false true false; false false false]
x[1, 1:3] = BitVector([true, true, false])
@test Array(x) == [true true false; false false false]
end

@testset "sliced getindex and setindex! with ranges" begin
# odd sizes (so the last workgroup is only partially used), ranges in inner and outer
# dimensions, strided and reversed ranges, and integer indices that drop dimensions
Expand Down
Loading