Repository navigation
Inline the range indexing kernel bodies and launch getindex linearly - #839
Open
unsound-mint wants to merge 2 commits into
Open
unsound-mint wants to merge 2 commits into
unsound-mint wants to merge 2 commits into
Conversation
`getindex_generated` and `setindex_generated` were not inlined into their kernels, so every work item passed the index tuples through local memory. Mark both as inline. The getindex kernel only uses the linear index, so launch it over `length(dest)` instead of `size(dest)` and skip the index arithmetic of a multidimensional ndrange. On an RTX 4070 Laptop GPU, `x[:, :, r, :]` and `x[:, :, r, :] = y` on a Float32 array of size (56, 56, 104, 32) go from about 630 and 540 us to 80 us, in line with `copyto!`. Add tests for range indexing with odd sizes, strided and reversed ranges, and indices that drop dimensions.
`vectorized_getindex!` and `vectorized_setindex!` check the indices against the indexed array, and the source length, before launching their kernel, so checking every access again in the kernel only costs time. Without the checks, `x[:, :, r, :]` and `x[:, :, r, :] = y` take about 12% less time on an RTX 5080, a little less than `copyto!`. The source of `setindex!` is now read relative to its `firstindex`. A vector source with offset indices used to throw a `BoundsError` in the kernel, and without the check it would read the wrong elements.
maleadt
force-pushed
the
perf-837-range-indexing
branch
from
October 27, 2026 20:17
491e7f6 to
fc23f30
Compare
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 #837.
Range
getindexandsetindex!(x[:, :, r, :], andcopy(view(x, :, :, r, :)), which calls it) were 7 to 17 times slower thancopyto!on the same data. The kernel bodiesgetindex_generatedandsetindex_generatedwere not inlined into their kernels, so every work item passed the index tuples through local memory: the PTX ofgetindex_kernelhad acalland a__local_depot.@inline @generated, aspermute_linearindexalready is.getindexlaunches overlength(dest)instead ofsize(dest). The kernel only uses the linear index, so a multidimensionalndrangeonly adds index arithmetic (cf. KA.jl-related slowdowns #565).setindex!source is now read relative to itsfirstindex. A vector source with offset indices (e.g.x[2:3, 1] = Base.IdentityUnitRange(5:6)) used to throw aBoundsErrorin the kernel. Without the bounds check it would have read the wrong elements.Float32,rthe second quarter of the third dimension, times in µs.RTX 4070 Laptop GPU, first commit only, median of 300 samples with
CUDA.@sync:getindexbeforesetindex!beforecopyto!RTX 5080, median of 15 runs of 100 calls (
Float16gives the same numbers):getindexmainsetindex!maincopyto!New tests: range indexing with odd sizes (so the last workgroup is only partly used), ranges in inner and outer dimensions, strided and reversed ranges, and integer indices that drop dimensions, for
getindex,viewcopies andsetindex!; andsetindex!with an offset source.