Skip to content

Copy from BitArrays to GPU arrays without scalar indexing - #849

Merged
maleadt merged 3 commits into
JuliaGPU:mainfrom
sasmith-14:bitarray-copyto
Nov 19, 2026
Merged

maleadt merged 3 commits into
JuliaGPU:mainfrom
sasmith-14:bitarray-copyto

Conversation

@sasmith-14

@sasmith-14 sasmith-14 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #836.

GPUArrays' host-device copies only handled Arrays on the CPU side, so a BitArray fell back to Base's elementwise copy and hit scalar indexing. Because its elements are packed into bits, this stages a BitArray through a Vector on the CPU, as the methods that convert between element types already do:

  • copyto! from a BitArray to a GPU array (this also fixes copy! and append!);
  • copyto! from a GPU array to a BitArray;
  • indexed assignment of a BitArray value, x[inds] = bitvec, which passed the BitArray into the kernel and failed to compile.
mask = BitVector([true, false, true])
copyto!(JLArray{Bool}(undef, 3), mask)       # used to throw, now works
copyto!(falses(3), JLArray(mask))            # likewise
x = JLArray(zeros(Bool, 2, 2)); x[:, 1] = BitVector([true, false])   # likewise

Block copies (copyto! with CartesianIndices) from or to a BitArray still go through Base.

(Used Claude to help with this; I've reviewed the change.)

@sasmith-14

Copy link
Copy Markdown
Contributor Author

hey @maleadt, friendly ping on this one when you get a chance! the red Metal 1.10 job is the Rational crash from JuliaGPU/Metal.jl#1139 (same on the other open PRs, e.g. #857), so it's not from this change. it should go green once JuliaGPU/Metal.jl#1138 is in.

sasmith-14 and others added 3 commits November 19, 2026 14:58
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 JuliaGPU#836
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.
`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.
@maleadt

maleadt commented Nov 19, 2026

Copy link
Copy Markdown
Member

Thanks! I rebased this and added the reverse direction (copyto! into a BitArray) and indexed assignment of a BitArray value, which had the same problem.

@sasmith-14

Copy link
Copy Markdown
Contributor Author

Thanks for extending it, makes sense to cover both directions.

@maleadt
maleadt merged commit 69085e8 into JuliaGPU:main Nov 19, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copyto! from a BitArray to a GPU array falls back to scalar indexing

2 participants