diff --git a/Project.toml b/Project.toml index 76fa02930..76a4b3ec9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "KernelAbstractions" uuid = "63c18a36-062a-441e-b654-da1e3ab1ce7c" -version = "0.10.9" +version = "0.10.10" authors = ["Valentin Churavy and contributors"] [workspace] diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index 441dc3822..8f866e89b 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -193,14 +193,15 @@ function launch_and_wait( # share conversion, the bulk of each path's per-kernel code converted = converted_arguments(kernel, args) if hp === nothing - info = exception_info() - info[] = ExceptionInfo_st() - GC.@preserve info begin - info_ptr = Base.unsafe_convert(Ptr{ExceptionInfo_st}, info) - event = launch_queued(with_fun(kernel, partial), converted, info_ptr; kwargs...) - wait_for_kernel(event) + queued = with_fun(kernel, partial) + fun = something(partial, kernel.fun) + if may_run_on_host(values(kwargs)) + # (behind a barrier: the queue is a fallback here, and inferring it would + # compile its argument handling for every kernel) + Base.inferencebarrier(launch_queued_and_wait)(queued, converted, fun, values(kwargs)) + else + launch_queued_and_wait(queued, converted, fun, values(kwargs)) end - info[].status == 0 || throw_kernel_exception(info[], something(partial, kernel.fun)) else global_size = values(kwargs).global_size num_groups = something(groups, global_size .÷ hp.local_size) @@ -218,6 +219,22 @@ function launch_and_wait( return nothing end +# launch `kernel` with the arguments `converted` on the queue, and wait for it; `fun` is the +# compiled kernel that reports exceptions +@noinline function launch_queued_and_wait( + kernel::AbstractKernel, converted::Tuple, fun::cl.Kernel, kwargs::NamedTuple + ) + info = exception_info() + info[] = ExceptionInfo_st() + GC.@preserve info begin + info_ptr = Base.unsafe_convert(Ptr{ExceptionInfo_st}, info) + event = launch_queued(kernel, converted, info_ptr; kwargs...) + wait_for_kernel(event) + end + info[].status == 0 || throw_kernel_exception(info[], fun) + return nothing +end + # kept out of `launch_and_wait`, which is compiled for every kernel @noinline function wait_for_kernel(event::cl.Event) try @@ -501,10 +518,15 @@ function host_kernel_max_items(k::cl.Kernel) return prevpow(2, max(host_kernel_stack_limit ÷ k.private_mem_size, 1)) end +# whether a launch with the keyword arguments `kwargs` may run a host kernel; others always +# run on the queue. (decided by the types of `kwargs`) +@inline may_run_on_host(kwargs::NamedTuple) = + haskey(kwargs, :local_size) && haskey(kwargs, :global_size) && + kwargs.local_size isa Dims{3} && kwargs.global_size isa Dims{3} + @inline function host_kernel(kernel::HostKernel, kwargs::NamedTuple, groups) - haskey(kwargs, :local_size) && haskey(kwargs, :global_size) || return nothing + may_run_on_host(kwargs) || return nothing local_size, global_size = kwargs.local_size, kwargs.global_size - local_size isa Dims{3} && global_size isa Dims{3} || return nothing # host kernels only run whole work-groups groups === nothing && global_size .% local_size != (0, 0, 0) && return nothing return host_kernel(kernel.fun, local_size, kernel.rng_state) diff --git a/src/precompile.jl b/src/precompile.jl index cf54ef37c..305664d6f 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -8,6 +8,12 @@ @inbounds A[i] = fma(2, B[i], 1) end +# a kernel for the queue, which launches without a 3-d work-group size take +function precompile_queued_kernel(A) + @inbounds A[1] = 1.0f0 + return +end + # whether the workload below launched its kernel, checked by the tests const precompiled_launch = Ref(false) @@ -27,6 +33,7 @@ PrecompileTools.@setup_workload begin A = Base.zeros(Float32, 4) B = Base.ones(Float32, 4) precompile_kernel(CPU())(A, B; ndrange = length(A)) + POCL.@opencl global_size = 1 local_size = 1 precompile_queued_kernel(A) synchronize(CPU()) end end diff --git a/test/runtests.jl b/test/runtests.jl index e8c08f10f..47f3fe49c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1761,6 +1761,42 @@ end @test all(==(-1), view(B, 1:m)) end +@kernel function queue_free_kernel!(A) + i = @index(Global) + @inbounds A[i] = i +end + +# The queue is a fallback, which launches only compile when they take it +@testset "POCL launches don't compile the queue for every kernel" begin + A = zeros(Int, 13) + queue_free_kernel!(POCL.POCLBackend(), 8)(A; ndrange = length(A)) # a split launch + @test A == 1:13 + F = typeof(gpu_queue_free_kernel!) + queued = [ + mi for m in methods(POCL.launch_converted) for mi in Base.specializations(m) + if mi isa Core.MethodInstance && occursin(string(F), string(mi.specTypes)) + ] + @test isempty(queued) +end + +function queue_only_kernel!(A) + @inbounds A[1] = 1 + return +end + +queued_launch_allocations(k, A) = @allocated k(A; global_size = 4, local_size = 4) + +# ... while launches that can only run on the queue (without 3-d sizes) call it directly +@testset "POCL launches on the queue don't allocate" begin + A = zeros(Int, 4) + k = @opencl launch = false queue_only_kernel!(A) + queued_launch_allocations(k, A) + # (waiting for the kernel allocates on some versions, see "POCL launch allocations") + skip_wait = VERSION < v"1.12" || isdefined(Base, :CANCEL_TOKEN) + @test queued_launch_allocations(k, A) == 0 skip = skip_wait + @test A[1] == 1 +end + @kernel function muladd_kernel!(D, @Const(A), @Const(B), @Const(C)) i = @index(Global) @inbounds D[i] = muladd(A[i], B[i], C[i])