← Back to Pull requests

Launch @kernel kernels on N-d grids, indexing in 32 bits #797

Closed
unknown closed this pull request without merging on Sep 30, 2026 11:33 UTC
View on GitHub
unknown unknown commented on Sep 27, 2026 11:37 UTC
Author

@index assumes 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-d ndrange, 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 @index compute 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%) |
| permutedims 3-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 a launch field 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, @index computes in T: Int32 whenever the padded iteration space fits, Int otherwise. It still returns Ints. Linear indices keep their x-fastest order, so sub-groups still hold consecutive work-items.

    KA.select_launch chooses the launch from the iteration space and the backend's limits (KI.max_work_group_size, KI.max_work_group_dims and KI.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 way select_launch assumes (KA.launch_workgroupsize). An iteration space with more than typemax(Int) work-items is now rejected with an ArgumentError.

    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.md describes 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 partition or expand, 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. An Adapt rule for CompilerMetadata has to pass the launch along, though.

    The second commit also fixes two bugs in the default indexing path: 0-d ndranges threw a bounds error, and under --check-bounds=yes the index bounds checks crashed PoCL's compiler for kernels with @synchronize (pocl/pocl#2345).

    Diff: 427 lines (+320 / -107) across 5 files Reviewer: maleadt Review duration: 4315m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 70%
    64%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 64% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.1% (MEDIUM) · Higher = larger blast radius
    45.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 64% · Lower = rushed or shallow review
    64%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.2% · Safe to merge
    16.2%