Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add threaded copy #184

Merged
merged 1 commit into from
Dec 21, 2021
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
3 changes: 3 additions & 0 deletions src/ThreadsX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function Set end

function foreach end
function map! end
function copy! end
function copyto! end

function sort end
function sort! end
Expand Down Expand Up @@ -85,6 +87,7 @@ include("basesizes.jl")
include("reduce.jl")
include("foreach.jl")
include("map.jl")
include("copy.jl")
include("mergesort.jl")
include("quicksort.jl")
include("countingsort.jl")
Expand Down
42 changes: 42 additions & 0 deletions src/copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ThreadsX.copy!(dest::AbstractVector, src::AbstractVector; kwargs...) =
ThreadsX.copyto!(resize!(dest, length(src)), src; kwargs...)

function ThreadsX.copyto!(
dest::AbstractArray,
src::AbstractArray;
basesize::Integer = default_copyto_basesize(dest, src),
)
if length(dest) <= basesize
copyto!(dest, src)
elseif IndexStyle(dest) isa IndexLinear && IndexStyle(src) isa IndexLinear
linear_copyto!(dest, src, basesize)
else
cartesian_copyto!(dest, src, basesize)
end
return dest
end

function linear_copyto!(dest, src, basesize)
# TODO: support size-compatible but index-incompatible arrays
@sync for p in _partition(eachindex(dest, src), basesize)
@spawn if p isa AbstractUnitRange
copyto!(dest, first(p), src, first(p), length(p))
else
copyto!(view(dest, p), view(src, p))
end
end
end

function cartesian_copyto!(dest, src, basesize)
ThreadsX.foreach(eachindex(dest, src); basesize = basesize) do i
@inbounds dest[i] = src[i]
end
end

# TODO: Take into account more properties like: sizeof(T), boxed?, union?,
# need conversion?
function default_copyto_basesize(dest::AbstractArray{T}, ::AbstractArray) where {T}
# 2^19 for 64 bit T
basesize = 4194304 ÷ elsizeof(T)
return max(cld(length(dest), Threads.nthreads()), basesize)
end
9 changes: 9 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ __verify_simd_flag(_, simd) =

verify_simd_flag(simd) = __verify_simd_flag(_asval(simd), simd)

# TODO: handle Union
function elsizeof(::Type{T}) where {T}
if Base.allocatedinline(Some{T})
return sizeof(Some{T})
else
return sizeof(Ptr{Any})
end
end

function _median(order, (a, b, c)::NTuple{3,Any})
# Sort `(a, b, c)`:
if Base.lt(order, b, a)
Expand Down
38 changes: 38 additions & 0 deletions test/test_copy.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module TestCopy

using Test
using ThreadsX

@testset "copy vector" begin
@testset for basesize in [[nothing]; 1:8], n in 0:10, T in [Int, Any]
ys = Vector{T}(undef, n)
if basesize === nothing
@test ThreadsX.copyto!(ys, 1:n) == 1:n
@test ThreadsX.copy!(ys, 1:n) == 1:n
else
@test ThreadsX.copyto!(ys, 1:n; basesize = basesize) == 1:n
@test ThreadsX.copy!(ys, 1:n; basesize = basesize) == 1:n
end
end
end

@testset "copy matrix" begin
@testset for basesize in [[nothing]; 1:8], n in 0:10, m in 1:5, T in [Int, Any]
A = Matrix{T}(undef, n, m)
B = reshape(1:n*m, n, m)
if basesize === nothing
@test ThreadsX.copyto!(A, B) == B
else
@test ThreadsX.copyto!(A, B; basesize = basesize) == B
end

Bt = transpose(reshape(1:n*m, m, n))
if basesize === nothing
@test ThreadsX.copyto!(A, Bt) == Bt
else
@test ThreadsX.copyto!(A, Bt; basesize = basesize) == Bt
end
end
end

end # module