-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBlockPartitionedArrays.jl
424 lines (348 loc) · 11.6 KB
/
BlockPartitionedArrays.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
struct BlockPRange{A} <: AbstractUnitRange{Int}
"""
struct BlockPRange{A} <: AbstractUnitRange{Int}
ranges::Vector{PRange{A}}
function BlockPRange(ranges::Vector{<:PRange{A}}) where A
new{A}(ranges)
end
end
Base.first(a::BlockPRange) = 1
Base.last(a::BlockPRange) = sum(map(last,a.ranges))
BlockArrays.blocklength(a::BlockPRange) = length(a.ranges)
BlockArrays.blocksize(a::BlockPRange) = (blocklength(a),)
BlockArrays.blockaxes(a::BlockPRange) = (Block.(Base.OneTo(blocklength(a))),)
BlockArrays.blocks(a::BlockPRange) = a.ranges
function PartitionedArrays.partition(a::BlockPRange)
return map(partition,blocks(a)) |> to_parray_of_arrays
end
function Base.getindex(a::BlockPRange,inds::Block{1})
a.ranges[inds.n...]
end
function PartitionedArrays.matching_local_indices(a::BlockPRange,b::BlockPRange)
c = map(PartitionedArrays.matching_local_indices,blocks(a),blocks(b))
reduce(&,c,init=true)
end
function PartitionedArrays.matching_own_indices(a::BlockPRange,b::BlockPRange)
c = map(PartitionedArrays.matching_own_indices,blocks(a),blocks(b))
reduce(&,c,init=true)
end
function PartitionedArrays.matching_ghost_indices(a::BlockPRange,b::BlockPRange)
c = map(PartitionedArrays.matching_ghost_indices,blocks(a),blocks(b))
reduce(&,c,init=true)
end
"""
struct BlockPArray{V,T,N,A,B} <: BlockArrays.AbstractBlockArray{T,N}
"""
struct BlockPArray{V,T,N,A,B} <: BlockArrays.AbstractBlockArray{T,N}
blocks::Array{A,N}
axes::NTuple{N,B}
function BlockPArray(blocks::Array{<:AbstractArray{T,N},N},
axes ::NTuple{N,<:BlockPRange}) where {T,N}
@check all(map(d->size(blocks,d)==blocklength(axes[d]),1:N))
local_type(::Type{<:PVector{V}}) where V = V
local_type(::Type{<:PSparseMatrix{V}}) where V = V
A = eltype(blocks)
B = typeof(first(axes))
V = local_type(A)
new{V,T,N,A,B}(blocks,axes)
end
end
const BlockPVector{V,T,A,B} = BlockPArray{V,T,1,A,B}
const BlockPMatrix{V,T,A,B} = BlockPArray{V,T,2,A,B}
@inline function BlockPVector(blocks::Vector{<:PVector},rows::BlockPRange)
BlockPArray(blocks,(rows,))
end
@inline function BlockPVector(blocks::Vector{<:PVector},rows::Vector{<:PRange})
BlockPVector(blocks,BlockPRange(rows))
end
@inline function BlockPMatrix(blocks::Matrix{<:PSparseMatrix},rows::BlockPRange,cols::BlockPRange)
BlockPArray(blocks,(rows,cols))
end
@inline function BlockPMatrix(blocks::Matrix{<:PSparseMatrix},rows::Vector{<:PRange},cols::Vector{<:PRange})
BlockPMatrix(blocks,BlockPRange(rows),BlockPRange(cols))
end
function BlockPVector{V}(::UndefInitializer,rows::BlockPRange) where {V}
vals = map(blocks(rows)) do r
PVector{V}(undef,partition(r))
end
return BlockPVector(vals,rows)
end
function BlockPMatrix{V}(::UndefInitializer,rows::BlockPRange,cols::BlockPRange) where {V}
block_ids = CartesianIndices((blocklength(rows),blocklength(cols)))
block_rows = blocks(rows)
block_cols = blocks(cols)
vals = map(block_ids) do I
r = block_rows[I[1]]
c = block_cols[I[2]]
PSparseMatrix{V}(undef,partition(r),partition(c))
end
return BlockPMatrix(vals,rows)
end
# AbstractArray API
Base.axes(a::BlockPArray) = a.axes
Base.size(a::BlockPArray) = Tuple(map(length,a.axes))
Base.IndexStyle(::Type{<:BlockPVector}) = IndexLinear()
Base.IndexStyle(::Type{<:BlockPMatrix}) = IndexCartesian()
function Base.similar(a::BlockPVector,::Type{T},inds::Tuple{<:BlockPRange}) where T
vals = map(blocks(a),blocks(inds[1])) do ai,i
similar(ai,T,i)
end
return BlockPArray(vals,inds)
end
function Base.similar(::Type{<:BlockPVector{V,T,A}},inds::Tuple{<:BlockPRange}) where {V,T,A}
rows = blocks(inds[1])
values = map(rows) do r
return similar(A,(r,))
end
return BlockPArray(values,inds)
end
function Base.similar(a::BlockPMatrix,::Type{T},inds::Tuple{<:BlockPRange,<:BlockPRange}) where T
vals = map(CartesianIndices(blocksize(a))) do I
rows = inds[1].ranges[I[1]]
cols = inds[2].ranges[I[2]]
similar(a.blocks[I],T,(rows,cols))
end
return BlockPArray(vals,inds)
end
function Base.similar(::Type{<:BlockPMatrix{V,T,A}},inds::Tuple{<:BlockPRange,<:BlockPRange}) where {V,T,A}
rows = blocks(inds[1])
cols = blocks(inds[2])
values = map(CartesianIndices((length(rows),length(cols)))) do I
i,j = I[1],I[2]
return similar(A,(rows[i],cols[j]))
end
return BlockPArray(values,inds)
end
function Base.getindex(a::BlockPArray{T,N},inds::Vararg{Int,N}) where {T,N}
@error "Scalar indexing not supported"
end
function Base.setindex(a::BlockPArray{T,N},v,inds::Vararg{Int,N}) where {T,N}
@error "Scalar indexing not supported"
end
function Base.show(io::IO,k::MIME"text/plain",data::BlockPArray{T,N}) where {T,N}
v = first(blocks(data))
s = prod(map(si->"$(si)x",blocksize(data)))[1:end-1]
map_main(partition(v)) do values
println(io,"$s-block BlockPArray{$T,$N}")
end
end
function Base.zero(v::BlockPArray)
return mortar(map(zero,blocks(v)))
end
function Base.copyto!(y::BlockPVector,x::BlockPVector)
@check blocklength(x) == blocklength(y)
yb, xb = blocks(y), blocks(x)
for i in 1:blocksize(x,1)
copyto!(yb[i],xb[i])
end
return y
end
function Base.copyto!(y::BlockPMatrix,x::BlockPMatrix)
@check blocksize(x) == blocksize(y)
yb, xb = blocks(y), blocks(x)
for i in 1:blocksize(x,1)
for j in 1:blocksize(x,2)
copyto!(yb[i,j],xb[i,j])
end
end
return y
end
function Base.fill!(a::BlockPVector,v)
map(blocks(a)) do a
fill!(a,v)
end
return a
end
function Base.sum(a::BlockPArray)
# TODO: This could use a single communication, instead of one for each block
# TODO: We could implement a generic reduce, that we apply to sum, all, any, etc..
return sum(map(sum,blocks(a)))
end
Base.maximum(x::BlockPArray) = maximum(identity,x)
function Base.maximum(f::Function,x::BlockPArray)
maximum(map(xi->maximum(f,xi),blocks(x)))
end
Base.minimum(x::BlockPArray) = minimum(identity,x)
function Base.minimum(f::Function,x::BlockPArray)
minimum(map(xi->minimum(f,xi),blocks(x)))
end
function Base.:(==)(a::BlockPVector,b::BlockPVector)
A = length(a) == length(b)
B = all(map((ai,bi)->ai==bi,blocks(a),blocks(b)))
return A && B
end
function Base.any(f::Function,x::BlockPVector)
any(map(xi->any(f,xi),blocks(x)))
end
function Base.all(f::Function,x::BlockPVector)
all(map(xi->all(f,xi),blocks(x)))
end
function LinearAlgebra.rmul!(a::BlockPVector,v::Number)
map(ai->rmul!(ai,v),blocks(a))
return a
end
# AbstractBlockArray API
BlockArrays.blocks(a::BlockPArray) = a.blocks
function Base.getindex(a::BlockPArray,inds::Block{1})
a.blocks[inds.n...]
end
function Base.getindex(a::BlockPArray{V,T,N},inds::Block{N}) where {V,T,N}
a.blocks[inds.n...]
end
function Base.getindex(a::BlockPArray{V,T,N},inds::Vararg{Block{1},N}) where {V,T,N}
a.blocks[map(i->i.n[1],inds)...]
end
function BlockArrays.mortar(blocks::Vector{<:PVector})
rows = map(b->axes(b,1),blocks)
BlockPVector(blocks,rows)
end
function BlockArrays.mortar(blocks::Matrix{<:PSparseMatrix})
rows = map(b->axes(b,1),blocks[:,1])
cols = map(b->axes(b,2),blocks[1,:])
function check_axes(a,r,c)
A = PartitionedArrays.matching_local_indices(axes(a,1),r)
B = PartitionedArrays.matching_local_indices(axes(a,2),c)
return A & B
end
@check all(map(I -> check_axes(blocks[I],rows[I[1]],cols[I[2]]),CartesianIndices(size(blocks))))
return BlockPMatrix(blocks,rows,cols)
end
# PartitionedArrays API
Base.wait(t::Array) = map(wait,t)
Base.fetch(t::Array) = map(fetch,t)
function PartitionedArrays.assemble!(a::BlockPArray)
map(assemble!,blocks(a))
end
function PartitionedArrays.consistent!(a::BlockPArray)
map(consistent!,blocks(a))
end
function PartitionedArrays.partition(a::BlockPArray)
vals = map(partition,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
function PartitionedArrays.to_trivial_partition(a::BlockPArray)
vals = map(PartitionedArrays.to_trivial_partition,blocks(a))
return mortar(vals)
end
function PartitionedArrays.local_values(a::BlockPArray)
vals = map(local_values,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
function PartitionedArrays.own_values(a::BlockPArray)
vals = map(own_values,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
function PartitionedArrays.ghost_values(a::BlockPArray)
vals = map(ghost_values,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
function PartitionedArrays.own_ghost_values(a::BlockPMatrix)
vals = map(own_ghost_values,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
function PartitionedArrays.ghost_own_values(a::BlockPMatrix)
vals = map(ghost_own_values,blocks(a)) |> to_parray_of_arrays
return map(mortar,vals)
end
# LinearAlgebra API
function Base.:*(a::Number,b::BlockPArray)
mortar(map(bi -> a*bi,blocks(b)))
end
Base.:*(b::BlockPMatrix,a::Number) = a*b
Base.:/(b::BlockPVector,a::Number) = (1/a)*b
function Base.:*(a::BlockPMatrix,b::BlockPVector)
c = similar(b)
mul!(c,a,b)
return c
end
for op in (:+,:-)
@eval begin
function Base.$op(a::BlockPArray)
mortar(map($op,blocks(a)))
end
function Base.$op(a::BlockPArray,b::BlockPArray)
@assert blocksize(a) == blocksize(b)
mortar(map($op,blocks(a),blocks(b)))
end
end
end
function LinearAlgebra.mul!(y::BlockPVector,A::BlockPMatrix,x::BlockPVector)
o = one(eltype(A))
mul!(y,A,x,o,o)
end
function LinearAlgebra.mul!(y::BlockPVector,A::BlockPMatrix,x::BlockPVector,α::Number,β::Number)
yb, Ab, xb = blocks(y), blocks(A), blocks(x)
z = zero(eltype(y))
o = one(eltype(A))
for i in 1:blocksize(A,1)
fill!(yb[i],z)
for j in 1:blocksize(A,2)
mul!(yb[i],Ab[i,j],xb[j],α,o)
end
rmul!(yb[i],β)
end
return y
end
function LinearAlgebra.dot(x::BlockPVector,y::BlockPVector)
return sum(map(dot,blocks(x),blocks(y)))
end
function LinearAlgebra.norm(v::BlockPVector,p::Real=2)
if p == 2
# More accurate, I think, given the fact we are not
# repeating the sqrt(square(sqrt...)) process in every block and every processor
return sqrt(dot(v,v))
end
block_norms = map(vi->norm(vi,p),blocks(v))
return sum(block_norms.^p)^(1/p)
end
function LinearAlgebra.fillstored!(a::BlockPMatrix,v)
map(blocks(a)) do a
LinearAlgebra.fillstored!(a,v)
end
return a
end
# Broadcasting
struct BlockPBroadcasted{A,B}
blocks :: A
axes :: B
end
BlockArrays.blocks(b::BlockPBroadcasted) = b.blocks
BlockArrays.blockaxes(b::BlockPBroadcasted) = b.axes
function Base.broadcasted(f, args::Union{BlockPArray,BlockPBroadcasted}...)
a1 = first(args)
@boundscheck @assert all(ai -> blockaxes(ai) == blockaxes(a1),args)
blocks_in = map(blocks,args)
blocks_out = map((largs...)->Base.broadcasted(f,largs...),blocks_in...)
return BlockPBroadcasted(blocks_out,blockaxes(a1))
end
function Base.broadcasted(f, a::Number, b::Union{BlockPArray,BlockPBroadcasted})
blocks_out = map(b->Base.broadcasted(f,a,b),blocks(b))
return BlockPBroadcasted(blocks_out,blockaxes(b))
end
function Base.broadcasted(f, a::Union{BlockPArray,BlockPBroadcasted}, b::Number)
blocks_out = map(a->Base.broadcasted(f,a,b),blocks(a))
return BlockPBroadcasted(blocks_out,blockaxes(a))
end
function Base.broadcasted(
f,
a::Union{BlockPArray,BlockPBroadcasted},
b::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}}
)
Base.broadcasted(f,a,Base.materialize(b))
end
function Base.broadcasted(
f,
a::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}},
b::Union{BlockPArray,BlockPBroadcasted}
)
Base.broadcasted(f,Base.materialize(a),b)
end
function Base.materialize(b::BlockPBroadcasted)
blocks_out = map(Base.materialize,blocks(b))
return mortar(blocks_out)
end
function Base.materialize!(a::BlockPArray,b::BlockPBroadcasted)
map(Base.materialize!,blocks(a),blocks(b))
return a
end