Launch @kernel kernels on N-d grids, indexing in 32 bits #797
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.45) × (1 − Review Confidence (0.64)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 427 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 70%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.363
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #797"
general
weight: 0.35
maleadt
@indexassumes that a kernel was launched on a 1-D grid. To get Cartesian indices, every work-item decomposes its linear group and local id with 64-bit integer divisions: 2(N−1) of them for a dynamically-sized N-dndrange, and the same again for the bounds check (#470, #396). On GPUs, that is often most of a kernel's index arithmetic.This PR lets a backend launch a kernel on a grid with the shape of the iteration space, and lets
@indexcompute in 32 bits. With the CUDA.jl side of it (JuliaGPU/CUDA.jl#3304), on an RTX 5080, compared to the KA 0.10 port (JuliaGPU/CUDA.jl#3302), both with the inlining fix from #798:| kernel | before | after |
|---|---|---|
| broadcast 3-D F32
A . v(256³) | 163 µs | 80 µs (−51%) || broadcast 3-D Int8
A .+ v| 633 µs | 296 µs (−53%) || broadcast 4-D F32
A . v| 255 µs | 191 µs (−25%) ||
permutedims3-D | 275 µs | 230 µs (−16%) || KA 3-D Cartesian kernel, dynamic ndrange | 179 µs | 161 µs (−10%) |
| KA 4-D Cartesian kernel, dynamic ndrange | 249 µs | 192 µs (−23%) |
1-D and memory-bound 2-D kernels don't change. Oceananigans runs 1–3% faster per F32 time step. There are more numbers in the CUDA.jl PR.
How it works
The hidden context argument of a
@kernel(CompilerMetadata) gets alaunchfield that says how the backend launched the kernel, and the index functions specialize on it:NDLaunch{T}: the grid has the shape of the iteration space, so the hardware group and local ids are the Cartesian positions and no divisions are needed. This works for as many dimensions as the hardware grid has (3, according to the KernelInterface limits); larger iteration spaces use a linear launch.LinearLaunch{T}: the 1-D grid used so far.Either way,
@indexcomputes inT:Int32whenever the padded iteration space fits,Intotherwise. It still returnsInts. Linear indices keep their x-fastest order, so sub-groups still hold consecutive work-items.KA.select_launchchooses the launch from the iteration space and the backend's limits (KI.max_work_group_size,KI.max_work_group_dimsandKI.max_num_groups). When the workgroup size is tuned after compiling, the choice has to hold for every size that tuning can pick, because the launch is part of the context type and thus of the compiled kernel. It does, because tuning distributes work-items the same wayselect_launchassumes (KA.launch_workgroupsize). An iteration space with more thantypemax(Int)work-items is now rejected with anArgumentError.Adopting this is optional for a backend: a context without a launch is indexed as before (now with the same code, as a
LinearLaunch{Int}). PoCL adopts it here.docs/src/implementations.mddescribes what a backend's launch has to do; #801 then moves that launch into KA, so that backends don't have to.Packages that customize the iteration space (a custom
partitionorexpand, like Oceananigans) keep working: only the iteration spaces KA creates itself take the direct path, and the launch is chosen from the iteration space that is actually launched. AnAdaptrule forCompilerMetadatahas to pass thelaunchalong, though.The second commit also fixes two bugs in the default indexing path: 0-d ndranges threw a bounds error, and under
--check-bounds=yesthe index bounds checks crashed PoCL's compiler for kernels with@synchronize(pocl/pocl#2345).