diff --git a/src/host/abstractarray.jl b/src/host/abstractarray.jl index 2016f2cb5..ed5214263 100644 --- a/src/host/abstractarray.jl +++ b/src/host/abstractarray.jl @@ -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)` diff --git a/src/host/base.jl b/src/host/base.jl index a1cc229dd..28599c245 100644 --- a/src/host/base.jl +++ b/src/host/base.jl @@ -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 diff --git a/test/testsuite/base.jl b/test/testsuite/base.jl index 55e2bac1e..7f7ff7178 100644 --- a/test/testsuite/base.jl +++ b/test/testsuite/base.jl @@ -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 diff --git a/test/testsuite/indexing.jl b/test/testsuite/indexing.jl index c3c38d423..edb8c624b 100644 --- a/test/testsuite/indexing.jl +++ b/test/testsuite/indexing.jl @@ -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