-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAlgebra.jl
1243 lines (1065 loc) · 37.3 KB
/
Algebra.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Vector allocation
function Algebra.allocate_vector(::Type{<:PVector{V}},ids::PRange) where {V}
PVector{V}(undef,partition(ids))
end
function Algebra.allocate_vector(::Type{<:BlockPVector{V}},ids::BlockPRange) where {V}
BlockPVector{V}(undef,ids)
end
function Algebra.allocate_in_range(matrix::PSparseMatrix)
V = Vector{eltype(matrix)}
allocate_in_range(PVector{V},matrix)
end
function Algebra.allocate_in_domain(matrix::PSparseMatrix)
V = Vector{eltype(matrix)}
allocate_in_domain(PVector{V},matrix)
end
function Algebra.allocate_in_range(matrix::BlockPMatrix)
V = Vector{eltype(matrix)}
allocate_in_range(BlockPVector{V},matrix)
end
function Algebra.allocate_in_domain(matrix::BlockPMatrix)
V = Vector{eltype(matrix)}
allocate_in_domain(BlockPVector{V},matrix)
end
# PSparseMatrix copy
function Base.copy(a::PSparseMatrix)
mats = map(copy,partition(a))
cache = map(PartitionedArrays.copy_cache,a.cache)
return PSparseMatrix(mats,partition(axes(a,1)),partition(axes(a,2)),cache)
end
# PartitionedArrays extras
function LinearAlgebra.axpy!(α,x::PVector,y::PVector)
@check partition(axes(x,1)) === partition(axes(y,1))
map(partition(x),partition(y)) do x,y
LinearAlgebra.axpy!(α,x,y)
end
consistent!(y) |> wait
return y
end
function LinearAlgebra.axpy!(α,x::BlockPVector,y::BlockPVector)
map(blocks(x),blocks(y)) do x,y
LinearAlgebra.axpy!(α,x,y)
end
return y
end
function Algebra.axpy_entries!(
α::Number, A::PSparseMatrix, B::PSparseMatrix;
check::Bool=true
)
# We should definitely check here that the index partitions are the same.
# However: Because the different matrices are assembled separately, the objects are not the
# same (i.e can't use ===). Checking the index partitions would then be costly...
@assert reduce(&,map(PartitionedArrays.matching_local_indices,partition(axes(A,1)),partition(axes(B,1))))
@assert reduce(&,map(PartitionedArrays.matching_local_indices,partition(axes(A,2)),partition(axes(B,2))))
map(partition(A),partition(B)) do A, B
Algebra.axpy_entries!(α,A,B;check)
end
return B
end
function Algebra.axpy_entries!(
α::Number, A::BlockPMatrix, B::BlockPMatrix;
check::Bool=true
)
map(blocks(A),blocks(B)) do A, B
Algebra.axpy_entries!(α,A,B;check)
end
return B
end
# This might go to Gridap in the future. We keep it here for the moment.
function change_axes(a::Algebra.ArrayCounter,axes)
@notimplemented
end
# This might go to Gridap in the future. We keep it here for the moment.
function change_axes(a::Algebra.CounterCOO{T,A}, axes::A) where {T,A}
b = Algebra.CounterCOO{T}(axes)
b.nnz = a.nnz
b
end
# This might go to Gridap in the future. We keep it here for the moment.
function change_axes(a::Algebra.AllocationCOO{T,A}, axes::A) where {T,A}
counter = change_axes(a.counter,axes)
Algebra.AllocationCOO(counter,a.I,a.J,a.V)
end
# Array of PArrays -> PArray of Arrays
function to_parray_of_arrays(a::AbstractArray{<:MPIArray})
indices = linear_indices(first(a))
map(indices) do i
map(a) do aj
PartitionedArrays.getany(aj)
end
end
end
function to_parray_of_arrays(a::AbstractArray{<:DebugArray})
indices = linear_indices(first(a))
map(indices) do i
map(a) do aj
aj.items[i]
end
end
end
# This type is required because MPIArray from PArrays
# cannot be instantiated with a NULL communicator
struct MPIVoidVector{T} <: AbstractVector{T}
comm::MPI.Comm
function MPIVoidVector(::Type{T}) where {T}
new{T}(MPI.COMM_NULL)
end
end
Base.size(a::MPIVoidVector) = (0,)
Base.IndexStyle(::Type{<:MPIVoidVector}) = IndexLinear()
function Base.getindex(a::MPIVoidVector,i::Int)
error("Indexing of MPIVoidVector not possible.")
end
function Base.setindex!(a::MPIVoidVector,v,i::Int)
error("Indexing of MPIVoidVector not possible.")
end
function Base.show(io::IO,k::MIME"text/plain",data::MPIVoidVector)
println(io,"MPIVoidVector")
end
function num_parts(comm::MPI.Comm)
if comm != MPI.COMM_NULL
nparts = MPI.Comm_size(comm)
else
nparts = -1
end
nparts
end
@inline num_parts(comm::MPIArray) = num_parts(comm.comm)
@inline num_parts(comm::DebugArray) = length(comm.items)
@inline num_parts(comm::MPIVoidVector) = num_parts(comm.comm)
function get_part_id(comm::MPI.Comm)
if comm != MPI.COMM_NULL
id = MPI.Comm_rank(comm)+1
else
id = -1
end
id
end
@inline get_part_id(comm::MPIArray) = get_part_id(comm.comm)
@inline get_part_id(comm::MPIVoidVector) = get_part_id(comm.comm)
"""
i_am_in(comm::MPIArray)
i_am_in(comm::DebugArray)
Returns `true` if the processor is part of the subcommunicator `comm`.
"""
function i_am_in(comm::MPI.Comm)
get_part_id(comm) >=0
end
@inline i_am_in(comm::MPIArray) = i_am_in(comm.comm)
@inline i_am_in(comm::MPIVoidVector) = i_am_in(comm.comm)
@inline i_am_in(comm::DebugArray) = true
function change_parts(x::Union{MPIArray,DebugArray,Nothing,MPIVoidVector}, new_parts; default=nothing)
x_new = map(new_parts) do p
if isa(x,MPIArray)
PartitionedArrays.getany(x)
elseif isa(x,DebugArray) && (p <= length(x.items))
x.items[p]
else
default
end
end
return x_new
end
function generate_subparts(parts::MPIArray,new_comm_size)
root_comm = parts.comm
root_size = MPI.Comm_size(root_comm)
rank = MPI.Comm_rank(root_comm)
@static if isdefined(MPI,:MPI_UNDEFINED)
mpi_undefined = MPI.MPI_UNDEFINED[]
else
mpi_undefined = MPI.API.MPI_UNDEFINED[]
end
if root_size == new_comm_size
return parts
else
if rank < new_comm_size
comm = MPI.Comm_split(root_comm,0,0)
return distribute_with_mpi(LinearIndices((new_comm_size,));comm=comm,duplicate_comm=false)
else
comm = MPI.Comm_split(root_comm,mpi_undefined,mpi_undefined)
return MPIVoidVector(eltype(parts))
end
end
end
function generate_subparts(parts::DebugArray,new_comm_size)
DebugArray(LinearIndices((new_comm_size,)))
end
# local_views
function local_views(a)
@abstractmethod
end
function get_parts(a)
return linear_indices(local_views(a))
end
function local_views(a::AbstractVector,rows)
@notimplemented
end
function local_views(a::AbstractMatrix,rows,cols)
@notimplemented
end
local_views(a::AbstractArray) = a
local_views(a::PRange) = partition(a)
local_views(a::PVector) = partition(a)
local_views(a::PSparseMatrix) = partition(a)
function local_views(a::BlockPRange)
map(blocks(a)) do a
local_views(a)
end |> to_parray_of_arrays
end
function local_views(a::BlockPArray)
vals = map(blocks(a)) do a
local_views(a)
end |> to_parray_of_arrays
return map(mortar,vals)
end
# change_ghost
function change_ghost(a::PVector{T},ids::PRange;is_consistent=false,make_consistent=false) where T
same_partition = (a.index_partition === partition(ids))
a_new = same_partition ? a : change_ghost(T,a,ids)
if make_consistent && (!same_partition || !is_consistent)
consistent!(a_new) |> wait
end
return a_new
end
function change_ghost(::Type{<:AbstractVector},a::PVector,ids::PRange)
a_new = similar(a,eltype(a),(ids,))
# Equivalent to copy!(a_new,a) but does not check that owned indices match
map(copy!,own_values(a_new),own_values(a))
return a_new
end
function change_ghost(::Type{<:OwnAndGhostVectors},a::PVector,ids::PRange)
values = map(own_values(a),partition(ids)) do own_vals,ids
ghost_vals = fill(zero(eltype(a)),ghost_length(ids))
perm = PartitionedArrays.local_permutation(ids)
OwnAndGhostVectors(own_vals,ghost_vals,perm)
end
return PVector(values,partition(ids))
end
function change_ghost(a::BlockPVector,ids::BlockPRange;is_consistent=false,make_consistent=false)
vals = map(blocks(a),blocks(ids)) do a, ids
change_ghost(a,ids;is_consistent=is_consistent,make_consistent=make_consistent)
end
return BlockPVector(vals,ids)
end
# This function computes a mapping among the local identifiers of a and b
# for which the corresponding global identifiers are both in a and b.
# Note that the haskey check is necessary because in the general case
# there might be gids in b which are not present in a
function find_local_to_local_map(a::AbstractLocalIndices,b::AbstractLocalIndices)
a_local_to_b_local = fill(Int32(-1),local_length(a))
b_local_to_global = local_to_global(b)
a_global_to_local = global_to_local(a)
for blid in 1:local_length(b)
gid = b_local_to_global[blid]
if a_global_to_local[gid] != zero(eltype(a_global_to_local))
alid = a_global_to_local[gid]
a_local_to_b_local[alid] = blid
end
end
a_local_to_b_local
end
# This type is required in order to be able to access the local portion
# of distributed sparse matrices and vectors using local indices from the
# distributed test and trial spaces
struct LocalView{T,N,A,B} <:AbstractArray{T,N}
plids_to_value::A
d_to_lid_to_plid::B
local_size::NTuple{N,Int}
function LocalView(
plids_to_value::AbstractArray{T,N},d_to_lid_to_plid::NTuple{N}) where {T,N}
A = typeof(plids_to_value)
B = typeof(d_to_lid_to_plid)
local_size = map(length,d_to_lid_to_plid)
new{T,N,A,B}(plids_to_value,d_to_lid_to_plid,local_size)
end
end
Base.size(a::LocalView) = a.local_size
Base.IndexStyle(::Type{<:LocalView}) = IndexCartesian()
function Base.getindex(a::LocalView{T,N},lids::Vararg{Integer,N}) where {T,N}
plids = map(_lid_to_plid,lids,a.d_to_lid_to_plid)
if all(i->i>0,plids)
a.plids_to_value[plids...]
else
zero(T)
end
end
function Base.setindex!(a::LocalView{T,N},v,lids::Vararg{Integer,N}) where {T,N}
plids = map(_lid_to_plid,lids,a.d_to_lid_to_plid)
@check all(i->i>0,plids) "You are trying to set a value that is not stored in the local portion"
a.plids_to_value[plids...] = v
end
function _lid_to_plid(lid,lid_to_plid)
plid = lid_to_plid[lid]
plid
end
function local_views(a::PVector,new_rows::PRange)
old_rows = axes(a,1)
if partition(old_rows) === partition(new_rows)
partition(a)
else
map(partition(a),partition(old_rows),partition(new_rows)) do vector_partition,old_rows,new_rows
LocalView(vector_partition,(find_local_to_local_map(new_rows,old_rows),))
end
end
end
function local_views(a::PSparseMatrix,new_rows::PRange,new_cols::PRange)
old_rows, old_cols = axes(a)
if (partition(old_rows) === partition(new_rows) && partition(old_cols) === partition(new_cols) )
partition(a)
else
map(partition(a),
partition(old_rows),partition(old_cols),
partition(new_rows),partition(new_cols)) do matrix_partition,old_rows,old_cols,new_rows,new_cols
rl2lmap = find_local_to_local_map(new_rows,old_rows)
cl2lmap = find_local_to_local_map(new_cols,old_cols)
LocalView(matrix_partition,(rl2lmap,cl2lmap))
end
end
end
function local_views(a::BlockPVector,new_rows::BlockPRange)
vals = map(local_views,blocks(a),blocks(new_rows)) |> to_parray_of_arrays
return map(mortar,vals)
end
function local_views(a::BlockPMatrix,new_rows::BlockPRange,new_cols::BlockPRange)
vals = map(CartesianIndices(blocksize(a))) do I
local_views(blocks(a)[I],blocks(new_rows)[I],blocks(new_cols)[I])
end |> to_parray_of_arrays
return map(mortar,vals)
end
# PSparseMatrix assembly
struct FullyAssembledRows end
struct SubAssembledRows end
# For the moment we use COO format even though
# it is quite memory consuming.
# We need to implement communication in other formats in
# PartitionedArrays.jl
struct PSparseMatrixBuilderCOO{T,B}
local_matrix_type::Type{T}
par_strategy::B
end
function Algebra.nz_counter(
builder::PSparseMatrixBuilderCOO{A}, axs::Tuple{<:PRange,<:PRange}) where A
test_dofs_gids_prange, trial_dofs_gids_prange = axs
counters = map(partition(test_dofs_gids_prange),partition(trial_dofs_gids_prange)) do r,c
axs = (Base.OneTo(local_length(r)),Base.OneTo(local_length(c)))
Algebra.CounterCOO{A}(axs)
end
DistributedCounterCOO(builder.par_strategy,counters,test_dofs_gids_prange,trial_dofs_gids_prange)
end
function Algebra.get_array_type(::PSparseMatrixBuilderCOO{Tv}) where Tv
T = eltype(Tv)
return PSparseMatrix{T}
end
"""
"""
struct DistributedCounterCOO{A,B,C,D} <: GridapType
par_strategy::A
counters::B
test_dofs_gids_prange::C
trial_dofs_gids_prange::D
function DistributedCounterCOO(
par_strategy,
counters::AbstractArray{<:Algebra.CounterCOO},
test_dofs_gids_prange::PRange,
trial_dofs_gids_prange::PRange)
A = typeof(par_strategy)
B = typeof(counters)
C = typeof(test_dofs_gids_prange)
D = typeof(trial_dofs_gids_prange)
new{A,B,C,D}(par_strategy,counters,test_dofs_gids_prange,trial_dofs_gids_prange)
end
end
function local_views(a::DistributedCounterCOO)
a.counters
end
function local_views(a::DistributedCounterCOO,test_dofs_gids_prange,trial_dofs_gids_prange)
@check test_dofs_gids_prange === a.test_dofs_gids_prange
@check trial_dofs_gids_prange === a.trial_dofs_gids_prange
a.counters
end
function Algebra.nz_allocation(a::DistributedCounterCOO)
allocs = map(nz_allocation,a.counters)
DistributedAllocationCOO(a.par_strategy,allocs,a.test_dofs_gids_prange,a.trial_dofs_gids_prange)
end
struct DistributedAllocationCOO{A,B,C,D} <: GridapType
par_strategy::A
allocs::B
test_dofs_gids_prange::C
trial_dofs_gids_prange::D
function DistributedAllocationCOO(
par_strategy,
allocs::AbstractArray{<:Algebra.AllocationCOO},
test_dofs_gids_prange::PRange,
trial_dofs_gids_prange::PRange)
A = typeof(par_strategy)
B = typeof(allocs)
C = typeof(test_dofs_gids_prange)
D = typeof(trial_dofs_gids_prange)
new{A,B,C,D}(par_strategy,allocs,test_dofs_gids_prange,trial_dofs_gids_prange)
end
end
function change_axes(a::DistributedAllocationCOO{A,B,<:PRange,<:PRange},
axes::Tuple{<:PRange,<:PRange}) where {A,B}
local_axes = map(partition(axes[1]),partition(axes[2])) do rows,cols
(Base.OneTo(local_length(rows)), Base.OneTo(local_length(cols)))
end
allocs = map(change_axes,a.allocs,local_axes)
DistributedAllocationCOO(a.par_strategy,allocs,axes[1],axes[2])
end
function change_axes(a::MatrixBlock{<:DistributedAllocationCOO},
axes::Tuple{<:Vector,<:Vector})
block_ids = CartesianIndices(a.array)
rows, cols = axes
array = map(block_ids) do I
change_axes(a[I],(rows[I[1]],cols[I[2]]))
end
return ArrayBlock(array,a.touched)
end
function local_views(a::DistributedAllocationCOO)
a.allocs
end
function local_views(a::DistributedAllocationCOO,test_dofs_gids_prange,trial_dofs_gids_prange)
@check test_dofs_gids_prange === a.test_dofs_gids_prange
@check trial_dofs_gids_prange === a.trial_dofs_gids_prange
a.allocs
end
function local_views(a::MatrixBlock{<:DistributedAllocationCOO})
array = map(local_views,a.array) |> to_parray_of_arrays
return map(ai -> ArrayBlock(ai,a.touched),array)
end
function get_allocations(a::DistributedAllocationCOO)
I,J,V = map(local_views(a)) do alloc
alloc.I, alloc.J, alloc.V
end |> tuple_of_arrays
return I,J,V
end
function get_allocations(a::ArrayBlock{<:DistributedAllocationCOO})
tuple_of_array_of_parrays = map(get_allocations,a.array) |> tuple_of_arrays
return tuple_of_array_of_parrays
end
get_test_gids(a::DistributedAllocationCOO) = a.test_dofs_gids_prange
get_trial_gids(a::DistributedAllocationCOO) = a.trial_dofs_gids_prange
get_test_gids(a::ArrayBlock{<:DistributedAllocationCOO}) = map(get_test_gids,diag(a.array))
get_trial_gids(a::ArrayBlock{<:DistributedAllocationCOO}) = map(get_trial_gids,diag(a.array))
function Algebra.create_from_nz(a::PSparseMatrix)
# For FullyAssembledRows the underlying Exchanger should
# not have ghost layer making assemble! do nothing (TODO check)
assemble!(a) |> wait
a
end
function Algebra.create_from_nz(a::DistributedAllocationCOO{<:FullyAssembledRows})
f(x) = nothing
A, = _fa_create_from_nz_with_callback(f,a)
return A
end
function Algebra.create_from_nz(a::ArrayBlock{<:DistributedAllocationCOO{<:FullyAssembledRows}})
f(x) = nothing
A, = _fa_create_from_nz_with_callback(f,a)
return A
end
function _fa_create_from_nz_with_callback(callback,a)
# Recover some data
I,J,V = get_allocations(a)
test_dofs_gids_prange = get_test_gids(a)
trial_dofs_gids_prange = get_trial_gids(a)
rows = _setup_prange(test_dofs_gids_prange,I;ghost=false,ax=:rows)
b = callback(rows)
# convert I and J to global dof ids
to_global_indices!(I,test_dofs_gids_prange;ax=:rows)
to_global_indices!(J,trial_dofs_gids_prange;ax=:cols)
# Create the range for cols
cols = _setup_prange(trial_dofs_gids_prange,J;ax=:cols)
# Convert again I,J to local numeration
to_local_indices!(I,rows;ax=:rows)
to_local_indices!(J,cols;ax=:cols)
# Adjust local matrix size to linear system's index sets
asys = change_axes(a,(rows,cols))
# Compress local portions
values = map(create_from_nz,local_views(asys))
# Finally build the matrix
A = _setup_matrix(values,rows,cols)
return A, b
end
function Algebra.create_from_nz(a::DistributedAllocationCOO{<:SubAssembledRows})
f(x) = nothing
A, = _sa_create_from_nz_with_callback(f,f,a,nothing)
return A
end
function Algebra.create_from_nz(a::ArrayBlock{<:DistributedAllocationCOO{<:SubAssembledRows}})
f(x) = nothing
A, = _sa_create_from_nz_with_callback(f,f,a,nothing)
return A
end
function _sa_create_from_nz_with_callback(callback,async_callback,a,b)
# Recover some data
I,J,V = get_allocations(a)
test_dofs_gids_prange = get_test_gids(a)
trial_dofs_gids_prange = get_trial_gids(a)
# convert I and J to global dof ids
to_global_indices!(I,test_dofs_gids_prange;ax=:rows)
to_global_indices!(J,trial_dofs_gids_prange;ax=:cols)
# Create the Prange for the rows
rows = _setup_prange(test_dofs_gids_prange,I;ax=:rows)
# Move (I,J,V) triplets to the owner process of each row I.
# The triplets are accompanyed which Jo which is the process column owner
Jo = get_gid_owners(J,trial_dofs_gids_prange;ax=:cols)
t = _assemble_coo!(I,J,V,rows;owners=Jo)
# Here we can overlap computations
# This is a good place to overlap since
# sending the matrix rows is a lot of data
if !isa(b,Nothing)
bprange=_setup_prange_from_pvector_allocation(b)
b = callback(bprange)
end
# Wait the transfer to finish
wait(t)
# Create the Prange for the cols
cols = _setup_prange(trial_dofs_gids_prange,J;ax=:cols,owners=Jo)
# Overlap rhs communications with CSC compression
t2 = async_callback(b)
# Convert again I,J to local numeration
to_local_indices!(I,rows;ax=:rows)
to_local_indices!(J,cols;ax=:cols)
# Adjust local matrix size to linear system's index sets
asys = change_axes(a,(rows,cols))
# Compress the local matrices
values = map(create_from_nz,local_views(asys))
# Wait the transfer to finish
if !isa(t2,Nothing)
wait(t2)
end
# Finally build the matrix
A = _setup_matrix(values,rows,cols)
return A, b
end
# PVector assembly
struct PVectorBuilder{T,B}
local_vector_type::Type{T}
par_strategy::B
end
function Algebra.nz_counter(builder::PVectorBuilder,axs::Tuple{<:PRange})
T = builder.local_vector_type
rows, = axs
counters = map(partition(rows)) do rows
axs = (Base.OneTo(local_length(rows)),)
nz_counter(ArrayBuilder(T),axs)
end
PVectorCounter(builder.par_strategy,counters,rows)
end
function Algebra.get_array_type(::PVectorBuilder{Tv}) where Tv
T = eltype(Tv)
return PVector{T}
end
struct PVectorCounter{A,B,C}
par_strategy::A
counters::B
test_dofs_gids_prange::C
end
Algebra.LoopStyle(::Type{<:PVectorCounter}) = DoNotLoop()
function local_views(a::PVectorCounter)
a.counters
end
function local_views(a::PVectorCounter,rows)
@check rows === a.test_dofs_gids_prange
a.counters
end
function Arrays.nz_allocation(a::PVectorCounter{<:FullyAssembledRows})
dofs = a.test_dofs_gids_prange
values = map(nz_allocation,a.counters)
PVectorAllocationTrackOnlyValues(a.par_strategy,values,dofs)
end
struct PVectorAllocationTrackOnlyValues{A,B,C}
par_strategy::A
values::B
test_dofs_gids_prange::C
end
function local_views(a::PVectorAllocationTrackOnlyValues)
a.values
end
function local_views(a::PVectorAllocationTrackOnlyValues,rows)
@check rows === a.test_dofs_gids_prange
a.values
end
function Algebra.create_from_nz(a::PVectorAllocationTrackOnlyValues{<:FullyAssembledRows})
rows = _setup_prange_without_ghosts(a.test_dofs_gids_prange)
_rhs_callback(a,rows)
end
function Algebra.create_from_nz(a::PVectorAllocationTrackOnlyValues{<:SubAssembledRows})
# This point MUST NEVER be reached. If reached there is an inconsistency
# in the parallel code in charge of vector assembly
@assert false
end
function _rhs_callback(row_partitioned_vector_partition,rows)
# The ghost values in row_partitioned_vector_partition are
# aligned with the FESpace but not with the ghost values in the rows of A
b_fespace = PVector(row_partitioned_vector_partition.values,
partition(row_partitioned_vector_partition.test_dofs_gids_prange))
# This one is aligned with the rows of A
b = similar(b_fespace,eltype(b_fespace),(rows,))
# First transfer owned values
b .= b_fespace
# Now transfer ghost
function transfer_ghost(b,b_fespace,ids,ids_fespace)
num_ghosts_vec = ghost_length(ids)
gho_to_loc_vec = ghost_to_local(ids)
loc_to_glo_vec = local_to_global(ids)
gid_to_lid_fe = global_to_local(ids_fespace)
for ghost_lid_vec in 1:num_ghosts_vec
lid_vec = gho_to_loc_vec[ghost_lid_vec]
gid = loc_to_glo_vec[lid_vec]
lid_fespace = gid_to_lid_fe[gid]
b[lid_vec] = b_fespace[lid_fespace]
end
end
map(
transfer_ghost,
partition(b),
partition(b_fespace),
b.index_partition,
b_fespace.index_partition)
return b
end
function Algebra.create_from_nz(a::PVector)
assemble!(a) |> wait
return a
end
function Algebra.create_from_nz(
a::DistributedAllocationCOO{<:FullyAssembledRows},
c_fespace::PVectorAllocationTrackOnlyValues{<:FullyAssembledRows})
function callback(rows)
_rhs_callback(c_fespace,rows)
end
A,b = _fa_create_from_nz_with_callback(callback,a)
return A,b
end
struct PVectorAllocationTrackTouchedAndValues{A,B,C}
allocations::A
values::B
test_dofs_gids_prange::C
end
function Algebra.create_from_nz(
a::DistributedAllocationCOO{<:SubAssembledRows},
b::PVectorAllocationTrackTouchedAndValues)
function callback(rows)
_rhs_callback(b,rows)
end
function async_callback(b)
# now we can assemble contributions
assemble!(b)
end
A,b = _sa_create_from_nz_with_callback(callback,async_callback,a,b)
return A,b
end
struct ArrayAllocationTrackTouchedAndValues{A}
touched::Vector{Bool}
values::A
end
Gridap.Algebra.LoopStyle(::Type{<:ArrayAllocationTrackTouchedAndValues}) = Gridap.Algebra.Loop()
function local_views(a::PVectorAllocationTrackTouchedAndValues,rows)
@check rows === a.test_dofs_gids_prange
a.allocations
end
@inline function Arrays.add_entry!(c::Function,a::ArrayAllocationTrackTouchedAndValues,v,i,j)
@notimplemented
end
@inline function Arrays.add_entry!(c::Function,a::ArrayAllocationTrackTouchedAndValues,v,i)
if i>0
if !(a.touched[i])
a.touched[i]=true
end
if !isa(v,Nothing)
a.values[i]=c(v,a.values[i])
end
end
nothing
end
@inline function Arrays.add_entries!(c::Function,a::ArrayAllocationTrackTouchedAndValues,v,i,j)
@notimplemented
end
@inline function Arrays.add_entries!(c::Function,a::ArrayAllocationTrackTouchedAndValues,v,i)
if !isa(v,Nothing)
for (ve,ie) in zip(v,i)
Arrays.add_entry!(c,a,ve,ie)
end
else
for ie in i
Arrays.add_entry!(c,a,nothing,ie)
end
end
nothing
end
function _setup_touched_and_allocations_arrays(values)
touched = map(values) do values
fill!(Vector{Bool}(undef,length(values)),false)
end
allocations = map(values,touched) do values,touched
ArrayAllocationTrackTouchedAndValues(touched,values)
end
touched, allocations
end
function Arrays.nz_allocation(a::DistributedCounterCOO{<:SubAssembledRows},
b::PVectorCounter{<:SubAssembledRows})
A = nz_allocation(a)
dofs = b.test_dofs_gids_prange
values = map(nz_allocation,b.counters)
touched,allocations=_setup_touched_and_allocations_arrays(values)
B = PVectorAllocationTrackTouchedAndValues(allocations,values,dofs)
return A,B
end
function Arrays.nz_allocation(a::PVectorCounter{<:SubAssembledRows})
dofs = a.test_dofs_gids_prange
values = map(nz_allocation,a.counters)
touched,allocations=_setup_touched_and_allocations_arrays(values)
return PVectorAllocationTrackTouchedAndValues(allocations,values,dofs)
end
function local_views(a::PVectorAllocationTrackTouchedAndValues)
a.allocations
end
function _setup_prange_from_pvector_allocation(a::PVectorAllocationTrackTouchedAndValues)
# Find the ghost rows
allocations = local_views(a.allocations)
indices = partition(a.test_dofs_gids_prange)
I_ghost_lids_to_dofs_ghost_lids = map(allocations, indices) do allocation, indices
dofs_lids_touched = findall(allocation.touched)
loc_to_gho = local_to_ghost(indices)
n_I_ghost_lids = count((x)->loc_to_gho[x]!=0,dofs_lids_touched)
I_ghost_lids = Vector{Int32}(undef,n_I_ghost_lids)
cur = 1
for lid in dofs_lids_touched
dof_lid = loc_to_gho[lid]
if dof_lid != 0
I_ghost_lids[cur] = dof_lid
cur = cur+1
end
end
I_ghost_lids
end
ghost_to_global, ghost_to_owner = map(
find_gid_and_owner,I_ghost_lids_to_dofs_ghost_lids,indices) |> tuple_of_arrays
ngids = length(a.test_dofs_gids_prange)
_setup_prange_impl_(ngids,indices,ghost_to_global,ghost_to_owner)
end
function Algebra.create_from_nz(a::PVectorAllocationTrackTouchedAndValues)
rows = _setup_prange_from_pvector_allocation(a)
b = _rhs_callback(a,rows)
t2 = assemble!(b)
# Wait the transfer to finish
if t2 !== nothing
wait(t2)
end
return b
end
# Common Assembly Utilities
function first_gdof_from_ids(ids)
if own_length(ids) == 0
return 1
end
lid_to_gid = local_to_global(ids)
owned_to_lid = own_to_local(ids)
return Int(lid_to_gid[first(owned_to_lid)])
end
function find_gid_and_owner(ighost_to_jghost,jindices)
jghost_to_local = ghost_to_local(jindices)
jlocal_to_global = local_to_global(jindices)
jlocal_to_owner = local_to_owner(jindices)
ighost_to_jlocal = sort(view(jghost_to_local,ighost_to_jghost))
ighost_to_global = jlocal_to_global[ighost_to_jlocal]
ighost_to_owner = jlocal_to_owner[ighost_to_jlocal]
return ighost_to_global, ighost_to_owner
end
# The given ids are assumed to be a sub-set of the lids
function ghost_lids_touched(a::AbstractLocalIndices,gids::AbstractVector{<:Integer})
glo_to_loc = global_to_local(a)
loc_to_gho = local_to_ghost(a)
# First pass: Allocate
i = 0
ghost_lids_touched = fill(false,ghost_length(a))
for gid in gids
lid = glo_to_loc[gid]
ghost_lid = loc_to_gho[lid]
if ghost_lid > 0 && !ghost_lids_touched[ghost_lid]
ghost_lids_touched[ghost_lid] = true
i += 1
end
end
gids_ghost_lid_to_ghost_lid = Vector{Int32}(undef,i)
# Second pass: fill
i = 1
fill!(ghost_lids_touched,false)
for gid in gids
lid = glo_to_loc[gid]
ghost_lid = loc_to_gho[lid]
if ghost_lid > 0 && !ghost_lids_touched[ghost_lid]
ghost_lids_touched[ghost_lid] = true
gids_ghost_lid_to_ghost_lid[i] = ghost_lid
i += 1
end
end
return gids_ghost_lid_to_ghost_lid
end
# Find the neighbours of partition1 trying
# to use those in partition2 as a hint
function _find_neighbours!(partition1, partition2)
partition2_snd, partition2_rcv = assembly_neighbors(partition2)
partition2_graph = ExchangeGraph(partition2_snd, partition2_rcv)
return assembly_neighbors(partition1; neighbors=partition2_graph)
end
# to_global! & to_local! analogs, needed for dispatching in block assembly
function to_local_indices!(I,ids::PRange;kwargs...)
map(to_local!,I,partition(ids))
end
function to_global_indices!(I,ids::PRange;kwargs...)
map(to_global!,I,partition(ids))
end
function get_gid_owners(I,ids::PRange;kwargs...)
map(I,partition(ids)) do I, indices
glo_to_loc = global_to_local(indices)
loc_to_own = local_to_owner(indices)
map(x->loc_to_own[glo_to_loc[x]], I)
end
end
for f in [:to_local_indices!, :to_global_indices!, :get_gid_owners]
@eval begin
function $f(I::Vector,ids::AbstractVector{<:PRange};kwargs...)
map($f,I,ids)
end
function $f(I::Matrix,ids::AbstractVector{<:PRange};ax=:rows)
@check ax ∈ [:rows,:cols]
block_ids = CartesianIndices(I)
map(block_ids) do id
i = id[1]; j = id[2];
if ax == :rows
$f(I[i,j],ids[i])
else
$f(I[i,j],ids[j])
end
end
end
end
end
# _setup_matrix : local matrices + global PRanges -> Global matrix
function _setup_matrix(values,rows::PRange,cols::PRange)
return PSparseMatrix(values,partition(rows),partition(cols))