From 16840f9732b9c5d684b35905eddc9784f9cab4bd Mon Sep 17 00:00:00 2001 From: sasmith-14 <210313780+sasmith-14@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:26:49 +0530 Subject: [PATCH 1/3] Copy from `BitArray`s to GPU arrays without scalar indexing The host-to-device `copyto!` methods only took `Array` sources, so a `BitArray` fell back to Base's elementwise copy. Unpack it into a `Vector` on the CPU first, as is already done for mismatched element types. Fixes #836 --- src/host/abstractarray.jl | 12 ++++++++++++ test/testsuite/base.jl | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/host/abstractarray.jl b/src/host/abstractarray.jl index 2016f2cb5..c05e8b19a 100644 --- a/src/host/abstractarray.jl +++ b/src/host/abstractarray.jl @@ -350,6 +350,18 @@ function Base.copyto!(dest::AnyGPUArray{T}, dstart::Integer, return dest end +# `BitArray`s pack their elements into bits, so unpack them on the CPU too +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)) + ## generalized blocks of heterogeneous memory # mirrors the checks of Base's `copyto!(dest, Rdest, src, Rsrc)` diff --git a/test/testsuite/base.jl b/test/testsuite/base.jl index 55e2bac1e..832b1be40 100644 --- a/test/testsuite/base.jl +++ b/test/testsuite/base.jl @@ -210,6 +210,29 @@ 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 end @testset "copyto! of blocks" begin From 2611aa97f8aa5d31bca90b95969807d07c188ac2 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 19 Nov 2026 15:00:38 +0100 Subject: [PATCH 2/3] Copy from GPU arrays to `BitArray`s without scalar indexing The reverse of the previous change: device-to-host `copyto!` methods only took `Array` destinations, so copying into a `BitArray` fell back to elementwise indexing of the GPU array. Download into a `Vector` first and pack it on the CPU. --- src/host/abstractarray.jl | 13 ++++++++++++- test/testsuite/base.jl | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/host/abstractarray.jl b/src/host/abstractarray.jl index c05e8b19a..ed5214263 100644 --- a/src/host/abstractarray.jl +++ b/src/host/abstractarray.jl @@ -350,7 +350,7 @@ function Base.copyto!(dest::AnyGPUArray{T}, dstart::Integer, return dest end -# `BitArray`s pack their elements into bits, so unpack them on the CPU too +# `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 @@ -362,6 +362,17 @@ 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/test/testsuite/base.jl b/test/testsuite/base.jl index 832b1be40..7f7ff7178 100644 --- a/test/testsuite/base.jl +++ b/test/testsuite/base.jl @@ -233,6 +233,29 @@ end @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 From 83c0f17a0b048686e5a74b71e9f094837f009ebe Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 19 Nov 2026 15:03:44 +0100 Subject: [PATCH 3/3] Upload `BitArray` values in indexed assignment to GPU arrays `x[inds] = v` uploads a CPU `Array` value before launching the kernel, but passed a `BitArray` through as is, which failed to compile. Upload it the same way, now that `copyto!` accepts `BitArray` sources. --- src/host/base.jl | 2 +- test/testsuite/indexing.jl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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/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