Skip to content

Commit 6522ba3

Browse files
add abstract types for views and permuted arrays (#255)
* add abstract types * replace a.v with subarray function * add AbstractReshapedDiskArray * fix reshapeddiskarray docstring * replace field access with function calls for permuteddiskarray * import genperm * more type parameters in AbstractPermutedDiskArray --------- Co-authored-by: Rafael Schouten <[email protected]>
1 parent b0a4a7b commit 6522ba3

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

src/DiskArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module DiskArrays
22

33
import ConstructionBase
4+
import Base.PermutedDimsArrays: genperm
45

56
using LRUCache: LRUCache, LRU
67

src/permute.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"""
2-
PermutedDiskArray <: AbstractDiskArray
2+
AbstractPermutedDiskArray <: AbstractDiskArray
3+
4+
Abstract supertype for diskarray with permuted dimensions.
5+
"""
6+
abstract type AbstractPermutedDiskArray{T,N,perm,iperm,A} <: AbstractDiskArray{T,N} end
7+
8+
"""
9+
PermutedDiskArray <: AbstractPermutedDiskArray
310
411
A lazily permuted disk array returned by `permutedims(diskarray, permutation)`.
512
"""
6-
struct PermutedDiskArray{T,N,perm,iperm,A<:AbstractArray{T,N}} <: AbstractDiskArray{T,N}
13+
struct PermutedDiskArray{T,N,perm,iperm,A<:AbstractArray{T,N}} <: AbstractPermutedDiskArray{T,N,perm,iperm,A}
714
parent::A
815
end
916
# We use PermutedDimsArray internals instead of duplicating them,
@@ -41,24 +48,24 @@ function eachchunk(a::PermutedDiskArray)
4148
# Return permuted GridChunks
4249
return GridChunks(genperm(gridchunks.chunks, perm)...)
4350
end
44-
function DiskArrays.readblock!(a::PermutedDiskArray, aout, i::OrdinalRange...)
51+
function DiskArrays.readblock!(a::AbstractPermutedDiskArray, aout, i::OrdinalRange...)
4552
iperm = _getiperm(a)
4653
# Permute the indices
4754
inew = genperm(i, iperm)
4855
# Permute the dest block and read from the true parent
4956
DiskArrays.readblock!(parent(a), PermutedDimsArray(aout, iperm), inew...)
5057
return nothing
5158
end
52-
function DiskArrays.writeblock!(a::PermutedDiskArray, v, i::OrdinalRange...)
59+
function DiskArrays.writeblock!(a::AbstractPermutedDiskArray, v, i::OrdinalRange...)
5360
iperm = _getiperm(a)
5461
inew = genperm(i, iperm)
5562
# Permute the dest block and write from the true parent
5663
DiskArrays.writeblock!(parent(a), PermutedDimsArray(v, iperm), inew...)
5764
return nothing
5865
end
5966

60-
_getperm(::PermutedDiskArray{<:Any,<:Any,perm}) where {perm} = perm
61-
_getiperm(::PermutedDiskArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm
67+
_getperm(::AbstractPermutedDiskArray{<:Any,<:Any,perm}) where {perm} = perm
68+
_getiperm(::AbstractPermutedDiskArray{<:Any,<:Any,<:Any,iperm}) where {iperm} = iperm
6269

6370
# Implementation macro
6471

src/reshape.jl

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
import Base: _throw_dmrs
2-
import Base.PermutedDimsArrays: genperm
32

43
"""
5-
ReshapedDiskArray <: AbstractDiskArray
4+
AbstractReshapedDiskArray <: AbstractDiskArray
5+
6+
Abstract supertype for a replacements of `Base.ReshapedArray` for `AbstractDiskArray`s`
7+
"""
8+
abstract type AbstractReshapedDiskArray{T,N,P,M} <: AbstractDiskArray{T,N} end
9+
10+
"""
11+
ReshapedDiskArray <: AbstractReshapedDiskArray
612
713
A replacement for `Base.ReshapedArray` for disk arrays,
814
returned by `reshape`.
915
10-
1116
Reshaping is really not trivial, because the access pattern would
1217
completely change for reshaped arrays, rectangles would not remain
1318
rectangles in the parent array.
1419
1520
However, we can support the case where only singleton dimensions are added,
1621
later we could allow more special cases like joining two dimensions to one
1722
"""
18-
struct ReshapedDiskArray{T,N,P<:AbstractArray{T},M} <: AbstractDiskArray{T,N}
23+
struct ReshapedDiskArray{T,N,P<:AbstractArray{T},M} <: AbstractReshapedDiskArray{T,N,P,M}
1924
parent::P
2025
keepdim::NTuple{M,Int}
2126
newsize::NTuple{N,Int}
2227
end
2328

2429
# Base methods
30+
Base.size(r::AbstractReshapedDiskArray) = r.newsize
31+
Base.parent(r::AbstractReshapedDiskArray) = r.parent
2532

26-
Base.parent(r::ReshapedDiskArray) = r.parent
27-
Base.size(r::ReshapedDiskArray) = r.newsize
33+
keepdim(r::AbstractReshapedDiskArray) = r.keepdim
2834

2935
# DiskArrays interface
3036

31-
haschunks(a::ReshapedDiskArray) = haschunks(a.parent)
32-
function eachchunk(a::ReshapedDiskArray{<:Any,N}) where {N}
33-
pchunks = eachchunk(a.parent)
37+
haschunks(a::AbstractReshapedDiskArray) = haschunks(parent(a))
38+
function eachchunk(a::AbstractReshapedDiskArray{<:Any,N}) where {N}
39+
pchunks = eachchunk(parent(a))
3440
inow::Int = 0
3541
outchunks = ntuple(N) do idim
36-
if in(idim, a.keepdim)
42+
if in(idim, keepdim(a))
3743
inow += 1
3844
pchunks.chunks[inow]
3945
else
@@ -42,14 +48,14 @@ function eachchunk(a::ReshapedDiskArray{<:Any,N}) where {N}
4248
end
4349
return GridChunks(outchunks...)
4450
end
45-
function DiskArrays.readblock!(a::ReshapedDiskArray, aout, i::OrdinalRange...)
46-
inew = tuple_tuple_getindex(i, a.keepdim)
47-
DiskArrays.readblock!(a.parent, reshape(aout, map(length, inew)), inew...)
51+
function DiskArrays.readblock!(a::AbstractReshapedDiskArray, aout, i::OrdinalRange...)
52+
inew = tuple_tuple_getindex(i, keepdim(a))
53+
DiskArrays.readblock!(parent(a), reshape(aout, map(length, inew)), inew...)
4854
return nothing
4955
end
50-
function DiskArrays.writeblock!(a::ReshapedDiskArray, v, i::OrdinalRange...)
51-
inew = tuple_tuple_getindex(i, a.keepdim)
52-
DiskArrays.writeblock!(a.parent, reshape(v, map(length, inew)), inew...)
56+
function DiskArrays.writeblock!(a::AbstractReshapedDiskArray, v, i::OrdinalRange...)
57+
inew = tuple_tuple_getindex(i, keepdim(a))
58+
DiskArrays.writeblock!(parent(a), reshape(v, map(length, inew)), inew...)
5359
return nothing
5460
end
5561
function reshape_disk(parent, dims)
@@ -93,6 +99,6 @@ macro implement_reshape(t)
9399
end
94100

95101
# For ambiguity
96-
function Base._reshape(A::DiskArrays.AbstractDiskArray{<:Any,1}, dims::Tuple{Int64})
102+
function Base._reshape(A::AbstractDiskArray{<:Any,1}, dims::Tuple{Int64})
97103
return reshape_disk(A, dims)
98104
end

src/subarray.jl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1+
"""
2+
SubDiskArray <: AbstractDiskArray
3+
4+
Abstract supertype for a view of an AbstractDiskArray
5+
"""
6+
abstract type AbstractSubDiskArray{T,N,P,I,L} <: AbstractDiskArray{T,N} end
7+
18
"""
29
SubDiskArray <: AbstractDiskArray
310
411
A replacement for `Base.SubArray` for disk arrays, returned by `view`.
512
"""
6-
struct SubDiskArray{T,N,P,I,L} <: AbstractDiskArray{T,N}
13+
struct SubDiskArray{T,N,P,I,L} <: AbstractSubDiskArray{T,N,P,I,L}
714
v::SubArray{T,N,P,I,L}
815
end
916

1017
# Base methods
11-
Base.view(a::SubDiskArray, i...) = SubDiskArray(view(a.v, i...))
12-
Base.view(a::SubDiskArray, i::CartesianIndices) = view(a, i.indices...)
13-
Base.size(a::SubDiskArray) = size(a.v)
14-
Base.parent(a::SubDiskArray) = a.v.parent
18+
subarray(a::SubDiskArray) = a.v
19+
function Base.view(a::T, i...) where T<:AbstractSubDiskArray
20+
basetype = Base.typename(T).wrapper
21+
basetype(view(subarray(a), i...))
22+
end
23+
Base.view(a::AbstractSubDiskArray, i::CartesianIndices) = view(a, i.indices...)
24+
Base.size(a::AbstractSubDiskArray) = size(subarray(a))
25+
Base.parent(a::AbstractSubDiskArray) = parent(subarray(a))
26+
Base.parentindices(a::AbstractSubDiskArray) = parentindices(subarray(a))
1527

1628
_replace_colon(s, ::Colon) = Base.OneTo(s)
1729
_replace_colon(s, r) = r
1830

1931
# Diskarrays.jl interface
20-
function readblock!(a::SubDiskArray, aout, i::OrdinalRange...)
21-
pinds = parentindices(view(a.v, i...))
22-
getindex_disk!(aout, parent(a.v), pinds...)
32+
function readblock!(a::AbstractSubDiskArray, aout, i::OrdinalRange...)
33+
pinds = parentindices(view(a, i...))
34+
getindex_disk!(aout, parent(a), pinds...)
2335
end
24-
function writeblock!(a::SubDiskArray, v, i::OrdinalRange...)
25-
pinds = parentindices(view(a.v, i...))
26-
setindex_disk!(parent(a.v), v, pinds...)
36+
function writeblock!(a::AbstractSubDiskArray, v, i::OrdinalRange...)
37+
pinds = parentindices(view(a, i...))
38+
setindex_disk!(parent(a), v, pinds...)
2739
end
28-
haschunks(a::SubDiskArray) = haschunks(parent(a.v))
29-
eachchunk(a::SubDiskArray) = eachchunk_view(haschunks(a.v.parent), a.v)
40+
haschunks(a::AbstractSubDiskArray) = haschunks(parent(a))
41+
eachchunk(a::AbstractSubDiskArray) = eachchunk_view(haschunks(parent(a)), a)
3042

3143
function eachchunk_view(::Chunked, vv)
3244
pinds = parentindices(vv)

0 commit comments

Comments
 (0)