From 4ccb4f049c6da86f589435c9da554d9856e52227 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 26 Oct 2026 12:43:43 +0100 Subject: [PATCH] POCL: initialize large arrays with a kernel in `zeros` and `ones` Since #795, `zeros` and `ones` on the CPU back-end allocate and `fill!` from the calling thread, which first-touches every page and so places all of the memory in that thread's NUMA domain. KernelAbstractions 0.9 filled them with a kernel, and the NUMA example relied on that. Fill arrays of bits types of at least 1 MiB with a kernel again. The kernel takes the value rather than its type: a `Type{T}` argument is what failed for bits unions in #795. Arrays of bits unions, which aren't `isbitstype`, and smaller arrays keep the host `fill!`. The threshold is where the launch stops dominating. Initializing a fresh `Float32` array on a Ryzen 9950X (one memory domain) with 1, 8 or 16 PoCL threads, the kernel takes 1.3 to 22 times as long as `fill!` up to 256 KiB, 0.8 to 1.1 times from 1 to 16 MiB, and 1.1 to 1.2 times from 64 MiB on, where both are bound by memory bandwidth. --- docs/src/examples/numa_aware.md | 2 +- src/pocl/backend.jl | 29 +++++++++++++++++++++++++++++ test/runtests.jl | 24 ++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/src/examples/numa_aware.md b/docs/src/examples/numa_aware.md index 3efa784ae..5cd4a3829 100644 --- a/docs/src/examples/numa_aware.md +++ b/docs/src/examples/numa_aware.md @@ -19,7 +19,7 @@ The `CPU` backend runs kernels with [PoCL](https://portablecl.org) on PoCL's own 1) Pin the threads that run the kernels. Set PoCL's `POCL_AFFINITY=1` environment variable before the backend is first used, which pins each of PoCL's threads to a core. Tools that pin Julia's threads, like [ThreadPinning.jl](https://github.com/carstenbauer/ThreadPinning.jl), don't affect the threads that run the kernels. 2) Choose the number of threads with `julia -t N` or the `JULIA_KA_CPU_THREADS` environment variable; see [`CPU`](@ref). -3) Initialize your data in parallel(!), with a kernel. Under the ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.) a page of memory is placed in the memory domain of the thread that first writes to it. `KernelAbstractions.zeros(backend, dtype, N)` and `fill!` write from the calling thread, which places all the memory in that thread's domain. The example instead allocates with `KernelAbstractions.allocate` and writes the initial values with a kernel (`init = :parallel`), so that the threads that run the computational kernel touch the memory first. +3) Initialize your data in parallel(!), with a kernel. Under the ["NUMA first-touch policy"](https://queue.acm.org/detail.cfm?id=2513149#:~:text=This%20is%20called%20the%20first,policy%20associated%20with%20a%20task.) a page of memory is placed in the memory domain of the thread that first writes to it. `fill!` writes from the calling thread, which places all the memory in that thread's domain. The example instead allocates with `KernelAbstractions.allocate` and writes the initial values with a kernel (`init = :parallel`), so that the threads that run the computational kernel touch the memory first. `KernelAbstractions.zeros(backend, dtype, N)` and `KernelAbstractions.ones` do the same for arrays of bits types of at least 1 MiB; smaller arrays, and arrays of other types, are filled from the calling thread. **Demonstration:** diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index a654b254d..037a7bad8 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -102,6 +102,35 @@ end # `unsafe_wrap`) are the user's responsibility, as documented for `CPU`. KI.allocate(::POCLBackend, ::Type{T}, dims::Tuple; unified::Bool = false) where {T} = Array{T}(undef, dims) +# Initialized + +KA.@kernel function fill_kernel(A, x) + I = KA.@index(Global) + @inbounds A[I] = x +end + +# Filling an array with a kernel first-touches its pages from PoCL's threads, which spreads +# them over the threads' memory domains instead of placing all of them in the calling +# thread's (see the NUMA example). Below 1 MiB, the launch overhead makes that up to several +# times slower than filling from the host, while there are too few pages for placement to +# matter. +const KERNEL_FILL_BYTES = 1 << 20 + +# arrays of bits unions, whose element type isn't `isbitstype`, are filled from the host +function fill_initialized!(backend::POCLBackend, A::Array{T}, x) where {T} + if isbitstype(T) && sizeof(A) >= KERNEL_FILL_BYTES + fill_kernel(backend)(A, x; ndrange = length(A)) + else + fill!(A, x) + end + return A +end + +KI.zeros(backend::POCLBackend, ::Type{T}, dims::Tuple; kwargs...) where {T} = + fill_initialized!(backend, KI.allocate(backend, T, dims; kwargs...), zero(T)) +KI.ones(backend::POCLBackend, ::Type{T}, dims::Tuple; kwargs...) where {T} = + fill_initialized!(backend, KI.allocate(backend, T, dims; kwargs...), one(T)) + # Adapt.jl's `Array` rule converts every `AbstractArray` leaf; `isbits` arrays (ranges, view indices) # which we want to keep as they are. Adapt.adapt_storage(::POCLBackend, x::AbstractArray) = isbits(x) ? x : Adapt.adapt(Array, x) diff --git a/test/runtests.jl b/test/runtests.jl index 961ab6047..8d6f176ba 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -712,6 +712,30 @@ end end end +struct ZeroOnePair + a::Int16 + b::Float32 +end +Base.zero(::Type{ZeroOnePair}) = ZeroOnePair(0, 0.0f0) +Base.one(::Type{ZeroOnePair}) = ZeroOnePair(1, 1.0f0) + +# large arrays are filled with a kernel, small ones from the host +@testset "POCL zeros/ones of large arrays" begin + threshold = POCL.POCLKernels.KERNEL_FILL_BYTES + for T in (Float32, Int8, ComplexF64, ZeroOnePair) + n = cld(threshold, sizeof(T)) + 3 + for dims in ((n,), (cld(n, 7), 7), (1,)) + Z = KernelAbstractions.zeros(POCLBackend(), T, dims) + @test Z isa Array{T, length(dims)} && size(Z) == dims + @test all(==(zero(T)), Z) + + O = KernelAbstractions.ones(POCLBackend(), T, dims...) + @test O isa Array{T, length(dims)} && size(O) == dims + @test all(==(one(T)), O) + end + end +end + # a mapping KernelAbstractions doesn't know, which the index functions have to go # through `expand`, `in` and `linear_index` for struct TransposedMapping end