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
59 changes: 42 additions & 17 deletions lib/KernelInterface/src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,9 @@ Validate the launch keywords of a [`Kernel`](@ref) call and turn them into the n
work-groups and the work-group size. A zero number of work-groups means nothing is launched.
"""
@inline function launch_geometry(kernel::Kernel, numgroups, workgroupsize, ndrange, max_work_group_size)
check_dims("numgroups", numgroups)
check_dims("workgroupsize", workgroupsize)
check_dims("ndrange", ndrange)
if ndrange != () && numgroups != ()
throw(ArgumentError("Only one of `numgroups` and `ndrange` can be used"))
end
max_work_group_size > 0 ||
throw(ArgumentError("`max_work_group_size` must be positive, got $max_work_group_size"))

check_launch(numgroups, workgroupsize, ndrange, max_work_group_size)
items = if workgroupsize != ()
wgsize = pad3(workgroupsize)
any(iszero, wgsize) &&
throw(ArgumentError("`workgroupsize` must be positive, got $(repr(workgroupsize))"))
check_work_group_size(kernel, wgsize)
wgsize
elseif ndrange == ()
Expand All @@ -170,7 +160,27 @@ work-groups and the work-group size. A zero number of work-groups means nothing
)
threads_to_workgroupsize(config.workgroupsize, wanted, max_work_group_dims(kernel.backend))
end
return launch_groups(numgroups, ndrange, items), items
end

# The parts of `launch_geometry` that don't need the kernel. They are kept out of it, which
# is compiled for every kernel.

function check_launch(numgroups, workgroupsize, ndrange, max_work_group_size)
check_dims("numgroups", numgroups)
check_dims("workgroupsize", workgroupsize)
check_dims("ndrange", ndrange)
if ndrange != () && numgroups != ()
throw(ArgumentError("Only one of `numgroups` and `ndrange` can be used"))
end
max_work_group_size > 0 ||
throw(ArgumentError("`max_work_group_size` must be positive, got $max_work_group_size"))
any(iszero, workgroupsize) &&
throw(ArgumentError("`workgroupsize` must be positive, got $(repr(workgroupsize))"))
return
end

function launch_groups(numgroups, ndrange, items::Dims{3})
groups = if ndrange != ()
cld.(pad3(ndrange), items)
elseif numgroups != ()
Expand All @@ -183,19 +193,34 @@ work-groups and the work-group size. A zero number of work-groups means nothing
if !any(iszero, groups) && any(map((g, i) -> g > typemax(Int) ÷ i, groups, items))
throw(ArgumentError("Launch of $groups work-groups of $items work-items has more than typemax(Int) work-items in a dimension"))
end
return groups, items
return groups
end

function check_work_group_size(kernel::Kernel, items::Dims{3})
"""
check_work_group_size(kernel::Kernel, items::Dims{3})

Check that work-groups of `items` work-items fit the limits of `kernel`, as
[`launch`](@ref) requires, throwing an `ArgumentError` otherwise.

Not part of the public interface; used by KernelInterface's and KernelAbstractions' launch
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(ArgumentError("Work-group size $items exceeds the maximum of $max_dims per dimension"))
all(items .<= max_dims) || throw_work_group_dims_error(items, max_dims)
max_items = max_work_group_size(kernel)
prod_exceeds(items, max_items) &&
throw(ArgumentError("Work-group size $items has more than $max_items work-items, the maximum for this kernel"))
prod_exceeds(items, max_items) && throw_work_group_size_error(items, max_items)
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

@noinline function throw_work_group_size_error(items, max_items)
throw(ArgumentError("Work-group size $items has more than $max_items work-items, the maximum for this kernel"))
end

"""
threads_to_workgroupsize(threads, ndrange, [limits])

Expand Down
18 changes: 11 additions & 7 deletions src/backend_launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ function launch_tuple(obj::Kernel, args::Tuple; ndrange = nothing, workgroupsize
# the common case, specialized statically
launch_kernel(obj, NDLaunch{Int32}(), ndrange, workgroupsize, iterspace, args)
else
launch_kernel(obj, launch, ndrange, workgroupsize, iterspace, args)
# the other modes are rare: hide the callee from inference, so that it doesn't
# analyze the whole launch chain for each of them when compiling every kernel
Base.inferencebarrier(launch_kernel)(obj, launch, ndrange, workgroupsize, iterspace, args)
end
return nothing
end
Expand All @@ -115,14 +117,16 @@ function launch_kernel(obj::Kernel, launch, ndrange, _workgroupsize, iterspace,
ctx = mkcontext(obj, ndrange, iterspace, launch)
end

# launching through the `KI.Kernel` validates the sizes against the kernel's limits
# the geometry is valid by construction, except for the kernel's limits
groups = size(blocks(iterspace))
items = size(workitems(iterspace))
if launch isa NDLaunch
call_kernel(kernel, ctx, args, groups, items)
groups, items = KI.pad3(groups), KI.pad3(items)
else
call_kernel(kernel, ctx, args, prod(groups), prod(items))
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
end

Expand All @@ -141,8 +145,8 @@ end
return :(Tuple{Core.Typeof(KI.argconvert(backend, ctx)), $(types...)})
end

# `kernel(ctx, args...; numgroups, workgroupsize)`
@inline @generated function call_kernel(kernel::KI.Kernel, ctx, args::Tuple, numgroups, workgroupsize)
# `(ctx, args...)`
@inline @generated function prepend(ctx, args::Tuple)
argexprs = (:(args[$i]) for i in 1:fieldcount(args))
return :(kernel(ctx, $(argexprs...); numgroups, workgroupsize))
return :((ctx, $(argexprs...)))
end
Loading