Add start/stop keywords to reverse and reverse! - #119
Conversation
|
How does this behave when incompatible args are used together? In base, |
a9ca217 to
06a8575
Compare
|
start/stop + dims already threw ArgumentError (mutually exclusive). |
d4f280b to
9fcc67e
Compare
|
@christiangnrd and @maleadt Can we get a review? This should land right after #114 and #118 |
9fcc67e to
e499dc8
Compare
|
Resolved conflicts. |
|
Taking a look. |
Reverse a vector subrange in place or into a separate destination, with omitted bounds defaulting to the ends. Keep bounds exclusive with dimension selection and use Base.reverse! semantics for trivial ranges. Reuse the in-place loop through a view and fuse out-of-place copying with reversal, avoiding extra passes and synchronous device copies.
e499dc8 to
c6e14bc
Compare
start/stop keywords to reverse and reverse!
|
Rebased and cleaned up. I'm not sure this has much value though; what does it add over |
|
The only real thing it adds over a view is the out-of-place case: reverse(v; start, stop) gives you a full array with the middle reversed and the ends copied in one pass. I'm fine either way if it's not worth the extra API I can close it. So its what do you think, would be better in this case? |
|
Closed as per discussed |
|
Yeah, we can always revisit this when people request it, or when it turns out necessary for a Base API. |
Reverse part of a vector without changing the elements outside it. This extends the
whole-array and
dimsforms from #114 to support the subranges accepted byBase.reverse!(v, start, stop). Bounds are keywords because AK uses positionalarguments for the backend.
Either integer bound may be omitted, defaulting to that end of the vector. Bounds
are supported only for vectors and cannot be combined with
dimsother than:.For
start < stop, both bounds must be in bounds. Forstart >= stop, the in-placeform does nothing and the out-of-place forms copy the source, without checking
bounds. This follows Base's in-place behavior; Base's allocating
reversecanthrow for out-of-bounds empty or singleton ranges. A destination must have the same
length as the source and must not alias it.
In place, the implementation reverses a view using the existing loop. Out of place,
one pass copies elements outside the range and reverses those inside it. This avoids
processing the reversed range twice and avoids separate device copies that can
synchronize on Metal.
For context, these earlier review measurements use
Float32vectors of length2^26, Julia 1.12.7, and synchronized medians of 30 samples (milliseconds):
The in-place keyword form measured similarly to reversing a view. Its work scales
with the range length; out of place, the whole vector still needs copying. CUDA.jl
measured 0.363 ms in place and 0.660 ms allocating for the middle half on the RTX
5080; AK's out-of-place measurement uses a preallocated destination. The Intel
middle-half copy/reversal was about 15% slower than its whole-vector reversal.