From 9a9386c5b060b864d4f2f1b461c5b2ce5e486390 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 18:03:50 +0100 Subject: [PATCH] Convert kernel arguments without `map` over the argument tuple `map` over a tuple of 32 or more elements isn't inferred, so launching a kernel with that many arguments (31 through KernelAbstractions, which adds its context) dispatched for every argument and allocated 10-40 KB on the host instead of a few hundred bytes per argument. Convert the arguments with an unrolled `ntuple`, as `onecall` already sets them, both when `@oneapi` compiles the kernel and when it is launched. Fixes #769. --- src/compiler/execution.jl | 11 ++++++++--- test/execution.jl | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/compiler/execution.jl b/src/compiler/execution.jl index 50f86caf..ab72ed42 100644 --- a/src/compiler/execution.jl +++ b/src/compiler/execution.jl @@ -137,8 +137,8 @@ end compiler_kwargs::NamedTuple) where {F,launch} GC.@preserve f args begin kernel_f = kernel_convert(f) - kernel_args = map(kernel_convert, args) - kernel_tt = Tuple{map(Core.Typeof, kernel_args)...} + kernel_args = convert_arguments(args) + kernel_tt = Tuple{ntuple(i -> Core.Typeof(kernel_args[i]), Val(length(args)))...} kernel = zefunction(kernel_f, kernel_tt; compiler_kwargs...) if launch launch_managed(kernel, f, args; call_kwargs...) @@ -218,6 +218,11 @@ register methods for the the `oneAPI.KernelAdaptor` type. """ kernel_convert(arg, refs=nothing) = adapt(KernelAdaptor(refs), arg) +# unrolled, as `map` over a tuple of 32 or more elements isn't inferred, making the launch +# dispatch and allocate for every argument +@inline convert_arguments(args::Tuple, refs=nothing) = + ntuple(i -> kernel_convert(args[i], refs), Val(length(args))) + ## abstract kernel functionality @@ -510,7 +515,7 @@ end kwargs...) refs = DataRef[] source === nothing || kernel_convert(source, refs) - converted = map(arg -> kernel_convert(arg, refs), args) + converted = convert_arguments(args, refs) managed = Managed[ref[] for ref in refs] ret = GC.@preserve source args with_ownership(queue, managed) do call(kernel, converted; queue, roots=refs, kwargs...) diff --git a/test/execution.jl b/test/execution.jl index cf829e86..a9ec3db3 100644 --- a/test/execution.jl +++ b/test/execution.jl @@ -80,6 +80,11 @@ end kernel(a) = return bar(a) = @oneapi kernel(a) @inferred bar(oneArray([1])) + + # also with 32 or more arguments, where `map` over a tuple isn't inferred + many(args...) = return + baz(args) = @oneapi many(args...) + @inferred baz(ntuple(_ -> oneArray([1]), 40)) end