From ba23e442242647b4a70d9a67133a23731c91b8ff Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 5 Oct 2026 21:07:52 +0200 Subject: [PATCH 01/13] Complete the 0.10 release notes and update the NUMA example The 0.10 notes did not mention the Julia 1.10 requirement, that the CPU backend now runs on PoCL (and that `CPU(; static=true)` is gone), the KernelInterface requirement for backends, the temporary removal of the Enzyme extension, `KernelAbstractions.@spawn`, or the removal of `isgpu`. The NUMA example still called `CPU(; static=true)`, which no longer exists. Drop that call and note on its page which of the remarks changed with the PoCL-based CPU backend. Assisted-by: Claude Code --- docs/src/examples/numa_aware.md | 16 ++++++++++++++++ docs/src/index.md | 18 ++++++++++++++++++ examples/numa_aware.jl | 3 --- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/src/examples/numa_aware.md b/docs/src/examples/numa_aware.md index e4df0a408..ed379efa8 100644 --- a/docs/src/examples/numa_aware.md +++ b/docs/src/examples/numa_aware.md @@ -2,6 +2,22 @@ This example demonstrates how to define and run a SAXPY kernel (single-precision `Y[i] = a * X[i] + Y[i]`) such that it runs efficiently on a system with multiple memory domains ([NUMA](https://en.wikipedia.org/wiki/Non-uniform_memory_access)) using multithreading. (You likely will need to fine-tune the value of `N` on your system of interest if you care about the particular measurement.) +!!! compat "KernelAbstractions 0.10" + This page describes the thread-based `CPU` backend of KernelAbstractions 0.9, and the + numbers below were measured with it. Since KernelAbstractions 0.10 the `CPU` backend + runs kernels with PoCL on PoCL's own threads, which changes the remarks below: + + 1) ThreadPinning.jl pins Julia's threads, not the threads that run the kernels. Set + PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used to + pin each of PoCL's threads to a core. + 2) `CPU(; static=true)` no longer exists: there is no dynamic task scheduling to opt + out of, so the example only measures `CPU()`. + 3) `KernelAbstractions.zeros(backend, dtype, N)` fills the array from the calling + thread. To initialize the data with the access pattern of the computational kernel, + allocate it with `KernelAbstractions.allocate` and fill it with a kernel. + + The example has not been measured again with KernelAbstractions 0.10. + ````@eval using Markdown using KernelAbstractions diff --git a/docs/src/index.md b/docs/src/index.md index a7644e22c..8d03add14 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -133,6 +133,24 @@ end ``` ### 0.10 +- KernelAbstractions requires Julia 1.10 or later. +- The `CPU` backend is an OpenCL backend: `CPU` is an alias of `POCLBackend`, which compiles + kernels like the GPU backends do and runs them with [PoCL](https://portablecl.org) on + PoCL's own threads, and no longer on Julia tasks. `CPU(; static=true)` has been removed, + as there is no dynamic task scheduling to opt out of anymore. +- KernelAbstractions is built on [KernelInterface](@ref kernelinterface), which defines + `Backend` and the host-side functions (`allocate`, `synchronize`, …) that + KernelAbstractions re-exports. User code is unaffected, but backends have to implement + KernelInterface, so KernelAbstractions 0.10 needs a release of the backend package that + supports it; see the [notes for backend implementations](@ref implementations_notes). +- The Enzyme extension has been removed temporarily, and is planned to return in a later + 0.10 release. Code that differentiates kernels with Enzyme has to stay on + KernelAbstractions 0.9 until then. +- [`KernelAbstractions.@spawn`](@ref) launches kernels from a task like `Threads.@spawn` + does, and additionally orders them after the work the spawning task has queued, and can + select the device the task uses; see the [Quickstart](@ref). +- `KernelAbstractions.isgpu` has been removed. Like for `GPU` below, query a capability + instead. - `ndrange` entries may be index ranges, given statically (`kernel(backend, workgroupsize, (-2:N+3, 0:M+1))`) or at launch (`ndrange=(-2:N+3, 0:M+1)`, a single range, or a `CartesianIndices`). `@index(Global, Cartesian)` and `@index(Global, NTuple)` return the shifted indices. diff --git a/examples/numa_aware.jl b/examples/numa_aware.jl index e039d0a36..18bac7f8e 100644 --- a/examples/numa_aware.jl +++ b/examples/numa_aware.jl @@ -51,10 +51,7 @@ function measure_membw( return mem_rate, flop_rate end -# Static should be much better (on a system with multiple NUMA domains) measure_membw(CPU()); -measure_membw(CPU(; static = true)); # The following has significantly worse performance (even on systems with a single memory domain)! # measure_membw(CPU(); init=:serial); -# measure_membw(CPU(; static=true); init=:serial); From 69fb1ee7f5c6255fa9ab37519e05aabe90062227 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 5 Oct 2026 22:07:00 +0200 Subject: [PATCH 02/13] Rewrite the NUMA example for the PoCL-based CPU backend The example pinned Julia's threads with ThreadPinning.jl and relied on `KernelAbstractions.zeros` for a parallel first touch. On 0.10 kernels run on PoCL's threads and `zeros` fills from the calling thread, so pin with `POCL_AFFINITY=1` and initialize the data with a kernel instead. The page now describes 0.10 only. Its numbers are from a system with a single memory domain, where the configurations don't differ; the page says that the gain on a system with multiple domains is not measured. Assisted-by: Claude Code --- docs/src/examples/numa_aware.md | 49 +++++++++++---------------------- examples/numa_aware.jl | 29 +++++++++++-------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/docs/src/examples/numa_aware.md b/docs/src/examples/numa_aware.md index ed379efa8..3efa784ae 100644 --- a/docs/src/examples/numa_aware.md +++ b/docs/src/examples/numa_aware.md @@ -1,22 +1,6 @@ # NUMA-aware SAXPY -This example demonstrates how to define and run a SAXPY kernel (single-precision `Y[i] = a * X[i] + Y[i]`) such that it runs efficiently on a system with multiple memory domains ([NUMA](https://en.wikipedia.org/wiki/Non-uniform_memory_access)) using multithreading. (You likely will need to fine-tune the value of `N` on your system of interest if you care about the particular measurement.) - -!!! compat "KernelAbstractions 0.10" - This page describes the thread-based `CPU` backend of KernelAbstractions 0.9, and the - numbers below were measured with it. Since KernelAbstractions 0.10 the `CPU` backend - runs kernels with PoCL on PoCL's own threads, which changes the remarks below: - - 1) ThreadPinning.jl pins Julia's threads, not the threads that run the kernels. Set - PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used to - pin each of PoCL's threads to a core. - 2) `CPU(; static=true)` no longer exists: there is no dynamic task scheduling to opt - out of, so the example only measures `CPU()`. - 3) `KernelAbstractions.zeros(backend, dtype, N)` fills the array from the calling - thread. To initialize the data with the access pattern of the computational kernel, - allocate it with `KernelAbstractions.allocate` and fill it with a kernel. - - The example has not been measured again with KernelAbstractions 0.10. +This example demonstrates how to define and run a SAXPY kernel (single-precision `Y[i] = a * X[i] + Y[i]`) such that it runs efficiently on a system with multiple memory domains ([NUMA](https://en.wikipedia.org/wiki/Non-uniform_memory_access)) with the multithreaded `CPU` backend. (You likely will need to fine-tune the value of `N` on your system of interest if you care about the particular measurement.) ````@eval using Markdown @@ -31,30 +15,29 @@ $(read(path, String)) **Important remarks:** -1) Pin your threads systematically to the available physical (or virtual) CPU-cores. [ThreadPinning.jl](https://github.com/carstenbauer/ThreadPinning.jl) is your friend. -2) Opt-out of Julia's dynamic task scheduling (especially task migration) by using `CPU(; static=true)` instead of `CPU()`. -3) Initialize your data in parallel(!). It is of utmost importance to use a parallel access pattern for initialization that is as similar as possible as the access pattern of your computational kernel. The reason for this is ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.). `KernelAbstractions.zeros(backend, dtype, N)` is your friend. +The `CPU` backend runs kernels with [PoCL](https://portablecl.org) on PoCL's own threads, not on Julia's. That determines how to apply the usual advice for NUMA systems: + +1) Pin the threads that run the kernels. Set PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used, which pins each of PoCL's threads to a core. Tools that pin Julia's threads, like [ThreadPinning.jl](https://github.com/carstenbauer/ThreadPinning.jl), don't affect the threads that run the kernels. +2) Choose the number of threads with `julia -t N` or the `JULIA_KA_CPU_THREADS` environment variable; see [`CPU`](@ref). +3) Initialize your data in parallel(!), with a kernel. Under the ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.) a page of memory is placed in the memory domain of the thread that first writes to it. `KernelAbstractions.zeros(backend, dtype, N)` and `fill!` write from the calling thread, which places all the memory in that thread's domain. The example instead allocates with `KernelAbstractions.allocate` and writes the initial values with a kernel (`init = :parallel`), so that the threads that run the computational kernel touch the memory first. **Demonstration:** -If above example is run with 128 Julia threads on a Noctua 2 compute node (128 physical cores distributed over two AMD Milan 7763 CPUs with 4 NUMA domains each), one may get the following numbers (comments for demonstration purposes): +So far, the example has only been measured with KernelAbstractions 0.10 on a system with a single memory domain, where pinning and the initialization don't change where memory is placed. With 16 Julia threads on an AMD Ryzen 9 5950X (16 physical cores, 1 NUMA domain), one gets the following numbers (comments for demonstration purposes): ``` -Memory Bandwidth (GB/s): 145.64 # backend = CPU(), init = :parallel -Compute (GFLOP/s): 24.27 +Memory Bandwidth (GB/s): 41.19 # POCL_AFFINITY=1, init = :parallel +Compute (GFLOP/s): 6.87 -Memory Bandwidth (GB/s): 333.83 # backend = CPU(; static=true), init = :parallel -Compute (GFLOP/s): 55.64 +Memory Bandwidth (GB/s): 41.22 # POCL_AFFINITY=1, init = :serial +Compute (GFLOP/s): 6.87 -Memory Bandwidth (GB/s): 32.74 # backend = CPU(), init = :serial -Compute (GFLOP/s): 5.46 +Memory Bandwidth (GB/s): 41.24 # POCL_AFFINITY unset, init = :parallel +Compute (GFLOP/s): 6.87 -Memory Bandwidth (GB/s): 32.46 # backend = CPU(; static=true), init = :serial -Compute (GFLOP/s): 5.41 +Memory Bandwidth (GB/s): 41.16 # POCL_AFFINITY unset, init = :serial +Compute (GFLOP/s): 6.86 ``` -The key observations are the following: - -* Serial initialization leads to subpar performance (at least a factor of 4.5) independent of the chosen CPU backend. This is a manifestation of remark 3 above. -* The static CPU backend gives >2x better performance than the one based on the dynamic `Threads.@spawn`. This is a manifestation of remark 2 (and, in some sense, also 1) above. \ No newline at end of file +As expected for a single memory domain, the four configurations don't differ: the kernel is limited by the memory bandwidth of the system. How much pinning and parallel initialization gain on a system with multiple memory domains has not been measured with the PoCL-based `CPU` backend yet. diff --git a/examples/numa_aware.jl b/examples/numa_aware.jl index 18bac7f8e..daa1dbe5e 100644 --- a/examples/numa_aware.jl +++ b/examples/numa_aware.jl @@ -1,17 +1,19 @@ # EXCLUDE FROM TESTING +# Run with `POCL_AFFINITY=1` to pin the threads that run the kernels, e.g. +# POCL_AFFINITY=1 julia -t 128 examples/numa_aware.jl using BenchmarkTools -using Statistics -using Random -using ThreadPinning using KernelAbstractions -ThreadPinning.pinthreads(:numa) - @kernel function saxpy_kernel(a, @Const(X), Y) I = @index(Global) @inbounds Y[I] = a * X[I] + Y[I] end +@kernel function fill_kernel(A, x) + I = @index(Global) + @inbounds A[I] = x +end + """ measure_membw(; kwargs...) -> membw, flops @@ -24,16 +26,21 @@ function measure_membw( ) bytes = 3 * sizeof(dtype) * N # num bytes transferred in SAXPY flops = 2 * N # num flops in SAXY + workgroup_size = 1024 a = dtype(3.1415) + X = KernelAbstractions.allocate(backend, dtype, N) + Y = KernelAbstractions.allocate(backend, dtype, N) if init == :serial - X = rand(dtype, N) - Y = rand(dtype, N) + # The calling thread touches all the memory first + fill!(X, dtype(1)) + fill!(Y, dtype(2)) else - X = copyto!(KernelAbstractions.zeros(backend, dtype, N), rand(dtype, N)) - Y = copyto!(KernelAbstractions.zeros(backend, dtype, N), rand(dtype, N)) + # The threads that run the kernels touch the memory first + fill_kernel(backend, workgroup_size)(X, dtype(1), ndrange = size(X)) + fill_kernel(backend, workgroup_size)(Y, dtype(2), ndrange = size(Y)) + KernelAbstractions.synchronize(backend) end - workgroup_size = 1024 t = @belapsed begin kernel = saxpy_kernel($backend, $workgroup_size, $(size(Y))) @@ -53,5 +60,5 @@ end measure_membw(CPU()); -# The following has significantly worse performance (even on systems with a single memory domain)! +# On a system with multiple NUMA domains, this places all the memory in the domain of the calling thread # measure_membw(CPU(); init=:serial); From e6c497b4af90967101805ec065c44ac0ef6ed3f6 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 6 Oct 2026 08:37:32 +0200 Subject: [PATCH 03/13] Note the CPU backend's launch cost and fixed workgroup sizes in the 0.10 notes Launching a kernel on the PoCL-based CPU backend costs several microseconds more than on the thread-based backend of 0.9, and a fixed one-dimensional workgroup size such as 64 pads multidimensional ranges. Recommend letting the workgroup size be chosen, and mention that kernels always run with the bounds mask (#845). Assisted-by: Claude Code --- docs/src/index.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/src/index.md b/docs/src/index.md index 8d03add14..6aaf6d371 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -183,6 +183,21 @@ end `GPUCompiler.SHARED_METHOD_TABLE`, which backends that override `GPUCompiler.method_table_view` have to include, e.g., by implementing `GPUCompiler.method_tables` instead. +- Launching a kernel on the `CPU` backend costs more than in 0.9: the kernel is handed to + PoCL's threads and the launching task waits for them, which takes several microseconds + per launch even for an empty kernel. Code that launches many small kernels pays this every + time. For example, a kernel over a 16×16×16 range took about 2.5 times as long per launch as + with 0.9, and one over a 4×4×4 range about 3 times as long. +- On the `CPU` backend, don't pass a fixed workgroup size such as `64`. Omit it + (`kernel(CPU())` instead of `kernel(CPU(), 64)`), so that it is chosen for every launch. A + one-dimensional workgroup size pads a multidimensional range: with a workgroup size of 64 + and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 + are inside the range. A 7-point stencil over that range took 24 µs per launch with a + workgroup size of 64, and 8 µs with the chosen one. Over a 128×128×128 range the chosen + size was still about 14% faster. +- The `CPU` backend checks in every kernel whether a work-item is inside the range, even when + the workgroups cover the range exactly, so kernels with a cheap body can be about twice + as slow as with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). - `foreach_index(f, A)` runs `f` once per index of the array `A` without writing a kernel out, and `foreach_index(f, backend, indices)` once per index in a range or `CartesianIndices`. From 3f55cf3de6ec490eacd4e93a21d876ce28fac157 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 11 Oct 2026 13:48:20 +0200 Subject: [PATCH 04/13] Apply suggestions from code review Assisted-by: Claude Code (Opus 5.5) --- docs/src/index.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 6aaf6d371..984dfef24 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -141,11 +141,9 @@ end - KernelAbstractions is built on [KernelInterface](@ref kernelinterface), which defines `Backend` and the host-side functions (`allocate`, `synchronize`, …) that KernelAbstractions re-exports. User code is unaffected, but backends have to implement - KernelInterface, so KernelAbstractions 0.10 needs a release of the backend package that - supports it; see the [notes for backend implementations](@ref implementations_notes). + KernelInterface; see the [notes for backend implementations](@ref implementations_notes). - The Enzyme extension has been removed temporarily, and is planned to return in a later - 0.10 release. Code that differentiates kernels with Enzyme has to stay on - KernelAbstractions 0.9 until then. + 0.10 release. - [`KernelAbstractions.@spawn`](@ref) launches kernels from a task like `Threads.@spawn` does, and additionally orders them after the work the spawning task has queued, and can select the device the task uses; see the [Quickstart](@ref). From f4d2d87b7469bfd62064c57cabb27222e92ee140 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 11 Oct 2026 13:58:48 +0200 Subject: [PATCH 05/13] Note in the 0.10 release notes that `@Const` constifies on the host Assisted-by: Claude Code (Opus 5.5) --- docs/src/index.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/src/index.md b/docs/src/index.md index 984dfef24..c094f36e0 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -181,6 +181,13 @@ end `GPUCompiler.SHARED_METHOD_TABLE`, which backends that override `GPUCompiler.method_table_view` have to include, e.g., by implementing `GPUCompiler.method_tables` instead. +- [`@Const`](@ref) arguments are constified on the host when the kernel is launched, instead + of at the start of the kernel. Arguments whose Adapt.jl rules don't compile for GPUs, e.g. + a `StepRangeLen` or `LinRange`, can now be passed as `@Const`. The kernel sees the same + types as before. `@Const` on a varargs argument, which didn't work before, is now an error + when the kernel is defined. Backends have to convert kernel arguments with Adapt.jl, + both in `KI.argconvert` and in `KI.launch`, since that conversion now constifies them; see + the [notes for backend implementations](@ref implementations_notes). - Launching a kernel on the `CPU` backend costs more than in 0.9: the kernel is handed to PoCL's threads and the launching task waits for them, which takes several microseconds per launch even for an empty kernel. Code that launches many small kernels pays this every From 34473da31e16c1f7546c53776288baa9e5e60854 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 08:04:40 +0200 Subject: [PATCH 06/13] Re-measure the CPU launch and workgroup-size numbers on main MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The numbers in the two `CPU` performance entries were measured before the default workgroup size was changed to follow PoCL's heuristic (#861), which is what "the chosen one" now refers to. Re-measured with KernelAbstractions 0.9.45 and this branch on 16 threads (Ryzen 9 5950X, Julia 1.12), with a Float32 7-point stencil kernel, timing a launch plus `synchronize`: 0.9.45 this branch empty kernel inline 4.7 µs 4³, chosen size 0.23 µs 3.5 µs 16³, chosen size 6.8 µs 7.5 µs 16³, size 64 14.6 µs 12.5 µs 128³, chosen size 520 µs 350 µs 128³, size 64 135 µs 367 µs 0.9 ran a launch that fits in one workgroup on the launching task, which is why its 4³ launch costs almost nothing; the 16³ launch ran on all threads in both versions and costs about the same. Assisted-by: Claude Code (Fable 5.1) --- docs/src/index.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index c094f36e0..2e3bb209f 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -189,17 +189,18 @@ end both in `KI.argconvert` and in `KI.launch`, since that conversion now constifies them; see the [notes for backend implementations](@ref implementations_notes). - Launching a kernel on the `CPU` backend costs more than in 0.9: the kernel is handed to - PoCL's threads and the launching task waits for them, which takes several microseconds - per launch even for an empty kernel. Code that launches many small kernels pays this every - time. For example, a kernel over a 16×16×16 range took about 2.5 times as long per launch as - with 0.9, and one over a 4×4×4 range about 3 times as long. + PoCL's threads and the launching task waits for them, which takes about 5 µs per launch + even for an empty kernel, whereas 0.9 ran a launch that fit in one workgroup on the + launching task itself. Code that launches many small kernels pays this every time. For + example, a 7-point stencil over a 4×4×4 range took about 3.5 µs per launch, against 0.2 µs + with 0.9. Over a 16×16×16 range, which 0.9 also ran on all threads, both took about 7 µs. - On the `CPU` backend, don't pass a fixed workgroup size such as `64`. Omit it (`kernel(CPU())` instead of `kernel(CPU(), 64)`), so that it is chosen for every launch. A one-dimensional workgroup size pads a multidimensional range: with a workgroup size of 64 and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 - are inside the range. A 7-point stencil over that range took 24 µs per launch with a - workgroup size of 64, and 8 µs with the chosen one. Over a 128×128×128 range the chosen - size was still about 14% faster. + are inside the range. A 7-point stencil over that range took about 12 µs per launch with a + workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen + size was still about 5% faster. - The `CPU` backend checks in every kernel whether a work-item is inside the range, even when the workgroups cover the range exactly, so kernels with a cheap body can be about twice as slow as with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). From 60b2e44c1ccd1c7f94006a9456ad6c2b423bc186 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 08:51:23 +0200 Subject: [PATCH 07/13] Apply suggestions from code review --- docs/src/index.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 2e3bb209f..d0c3d4165 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -183,19 +183,16 @@ end `GPUCompiler.method_tables` instead. - [`@Const`](@ref) arguments are constified on the host when the kernel is launched, instead of at the start of the kernel. Arguments whose Adapt.jl rules don't compile for GPUs, e.g. - a `StepRangeLen` or `LinRange`, can now be passed as `@Const`. The kernel sees the same - types as before. `@Const` on a varargs argument, which didn't work before, is now an error - when the kernel is defined. Backends have to convert kernel arguments with Adapt.jl, - both in `KI.argconvert` and in `KI.launch`, since that conversion now constifies them; see - the [notes for backend implementations](@ref implementations_notes). + a `StepRangeLen` or `LinRange`, can now be passed as `@Const`. + Backends have to convert kernel arguments with Adapt.jl, both in `KI.argconvert` and in `KI.launch`, + since that conversion now constifies them; see the [notes for backend implementations](@ref implementations_notes). - Launching a kernel on the `CPU` backend costs more than in 0.9: the kernel is handed to PoCL's threads and the launching task waits for them, which takes about 5 µs per launch - even for an empty kernel, whereas 0.9 ran a launch that fit in one workgroup on the - launching task itself. Code that launches many small kernels pays this every time. For + even for an empty kernel. Code that launches many small kernels suffers the most. For example, a 7-point stencil over a 4×4×4 range took about 3.5 µs per launch, against 0.2 µs with 0.9. Over a 16×16×16 range, which 0.9 also ran on all threads, both took about 7 µs. - On the `CPU` backend, don't pass a fixed workgroup size such as `64`. Omit it - (`kernel(CPU())` instead of `kernel(CPU(), 64)`), so that it is chosen for every launch. A + (`kernel(CPU())` instead of `kernel(CPU(), 64)`), so that it is automatically chosen for every launch. A one-dimensional workgroup size pads a multidimensional range: with a workgroup size of 64 and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 are inside the range. A 7-point stencil over that range took about 12 µs per launch with a From c7ca40d64220ef609944e0b6821962ea5f4934d6 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 10:46:49 +0200 Subject: [PATCH 08/13] Document opting out of the bounds check, with the cost of the check on the CPU backend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 0.10 entry about the bounds mask (#845) now gives measured numbers, and the quickstart gains a section on `unsafe_indices = true`, with the masked kernel from the quickstart and its unmasked counterpart. Measured on 16 threads (Ryzen 9 5950X, Julia 1.12) on an otherwise idle machine, per launch including `synchronize`, with the chosen workgroup size; the table in the previous re-measurement commit was taken while other processes were running, which inflated its 128³ numbers: 0.9.45 masked unmasked, guarded unmasked 7-point stencil 128³ 95 µs 140 µs 150 µs 80 µs axpy 2^22 55 µs 185 µs 215 µs 85 µs A check written in the kernel (`if I <= length(A)`) costs as much as the check it replaces, so the opt-out only pays off when the launch has no padded work-items, i.e. when the ndrange is a multiple of the workgroup size, and the docs say so. The 16³ and 128³ numbers of the workgroup-size entry are corrected from the idle-machine runs (13 µs, 5 to 15%). Assisted-by: Claude Code (Fable 5.1) --- docs/src/index.md | 13 +++++++++---- docs/src/quickstart.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index d0c3d4165..fb376b778 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -195,12 +195,17 @@ end (`kernel(CPU())` instead of `kernel(CPU(), 64)`), so that it is automatically chosen for every launch. A one-dimensional workgroup size pads a multidimensional range: with a workgroup size of 64 and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 - are inside the range. A 7-point stencil over that range took about 12 µs per launch with a + are inside the range. A 7-point stencil over that range took about 13 µs per launch with a workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen - size was still about 5% faster. + size was still 5 to 15% faster. - The `CPU` backend checks in every kernel whether a work-item is inside the range, even when - the workgroups cover the range exactly, so kernels with a cheap body can be about twice - as slow as with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). + the workgroups cover the range exactly, which makes kernels with a cheap body slower than + with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)): a 7-point + stencil over a 128×128×128 range took about 140 µs per launch against 95 µs with 0.9, and + an `axpy` over 2²² elements 185 µs against 55 µs. A kernel that is launched with an + `ndrange` that is a multiple of its workgroup size can opt out of the check with + `@kernel unsafe_indices = true`, which brought the stencil to 80 µs and the `axpy` to + 85 µs; see [Opting out of the bounds check](@ref unsafe_indices). - `foreach_index(f, A)` runs `f` once per index of the array `A` without writing a kernel out, and `foreach_index(f, backend, indices)` once per index in a range or `CartesianIndices`. diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index 5274238b5..f8e3e7afb 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -69,6 +69,38 @@ synchronize(dev) See also [Memcopy with static NDRange](@ref memcopy_static). +### [Opting out of the bounds check](@id unsafe_indices) + +The `ndrange` is padded to a multiple of the workgroup size, so every kernel checks for each +work-item whether it is inside the `ndrange`, and runs the body only for the work-items that +are. On the `CPU` backend this check is a branch in the loop over the work-items of a +workgroup, which for a kernel with a cheap body costs about as much as the body itself (see +[#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). A check that the +kernel writes itself costs the same. Such a kernel can opt out of the check with +`unsafe_indices = true`, which only pays off when there are no padded work-items: every +extent of the `ndrange` has to be a multiple of the workgroup size, which the launch has to +guarantee, as the body then runs for every work-item unconditionally. + +```@example mul2_kernel +@kernel unsafe_indices = true function mul2_unsafe_kernel(A) + I = @index(Global) + A[I] = 2 * A[I] +end + +@assert length(A) % 64 == 0 +mul2_unsafe_kernel(dev, 64)(A, ndrange = length(A)) +synchronize(dev) +@assert all(A .== 8) +``` + +If the kernel has to run with padded work-items after all, e.g. because the `ndrange` isn't +a multiple of the workgroup size, it has to test the index itself before touching memory, +and gains nothing over the check it replaced. For a multidimensional `ndrange`, compare +`@index(Global, NTuple)` with [`@ndrange`](@ref) rather than the linear index, which for a +padded work-item can coincide with the index of a work-item inside the `ndrange`. Padded +work-items also take part in [`@synchronize`](@ref), so they must reach every barrier that +the other work-items of their workgroup reach. + ## Launching kernel on the backend To launch the kernel on a backend (e.g., `CUDABackend()`, `ROCBackend()`, `oneAPIBackend()`, `MetalBackend()`), we generate the kernel From 01a8376b299d574c6a831788c5b62369c9b93d22 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 12:08:04 +0200 Subject: [PATCH 09/13] Address review comments on the unsafe_indices docs Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/src/index.md | 13 +++++-------- docs/src/quickstart.md | 37 +++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index fb376b778..25f334509 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -197,15 +197,12 @@ end and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 are inside the range. A 7-point stencil over that range took about 13 µs per launch with a workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen - size was still 5 to 15% faster. -- The `CPU` backend checks in every kernel whether a work-item is inside the range, even when + size was still about 5% faster. +- KernelAbstractions checks in every kernel whether a work-item is inside the range, even when the workgroups cover the range exactly, which makes kernels with a cheap body slower than - with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)): a 7-point - stencil over a 128×128×128 range took about 140 µs per launch against 95 µs with 0.9, and - an `axpy` over 2²² elements 185 µs against 55 µs. A kernel that is launched with an - `ndrange` that is a multiple of its workgroup size can opt out of the check with - `@kernel unsafe_indices = true`, which brought the stencil to 80 µs and the `axpy` to - 85 µs; see [Opting out of the bounds check](@ref unsafe_indices). + with 0.9 on the CPU backend ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). + A kernel can opt out of the check with `@kernel unsafe_indices = true` and test its index against the + range itself; see [Opting out of the bounds check](@ref unsafe_indices). - `foreach_index(f, A)` runs `f` once per index of the array `A` without writing a kernel out, and `foreach_index(f, backend, indices)` once per index in a range or `CartesianIndices`. diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index f8e3e7afb..3a7fdc8e9 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -71,35 +71,32 @@ See also [Memcopy with static NDRange](@ref memcopy_static). ### [Opting out of the bounds check](@id unsafe_indices) -The `ndrange` is padded to a multiple of the workgroup size, so every kernel checks for each -work-item whether it is inside the `ndrange`, and runs the body only for the work-items that -are. On the `CPU` backend this check is a branch in the loop over the work-items of a -workgroup, which for a kernel with a cheap body costs about as much as the body itself (see -[#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). A check that the -kernel writes itself costs the same. Such a kernel can opt out of the check with -`unsafe_indices = true`, which only pays off when there are no padded work-items: every -extent of the `ndrange` has to be a multiple of the workgroup size, which the launch has to -guarantee, as the body then runs for every work-item unconditionally. +The `ndrange` is padded to a multiple of the workgroup size, so a kernel checks for every +work-item whether it is inside the `ndrange` before running the body, and the body only +runs for the work-items that are. A kernel can opt out of the check with `unsafe_indices = true`. +Its body then also runs for the padded work-items, so it has to test the index itself before touching memory: ```@example mul2_kernel @kernel unsafe_indices = true function mul2_unsafe_kernel(A) - I = @index(Global) - A[I] = 2 * A[I] + I = @index(Global, NTuple) + if all(I .<= @ndrange()) + A[I...] = 2 * A[I...] + end end -@assert length(A) % 64 == 0 -mul2_unsafe_kernel(dev, 64)(A, ndrange = length(A)) +mul2_unsafe_kernel(dev, 64)(A, ndrange = size(A)) synchronize(dev) @assert all(A .== 8) ``` -If the kernel has to run with padded work-items after all, e.g. because the `ndrange` isn't -a multiple of the workgroup size, it has to test the index itself before touching memory, -and gains nothing over the check it replaced. For a multidimensional `ndrange`, compare -`@index(Global, NTuple)` with [`@ndrange`](@ref) rather than the linear index, which for a -padded work-item can coincide with the index of a work-item inside the `ndrange`. Padded -work-items also take part in [`@synchronize`](@ref), so they must reach every barrier that -the other work-items of their workgroup reach. +We recommend against using `@index(Global)` with `unsafe_indices = true`. Use +`@index(Global, NTuple)` or `@index(Global, Cartesian)` and compare it with +[`@ndrange`](@ref) instead: the linear index of a padded work-item is computed as if it +were inside the `ndrange`, and can coincide with the index of a work-item that is. + +!!! note + The padded work-items also take part in [`@synchronize`](@ref), + so they must reach every barrier that the other work-items of their workgroup reach. ## Launching kernel on the backend From 2cd8dcf874ba49ca53df22146d3eff58277f38c2 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 12:35:35 +0200 Subject: [PATCH 10/13] Derive the index from Group and Local in the unsafe_indices example Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/src/quickstart.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index 3a7fdc8e9..7ac6cb745 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -78,7 +78,9 @@ Its body then also runs for the padded work-items, so it has to test the index i ```@example mul2_kernel @kernel unsafe_indices = true function mul2_unsafe_kernel(A) - I = @index(Global, NTuple) + g = @index(Group, NTuple) + l = @index(Local, NTuple) + I = (g .- 1) .* @groupsize() .+ l if all(I .<= @ndrange()) A[I...] = 2 * A[I...] end @@ -89,10 +91,9 @@ synchronize(dev) @assert all(A .== 8) ``` -We recommend against using `@index(Global)` with `unsafe_indices = true`. Use -`@index(Global, NTuple)` or `@index(Global, Cartesian)` and compare it with -[`@ndrange`](@ref) instead: the linear index of a padded work-item is computed as if it -were inside the `ndrange`, and can coincide with the index of a work-item that is. +Don't use `@index(Global)` in any form in such a kernel. Compute the global index from +`@index(Group)`, `@index(Local)` and [`@groupsize`](@ref) instead, as above, and compare it +with [`@ndrange`](@ref). !!! note The padded work-items also take part in [`@synchronize`](@ref), From 67c1297dad94821ad57c15e6ca9cafed12e36e51 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 12:36:56 +0200 Subject: [PATCH 11/13] Use linear indices in the unsafe_indices example Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/src/quickstart.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index 7ac6cb745..90fe0583b 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -78,22 +78,22 @@ Its body then also runs for the padded work-items, so it has to test the index i ```@example mul2_kernel @kernel unsafe_indices = true function mul2_unsafe_kernel(A) - g = @index(Group, NTuple) - l = @index(Local, NTuple) - I = (g .- 1) .* @groupsize() .+ l - if all(I .<= @ndrange()) - A[I...] = 2 * A[I...] + g = @index(Group, Linear) + l = @index(Local, Linear) + I = (g - 1) * prod(@groupsize()) + l + if I <= length(A) + A[I] = 2 * A[I] end end -mul2_unsafe_kernel(dev, 64)(A, ndrange = size(A)) +mul2_unsafe_kernel(dev, 64)(A, ndrange = length(A)) synchronize(dev) @assert all(A .== 8) ``` Don't use `@index(Global)` in any form in such a kernel. Compute the global index from `@index(Group)`, `@index(Local)` and [`@groupsize`](@ref) instead, as above, and compare it -with [`@ndrange`](@ref). +with the size of the range. !!! note The padded work-items also take part in [`@synchronize`](@ref), From 06fd59571d1ec6c69afc5d2e4fc01db00c328bb6 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 12:50:18 +0200 Subject: [PATCH 12/13] Tone down the bounds-check note in the 0.10 release notes Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/src/index.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 25f334509..504232eb9 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -198,11 +198,9 @@ end are inside the range. A 7-point stencil over that range took about 13 µs per launch with a workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen size was still about 5% faster. -- KernelAbstractions checks in every kernel whether a work-item is inside the range, even when - the workgroups cover the range exactly, which makes kernels with a cheap body slower than - with 0.9 on the CPU backend ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). - A kernel can opt out of the check with `@kernel unsafe_indices = true` and test its index against the - range itself; see [Opting out of the bounds check](@ref unsafe_indices). +- On the `CPU` backend, every work-item checks whether it is inside the range, even when the + workgroups cover the range exactly. This can make kernels with a cheap body slightly slower + than with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). - `foreach_index(f, A)` runs `f` once per index of the array `A` without writing a kernel out, and `foreach_index(f, backend, indices)` once per index in a range or `CartesianIndices`. From ed164b69f4f836eca5f25ae252cc6e28289bce87 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Mon, 12 Oct 2026 12:58:17 +0200 Subject: [PATCH 13/13] Drop the issue reference from the bounds-check note Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/src/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 504232eb9..1f13edeef 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -197,10 +197,10 @@ end and a range of `(16, 16, 16)`, each of the 256 workgroups has 64 work-items, of which 16 are inside the range. A 7-point stencil over that range took about 13 µs per launch with a workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen - size was still about 5% faster. + size was still 5 to 15% faster. - On the `CPU` backend, every work-item checks whether it is inside the range, even when the workgroups cover the range exactly. This can make kernels with a cheap body slightly slower - than with 0.9 ([#845](https://github.com/JuliaGPU/KernelAbstractions.jl/issues/845)). + than with 0.9. - `foreach_index(f, A)` runs `f` once per index of the array `A` without writing a kernel out, and `foreach_index(f, backend, indices)` once per index in a range or `CartesianIndices`.