Skip to content

feat(reverse): reverse a linear sub-range via start/stop - #119

Open
shreyas-omkar wants to merge 5 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/reverse-subrange
Open

feat(reverse): reverse a linear sub-range via start/stop#119
shreyas-omkar wants to merge 5 commits into
JuliaGPU:mainfrom
shreyas-omkar:sh/reverse-subrange

Conversation

@shreyas-omkar

Copy link
Copy Markdown
Member

Adds start/stop keyword arguments to reverse! and reverse, matching Base.reverse!(v, start, stop) / Base.reverse(v, start, stop): reverses only the linear sub-range v[start:stop], leaving the rest in place (in-place) or copied verbatim (out-of-place).

The positional reverse!(v, start, stop) form is unavailable in AK because the second positional argument is the backend, so these are keyword-only. start/stop apply to the flat (dims=:) path and are mutually exclusive with dims; out-of-bounds ranges throw BoundsError, empty ranges (start > stop) are a no-op.

Follow-up to #114. Tested on CPU (threaded) and AMDGPU (RX 9060 XT) — full reverse suite green (1795/1795).

@christiangnrd

Copy link
Copy Markdown
Member

How does this behave when incompatible args are used together? In base, start and stop are only defined for AbstractVector, and dims isn't an option with start and stop

@shreyas-omkar

Copy link
Copy Markdown
Member Author

start/stop + dims already threw ArgumentError (mutually exclusive).
But start/stop were being accepted on N-D arrays too which Base doesn't allow. they're now restricted to vectors with an ArgumentError, matching Base.reverse, while the whole-array default range still works on any array.

shreyas-omkar and others added 5 commits September 3, 2026 13:59
Add a `dims` keyword to `reverse!`/`reverse`, reaching parity with
`Base.reverse` and the vendor reverse kernels. `dims=:` (the default)
keeps the fast flat path - each thread swaps one mirrored pair - while
`dims=d` (an integer or iterable) reverses only along those dimensions
via a general ND kernel written on `foreachindex`, so it runs on every
backend (CUDA/AMDGPU/oneAPI/Metal/POCL) and the CPU-threaded path from
one implementation. Invalid dims throw `ArgumentError`.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Exercise single-dim, multi-dim ((1,2)/(2,3)/(1,3)/:), size-1 degenerate
dims and 3-D arrays across in-place, out-of-place and allocating forms,
plus ArgumentError on out-of-range dims. Verified on CPU-threaded,
AMDGPU (ROCm) and POCL backends.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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