Repository navigation
Run operations on BitArrays on the host, writing to them on one task - #210
Open
sasmith-14 wants to merge 1 commit into
Open
sasmith-14 wants to merge 1 commit into
sasmith-14 wants to merge 1 commit into
Conversation
sasmith-14
force-pushed
the
bitarray-host
branch
from
November 13, 2026 09:44
951e349 to
7583c29
Compare
A `BitArray` lives in host memory, but KernelAbstractions only implements `get_backend` for `Array`, so every operation on one threw unless given a `backend` (JuliaGPU#143). Arrays whose memory is a `BitArray`, including Base's views and reshapes of one, now vote for the host backend. A `BitArray` packs 64 elements into each chunk, so tasks writing neighbouring elements race on a chunk: with an explicit backend, `map!`, `reverse!`, `accumulate!` and `sort!` into a `BitArray` already returned corrupted results on several threads. Operations that write to one (`map!`, `reverse!`, `accumulate!`, `sort!`, `sort_by_key!` and `mapreducedim!`) now use a single task.
sasmith-14
force-pushed
the
bitarray-host
branch
from
November 23, 2026 10:25
7583c29 to
592d8fe
Compare
Author
|
Hi @maleadt, could you review this when you get a chance? Thanks! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #143
operations on a
BitArraythrew unless you passedbackend, since KernelAbstractions only implementsget_backendforArray:all 20 operations i tried failed like this on main (
sum,count,findall,cumsum,sort,reverse,map, ...), and on views, reshapes and transposes of aBitArraytoo. arrays whose memory is aBitArraynow vote for the host backend (storage_backendinsrc/algorithms.jl).fixing that showed a race that already happens with an explicit
backend: aBitArraypacks 64 elements into each chunk, so tasks writing neighbouring elements overwrite each other's bits. on main withbackend=AK.HOST_BACKENDand 8 threads, 2000 runs on 1000-element arrays gave wrong results formap!(1154),map(1118),reverse!(832),accumulate!(597) and SampleSortsort!(1). so operations that write into aBitArray(map!/map,reverse!,accumulate!,sort!,sort_by_key!,mapreducedim!) now use one task. reads are still threaded.this is the simple option from my question on the issue, and it costs speed on big arrays: on my M5 with 8 threads,
AK.map!(!, dst, src)on a 10^7-elementBitVectortakes 18 ms with one task vs 2.9 ms on main with 8 (racing) tasks. splitting the work at multiples of 64 elements would get the threads back without the race. happy to do that here or in a follow-up if you'd prefer it.tested on an M5 (macOS 26.5.1, Julia 1.13.3):
test/bitarray.jl(89 tests): backends ofBitArrays and their wrappers, the operations withoutbackendfor lengths 1, 63, 64, 65 and 1000, and wrong results counted over 200 runs of each writing operation with 8 taskssrcchanges the new tests error; with only the one-task guard removed, all 9 race tests fail at 2 threads (8 of 9 at 8)--cpu --cpu-ka, 2 threads): 97,871 pass, 0 fail (97,781 on main)foreachindex/foraxesstill run the user's loop on several tasks, so writing into aBitArrayfrom them races likeThreads.@threadsdoes. i left those alone since AK can't know what the loop writes.