Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/examples/numa_aware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
29 changes: 29 additions & 0 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading