Repository navigation
Conversation
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
force-pushed
the
tb/launch-layout-key
branch
from
December 3, 2026 18:54
1892741 to
9d533c9
Compare
@cuda backend=cuTile behind one function barrier
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.
A
TileArray's type records the layout facts its kernel is compiled for: the index type (Int32orInt64) and, in itsArraySpec, the alignment, contiguity and divisibility of the array. The arguments a launch converts therefore have types only known at run time. CUDACore's@cudapipeline then builds itsKernelCalland theNamedTupleof 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, forTileBackend. The kernel function and every argument are converted once, withKernelAdaptorand so with every Adapt rule as before, and passed as separate arguments to one@noinlinefunction. 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:
KernelCall,kernel_compile,kernel_launchandrebindkeep CUDACore's path, sharing the conversion, the lookup and the parameter launch.cuTile.launchgoes throughkernel_pipelinelike@cuda.Extrudedleaf at run time.This replaces an earlier version of this PR that converted arrays to a layout-independent
LaunchArrayand 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 rebuiltTileArrays on a miss, which isn't sound for Adapt rules that transform values.RTX 5080, Julia 1.13, three interleaved runs, against #392:
@cuda@cuda@cudalaunch=falsect.@.broadcastsum!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.