Repository navigation
Keep the source type out of the launch dispatch of reductions along dims - #212
Merged
Merged
Conversation
A reduction along dims went through `launch_segments!`, which took the source and its kernel buffer and called `launch!` from each branch of the `with_segments` closure. `kernel_buffer` returns one of three types, so for every new source type inference visited three specializations of `launch_segments!` and six of the closure, none of which do more than forward the source to the already dynamic `launch!` call. Inferring them costs time proportional to the size of the source type: for lazy arrays that carry many arrays it made the first reduction of a new source twice as slow as with GPUArrays 11. Build the segment indices from the setup's launch alone and call `launch!` once from `mapreduce_nd!`. The launches take the kind of buffer as a `Val` and build the buffer themselves, so that inference visits only the buffer a launch reads, and none where it reads the source (the by-thread kernel of a lazy source). Fixes #211
The setup of a reduction along dims took the source, although it only needs its element type, size and memory layout. Every new source type therefore inferred `reducedim_setup` and `select_launch` again, and the caller's inference of `mapreducedim!` included a generator over the reduced dimensions that captured the source. For large lazy sources this was most of the remaining compile time over GPUArrays 11. Have the setup take a `source_layout` (size, kind of buffer, offset and strides) and the mapped element type instead, compute the reduced dimensions from the sizes, and leave the source out of the setup's result: the run takes it from its own argument.
maleadt
force-pushed
the
tb/reduce-compile-time
branch
from
November 12, 2026 16:23
37b12ec to
a9e3b3f
Compare
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 #211.
The first reduction along
dimsof a new source type took time that grew with the size of that type. Large lazy sources, such as Oceananigans expressions that carry many arrays, therefore compiled up to 2.5× slower than with GPUArrays 11.SnoopCompile shows that the cost is type inference of host frames that take the source but only forward it or read its shape. Inference of a frame is repeated for every new type of its arguments, and takes longer the larger that type is. This PR keeps the source out of those frames:
kernel_bufferreturned one of three types, so for every new source typelaunch_segments!was inferred three times and itswith_segmentsclosure six times. The segment indices are now built from the setup's launch alone, andlaunch!is called once frommapreduce_nd!. The launches take the kind of buffer as aValand build the buffer themselves. Inference then visits only the buffer a launch reads, and none at all for the by-thread kernel of a lazy source, which reads the source.reducedim_setupandselect_launchtook the source, although they only need its element type, size and memory layout. They now take asource_layout(size, kind of buffer, offset and strides) and the mapped element type. Their inference is then shared by sources of every type. The reduced dimensions are computed from the sizes; a generator that captured the source cost 20 ms per source type in the caller's inference ofmapreducedim!.First reduction of a new source type (median of 5–7, RTX 5080, Julia 1.13, CUDA.jl 6.5.1):
NDATA=0NDATA=48NDATA=96sum(o)sum(o; dims=3)maximum(o)Field(Integral(o; dims=3))The Oceananigans operand is
u * v + ∂x(T) + KernelFunctionOperation{Center, Center, Center}(kf, grid, u, T)on a 16³ GPU grid, with a freshkffor every measurement. The GPUArrays 11 column uses CUDA.jl 6.4.2, GPUArrays 11.5.16 and KernelAbstractions 0.9.46, with Oceananigansmainfor the Oceananigans rows (mg/ka-0.10for the others).The frames still inferred for every source type are those that launch the kernels:
mapreducedim_run!andmapreduce_nd!(about 25 ms), andlaunch!throughmapreduce_slices!,foreachindex_onand the KernelAbstractions launch, including Adapt's conversion of the kernel's closure (about 65 ms with the #211 reproducer).The host time of small reductions along
dimsis unchanged (3.1–3.3 µs for dense, view and permuted sources, 3.7–3.9 µs forBroadcastedones), with 4–10 fewer allocations per call.