Skip to content

Run operations on BitArrays on the host, writing to them on one task - #210

Open
sasmith-14 wants to merge 1 commit into
JuliaGPU:mainfrom
sasmith-14:bitarray-host
Open

sasmith-14 wants to merge 1 commit into
JuliaGPU:mainfrom
sasmith-14:bitarray-host

Conversation

@sasmith-14

Copy link
Copy Markdown

fixes #143

operations on a BitArray threw unless you passed backend, since KernelAbstractions only implements get_backend for Array:

import AcceleratedKernels as AK
AK.sum(trues(100))
# main:    ArgumentError: Implement `KernelAbstractions.get_backend(::BitVector)`
# this PR: 100

all 20 operations i tried failed like this on main (sum, count, findall, cumsum, sort, reverse, map, ...), and on views, reshapes and transposes of a BitArray too. arrays whose memory is a BitArray now vote for the host backend (storage_backend in src/algorithms.jl).

fixing that showed a race that already happens with an explicit backend: a BitArray packs 64 elements into each chunk, so tasks writing neighbouring elements overwrite each other's bits. on main with backend=AK.HOST_BACKEND and 8 threads, 2000 runs on 1000-element arrays gave wrong results for map! (1154), map (1118), reverse! (832), accumulate! (597) and SampleSort sort! (1). so operations that write into a BitArray (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-element BitVector takes 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):

  • new test/bitarray.jl (89 tests): backends of BitArrays and their wrappers, the operations without backend for lengths 1, 63, 64, 65 and 1000, and wrong results counted over 200 runs of each writing operation with 8 tasks
  • without the src changes the new tests error; with only the one-task guard removed, all 9 race tests fail at 2 threads (8 of 9 at 8)
  • with the fix: 10 of 10 runs pass at 2 and 8 threads, on KernelAbstractions 0.10.6 and 0.9.45
  • full suite (--cpu --cpu-ka, 2 threads): 97,871 pass, 0 fail (97,781 on main)

foreachindex/foraxes still run the user's loop on several tasks, so writing into a BitArray from them races like Threads.@threads does. i left those alone since AK can't know what the loop writes.

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

Copy link
Copy Markdown
Author

Hi @maleadt, could you review this when you get a chance? Thanks!

This branch has not been deployed

No deployments
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.

Operations on a BitArray fail without an explicit backend

1 participant