Skip to content
Closed
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
7 changes: 4 additions & 3 deletions docs/src/implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ it should return its method tables from `GPUCompiler.method_tables` rather than
to recurse into the arguments.

[`@index`](@ref) computes its indices from how a kernel was launched: on a grid with the
shape of the iteration space ([`NDLaunch`](@ref KernelAbstractions.NDLaunch), for up to as
many dimensions as the backend's grid has), which doesn't need any divisions, or on a 1-D
grid ([`LinearLaunch`](@ref KernelAbstractions.LinearLaunch)). Either way it computes in a
shape of the iteration space ([`NDLaunch`](@ref KernelAbstractions.NDLaunch), where the
dimensions past the third share the grid's z-dimension), which doesn't need any divisions
for the ids of the work-items, or on a 1-D grid
([`LinearLaunch`](@ref KernelAbstractions.LinearLaunch)). Either way it computes in a
narrow index type such as `Int32` when the iteration space fits, which is why the typed
[`KI.get_group_id`](@ref KernelInterface.get_group_id) and
[`KI.get_local_id`](@ref KernelInterface.get_local_id) queries have to compute in that type
Expand Down
2 changes: 1 addition & 1 deletion src/backend_launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function launch_kernel(obj::Kernel, launch, subgroups, ndrange, _workgroupsize,
groups = size(blocks(iterspace))
items = size(workitems(iterspace))
if launch isa NDLaunch
groups, items = KI.pad3(groups), KI.pad3(items)
groups, items = KI.pad3(nd_grid(groups)), KI.pad3(nd_grid(items))
else
groups, items = (prod(groups), 1, 1), (prod(items), 1, 1)
end
Expand Down
63 changes: 44 additions & 19 deletions src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ struct LinearLaunch{T <: Integer} end
"""
NDLaunch{T}()

Launch configuration for a kernel launched on an N-d grid, where `N = ndims(iterspace)` is
at most the number of grid dimensions of the backend (the length of
[`KI.max_work_group_dims`](@ref KernelInterface.max_work_group_dims), i.e. 3): the grid
consists of `size(blocks(iterspace))` groups of `size(workitems(iterspace))` work-items, so
the x, y and z-components of the hardware group and local ids are the positions along the
first `N` dimensions of the iteration space. This avoids decomposing linear ids into
Cartesian positions, i.e., divisions.
Launch configuration for a kernel launched on an N-d grid: the grid consists of
`size(blocks(iterspace))` groups of `size(workitems(iterspace))` work-items, so the x, y
and z-components of the hardware group and local ids are the positions along the
dimensions of the iteration space (at most the three grid dimensions of the backend, see
[`KI.max_work_group_dims`](@ref KernelInterface.max_work_group_dims)). This avoids
decomposing linear ids into Cartesian positions, i.e., divisions.

An iteration space with more dimensions is launched like this if its workgroups have one
work-item along the dimensions past the third. These share the z-dimension of the grid
with the third, so the group's z-id is decomposed along them, but the local id is not.

The linear group and local indices are computed x-fastest, so they agree with the ordering
of a [`LinearLaunch`](@ref) (and with how GPUs typically form sub-groups).
Expand Down Expand Up @@ -57,9 +60,11 @@ narrow_index_type(backend) = backend isa POCL.POCLBackend ? Int : Int32
Choose how to launch `kernel` with the (possibly preliminary) iteration space `iterspace`
and the given `workgroupsize` (`nothing` if it will be tuned), as returned by
`launch_config`: an [`NDLaunch`](@ref) if the iteration space fits the grid of
`backend(kernel)`, i.e. its number of dimensions and per-dimension limits, and a
[`LinearLaunch`](@ref) otherwise, both computing indices in `Int32` (`Int` on PoCL) if
possible.
`backend(kernel)`, i.e. its per-dimension limits, and a [`LinearLaunch`](@ref) otherwise,
both computing indices in `Int32` (`Int` on PoCL) if possible. A tuned workgroup of an
iteration space with more than three dimensions only has work-items along the first three,
so it is launched on the N-d grid only if these hold at least 64 work-items: GPUs run
smaller workgroups slower than a linear launch.

If the workgroup size will be tuned, the choice holds for any workgroup size the backend
tunes afterwards, so the context and thus the compiled kernel are the same before and
Expand Down Expand Up @@ -89,17 +94,23 @@ function select_launch(
extent::Dims{N}, groupsize, max_items::Int,
max_dims::Dims, max_groups::Dims, ::Type{T} = Int32
) where {N, T}
nd = N <= length(max_dims)
# past three dimensions, an `NDLaunch` folds the later ones into the grid's z-dimension
fold = N > 3 && length(max_dims) == 3
nd = N <= length(max_dims) || fold
limits = nd_limits(max_dims, extent)
grid = fold ? nd_grid : identity
if groupsize === nothing
if nd
# a tuned workgroup has at least one work-item per dimension
nd = all(ntuple(d -> extent[d] <= max_groups[d], Val(N)))
# a tuned workgroup has at least one work-item per dimension, and one past the
# third when folding, which is slower on GPUs if the first three hold fewer than 64
nd = within(grid(extent), max_groups) &&
(!fold || prod(ntuple(d -> min(extent[d], max_dims[d]), Val(3))) >= 64)
end
padded = tuned_padded(extent, max_items, nd ? max_dims : ())
padded = tuned_padded(extent, max_items, nd ? limits : ())
else
blocks, items, _ = NDIteration.partition(extent, groupsize)
if nd
nd = all(ntuple(d -> items[d] <= max_dims[d] && blocks[d] <= max_groups[d], Val(N))) &&
nd = within(items, limits) && within(grid(blocks), max_groups) &&
prod(items) <= max_items
end
padded = map(mul_extent, blocks, items)
Expand Down Expand Up @@ -127,6 +138,13 @@ function add_extent(a::Int, b::Int)
return n
end

# the workgroup limits and grid of an `NDLaunch`, with one work-item past the third dimension
nd_limits(max_dims::Dims, ::NTuple{N, Any}) where {N} =
ntuple(d -> d <= length(max_dims) ? max_dims[d] : 1, Val(N))
nd_grid(dims::Dims{N}) where {N} =
N <= 3 ? dims : (dims[1], dims[2], prod(Base.tail(Base.tail(dims))))
within(a::NTuple{N, Any}, b) where {N} = all(ntuple(d -> a[d] <= b[d], Val(N)))

# Upper bound on the padded extents of an iteration space whose workgroup size is chosen
# by `launch_workgroupsize`, i.e., by `KI.threads_to_workgroupsize` with at most `capacity`
# work-items and the per-dimension `limits`. That fills the first dimensions first, so a
Expand Down Expand Up @@ -160,8 +178,9 @@ The workgroup size for launching `threads` work-items per workgroup over `ndrang
`launch` (see [`select_launch`](@ref)). Backends that tune the workgroup size of a kernel
launched with a [`LinearLaunch`](@ref) or [`NDLaunch`](@ref) have to use this.
"""
launch_workgroupsize(backend, ::NDLaunch, threads, ndrange) =
KI.threads_to_workgroupsize(threads, extents(ndrange), KI.max_work_group_dims(backend))
launch_workgroupsize(backend, ::NDLaunch, threads, ndrange) = KI.threads_to_workgroupsize(
threads, extents(ndrange), nd_limits(KI.max_work_group_dims(backend), extents(ndrange))
)
launch_workgroupsize(backend, ::Union{LinearLaunch, Nothing}, threads, ndrange) =
KI.threads_to_workgroupsize(threads, extents(ndrange))

Expand Down Expand Up @@ -206,7 +225,13 @@ end
I[1], mul_nowrap(dims[1], sub_nowrap(linearize(T, Base.tail(dims), Base.tail(I)), one(T)))
)

@inline hardware_position(id, ::Val{N}) where {N} = ntuple(d -> (id.x, id.y, id.z)[d], Val(N))
@inline hardware_position(id, ::Val{N}) where {N} =
ntuple(d -> d <= 3 ? (id.x, id.y, id.z)[d] : one(id.z), Val(N))
# past three dimensions, the z-id of a group is its column-major position along the later ones
@inline function group_hardware_position(id, dims::NTuple{N, Any}) where {N}
N <= 3 && return hardware_position(id, Val(N))
return (id.x, id.y, ind2sub(Base.tail(Base.tail(dims)), id.z - one(id.z))...)
end

# The hardware ids are bounded by the launch, which e.g. lets the compiler fold the validity
# check of a statically-sized kernel.
Expand All @@ -225,7 +250,7 @@ end
# work-item in `workitems(iterspace)`, as tuples of the index type
@inline function group_position(ctx, ::NDLaunch{T}) where {T}
dims = narrow(T, size(blocks(__iterspace(ctx))))
return assume_bounded(hardware_position(KI.get_group_id(T), Val(ndims(ctx))), dims)
return assume_bounded(group_hardware_position(KI.get_group_id(T), dims), dims)
end
@inline function local_position(ctx, ::NDLaunch{T}) where {T}
dims = narrow(T, size(workitems(__iterspace(ctx))))
Expand Down
17 changes: 15 additions & 2 deletions test/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ end

function launch_testsuite(backend, AT; launcher = default_launcher, skip_tests = Set{String}())
@testset "index layout" begin
shapes = Tuple[(), (7,), (37,), (5, 7), (33, 3), (3, 5, 7), (2, 3, 4, 5)]
shapes = Tuple[
(), (7,), (37,), (5, 7), (33, 3), (3, 5, 7), (2, 3, 4, 5), (2, 3, 2, 3, 2),
(9, 7, 3, 5, 2),
]
@testset "$shape, workgroupsize=$wgs" for shape in shapes,
wgs in (nothing, 4, (2, 3), (4, 1, 2))
wgs !== nothing && length(wgs) > length(shape) && continue
Expand Down Expand Up @@ -237,6 +240,16 @@ function select_launch_testsuite()
# tuning respects the per-dimension limits
@test select((1, 1, 5000)) === NDLaunch{Int32}()

# and more if the workgroups have one work-item past the third
@test select((8, 8, 8, 1536)) === NDLaunch{Int32}()
@test select((9, 7, 3, 5, 2)) === NDLaunch{Int32}()
@test select((4, 4, 2, 1536)) === LinearLaunch{Int32}()
@test select((2, 3, 4, 5), (2, 3)) === NDLaunch{Int32}()
@test select((2, 3, 4, 5), (1, 1, 1, 5)) === LinearLaunch{Int32}()
@test select((1024, 1, 300, 300), (1024,)) === LinearLaunch{Int32}()
@test select((10, 10, 10, 10); max_dims = (1024, 1024), max_groups = (65535, 65535)) ===
LinearLaunch{Int32}()

# indices are computed in Int32 if the padded iteration space fits
@test select((1024, 1024, 1024)) === NDLaunch{Int32}()
@test select((1025, 1024, 1024)) === NDLaunch{Int}()
Expand Down Expand Up @@ -269,7 +282,7 @@ function select_launch_testsuite()
(5,), (1000,), (3, 7), (33, 1000), (1, 1, 5000), (7, 9, 11),
(1500, 3, 2), (2, 3, 4, 5), (1, 1, 1, 3000), (0, 7),
)
for limits in ((), max_dims)
for limits in ((), max_dims, KernelAbstractions.nd_limits(max_dims, extent))
bound = KernelAbstractions.tuned_padded(extent, max_items, limits)
@test all(1:max_items) do threads
wgs = KI.threads_to_workgroupsize(threads, extent, limits)
Expand Down
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,10 @@ end
for launch in (nothing, KA.LinearLaunch{Int}(), KA.NDLaunch{Int}())
function launcher(kernel, args...; ndrange, workgroupsize = nothing)
ndrange, workgroupsize, iterspace, _ = KA.launch_config(kernel, ndrange, workgroupsize)
# an N-d launch is limited to three dimensions
l = launch isa KA.NDLaunch && ndims(iterspace) > 3 ? KA.LinearLaunch{Int}() : launch
# an N-d launch needs one work-item past the third dimension, as tuned workgroups have
fold = KA.workgroupsize(kernel) <: KA.DynamicSize && workgroupsize === nothing ||
all(==(1), size(KA.workitems(iterspace))[4:end])
l = launch isa KA.NDLaunch && !fold ? KA.LinearLaunch{Int}() : launch
KernelAbstractions.launch_kernel(kernel, l, ndrange, workgroupsize, iterspace, args)
end
@testset "$launch" begin
Expand Down
Loading