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
9 changes: 9 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@ UnsafeAtomics = "013be700-e6cd-48c3-b4a1-df204f14c38f"
pocl_standalone_jll = "54f56a70-6062-5590-a942-1226658f6c83"

[weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"

[sources]
KernelInterface = {path = "lib/KernelInterface"}

[extensions]
AMDGPUExt = "AMDGPU"
LinearAlgebraExt = "LinearAlgebra"
MetalExt = "Metal"
SparseArraysExt = "SparseArrays"
StaticArraysExt = "StaticArrays"
oneAPIExt = "oneAPI"

[compat]
AMDGPU = "2"
Adapt = "0.4, 1.0, 2.0, 3.0, 4"
Atomix = "1.5"
GPUCompiler = "2.13"
Expand All @@ -48,6 +55,7 @@ KernelInterface = "0.4"
LLVM = "10"
LinearAlgebra = "1.6"
MacroTools = "0.5"
Metal = "1"
PrecompileTools = "1"
Printf = "<0.0.1, 1"
Random = "1"
Expand All @@ -61,6 +69,7 @@ StaticArrays = "1.5"
StaticArraysCore = "1"
UnsafeAtomics = "0.4"
julia = "1.10"
oneAPI = "2"
pocl_standalone_jll = "7.2.2"

[extras]
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ KernelAbstractions.NDIteration.linear_index
KernelAbstractions.LinearLaunch
KernelAbstractions.NDLaunch
KernelAbstractions.select_launch
KernelAbstractions.fold_threshold
KernelAbstractions.launch_workgroupsize
KernelAbstractions.compiler_options
KernelAbstractions.PrivateArray
Expand Down
8 changes: 8 additions & 0 deletions ext/AMDGPUExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module AMDGPUExt

import KernelAbstractions
import AMDGPU

KernelAbstractions.fold_threshold(::AMDGPU.ROCBackend) = 16

end
8 changes: 8 additions & 0 deletions ext/MetalExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module MetalExt

import KernelAbstractions
import Metal

KernelAbstractions.fold_threshold(::Metal.MetalBackend) = 16

end
8 changes: 8 additions & 0 deletions ext/oneAPIExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module oneAPIExt

import KernelAbstractions
import oneAPI

KernelAbstractions.fold_threshold(::oneAPI.oneAPIBackend) = 16

end
27 changes: 21 additions & 6 deletions src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ and the given `workgroupsize` (`nothing` if it will be tuned), as returned by
`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.
so it is launched on the N-d grid only if these hold at least
[`fold_threshold(backend)`](@ref fold_threshold) work-items.

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 All @@ -85,14 +85,14 @@ function select_launch(kernel::Kernel, workgroupsize, iterspace)
return select_launch(
map(mul_extent, groups, items), tuned ? nothing : items,
KI.max_work_group_size(b), KI.max_work_group_dims(b), KI.max_num_groups(b),
narrow_index_type(b)
narrow_index_type(b); fold_items = fold_threshold(b)
)
end

# `groupsize === nothing` means that the workgroup size will be tuned
function select_launch(
extent::Dims{N}, groupsize, max_items::Int,
max_dims::Dims, max_groups::Dims, ::Type{T} = Int32
max_dims::Dims, max_groups::Dims, ::Type{T} = Int32; fold_items::Int = 64
) where {N, T}
# past three dimensions, an `NDLaunch` folds the later ones into the grid's z-dimension
fold = N > 3 && length(max_dims) == 3
Expand All @@ -102,9 +102,9 @@ function select_launch(
if groupsize === nothing
if nd
# 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
# third when folding, which is slow if the first three can't fill it
nd = within(grid(extent), max_groups) &&
(!fold || prod(ntuple(d -> min(extent[d], max_dims[d]), Val(3))) >= 64)
(!fold || prod(ntuple(d -> min(extent[d], max_dims[d]), Val(3))) >= fold_items)
end
padded = tuned_padded(extent, max_items, nd ? limits : ())
else
Expand All @@ -124,6 +124,21 @@ function select_launch(
end
end

"""
fold_threshold(backend)::Int

The number of work-items that the first three dimensions of an iteration space with more
dimensions have to hold for a tuned launch to be folded onto the N-d grid (see
[`NDLaunch`](@ref)). Its workgroups then only have work-items along these dimensions, and
GPUs run small workgroups slower than a [`LinearLaunch`](@ref), which decomposes its ids
with divisions instead. Where that happens depends on the hardware, so backends without a
measured value keep the default of 64, chosen for NVIDIA GPUs.

Set here and in KernelAbstractions' extensions, not by the backends, so that it can be
tuned without releasing them.
"""
fold_threshold(backend) = 64

# arithmetic on the extents of an iteration space, which has to fit an `Int`
@noinline throw_too_large() =
throw(ArgumentError("Iteration space has more than typemax(Int) work-items"))
Expand Down
2 changes: 2 additions & 0 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ end

KI.functional(::POCLBackend) = true
KA.pagelock!(::POCLBackend, x) = nothing
# its work-items are iterations of a loop, which is faster than dividing for any group size
KA.fold_threshold(::POCLBackend) = 1

KI.get_backend(::Array) = POCLBackend()

Expand Down
6 changes: 4 additions & 2 deletions test/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ function select_launch_testsuite()
max_items = 1024
max_dims = (1024, 1024, 64)
max_groups = (Int(typemax(Int32)), 65535, 65535)
select(extent, groupsize = nothing; max_dims = max_dims, max_groups = max_groups) =
KernelAbstractions.select_launch(extent, groupsize, max_items, max_dims, max_groups)
select(extent, groupsize = nothing; max_dims = max_dims, max_groups = max_groups, fold_items = 64) =
KernelAbstractions.select_launch(extent, groupsize, max_items, max_dims, max_groups; fold_items)
LinearLaunch = KernelAbstractions.LinearLaunch
NDLaunch = KernelAbstractions.NDLaunch

Expand Down Expand Up @@ -244,6 +244,8 @@ function select_launch_testsuite()
@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((4, 4, 1, 1536); fold_items = 16) === NDLaunch{Int32}()
@test select((5, 3, 1, 1536); fold_items = 16) === 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}()
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,13 @@ end
Testsuite.select_launch_testsuite()
end

@testset "select_launch on PoCL" begin
# tuned launches are folded for any number of work-items in the first three dimensions
kernel = mapped_indices!(CPU())
_, wgs, iterspace, _ = KernelAbstractions.launch_config(kernel, (1, 1, 1, 3000), nothing)
@test KernelAbstractions.select_launch(kernel, wgs, iterspace) === KernelAbstractions.NDLaunch{Int}()
end

@kernel function fill_index!(A)
I = @index(Global, Linear)
@inbounds A[I] = I
Expand Down
Loading