Skip to content

Keep the source type out of the launch dispatch of reductions along dims - #212

Merged
maleadt merged 2 commits into
mainfrom
tb/reduce-compile-time
Nov 13, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/reduce-compile-time

Conversation

@maleadt

@maleadt maleadt commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #211.

The first reduction along dims of 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:

  • The launch dispatch. kernel_buffer returned one of three types, so for every new source type launch_segments! was inferred three times and its with_segments closure six times. The segment indices are now built from the setup's launch alone, and launch! is called once from mapreduce_nd!. The launches take the kind of buffer as a Val and 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.
  • The setup. reducedim_setup and select_launch took the source, although they only need its element type, size and memory layout. They now take a source_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 of mapreducedim!.

First reduction of a new source type (median of 5–7, RTX 5080, Julia 1.13, CUDA.jl 6.5.1):

type name length GPUArrays 11 before after
#211 reproducer, NDATA=0 82 0.451 s 0.414 s 0.248 s
#211 reproducer, NDATA=48 2192 0.328 s 0.659 s 0.301 s
#211 reproducer, NDATA=96 4304 0.414 s 1.049 s 0.368 s
Oceananigans sum(o) 18082 0.784 s 0.828 s 0.606 s
Oceananigans sum(o; dims=3) 18082 0.580 s 0.833 s 0.546 s
Oceananigans maximum(o) 18082 1.263 s 1.302 s 1.028 s
Oceananigans Field(Integral(o; dims=3)) 18082 0.723 s 0.769 s 0.624 s

The Oceananigans operand is u * v + ∂x(T) + KernelFunctionOperation{Center, Center, Center}(kf, grid, u, T) on a 16³ GPU grid, with a fresh kf for every measurement. The GPUArrays 11 column uses CUDA.jl 6.4.2, GPUArrays 11.5.16 and KernelAbstractions 0.9.46, with Oceananigans main for the Oceananigans rows (mg/ka-0.10 for the others).

The frames still inferred for every source type are those that launch the kernels: mapreducedim_run! and mapreduce_nd! (about 25 ms), and launch! through mapreduce_slices!, foreachindex_on and 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 dims is unchanged (3.1–3.3 µs for dense, view and permuted sources, 3.7–3.9 µs for Broadcasted ones), with 4–10 fewer allocations per call.

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
maleadt force-pushed the tb/reduce-compile-time branch from 37b12ec to a9e3b3f Compare November 12, 2026 16:23
@maleadt
maleadt merged commit 6d49852 into main Nov 13, 2026
16 of 17 checks passed
@maleadt
maleadt deleted the tb/reduce-compile-time branch November 13, 2026 07:31
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.

Compile time of mapreducedim! grows with the size of the source type (2.7× slower than GPUArrays 11 for large lazy sources)

1 participant