Skip to content

Add 5 arg copyto! into Vector with matching type #264

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ function _disk_copyto!(dest, Rdest, src, Rsrc)
return dest
end
view(dest, Rdest) .= view(src, Rsrc)
return dest
end
function _disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
if iszero(n)
return dest
end
if n < 0
throw(ArgumentError(LazyString("tried to copy n=",
n," elements, but n should be non-negative")))
end
destv = view(dest, range(dstart, length=n))
DiskArrays.readblock!(src, destv, range(sstart, length=n))
return dest
end
function _disk_copyto_vector!(dest, dstart, src, sstart, n)
if iszero(n)
return dest
end
if n < 0
throw(ArgumentError(LazyString("tried to copy n=",
n," elements, but n should be non-negative")))
end
view(dest, range(dstart, length=n)) .= view(src, range(dstart, length=n))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the use case it might be better for performance to avoid the DiskArray broadcast machinery, broadcast on views should work but might be a bit slow if chunk sizes are too small. Instead we might instantiate the result and copy with coptyo!(dest,dstart,src[range(dstart, length=n)])

Can we assume that the amount of data we copy fits in memory, or is dest mmapped in your case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not doing any mmapping in InputBuffers.jl, though there is nothing stopping someone from passing a mmapped out to Base.readbytes!(b::InputBuffer, out::AbstractArray{UInt8}, nb=length(out))
Also, the copyto! in InputBuffers.jl should end up calling _disk_copyto_same_type_vector with the direct readblock! call since src and dest both have eltype UInt8.

return dest
end

# Use a view for lazy reverse
Expand Down Expand Up @@ -76,6 +100,18 @@ macro implement_array_methods(t)
function Base.copyto!(dest::PermutedDimsArray{T,N}, src::$t{T,N}) where {T,N}
return $_disk_copyto!(dest, src)
end
function Base.copyto!(dest::Vector{T}, dstart::Integer, src::$t{T, 1}, sstart::Integer, n::Integer) where {T}
Copy link
Collaborator

@rafaqz rafaqz Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is T always identical for scr and dest in Base copyto! ? What if you have different Real types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the copyto! in Base uses setindex! so will call convert on each element.
I added a version that uses broadcasting to support that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming here that readblock! requires the element types to match, and aout to be stored in contiguous memory.

Copy link
Collaborator

@rafaqz rafaqz Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use setindex! when writing into the chunk buffer, so it should be the same?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Looking at the docs, it just says "The results shall be written into aout" but I'm not sure if there are implementations of readblock! that make other assumptions about aout's eltype, or memory layout.

return $_disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
end
function Base.copyto!(dest::SubArray{T, 1, Vector{T}, <:Tuple{AbstractUnitRange}, true}, dstart::Integer, src::$t{T, 1}, sstart::Integer, n::Integer) where {T}
return $_disk_copyto_same_type_vector!(dest, dstart, src, sstart, n)
end
function Base.copyto!(dest::Vector, dstart::Integer, src::$t{T, 1}, sstart::Integer, n::Integer) where {T}
return $_disk_copyto_vector!(dest, dstart, src, sstart, n)
end
function Base.copyto!(dest::SubArray{TD, 1, Vector{TD}}, dstart::Integer, src::$t{TS, 1}, sstart::Integer, n::Integer) where {TD, TS}
return $_disk_copyto_vector!(dest, dstart, src, sstart, n)
end

Base.reverse(a::$t; dims=:) = $_disk_reverse(a, dims)
Base.reverse(a::$t{<:Any,1}) = $_disk_reverse1(a)
Expand Down
27 changes: 23 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,34 @@ end
@test Array(a_disk) == a
@testset "copyto" begin
x = zero(a)
copyto!(x, a_disk)
@test copyto!(x, a_disk) === x
@test x == a
copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9)))
@test copyto!(x, CartesianIndices((1:3, 1:2)), a_disk, CartesianIndices((8:10, 8:9))) === x
# Test copyto! with zero length index
x_empty = Matrix{Int64}(undef, 0, 2)
copyto!(x_empty, CartesianIndices((1:0, 1:2)), a_disk, CartesianIndices((8:7, 8:9)))
# copyto! with different length should throw an error
@test_throws ArgumentError copyto!(x, CartesianIndices((1:1, 1:2)), a_disk, CartesianIndices((4:6, 8:9)))

# 5 arg copyto!
a_vec = collect(0x00:0x90)
test_dests = [
zero(a_vec),
zeros(Float32, length(a_vec)),
view(zero(a_vec), 1:10),
view(zero(a_vec), 1:2:20),
view(zeros(Float32, length(a_vec)), 1:10),
view(zeros(Float32, length(a_vec)), 1:2:20),
]
for x in test_dests
local a_disk = AccessCountDiskArray(a_vec; chunksize=(15,))
x = zero(a_vec)
@test_throws ArgumentError copyto!(x, 1, a_disk, 1, -1)
@test copyto!(x, 1, a_disk, 1, 0) === x
@test copyto!(x, 6, a_disk, 3, 2) === x
@test x[6] == a_vec[3]
@test x[7] == a_vec[4]
@test getindex_count(a_disk) == 1
end
end

@test collect(reverse(a_disk)) == reverse(a)
Expand All @@ -804,7 +823,7 @@ end
@test vcat(a_disk, a_disk) == vcat(a, a)
@test hcat(a_disk, a_disk) == hcat(a, a)
@test cat(a_disk, a_disk; dims=3) == cat(a, a; dims=3)
@test_broken circshift(a_disk, 2) == circshift(a, 2) # This one is super weird. The size changes.
@test circshift(a_disk, 2) == circshift(a, 2)
end

@testset "Reshape" begin
Expand Down
Loading