From 1347c84b1b80c2d1d962424c84a4a3fc22250605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Fri, 11 Sep 2026 05:38:03 -0500 Subject: [PATCH] Cut allocations when launching CPU kernels A warm launch of a two-argument kernel on the CPU backend allocated about 50 times (2.2 KiB). Most of it came from Julia not specializing on `args...` that a method only splats onward, which boxed every kernel argument on the way from `Kernel` to `clSetKernelArg`; the rest from per-launch vectors for the work sizes and SVM pointers, a keyword splat through the generated kernel call, and a device property query. - Annotate the splatted arguments as `Vararg{Any, N}` in the `Kernel` call, the generated `AbstractKernel` call, `call` and `set_args!`, and unroll `set_args!` by recursion instead of `enumerate`. - Pass each kernel argument to `clSetKernelArg` as a `Ref{T}` `ccall` argument, which copies it to the stack. - Pass the global and local work sizes as three-element tuples through a `ccall` taking `Ref{NTuple{3, Csize_t}}` when there is no offset or device RNG state, skipping the `max_work_item_dims` query. - The generated kernel call takes `global_size` and `local_size` as explicit keywords. - `clconvert` and `call` take `nothing` for the SVM pointers instead of allocating an empty vector; no adaptor rule collects such pointers. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01VHciC8x39gm97sABrSvBkt --- src/pocl/backend.jl | 2 +- src/pocl/compiler/execution.jl | 15 ++++---- src/pocl/nanoOpenCL.jl | 63 ++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index a8dfcd0cf..94fe76372 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -185,7 +185,7 @@ function threads_to_workgroupsize(threads, ndrange) end end -function (obj::KA.Kernel{POCLBackend})(args...; ndrange = nothing, workgroupsize = nothing) +function (obj::KA.Kernel{POCLBackend})(args::Vararg{Any, N}; ndrange = nothing, workgroupsize = nothing) where {N} ndrange, workgroupsize, iterspace, dynamic = KA.launch_config(obj, ndrange, workgroupsize) diff --git a/src/pocl/compiler/execution.jl b/src/pocl/compiler/execution.jl index 3e2c78135..5b360842d 100644 --- a/src/pocl/compiler/execution.jl +++ b/src/pocl/compiler/execution.jl @@ -86,7 +86,7 @@ end ## argument conversion struct KernelAdaptor - svm_pointers::Vector{Ptr{Cvoid}} + svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}} end # # assume directly-passed pointers are SVM pointers @@ -137,7 +137,7 @@ register methods for the the `OpenCL.KernelAdaptor` type. The `pointers` argument is used to collect pointers to indirect SVM buffers, which need to be registered with OpenCL before invoking the kernel. """ -function clconvert(arg, pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[]) +function clconvert(arg, pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing) return adapt(KernelAdaptor(pointers), arg) end @@ -149,11 +149,11 @@ abstract type AbstractKernel{F, TT} end pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt)) @inline @generated function (kernel::AbstractKernel{F, TT})( - args...; - call_kwargs... - ) where {F, TT} + args::Vararg{Any, N}; + global_size = (1,), local_size = nothing + ) where {F, TT, N} sig = Tuple{F, TT.parameters...} # Base.signature_type with a function type - args = (:(kernel.f), (:(clconvert(args[$i], svm_pointers)) for i in 1:length(args))...) + args = (:(kernel.f), (:(clconvert(args[$i])) for i in 1:length(args))...) # filter out ghost arguments that shouldn't be passed to_pass = map(pass_arg, sig.parameters) @@ -175,8 +175,7 @@ pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isco call_tt = Base.to_tuple_type(call_t) return quote - svm_pointers = Ptr{Cvoid}[] - $cl.clcall(kernel.fun, $call_tt, $(call_args...); svm_pointers, kernel.rng_state, call_kwargs...) + $cl.clcall(kernel.fun, $call_tt, $(call_args...); global_size, local_size, kernel.rng_state) end end diff --git a/src/pocl/nanoOpenCL.jl b/src/pocl/nanoOpenCL.jl index 2d196c9fe..e3adadbf1 100644 --- a/src/pocl/nanoOpenCL.jl +++ b/src/pocl/nanoOpenCL.jl @@ -665,6 +665,23 @@ end )::cl_int end +# sizes passed as tuples, which `ccall` copies to the stack +@checked function clEnqueueNDRangeKernel( + command_queue, kernel, work_dim, + global_work_size::NTuple{3, Csize_t}, local_work_size::NTuple{3, Csize_t}, event::Ref{cl_event} + ) + @ccall libopencl.POclEnqueueNDRangeKernel( + command_queue::cl_command_queue, + kernel::cl_kernel, work_dim::cl_uint, + C_NULL::Ptr{Csize_t}, + global_work_size::Ref{NTuple{3, Csize_t}}, + local_work_size::Ref{NTuple{3, Csize_t}}, + 0::cl_uint, + C_NULL::Ptr{cl_event}, + event::Ref{cl_event} + )::cl_int +end + @checked function clEnqueueNDRangeKernel( command_queue, kernel, work_dim, global_work_offset, global_work_size, @@ -1254,9 +1271,10 @@ function set_arg!(k::Kernel, idx::Integer, arg::LocalMem) end function set_arg!(k::Kernel, idx::Integer, arg::T) where {T} - ref = Ref(arg) - tsize = sizeof(ref) - err = unchecked_clSetKernelArg(k, cl_uint(idx - 1), tsize, ref) + # `Ref{T}` makes `ccall` pass a pointer to a stack copy of `arg` + err = @ccall libopencl.POclSetKernelArg( + k::cl_kernel, cl_uint(idx - 1)::cl_uint, sizeof(T)::Csize_t, arg::Ref{T} + )::cl_int if err == CL_INVALID_ARG_SIZE error( """Mismatch between Julia and OpenCL type for kernel argument $idx. @@ -1275,19 +1293,36 @@ function set_arg!(k::Kernel, idx::Integer, arg::T) where {T} return k end -function set_args!(k::Kernel, args...) - for (i, a) in enumerate(args) - set_arg!(k, i, a) - end - return +set_args!(k::Kernel, args::Vararg{Any, N}) where {N} = set_args!(k, 1, args...) +@inline set_args!(k::Kernel, i::Int) = nothing +@inline function set_args!(k::Kernel, i::Int, arg, args::Vararg{Any, N}) where {N} + set_arg!(k, i, arg) + return set_args!(k, i + 1, args...) end +# work sizes padded to the three dimensions OpenCL devices support +const WorkSize = NTuple{3, Csize_t} +@inline work_size(sizes) = ntuple(i -> i <= length(sizes) ? Csize_t(sizes[i]) : Csize_t(0), Val(3)) + function enqueue_kernel( k::Kernel, global_work_size, local_work_size = nothing; global_work_offset = nothing, rng_state = false, nargs = nothing ) - max_work_dim = device().max_work_item_dims work_dim = length(global_work_size) + + if global_work_offset === nothing && local_work_size !== nothing && !rng_state && work_dim <= 3 + if length(local_work_size) != work_dim + throw(ArgumentError("global_work_size and local_work_size have differing dims")) + end + ret_event = Ref{cl_event}() + clEnqueueNDRangeKernel( + queue(), k, cl_uint(work_dim), + work_size(global_work_size), work_size(local_work_size), ret_event + ) + return Event(ret_event[]) + end + + max_work_dim = device().max_work_item_dims if work_dim > max_work_dim throw(ArgumentError("global_work_size has max dim of $max_work_dim")) end @@ -1354,19 +1389,19 @@ function enqueue_kernel( end function call( - k::Kernel, args...; global_size = (1,), local_size = nothing, + k::Kernel, args::Vararg{Any, N}; global_size = (1,), local_size = nothing, global_work_offset = nothing, - svm_pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[], + svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing, rng_state = false - ) + ) where {N} set_args!(k, args...) - if !isempty(svm_pointers) + if svm_pointers !== nothing && !isempty(svm_pointers) clSetKernelExecInfo( k, CL_KERNEL_EXEC_INFO_SVM_PTRS, sizeof(svm_pointers), svm_pointers ) end - return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = length(args)) + return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = N) end # convert the argument values to match the kernel's signature (specified by the user)