Skip to content

Launch @cuda backend=cuTile behind one function barrier - #394

Open
maleadt wants to merge 4 commits into
tb/launch-overheadfrom
tb/launch-layout-key
Open

maleadt wants to merge 4 commits into
tb/launch-overheadfrom
tb/launch-layout-key

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

A TileArray's type records the layout facts its kernel is compiled for: the index type (Int32 or Int64) and, in its ArraySpec, the alignment, contiguity and divisibility of the array. The arguments a launch converts therefore have types only known at run time. CUDACore's @cuda pipeline then builds its KernelCall and the NamedTuple of the call's sources at run time (~0.5 µs per launch), and a launch with arrays of a new layout compiled the host launch code again, 60–110 ms each time.

This overrides CUDACore.kernel_pipeline, the hook CUDACore documents for intercepting a launch as a whole, for TileBackend. The kernel function and every argument are converted once, with KernelAdaptor and so with every Adapt rule as before, and passed as separate arguments to one @noinline function. Dispatch resolves their types; behind it, overlap labelling, the per-signature kernel lookup of #392 and the launch are static. Conversion records the arrays' memory for CUDACore's managed launch, as #396 does.

Some details:

  • The function behind the barrier is compiled for every layout, so it does little: it looks the kernel up and flattens the arguments to the kernel's parameters, whose types depend only on element types, ranks and index types. Launching the parameters, including CUDACore's managed launch, and labelling overlapping arrays are compiled once for all layouts.
  • KernelCall, kernel_compile, kernel_launch and rebind keep CUDACore's path, sharing the conversion, the lookup and the parameter launch. cuTile.launch goes through kernel_pipeline like @cuda.
  • Broadcasts and reductions convert their arrays once and launch the same way (second commit). A broadcast collects its array leaves from the host-side tree, whose type is static, and builds the kernel's tree behind one barrier too (third commit), rather than constructing every node and Extruded leaf at run time.

This replaces an earlier version of this PR that converted arrays to a layout-independent LaunchArray and selected the kernel by a layout key. That launched slightly faster with two arrays and compiled less per new layout, but needed a second conversion for the arrays it handled, a separate path for everything else (user structs with Adapt rules, NarrowArray), and rebuilt TileArrays on a miss, which isn't sound for Adapt rules that transform values.

RTX 5080, Julia 1.13, three interleaved runs, against #392:

#392 this PR CUDA.jl @cuda
2 arrays, @cuda 2.38–2.46 µs, 1104 B 1.88–1.92 µs, 528 B 1.46–1.49 µs
9 arrays, @cuda 3.13–3.35 µs, 3216 B 2.43 µs, 1344 B 1.61–1.68 µs
2 / 9 arrays, launch=false 0.73 / 1.47 µs 0.07 / 0.24 µs
struct with an Adapt rule 2.55–2.61 µs 1.96–1.97 µs
ct.@. broadcast 4.62 µs 2.33–2.48 µs, 656 B
sum! 7.23–7.27 µs 6.88–6.98 µs

In a fresh session with the CUBINs cached on disk, the first launch takes 0.41–0.48 s instead of 0.57–0.62 s with two arrays and 0.52–0.58 s instead of 0.62–0.69 s with nine, a second kernel 30–43 ms instead of 63–110 ms, and a new layout of the same kernel (e.g. an offset view) 23–49 ms instead of 65–114 ms. The package image grows from 35.6 to 37.5 MiB.

What remains of the gap to CUDA.jl: each converted array is boxed (its type is only known at run time), and a kernel takes a pointer, sizes and strides per array, so two 1-D arrays make seven parameters against CUDA.jl's three.

Depends on #396.

@maleadt
maleadt added this pull request to stack #395 December 3, 2026 16:23
Converting an array for a launch took its pointer with `pointer`, which,
outside an operation that CUDACore submits, takes ownership of the memory
for the task's stream and marks it escaped. A launch on another stream
(`@cuda backend=cuTile stream=s`, or `cuTile.launch` with `stream=s`) was
then not ordered after work on the task's stream that used the array, and
later work on the task's stream was not ordered after the launch: both
read stale data. Only memory last used on a third stream was ordered, by
blocking the host until that stream finished.

Do what CUDACore's conversion of a `CuArray` for its own kernels does:
`kernel_convert(::TileBackend, x, managed)` converts with a
`KernelAdaptor` that records the memory of `CuArray`s and their strided
views in `managed` and computes their address without taking ownership,
and CUDACore's launch acquires that memory on the launch's stream.
`cuTile.launch` now launches through the same pipeline as `@cuda`.
`cuTileconvert` and other arrays take their pointer as before.

CUDACore's `check_capture`, which the conversion uses as CUDACore's own
does, needs CUDACore 6.5.
A converted array's type records its layout, so it is only known at run
time. CUDACore's `@cuda` pipeline then builds its `KernelCall` and the
`NamedTuple` of the call's sources at run time (~0.5 µs), and a launch
with arrays of a new layout compiled the host launch code again, 60-110 ms
each time.

Override `CUDACore.kernel_pipeline`, the hook CUDACore documents for
intercepting a launch as a whole, for `TileBackend`: convert the kernel
function and every argument once, with `KernelAdaptor` and therefore all
Adapt rules as before, and pass the results as separate arguments to
`compile_and_launch`. Dispatch resolves their types, and behind it
compiling and launching are static. `KernelCall`, `kernel_compile`,
`kernel_launch` and `rebind` keep CUDACore's path, sharing the conversion,
`compile_converted` and `launch_params`.

The function behind the barrier is compiled for every layout, so it does
little: it looks the kernel up and flattens the arguments to the kernel's
parameters, whose types depend only on element types, ranks and index
types. Launching the parameters, including CUDACore's managed launch, and
labelling overlapping arrays are compiled once for all layouts. A new
layout now takes 23-31 ms with two arrays and 33-49 ms with nine,
including the device-side compilation that finds its CUBIN in the cache.

RTX 5080, Julia 1.13, three interleaved runs, before -> after:
two-array launch 2.38-2.46 -> 1.88-1.92 µs, 1104 -> 528 B; nine arrays
3.13-3.35 -> 2.43 µs; `launch=false` 0.73 -> 0.07 µs (two arrays), 1.44-1.50
-> 0.24 µs (nine); CUDA.jl takes 1.46-1.49 and 1.61-1.65 µs. A fresh
session's first launch takes 0.41-0.48 instead of 0.57-0.62 s with two
arrays and 0.52-0.58 instead of 0.62-0.69 s with nine, and a second kernel
30-43 instead of 63-110 ms. The package image grows from 35.6 to 37.5 MiB.
Broadcasts and reductions converted their arrays with `cuTileconvert`,
taking ownership of their memory on the task's stream, and `launch` then
converted the converted values again. Reductions now pass their arrays
to `launch`, which converts them once. A broadcast converts its
destination and leaves itself, as preprocessing needs the converted
destination, with an adaptor that records their memory, and goes to
`compile_and_launch` directly. A small `ct.@.` broadcast takes ~3.9 instead
of ~4.6 µs, and a `sum!` ~6.9 instead of ~7.2 µs.
A converted array's type records its layout, so the tree that `preprocess`
built from converted leaves had types known only at run time: every node
and `Extruded{keeps}` leaf was constructed through dynamic dispatch and
run-time type instantiation, as were the `Constant`s of the tile sizes,
~1.6 µs of a ~4 µs `ct.@.` broadcast.

Collect the array leaves from the host-side tree, whose type is static,
convert them and the destination to separate arguments, and pass them with
the leaves' extrusion masks, tile sizes and grid overflow, as one `Val`, to
a function that is specialized on all of them. Behind it, checking leaves
for overlap with the destination and building the tree are static. A small
`ct.@.` broadcast takes ~2.4 instead of ~4.0 µs, with 17 instead of 41
allocations; the first broadcast in a session compiles no more than before.
@maleadt
maleadt force-pushed the tb/launch-layout-key branch from 1892741 to 9d533c9 Compare December 3, 2026 18:54
@maleadt maleadt changed the title Look up launched kernels by array layout rather than type Launch @cuda backend=cuTile behind one function barrier Dec 3, 2026

This branch has not been deployed

No deployments
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.

1 participant