From 1b2fcded38a59c4be9efa64537b8accf80c51c34 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 28 Oct 2026 09:16:54 +0100 Subject: [PATCH 1/3] Check a work-group's total size before its per-dimension limits Linearized launches put all work-items in the first dimension, so an oversized (96, 48) work-group was reported as a per-dimension error for (4608, 1, 1). Check the total first, which reports the kernel's limit of 4096 work-items instead. --- lib/KernelInterface/src/launch.jl | 3 ++- test/runtests.jl | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/KernelInterface/src/launch.jl b/lib/KernelInterface/src/launch.jl index 568affccb..b5ab226a1 100644 --- a/lib/KernelInterface/src/launch.jl +++ b/lib/KernelInterface/src/launch.jl @@ -206,9 +206,10 @@ Not part of the public interface; used by KernelInterface's and KernelAbstractio code. """ @inline function check_work_group_size(kernel::Kernel, items::Dims{3}) - check_work_group_dims(kernel.backend, items) + # check the total first: linearized launches put all work-items in the first dimension max_items = max_work_group_size(kernel) prod_exceeds(items, max_items) && throw_work_group_size_error(items, max_items) + check_work_group_dims(kernel.backend, items) return end diff --git a/test/runtests.jl b/test/runtests.jl index 6f18d7481..3c1323643 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -926,6 +926,10 @@ end # workgroup sizes are validated against the kernel's limits limit = KernelAbstractions.KI.max_work_group_size(CPU()) @test_throws ArgumentError fill_index!(CPU())(zeros(Int, 2limit); ndrange = 2limit, workgroupsize = 2limit) + # report the total-size limit even when linearization puts all work-items in the + # first dimension + n = limit ÷ 64 + 1 + @test_throws "work-items" fill_index!(CPU(), (64, n))(zeros(Int, 128n); ndrange = (64, n, 2)) # iteration spaces with more work-items than an `Int` can count are rejected, instead # of launching nothing because the number of workgroups overflowed From f5a6c9ab9e4629e1f0a218a133570b95246da90a Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 28 Oct 2026 09:17:00 +0100 Subject: [PATCH 2/3] KernelInterface: check work-group sizes without division `prod_exceeds` runs on every launch. Checked multiplication avoids a 64-bit division per dimension, which costs tens of cycles on CPUs such as Skylake. --- lib/KernelInterface/src/launch.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/KernelInterface/src/launch.jl b/lib/KernelInterface/src/launch.jl index b5ab226a1..86e4babd2 100644 --- a/lib/KernelInterface/src/launch.jl +++ b/lib/KernelInterface/src/launch.jl @@ -126,12 +126,13 @@ end return p end -# whether the product of positive `dims` exceeds `limit`, without overflowing +# whether the product of positive `dims` exceeds `limit`, without overflow or division +# (this runs on every launch, and a division costs tens of cycles on some CPUs) @inline function prod_exceeds(dims::Dims, limit::Int) p = 1 for d in dims - d > limit ÷ p && return true - p *= d + p, overflow = Base.mul_with_overflow(p, d) + (overflow || p > limit) && return true end return false end From 81fe6df1b2cd1279058d0c5a88583ba5d37497be Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 28 Oct 2026 09:17:00 +0100 Subject: [PATCH 3/3] Bump KernelInterface to 0.4.4 --- lib/KernelInterface/Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/KernelInterface/Project.toml b/lib/KernelInterface/Project.toml index 45450fc57..3b7847df1 100644 --- a/lib/KernelInterface/Project.toml +++ b/lib/KernelInterface/Project.toml @@ -1,7 +1,7 @@ name = "KernelInterface" uuid = "4ee993da-d684-4d17-a7dd-4e58e78d92bf" authors = ["Valentin Churavy and contributors"] -version = "0.4.3" +version = "0.4.4" [compat] julia = "1.10"