Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
10 changes: 10 additions & 0 deletions src/nditeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions test/launch.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Adapt
using KernelAbstractions
using KernelAbstractions.NDIteration
import KernelAbstractions.KernelInterface as KI
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
Loading