From c900f07acef93f41a6f611648ebc92368dff97e8 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Thu, 10 Apr 2025 17:04:33 +0300 Subject: [PATCH 1/7] add abstract types --- src/permute.jl | 36 +++++++++++++++++++++--------------- src/subarray.jl | 25 ++++++++++++++++--------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/permute.jl b/src/permute.jl index b092f00..3f652f2 100644 --- a/src/permute.jl +++ b/src/permute.jl @@ -1,46 +1,52 @@ """ - PermutedDiskArray <: AbstractDiskArray + AbstractPermutedDiskArray <: AbstractDiskArray + +Abstract supertype for diskarray with permuted dimensions. +""" +abstract type AbstractPermutedDiskArray{T,N,P<:PermutedDimsArray{T,N}} <: AbstractDiskArray{T,N} end + +""" + PermutedDiskArray <: AbstractPermutedDiskArray A lazily permuted disk array returned by `permutedims(diskarray, permutation)`. """ -struct PermutedDiskArray{T,N,P<:PermutedDimsArray{T,N}} <: AbstractDiskArray{T,N} +struct PermutedDiskArray{T,N,P<:PermutedDimsArray{T,N}} <: AbstractPermutedDiskArray{T,N,P} a::P end # Base methods - -Base.size(a::PermutedDiskArray) = size(a.a) - +Base.size(a::AbstractPermutedDiskArray) = size(a.a) +Base.parent(a::AbstractPermutedDiskArray) = a.a.parent # DiskArrays interface -haschunks(a::PermutedDiskArray) = haschunks(a.a.parent) -function eachchunk(a::PermutedDiskArray) +haschunks(a::AbstractPermutedDiskArray) = haschunks(parent(a)) +function eachchunk(a::AbstractPermutedDiskArray) # Get the parent chunks - gridchunks = eachchunk(a.a.parent) - perm = _getperm(a.a) + gridchunks = eachchunk(parent(a)) + perm = _getperm(a) # Return permuted GridChunks return GridChunks(genperm(gridchunks.chunks, perm)...) end -function DiskArrays.readblock!(a::PermutedDiskArray, aout, i::OrdinalRange...) +function DiskArrays.readblock!(a::AbstractPermutedDiskArray, aout, i::OrdinalRange...) iperm = _getiperm(a) # Permute the indices inew = genperm(i, iperm) # Permute the dest block and read from the true parent - DiskArrays.readblock!(a.a.parent, PermutedDimsArray(aout, iperm), inew...) + DiskArrays.readblock!(parent(a), PermutedDimsArray(aout, iperm), inew...) return nothing end -function DiskArrays.writeblock!(a::PermutedDiskArray, v, i::OrdinalRange...) +function DiskArrays.writeblock!(a::AbstractPermutedDiskArray, v, i::OrdinalRange...) iperm = _getiperm(a) inew = genperm(i, iperm) # Permute the dest block and write from the true parent - DiskArrays.writeblock!(a.a.parent, PermutedDimsArray(v, iperm), inew...) + DiskArrays.writeblock!(parent(a), PermutedDimsArray(v, iperm), inew...) return nothing end -_getperm(a::PermutedDiskArray) = _getperm(a.a) +_getperm(a::AbstractPermutedDiskArray) = _getperm(a.a) _getperm(::PermutedDimsArray{<:Any,<:Any,perm}) where {perm} = perm -_getiperm(a::PermutedDiskArray) = _getiperm(a.a) +_getiperm(a::AbstractPermutedDiskArray) = _getiperm(a.a) _getiperm(::PermutedDimsArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm # Implementaion macros diff --git a/src/subarray.jl b/src/subarray.jl index 847752a..8c1cd03 100644 --- a/src/subarray.jl +++ b/src/subarray.jl @@ -1,32 +1,39 @@ +""" + SubDiskArray <: AbstractDiskArray + +Abstract supertype for a view of an AbstractDiskArray +""" +abstract type AbstractSubDiskArray{T,N,P,I,L} <: AbstractDiskArray{T,N} end + """ SubDiskArray <: AbstractDiskArray A replacement for `Base.SubArray` for disk arrays, returned by `view`. """ -struct SubDiskArray{T,N,P,I,L} <: AbstractDiskArray{T,N} +struct SubDiskArray{T,N,P,I,L} <: AbstractSubDiskArray{T,N,P,I,L} v::SubArray{T,N,P,I,L} end # Base methods -Base.view(a::SubDiskArray, i...) = SubDiskArray(view(a.v, i...)) -Base.view(a::SubDiskArray, i::CartesianIndices) = view(a, i.indices...) -Base.size(a::SubDiskArray) = size(a.v) -Base.parent(a::SubDiskArray) = a.v.parent +Base.view(a::AbstractSubDiskArray, i...) = SubDiskArray(view(a.v, i...)) +Base.view(a::AbstractSubDiskArray, i::CartesianIndices) = view(a, i.indices...) +Base.size(a::AbstractSubDiskArray) = size(a.v) +Base.parent(a::AbstractSubDiskArray) = a.v.parent _replace_colon(s, ::Colon) = Base.OneTo(s) _replace_colon(s, r) = r # Diskarrays.jl interface -function readblock!(a::SubDiskArray, aout, i::OrdinalRange...) +function readblock!(a::AbstractSubDiskArray, aout, i::OrdinalRange...) pinds = parentindices(view(a.v, i...)) getindex_disk!(aout, parent(a.v), pinds...) end -function writeblock!(a::SubDiskArray, v, i::OrdinalRange...) +function writeblock!(a::AbstractSubDiskArray, v, i::OrdinalRange...) pinds = parentindices(view(a.v, i...)) setindex_disk!(parent(a.v), v, pinds...) end -haschunks(a::SubDiskArray) = haschunks(parent(a.v)) -eachchunk(a::SubDiskArray) = eachchunk_view(haschunks(a.v.parent), a.v) +haschunks(a::AbstractSubDiskArray) = haschunks(parent(a.v)) +eachchunk(a::AbstractSubDiskArray) = eachchunk_view(haschunks(a.v.parent), a.v) function eachchunk_view(::Chunked, vv) pinds = parentindices(vv) From 15557d1e6a6d65265bc13e8c5bfa8f2ea4be9716 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 22 Apr 2025 10:54:49 +0300 Subject: [PATCH 2/7] replace a.v with subarray function --- src/subarray.jl | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/subarray.jl b/src/subarray.jl index 8c1cd03..46faf6b 100644 --- a/src/subarray.jl +++ b/src/subarray.jl @@ -15,25 +15,30 @@ struct SubDiskArray{T,N,P,I,L} <: AbstractSubDiskArray{T,N,P,I,L} end # Base methods -Base.view(a::AbstractSubDiskArray, i...) = SubDiskArray(view(a.v, i...)) +subarray(a::SubDiskArray) = a.v +function Base.view(a::T, i...) where T<:AbstractSubDiskArray + basetype = Base.typename(T).wrapper + basetype(view(subarray(a), i...)) +end Base.view(a::AbstractSubDiskArray, i::CartesianIndices) = view(a, i.indices...) -Base.size(a::AbstractSubDiskArray) = size(a.v) -Base.parent(a::AbstractSubDiskArray) = a.v.parent +Base.size(a::AbstractSubDiskArray) = size(subarray(a)) +Base.parent(a::AbstractSubDiskArray) = parent(subarray(a)) +Base.parentindices(a::AbstractSubDiskArray) = parentindices(subarray(a)) _replace_colon(s, ::Colon) = Base.OneTo(s) _replace_colon(s, r) = r # Diskarrays.jl interface function readblock!(a::AbstractSubDiskArray, aout, i::OrdinalRange...) - pinds = parentindices(view(a.v, i...)) - getindex_disk!(aout, parent(a.v), pinds...) + pinds = parentindices(view(a, i...)) + getindex_disk!(aout, parent(a), pinds...) end function writeblock!(a::AbstractSubDiskArray, v, i::OrdinalRange...) - pinds = parentindices(view(a.v, i...)) - setindex_disk!(parent(a.v), v, pinds...) + pinds = parentindices(view(a, i...)) + setindex_disk!(parent(a), v, pinds...) end -haschunks(a::AbstractSubDiskArray) = haschunks(parent(a.v)) -eachchunk(a::AbstractSubDiskArray) = eachchunk_view(haschunks(a.v.parent), a.v) +haschunks(a::AbstractSubDiskArray) = haschunks(parent(a)) +eachchunk(a::AbstractSubDiskArray) = eachchunk_view(haschunks(parent(a)), a) function eachchunk_view(::Chunked, vv) pinds = parentindices(vv) From 223bd7286c1f3291340551c51f43de637b948d5d Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 22 Apr 2025 11:17:04 +0300 Subject: [PATCH 3/7] add AbstractReshapedDiskArray --- src/reshape.jl | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/reshape.jl b/src/reshape.jl index fe71d77..b43dde7 100644 --- a/src/reshape.jl +++ b/src/reshape.jl @@ -1,6 +1,13 @@ import Base: _throw_dmrs import Base.PermutedDimsArrays: genperm +""" + AbstractReshapedDiskArray <: AbstractDiskArray + +Abstract supertype for a replacements of `Base.ReshapedArray` for `AbstractDiskArray`s` +""" +abstract type AbstractReshapedDiskArray{T,N,P,M} <: AbstractDiskArray{T,N} end + """ ReshapedDiskArray <: AbstractDiskArray @@ -15,24 +22,25 @@ rectangles in the parent array. However, we can support the case where only singleton dimensions are added, later we could allow more special cases like joining two dimensions to one """ -struct ReshapedDiskArray{T,N,P<:AbstractArray{T},M} <: AbstractDiskArray{T,N} +struct ReshapedDiskArray{T,N,P<:AbstractArray{T},M} <: AbstractReshapedDiskArray{T,N,P,M} parent::P keepdim::NTuple{M,Int} newsize::NTuple{N,Int} end # Base methods - -Base.size(r::ReshapedDiskArray) = r.newsize +Base.size(r::AbstractReshapedDiskArray) = r.newsize +Base.parent(r::AbstractReshapedDiskArray) = r.parent +keepdim(r::AbstractReshapedDiskArray) = r.keepdim # DiskArrays interface -haschunks(a::ReshapedDiskArray) = haschunks(a.parent) -function eachchunk(a::ReshapedDiskArray{<:Any,N}) where {N} - pchunks = eachchunk(a.parent) +haschunks(a::AbstractReshapedDiskArray) = haschunks(parent(a)) +function eachchunk(a::AbstractReshapedDiskArray{<:Any,N}) where {N} + pchunks = eachchunk(parent(a)) inow::Int = 0 outchunks = ntuple(N) do idim - if in(idim, a.keepdim) + if in(idim, keepdim(a)) inow += 1 pchunks.chunks[inow] else @@ -41,14 +49,14 @@ function eachchunk(a::ReshapedDiskArray{<:Any,N}) where {N} end return GridChunks(outchunks...) end -function DiskArrays.readblock!(a::ReshapedDiskArray, aout, i::OrdinalRange...) - inew = tuple_tuple_getindex(i, a.keepdim) - DiskArrays.readblock!(a.parent, reshape(aout, map(length, inew)), inew...) +function DiskArrays.readblock!(a::AbstractReshapedDiskArray, aout, i::OrdinalRange...) + inew = tuple_tuple_getindex(i, keepdim(a)) + DiskArrays.readblock!(parent(a), reshape(aout, map(length, inew)), inew...) return nothing end -function DiskArrays.writeblock!(a::ReshapedDiskArray, v, i::OrdinalRange...) - inew = tuple_tuple_getindex(i, a.keepdim) - DiskArrays.writeblock!(a.parent, reshape(v, map(length, inew)), inew...) +function DiskArrays.writeblock!(a::AbstractReshapedDiskArray, v, i::OrdinalRange...) + inew = tuple_tuple_getindex(i, keepdim(a)) + DiskArrays.writeblock!(parent(a), reshape(v, map(length, inew)), inew...) return nothing end function reshape_disk(parent, dims) @@ -92,6 +100,6 @@ macro implement_reshape(t) end # For ambiguity -function Base._reshape(A::DiskArrays.AbstractDiskArray{<:Any,1}, dims::Tuple{Int64}) +function Base._reshape(A::AbstractDiskArray{<:Any,1}, dims::Tuple{Int64}) return reshape_disk(A, dims) end From be71674ce77092744f1b74736262472ad0f221a1 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 22 Apr 2025 11:20:47 +0300 Subject: [PATCH 4/7] fix reshapeddiskarray docstring --- src/reshape.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/reshape.jl b/src/reshape.jl index b43dde7..338669a 100644 --- a/src/reshape.jl +++ b/src/reshape.jl @@ -9,12 +9,11 @@ Abstract supertype for a replacements of `Base.ReshapedArray` for `AbstractDiskA abstract type AbstractReshapedDiskArray{T,N,P,M} <: AbstractDiskArray{T,N} end """ - ReshapedDiskArray <: AbstractDiskArray + ReshapedDiskArray <: AbstractReshapedDiskArray A replacement for `Base.ReshapedArray` for disk arrays, returned by `reshape`. - Reshaping is really not trivial, because the access pattern would completely change for reshaped arrays, rectangles would not remain rectangles in the parent array. From 5d7af2694cf127c321127ffd1f4fc5e792436bd0 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Tue, 22 Apr 2025 11:33:56 +0300 Subject: [PATCH 5/7] replace field access with function calls for permuteddiskarray --- src/permute.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/permute.jl b/src/permute.jl index 3f652f2..e2fb7ee 100644 --- a/src/permute.jl +++ b/src/permute.jl @@ -15,8 +15,9 @@ struct PermutedDiskArray{T,N,P<:PermutedDimsArray{T,N}} <: AbstractPermutedDiskA end # Base methods -Base.size(a::AbstractPermutedDiskArray) = size(a.a) -Base.parent(a::AbstractPermutedDiskArray) = a.a.parent +permuteddimsarray(a) = a.a +Base.size(a::AbstractPermutedDiskArray) = size(permuteddimsarray(a)) +Base.parent(a::AbstractPermutedDiskArray) = parent(permuteddimsarray(a)) # DiskArrays interface haschunks(a::AbstractPermutedDiskArray) = haschunks(parent(a)) @@ -43,10 +44,10 @@ function DiskArrays.writeblock!(a::AbstractPermutedDiskArray, v, i::OrdinalRange return nothing end -_getperm(a::AbstractPermutedDiskArray) = _getperm(a.a) +_getperm(a::AbstractPermutedDiskArray) = _getperm(permuteddimsarray(a)) _getperm(::PermutedDimsArray{<:Any,<:Any,perm}) where {perm} = perm -_getiperm(a::AbstractPermutedDiskArray) = _getiperm(a.a) +_getiperm(a::AbstractPermutedDiskArray) = _getiperm(permuteddimsarray(a)) _getiperm(::PermutedDimsArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm # Implementaion macros From aa47a28d2461580a9b2abbfb64b0a96b0c607171 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Fri, 30 May 2025 14:32:27 +0200 Subject: [PATCH 6/7] import genperm --- src/DiskArrays.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DiskArrays.jl b/src/DiskArrays.jl index 5443bd0..d623397 100644 --- a/src/DiskArrays.jl +++ b/src/DiskArrays.jl @@ -1,6 +1,7 @@ module DiskArrays import ConstructionBase +import Base.PermutedDimsArrays: genperm using LRUCache: LRUCache, LRU From f1ee1074238c92196c32de7b0029db304dddd923 Mon Sep 17 00:00:00 2001 From: tiemvanderdeure Date: Sat, 31 May 2025 16:09:48 +0200 Subject: [PATCH 7/7] more type parameters in AbstractPermutedDiskArray --- src/permute.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/permute.jl b/src/permute.jl index 007533e..95e86ee 100644 --- a/src/permute.jl +++ b/src/permute.jl @@ -3,14 +3,14 @@ Abstract supertype for diskarray with permuted dimensions. """ -abstract type AbstractPermutedDiskArray{T,N} <: AbstractDiskArray{T,N} end +abstract type AbstractPermutedDiskArray{T,N,perm,iperm,A} <: AbstractDiskArray{T,N} end """ PermutedDiskArray <: AbstractPermutedDiskArray A lazily permuted disk array returned by `permutedims(diskarray, permutation)`. """ -struct PermutedDiskArray{T,N,perm,iperm,A<:AbstractArray{T,N}} <: AbstractPermutedDiskArray{T,N} +struct PermutedDiskArray{T,N,perm,iperm,A<:AbstractArray{T,N}} <: AbstractPermutedDiskArray{T,N,perm,iperm,A} parent::A end # We use PermutedDimsArray internals instead of duplicating them, @@ -64,8 +64,8 @@ function DiskArrays.writeblock!(a::AbstractPermutedDiskArray, v, i::OrdinalRange return nothing end -_getperm(::PermutedDiskArray{<:Any,<:Any,perm}) where {perm} = perm -_getiperm(::PermutedDiskArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm +_getperm(::AbstractPermutedDiskArray{<:Any,<:Any,perm}) where {perm} = perm +_getiperm(::AbstractPermutedDiskArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm # Implementation macro