From 1656bf6633b11bd917c72e6625fefe2943036da6 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 6 Oct 2026 14:08:14 +0200 Subject: [PATCH] Let a custom mapping define the linear index of its work-items `@index(Global, Linear)` for a custom `NDRange` mapping was the position of the expanded index in the context's `ndrange`. A mapping whose linear index isn't a function of the expanded index alone, such as a list of indices whose linear index is the position in the list, had to override the internal `__index_Global_Linear` instead. Add `NDIteration.linear_index(iterspace, ndrange, groupidx, idx)`, which defaults to the previous definition and which a mapping can specialize on its `NDRange` type. Test a launch over a list of indices in the launch testsuite. Assisted-by: Claude Code --- src/launch.jl | 2 +- src/nditeration.jl | 10 +++++++++ test/launch.jl | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/launch.jl b/src/launch.jl index 37667a75b..22656284b 100644 --- a/src/launch.jl +++ b/src/launch.jl @@ -263,7 +263,7 @@ if builtin(iterspace) && ndrange isa CartesianIndices T = index_type(launch) widen_index(linearize(T, narrow(T, size(ndrange)), blocked_position(ctx, launch))) else - linear_index(ndrange, global_cartesian(ctx, launch, iterspace, ndrange)) + linear_index(iterspace, ndrange, group_index(ctx, launch), local_index(ctx, launch)) end @inline validindex(ctx, launch::Launch, iterspace, ndrange) = diff --git a/src/nditeration.jl b/src/nditeration.jl index d588f713d..2848295fb 100644 --- a/src/nditeration.jl +++ b/src/nditeration.jl @@ -205,6 +205,16 @@ Base.length(range::NDRange) = length(blocks(range)) return CartesianIndex(nI) end +""" + linear_index(iterspace::NDRange, ndrange, groupidx::CartesianIndex, idx::CartesianIndex) + +Linear index of work item `idx` of workgroup `groupidx`, as returned by `@index(Global, Linear)`. +Defaults to the position of `expand(iterspace, groupidx, idx)` within `ndrange`. A custom mapping +whose linear index is not a function of the expanded index alone, e.g. a list of indices whose +linear index is the position in the list, can specialize this on its `NDRange` type. +""" +@inline linear_index(iterspace::NDRange, ndrange, groupidx::CartesianIndex, idx::CartesianIndex) = + linear_index(ndrange, @inbounds expand(iterspace, groupidx, idx)) """ assume(cond::Bool) diff --git a/test/launch.jl b/test/launch.jl index ac8e62e82..10ee27855 100644 --- a/test/launch.jl +++ b/test/launch.jl @@ -1,3 +1,4 @@ +using Adapt using KernelAbstractions using KernelAbstractions.NDIteration import KernelAbstractions.KernelInterface as KI @@ -45,6 +46,48 @@ const MANY_ARGS = [Symbol(:x, i) for i in 1:40] @inbounds A[I] = $(foldl((a, b) -> :($a + $b), MANY_ARGS)) end +# A custom iteration space: one work-item per index in a list, whose linear index is its +# position in the list (as Oceananigans launches kernels over the active cells of a grid) +struct IndexList{V <: AbstractVector} + indices::V +end +Adapt.@adapt_structure IndexList + +# the `ndrange` of the context: every index but `UNLISTED` is part of the space +struct ListedIndices + length::Int +end +const UNLISTED = CartesianIndex(typemin(Int), typemin(Int)) +Base.in(I::CartesianIndex{2}, ::ListedIndices) = I != UNLISTED + +const ListNDRange = NDRange{1, <:Any, <:Any, <:Any, <:Any, <:IndexList} + +function KernelAbstractions.partition(kernel::KernelAbstractions.Kernel, list::IndexList, workgroupsize) + static_workgroupsize = KernelAbstractions.workgroupsize(kernel) + items = NDIteration.get(static_workgroupsize) + blocks, _, dynamic = NDIteration.partition((length(list.indices),), items) + return NDRange{1, DynamicSize, static_workgroupsize}(CartesianIndices(blocks), nothing, list), dynamic +end +KernelAbstractions.cartesian(list::IndexList) = ListedIndices(length(list.indices)) +KernelAbstractions.cartesian(r::ListedIndices) = r + +@inline list_position(r::ListNDRange, g::CartesianIndex{1}, i::CartesianIndex{1}) = + (g[1] - 1) * length(workitems(r)) + i[1] +@inline function NDIteration.expand(r::ListNDRange, g::CartesianIndex{1}, i::CartesianIndex{1}) + p = list_position(r, g, i) + return p <= length(r.mapping.indices) ? (@inbounds r.mapping.indices[p]) : UNLISTED +end +@inline NDIteration.linear_index(r::ListNDRange, ::ListedIndices, g::CartesianIndex{1}, i::CartesianIndex{1}) = + list_position(r, g, i) + +@kernel function launch_listed!(visits, position) + I = @index(Global, Cartesian) + @inbounds begin + visits[I] += 1 + position[I] = @index(Global, Linear) + end +end + default_launcher(kernel, args...; ndrange, workgroupsize = nothing) = kernel(args...; ndrange, workgroupsize) @@ -140,6 +183,19 @@ function launch_testsuite(backend, AT; launcher = default_launcher, skip_tests = @test all(==(sum(1:40)), Array(A)) end + @testset "custom iteration space" begin + shape = (7, 5) + # 18 indices: the last workgroup of 4 work-items is partial + listed = [I for I in CartesianIndices(shape) if isodd(sum(Tuple(I)))] + visits = AT(zeros(Int, shape)) + position = AT(zeros(Int, shape)) + launcher(launch_listed!(backend(), 4), visits, position; ndrange = IndexList(AT(listed))) + synchronize(backend()) + visits, position = Array(visits), Array(position) + @test all(I -> visits[I] == (I in listed), CartesianIndices(shape)) + @test all(p -> position[listed[p]] == p, eachindex(listed)) + end + @testset "synchronize with padding lanes" begin for (shape, wgs) in (((37,), (8,)), ((7, 6), (4, 4)), ((5, 3, 3), (2, 2, 2))) A = AT(zeros(Int, shape))