Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ function _disk_copyto!(dest, Rdest, src, Rsrc)
return dest
end
view(dest, Rdest) .= view(src, Rsrc)
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
destv = view(dest, range(dstart, length=n))
DiskArrays.readblock!(src, destv, range(sstart, length=n))
return dest
end

# Use a view for lazy reverse
Expand Down Expand Up @@ -76,6 +89,9 @@ 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.

Copy link
Collaborator

@rafaqz rafaqz Aug 22, 2025

Choose a reason for hiding this comment

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

Julia does automatic type conversion when writing to an array. (And dest is a Vector)

If any other package breaks that we will fix it on that side.

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
16 changes: 12 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,15 +770,23 @@ 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)
a_vec_disk = AccessCountDiskArray(a_vec; chunksize=(15,))
x_vec = zero(a_vec)
@test_throws ArgumentError copyto!(x_vec, 1, a_vec_disk, 1, -1)
@test copyto!(x_vec, 1, a_vec_disk, 1, 0) === x_vec
@test copyto!(x_vec, 6, a_vec_disk, 3, 2) === x_vec
@test x_vec[6] == a_vec[3]
@test x_vec[7] == a_vec[4]
end

@test collect(reverse(a_disk)) == reverse(a)
Expand All @@ -804,7 +812,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