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
10 changes: 8 additions & 2 deletions lib/KernelInterface/src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,19 @@ Not part of the public interface; used by KernelInterface's and KernelAbstractio
code.
"""
@inline function check_work_group_size(kernel::Kernel, items::Dims{3})
max_dims = max_work_group_dims(kernel.backend)
all(items .<= max_dims) || throw_work_group_dims_error(items, max_dims)
check_work_group_dims(kernel.backend, items)
max_items = max_work_group_size(kernel)
prod_exceeds(items, max_items) && throw_work_group_size_error(items, max_items)
return
end

# kept out of `check_work_group_size`, which is inlined into code compiled for every kernel
@noinline function check_work_group_dims(backend::Backend, items::Dims{3})
max_dims = max_work_group_dims(backend)
all(items .<= max_dims) || throw_work_group_dims_error(items, max_dims)
return
end

@noinline function throw_work_group_dims_error(items, max_dims)
throw(ArgumentError("Work-group size $items exceeds the maximum of $max_dims per dimension"))
end
Expand Down
45 changes: 36 additions & 9 deletions src/backend_launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ Core.kwcall(kwargs::NamedTuple, obj::Kernel{<:KI.Backend}, args::Vararg{Any, N})
launch_tuple(obj, args; kwargs...)

function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize = nothing)
ndrange, workgroupsize, iterspace, dynamic = launch_config(obj, ndrange, workgroupsize)
setup = launch_setup(obj, ndrange, workgroupsize)
# nothing to launch (or compile) for an empty ndrange
any(iszero, size(blocks(iterspace))) && return nothing
setup === nothing && return nothing
ndrange, workgroupsize, iterspace = setup

# launch on an N-d grid, computing indices in a narrow type, if possible. this doesn't
# depend on the tuned workgroup size, so the context (and thus the kernel) doesn't either.
launch = select_launch(obj, workgroupsize, iterspace)
# (not done by `launch_setup`: the launch mode isn't inferred concretely, so returning
# it would box the result)
launch = @noinline select_launch(obj, workgroupsize, iterspace)
T = narrow_index_type(backend(obj))
if launch === NDLaunch{T}()
# the common case, specialized statically
Expand All @@ -115,6 +118,16 @@ function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize
return nothing
end

# The parts of the launch that don't depend on the arguments are kept out of
# `launch_tuple` and `launch_kernel`, which are compiled for every kernel.

# normalize the launch arguments, or return `nothing` if there is nothing to launch
@noinline function launch_setup(obj::Kernel, ndrange, workgroupsize)
ndrange, workgroupsize, iterspace, dynamic = launch_config(obj, ndrange, workgroupsize)
any(iszero, size(blocks(iterspace))) && return nothing
return ndrange, workgroupsize, iterspace
end

function launch_kernel(obj::Kernel, launch, ndrange, _workgroupsize, iterspace, args::Tuple)
# a constant `nothing` for kernels without collectives; for kernels with, the call below
# is a function barrier that makes the rest of the launch type stable
Expand All @@ -132,10 +145,26 @@ function launch_kernel(obj::Kernel, launch, subgroups, ndrange, _workgroupsize,
kernel = KI.kernel_function(b, obj.f, tt; compiler_options(obj)...)

# tune the workgroup size, keeping the context type (and thus the kernel) the same
if workgroupsize(obj) <: DynamicSize && _workgroupsize === nothing
threads = if workgroupsize(obj) <: DynamicSize && _workgroupsize === nothing
range = something(ndrange, static_ndrange(obj))
KI.launch_configuration(kernel; nitems = saturated_prod(extents(range))).workgroupsize
else
nothing
end
ctx, groups, items = launch_geometry(obj, launch, subgroups, ndrange, iterspace, ctx, threads)

KI.check_work_group_size(kernel, items)
KI.launch(kernel, groups, items, prepend(ctx, args))
return nothing
end

# the context and launch geometry, re-partitioned for the tuned workgroup size `threads`
@noinline function launch_geometry(
obj::Kernel, launch, subgroups, ndrange, iterspace, ctx, threads
)
if threads !== nothing
range = something(ndrange, static_ndrange(obj))
threads = KI.launch_configuration(kernel; nitems = saturated_prod(extents(range))).workgroupsize
iterspace, _ = partition(obj, ndrange, launch_workgroupsize(b, launch, threads, range))
iterspace, _ = partition(obj, ndrange, launch_workgroupsize(backend(obj), launch, threads, range))
ctx = mkcontext(obj, ndrange, iterspace, launch; subgroups)
end

Expand All @@ -147,9 +176,7 @@ function launch_kernel(obj::Kernel, launch, subgroups, ndrange, _workgroupsize,
else
groups, items = (prod(groups), 1, 1), (prod(items), 1, 1)
end
KI.check_work_group_size(kernel, items)
KI.launch(kernel, groups, items, prepend(ctx, args))
return nothing
return ctx, groups, items
end

# The helpers below avoid splatting the arguments, and `map`, which isn't type stable for 32
Expand Down
Loading