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
24 changes: 11 additions & 13 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ KA.@kernel function fill_kernel(A, x)
@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.
# Filling or copying into 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 writing 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
Expand Down Expand Up @@ -149,20 +149,18 @@ KA.@kernel function copy_kernel(A, @Const(B))
end


# small copies, copies of bits unions and overlapping copies are done from the host
function KI.copyto!(backend::POCLBackend, A, B)
length(A) == length(B) ||
throw(ArgumentError("Arrays must match in length, got $(length(A)) and $(length(B))"))
if KI.get_backend(A) == KI.get_backend(B) && KI.get_backend(A) isa POCLBackend
if Base.mightalias(A, B)
error("Arrays may not alias")
end
kernel = copy_kernel(backend)
kernel(A, B, ndrange = length(A))
return A
T = eltype(A)
if KI.get_backend(A) == KI.get_backend(B) && KI.get_backend(A) isa POCLBackend &&
isbitstype(T) && length(A) * sizeof(T) >= KERNEL_FILL_BYTES && !Base.mightalias(A, B)
copy_kernel(backend)(A, B, ndrange = length(A))
else
Base.copyto!(A, B)
return A
end
return A
end

KI.functional(::POCLBackend) = true
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,24 @@ Base.one(::Type{ZeroOnePair}) = ZeroOnePair(1, 1.0f0)
end
end

# large copies use a kernel, small ones the host, as do overlapping ones
@testset "POCL copyto!" begin
threshold = POCL.POCLKernels.KERNEL_FILL_BYTES
values = (Float32 => Float32, Union{Missing, Int32} => i -> isodd(i) ? Int32(i) : missing)
for (T, value) in values
@testset "$T, $m elements" for m in (16, cld(threshold, 4) + 3)
x = T[value(i) for i in 1:m]
y = Array{T}(undef, m)
@test KernelAbstractions.copyto!(POCLBackend(), y, x) === y
@test isequal(y, x)

z = copy(x)
KernelAbstractions.copyto!(POCLBackend(), view(z, 2:m), view(z, 1:(m - 1)))
@test isequal(z, [x[1]; x[1:(m - 1)]])
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