diff --git a/Project.toml b/Project.toml index cdddadd42..51e0de7f3 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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" @@ -61,6 +69,7 @@ StaticArrays = "1.5" StaticArraysCore = "1" UnsafeAtomics = "0.4" julia = "1.10" +oneAPI = "2" pocl_standalone_jll = "7.2.2" [extras] diff --git a/docs/src/api.md b/docs/src/api.md index f5f3e3845..450e9f074 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -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 diff --git a/ext/AMDGPUExt.jl b/ext/AMDGPUExt.jl new file mode 100644 index 000000000..6c3a22755 --- /dev/null +++ b/ext/AMDGPUExt.jl @@ -0,0 +1,8 @@ +module AMDGPUExt + +import KernelAbstractions +import AMDGPU + +KernelAbstractions.fold_threshold(::AMDGPU.ROCBackend) = 16 + +end diff --git a/ext/MetalExt.jl b/ext/MetalExt.jl new file mode 100644 index 000000000..ddfe7187a --- /dev/null +++ b/ext/MetalExt.jl @@ -0,0 +1,8 @@ +module MetalExt + +import KernelAbstractions +import Metal + +KernelAbstractions.fold_threshold(::Metal.MetalBackend) = 16 + +end diff --git a/ext/oneAPIExt.jl b/ext/oneAPIExt.jl new file mode 100644 index 000000000..0be926561 --- /dev/null +++ b/ext/oneAPIExt.jl @@ -0,0 +1,8 @@ +module oneAPIExt + +import KernelAbstractions +import oneAPI + +KernelAbstractions.fold_threshold(::oneAPI.oneAPIBackend) = 16 + +end diff --git a/src/launch.jl b/src/launch.jl index 49aa9af21..cba017925 100644 --- a/src/launch.jl +++ b/src/launch.jl @@ -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 @@ -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 @@ -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 @@ -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")) diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index 32137671d..85e454920 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -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() diff --git a/test/launch.jl b/test/launch.jl index 629a2bbe8..e17833291 100644 --- a/test/launch.jl +++ b/test/launch.jl @@ -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 @@ -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}() diff --git a/test/runtests.jl b/test/runtests.jl index 0207d3d87..1df607fe5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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