Skip to content

Add a hook to switch compute backends - #308

Closed
meggart wants to merge 12 commits into
mainfrom
fg/compute_backends
Closed

meggart wants to merge 12 commits into
mainfrom
fg/compute_backends

Conversation

@meggart

@meggart meggart commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

As part of the discussion in #273 we discussed the necessity for optional compute backends. This is a draft implementation that let's users switch backends using Preferences.jl. It is also possible to set the backend to "dynamic", which let's you switch backends at runtime but involves some dynamic dispatch each time a computation is triggered (a cost which should be negligible, given we are concerned with operations that involve IO). A first backend would be DiskArrayEngine, the PR is here: meggart/DiskArrayEngine.jl#69

Comment thread src/compute.jl
Comment thread src/compute.jl Outdated
- Define `DiskArrayEngineBackend` in DiskArrays itself and add a MethodError
  hint pointing to DiskArrayEngine.jl. Drops the DiskArrayEngine dependency
  and extension, which only existed to import the type.
- `DynamicBackend` now holds a `Union{DefaultBackend,DiskArrayEngineBackend}`
  so dispatch on it is union-split into a branch instead of a dynamic
  dispatch. `set_dynamic_backend!` switches it at runtime.
- Backends unknown to DiskArrays are attached per array with
  `withbackend(array, backend)`. Entry points find the backend with
  `get_backend(array)`, which walks up `parent` until `parent(a) === a`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
asinghvi17 and others added 6 commits September 18, 2026 12:00
The union-split branch costs ~nothing (measured: same time and allocations
as a hard-coded DefaultBackend), so runtime switching no longer needs a
"dynamic" preference. The preference now only selects the backend that is
active at load time, and `set_backend` also switches the running session
instead of asking for a restart.

Also force specialization on `f` in the `sum`/`prod`/.../`extrema`/`mean`
entry points. `f::Function` is only passed through there, so Julia did not
specialize on it and the inner call was a dynamic dispatch: `sum(a)` on a
2-element array goes from 142 ns / 11 allocs to 61 ns / 7 allocs.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
- `unique` visits chunks in a different order than an Array's elements, so
  test with `issetequal` (of the `f` values for `unique(f, x)`, where the
  first representative differs) instead of `@test_broken ... ==`, which
  passed or failed depending on the random data.
- Qualify `Base.mapreducedim!`, size the output from `ndims` instead of
  assuming 3 dimensions, and start the reference from `zero(R)` instead of
  uninitialized memory.
- Only test `median(dims=2)` for ndims >= 2 and `cumsum`/`cumprod` without
  `dims` for vectors.
- `init=0.0` for float data, an Int `init` with `dims` throws in Base too.
- Compare chunked float reductions with `≈`, and do not `count` floats.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
…I on stacked PRs

`count`, `unique`, `extrema` and `mean` were only defined for
`DefaultBackend`, so unlike the other hooks a backend had to implement them
or they threw a MethodError. They now have a `::ComputeBackend` fallback
like `sum`, `mapreduce` etc.

CI only ran for pull requests into `main`, so stacked PRs were never tested.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
…erred`

On Julia 1.10 the default `sum` over chunks infers `Any`, also on `main`
and with a hard-coded `DefaultBackend`. What the backend tests should
guarantee is that the indirection through `DynamicBackend` does not change
the inferred type.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ra7EAJeFE1dtekwf4Tzxh
@meggart
meggart marked this pull request as ready for review September 21, 2026 15:06
@meggart meggart changed the title WIP: Add a hook to switch compute backends Add a hook to switch compute backends Sep 21, 2026
@meggart

meggart commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator Author

Replaced by #316

@meggart meggart closed this Sep 21, 2026
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.

2 participants