Skip to content

Conversation

nhz2
Copy link
Member

@nhz2 nhz2 commented Jun 22, 2025

Fixes #263

I only implemented the one-dimensional case because it wasn't obvious to me how to handle multiple dimensions, and this is all that is required for better InputBuffers.jl compatibility for now.

I also modified the copyto! function to always return dest, which somehow fixed the circshift test.

@nhz2 nhz2 marked this pull request as ready for review June 22, 2025 02:22
src/array.jl Outdated
@@ -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.

src/array.jl Outdated
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.

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.

I would also prefer @meggart s approach, although you can use a view rather than get index on src. But let's try to avoid nested disk broadcasts

@nhz2 nhz2 changed the title Add 5 arg copyto! into Vector Add 5 arg copyto! into Vector with matching type Jun 28, 2025
@nhz2
Copy link
Member Author

nhz2 commented Jun 28, 2025

I removed the specialization that does type conversion to limit the scope of this PR.

@nhz2
Copy link
Member Author

nhz2 commented Aug 21, 2025

Bump

@rafaqz
Copy link
Collaborator

rafaqz commented Aug 22, 2025

I would like this to work on e.g. Float64 dest from a Float32 diskarray.

Dest is an Array so it calls convert. Just a few Ts to delete.

@nhz2 nhz2 marked this pull request as draft August 22, 2025 00:28
@nhz2 nhz2 marked this pull request as ready for review August 22, 2025 04:54
@rafaqz rafaqz merged commit 877fd67 into main Aug 31, 2025
10 checks passed
@rafaqz rafaqz deleted the nz/copyto-5-args branch August 31, 2025 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scalar indexing in function copyto!(dest::Array, doffs::Integer, src::AbstractDiskArray, soffs::Integer, n::Integer)
3 participants