@@ -9,15 +9,20 @@ import Serialization: serialize, deserialize
99
1010An `N`-dimensional domain over an array.
1111"""
12- struct ArrayDomain{N}
13- indexes:: NTuple{N, Any}
12+ struct ArrayDomain{N,T <: Tuple }
13+ indexes:: T
1414end
15- include (" ../lib/domain-blocks.jl" )
16-
1715
18- ArrayDomain (xs... ) = ArrayDomain (xs)
16+ ArrayDomain (xs:: T ) where T<: Tuple = ArrayDomain {length(xs),T} (xs)
17+ ArrayDomain (xs:: NTuple{N,Base.OneTo} ) where N =
18+ ArrayDomain {N,NTuple{N,UnitRange{Int}}} (ntuple (i-> UnitRange (xs[i]), N))
19+ ArrayDomain (xs:: NTuple{N,Int} ) where N =
20+ ArrayDomain {N,NTuple{N,UnitRange{Int}}} (ntuple (i-> xs[i]: xs[i], N))
21+ ArrayDomain (xs... ) = ArrayDomain ((xs... ,))
1922ArrayDomain (xs:: Array ) = ArrayDomain ((xs... ,))
2023
24+ include (" ../lib/domain-blocks.jl" )
25+
2126indexes (a:: ArrayDomain ) = a. indexes
2227chunks (a:: ArrayDomain{N} ) where {N} = DomainBlocks (
2328 ntuple (i-> first (indexes (a)[i]), Val (N)), map (x-> [length (x)], indexes (a)))
@@ -117,6 +122,7 @@ indicates the number of dimensions in the resulting array.
117122"""
118123Blocks (xs:: Int... ) = Blocks (xs)
119124
125+ const DArrayDomain{N} = ArrayDomain{N, NTuple{N, UnitRange{Int}}}
120126
121127"""
122128 DArray{T,N,F}(domain, subdomains, chunks, concat)
@@ -133,8 +139,8 @@ An N-dimensional distributed array of element type T, with a concatenation funct
133139 and concatenates them along dimension `d`. `cat` is used by default.
134140"""
135141mutable struct DArray{T,N,B<: AbstractBlocks{N} ,F} <: ArrayOp{T, N}
136- domain:: ArrayDomain {N}
137- subdomains:: AbstractArray{ArrayDomain {N}, N}
142+ domain:: DArrayDomain {N}
143+ subdomains:: AbstractArray{DArrayDomain {N}, N}
138144 chunks:: AbstractArray{Any, N}
139145 partitioning:: B
140146 concat:: F
@@ -143,20 +149,27 @@ mutable struct DArray{T,N,B<:AbstractBlocks{N},F} <: ArrayOp{T, N}
143149 end
144150end
145151
152+ WrappedDArray{T,N} = Union{<: DArray{T,N} , Transpose{<: DArray{T,N} }, Adjoint{<: DArray{T,N} }}
153+ WrappedDMatrix{T} = WrappedDArray{T,2 }
154+ WrappedDVector{T} = WrappedDArray{T,1 }
155+ DMatrix{T} = DArray{T,2 }
156+ DVector{T} = DArray{T,1 }
157+
158+
146159# mainly for backwards-compatibility
147160DArray {T, N} (domain, subdomains, chunks, partitioning, concat= cat) where {T,N} =
148161 DArray (T, domain, subdomains, chunks, partitioning, concat)
149162
150- function DArray (T, domain:: ArrayDomain {N} ,
151- subdomains:: AbstractArray{ArrayDomain {N}, N} ,
152- chunks:: AbstractArray{<:Any, N} , partitioning:: B , concat= cat) where {N,B<: AbstractMultiBlocks {N} }
163+ function DArray (T, domain:: DArrayDomain {N} ,
164+ subdomains:: AbstractArray{DArrayDomain {N}, N} ,
165+ chunks:: AbstractArray{<:Any, N} , partitioning:: B , concat= cat) where {N,B<: AbstractBlocks {N} }
153166 DArray {T,N,B,typeof(concat)} (domain, subdomains, chunks, partitioning, concat)
154167end
155168
156- function DArray (T, domain:: ArrayDomain {N} ,
157- subdomains:: ArrayDomain {N} ,
169+ function DArray (T, domain:: DArrayDomain {N} ,
170+ subdomains:: DArrayDomain {N} ,
158171 chunks:: Any , partitioning:: B , concat= cat) where {N,B<: AbstractSingleBlocks{N} }
159- _subdomains = Array {ArrayDomain {N}, N} (undef, ntuple (i-> 1 , N)... )
172+ _subdomains = Array {DArrayDomain {N}, N} (undef, ntuple (i-> 1 , N)... )
160173 _subdomains[1 ] = subdomains
161174 _chunks = Array {Any, N} (undef, ntuple (i-> 1 , N)... )
162175 _chunks[1 ] = chunks
@@ -201,6 +214,13 @@ function Base.similar(x::DArray{T,N}) where {T,N}
201214 return DArray (T, x. domain, x. subdomains, thunks, x. partitioning, x. concat)
202215end
203216
217+ function Base. similar (A:: DArray{T,N} where T, :: Type{S} , dims:: Dims{N} ) where {S,N}
218+ d = ArrayDomain (map (x-> 1 : x, dims))
219+ p = A. partitioning
220+ a = AllocateArray (S, (_, _, x... ) -> Array {S,N} (undef, x... ), d, partition (p, d), p)
221+ return _to_darray (a)
222+ end
223+
204224Base. copy (x:: DArray{T,N,B,F} ) where {T,N,B,F} =
205225 map (identity, x):: DArray{T,N,B,F}
206226
@@ -214,7 +234,7 @@ Base.:(/)(x::DArray{T,N,B,F}, y::U) where {T<:Real,U<:Real,N,B,F} =
214234A `view` of a `DArray` chunk returns a `DArray` of `Thunk`s.
215235"""
216236function Base. view (c:: DArray , d)
217- subchunks, subdomains = lookup_parts (chunks (c), domainchunks (c), d)
237+ subchunks, subdomains = lookup_parts (c, chunks (c), domainchunks (c), d)
218238 d1 = alignfirst (d)
219239 DArray (eltype (c), d1, subdomains, subchunks, c. partitioning, c. concat)
220240end
@@ -252,7 +272,7 @@ function group_indices(cumlength, idxs::AbstractRange)
252272end
253273
254274_cumsum (x:: AbstractArray ) = length (x) == 0 ? Int[] : cumsum (x)
255- function lookup_parts (ps:: AbstractArray , subdmns:: DomainBlocks{N} , d:: ArrayDomain{N} ) where N
275+ function lookup_parts (A :: DArray , ps:: AbstractArray , subdmns:: DomainBlocks{N} , d:: ArrayDomain{N} ) where N
256276 groups = map (group_indices, subdmns. cumlength, indexes (d))
257277 sz = map (length, groups)
258278 pieces = Array {Any} (undef, sz)
@@ -264,7 +284,15 @@ function lookup_parts(ps::AbstractArray, subdmns::DomainBlocks{N}, d::ArrayDomai
264284 end
265285 out_cumlength = map (g-> _cumsum (map (x-> length (x[2 ]), g)), groups)
266286 out_dmn = DomainBlocks (ntuple (x-> 1 ,Val (N)), out_cumlength)
267- pieces, out_dmn
287+ return pieces, out_dmn
288+ end
289+ function lookup_parts (A:: DArray , ps:: AbstractArray , subdmns:: DomainBlocks{N} , d:: ArrayDomain{S} ) where {N,S}
290+ if S != 1
291+ throw (BoundsError (A, d. indexes))
292+ end
293+ inds = CartesianIndices (A)[d. indexes... ]
294+ new_d = ntuple (i-> first (inds). I[i]: last (inds). I[i], N)
295+ return lookup_parts (A, ps, subdmns, ArrayDomain (new_d))
268296end
269297
270298"""
0 commit comments