-
Notifications
You must be signed in to change notification settings - Fork 12
/
p_range.jl
1661 lines (1405 loc) · 47.5 KB
/
p_range.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
"""
abstract type AbstractLocalIndices
Abstract type representing the *local*, *own*, and *ghost* indices in
a part of a partition of a range `1:n` with length `n`.
# Notation
Let `1:n` be an integer range with length `n`. We denote the indices in `1:n` as the
*global* indices. Let us consider a partition of `1:n`. The indices in a part
in the partition are called the *own* indices of this part.
I.e., each part *owns* a subset of `1:n`. All these subsets are disjoint.
Let us assume that each part is equipped with a second set of indices called the *ghost* indices.
The set of ghost indices in a given part is an arbitrary subset
of the global indices `1:n` that are owned by other parts. The union of the own and ghost
indices is referred to as the *local* indices of this part. The sets of local indices might overlap
between the different parts.
The sets of own, ghost, and local indices are stored using vector-like containers
in concrete implementations of `AbstractLocalIndices`. This
equips them with a certain order. The `i`-th own index
in a part is defined as the one being stored at index `i` in the array that contains
the own indices in this part (idem for ghost and local indices).
The map between indices in these ordered index sets are given by functions such as [`local_to_global`](@ref),
[`own_to_local`](@ref) etc.
# Supertype hierarchy
AbstractLocalIndices <: AbstractVector{Int}
"""
abstract type AbstractLocalIndices <: AbstractVector{Int} end
Base.size(a::AbstractLocalIndices) = (local_length(a),)
Base.IndexStyle(::Type{<:AbstractLocalIndices}) = IndexLinear()
@inline Base.getindex(a::AbstractLocalIndices,i::Int) = local_to_global(a)[i]
"""
local_length(indices)
Get number of local ids in `indices`.
"""
local_length(a) = own_length(a) + ghost_length(a)
"""
own_length(indices)
Get number of own ids in `indices`.
"""
own_length(a) = length(own_to_owner(a))
"""
ghost_length(indices)
Get number of ghost ids in `indices`.
"""
ghost_length(a) = length(ghost_to_global(a))
"""
global_length(indices)
Get number of global ids associated with `indices`.
"""
global_length(a) = length(global_to_own(a))
"""
part_id(indices)
Return the id of the part that is storing `indices`.
"""
function part_id end
"""
local_to_global(indices)
Return an array with the global indices of the local indices in `indices`.
"""
function local_to_global end
"""
own_to_global(indices)
Return an array with the global indices of the own indices in `indices`.
"""
function own_to_global end
"""
ghost_to_global(indices)
Return an array with the global indices of the ghost indices in `indices`.
"""
function ghost_to_global end
"""
local_to_owner(indices)
Return an array with the owners of the local indices in `indices`.
"""
function local_to_owner end
"""
own_to_owner(indices)
Return an array with the owners of the own indices in `indices`.
"""
function own_to_owner end
"""
ghost_to_owner(indices)
Return an array with the owners of the ghost indices in `indices`.
"""
function ghost_to_owner end
"""
global_to_local(indices)
Return an array with the inverse index map of `local_to_global(indices)`.
"""
function global_to_local end
"""
global_to_own(indices)
Return an array with the inverse index map of `own_to_global(indices)`.
"""
function global_to_own end
"""
global_to_ghost(indices)
Return an array with the inverse index map of `ghost_to_global(indices)`.
"""
function global_to_ghost end
"""
own_to_local(indices)
Return an array with the local ids of the own indices in `indices`.
"""
function own_to_local end
"""
ghost_to_local(indices)
Return an array with the local ids of the ghost indices in `indices`.
"""
function ghost_to_local end
"""
local_to_own(indices)
Return an array with the inverse index map of `own_to_local(indices)`.
"""
function local_to_own end
"""
local_to_ghost(indices)
Return an array with the inverse index map of `ghost_to_local(indices)`.
"""
function local_to_ghost end
function local_permutation(indices)
n_local = local_length(indices)
n_own = own_length(indices)
n_ghost = ghost_length(indices)
perm = zeros(Int32,n_local)
perm[own_to_local(indices)] = 1:n_own
perm[ghost_to_local(indices)] = (1:n_ghost) .+ n_own
perm
end
function matching_local_indices(a,b)
a === b && return true
local_to_global(a) == local_to_global(b) &&
local_to_owner(a) == local_to_owner(b)
end
function matching_own_indices(a,b)
a === b && return true
own_to_global(a) == own_to_global(b) &&
part_id(a) == part_id(b)
end
function matching_ghost_indices(a,b)
a === b && return true
ghost_to_global(a) == ghost_to_global(b) &&
ghost_to_owner(a) == ghost_to_owner(b)
end
"""
replace_ghost(indices,gids,owners)
Replaces the ghost indices in `indices` with global ids in `gids` and owners in
`owners`. Returned object takes ownership of `gids` and `owners`. This method
only makes sense if `indices` stores ghost ids in separate vectors like in
[`OwnAndGhostIndices`](@ref). `gids` should be unique and not being owned by
`indices`.
"""
function replace_ghost end
function filter_ghost(indices,gids,owners)
set = Set{Int}()
part_owner = part_id(indices)
n_new_ghost = 0
global_to_ghost_indices = global_to_ghost(indices)
for (global_i,owner) in zip(gids,owners)
if owner != part_owner
ghost_i = global_to_ghost_indices[global_i]
if ghost_i == 0 && !(global_i in set)
n_new_ghost += 1
push!(set,global_i)
end
end
end
new_ghost_to_global = zeros(Int,n_new_ghost)
new_ghost_to_owner = zeros(Int32,n_new_ghost)
new_ghost_i = 0
set = Set{Int}()
for (global_i,owner) in zip(gids,owners)
if owner != part_owner
ghost_i = global_to_ghost_indices[global_i]
if ghost_i == 0 && !(global_i in set)
new_ghost_i += 1
new_ghost_to_global[new_ghost_i] = global_i
new_ghost_to_owner[new_ghost_i] = owner
push!(set,global_i)
end
end
end
new_ghost_to_global, new_ghost_to_owner
end
"""
union_ghost(indices,gids,owners)
Make the union of the ghost indices in `indices` with
the global indices `gids` and owners `owners`.
Return an object of the same type as `indices` with the new ghost indices and the same
own indices as in `indices`.
The result does not take ownership of `gids` and `owners`.
"""
function union_ghost(indices,gids,owners)
extra_gids, extra_owners = filter_ghost(indices,gids,owners)
new_gids = vcat(ghost_to_global(indices),extra_gids)
new_owners = vcat(ghost_to_owner(indices),extra_owners)
n_global = global_length(indices)
ghost = GhostIndices(n_global,new_gids,new_owners)
replace_ghost(indices,ghost)
end
"""
to_local!(I,indices)
Transform the global indices in `I` into local ids according to `indices`.
"""
function to_local!(I,indices)
global_to_local_indices = global_to_local(indices)
for k in 1:length(I)
I[k] = global_to_local_indices[I[k]]
end
I
end
"""
to_global!(I,indices)
Transform the local indices in `I` into global ids according to `indices`.
"""
function to_global!(I,indices)
local_to_global_indices = local_to_global(indices)
for k in 1:length(I)
I[k] = local_to_global_indices[I[k]]
end
I
end
"""
find_owner(index_partition,global_ids)
Find the owners of the global ids in `global_ids`. The input `global_ids` is
a vector of vectors distributed over the same parts as `index_partition`. Each part will
look for the owners in parallel, when using a parallel back-end.
# Example
```jldoctest
julia> using PartitionedArrays
julia> rank = LinearIndices((4,));
julia> index_partition = uniform_partition(rank,10)
4-element Vector{PartitionedArrays.LocalIndicesWithConstantBlockSize{1}}:
[1, 2]
[3, 4]
[5, 6, 7]
[8, 9, 10]
julia> gids = [[3],[4,5],[7,2],[9,10,1]]
4-element Vector{Vector{Int64}}:
[3]
[4, 5]
[7, 2]
[9, 10, 1]
julia> find_owner(index_partition,gids)
4-element Vector{Vector{Int32}}:
[2]
[2, 3]
[3, 1]
[4, 4, 1]
```
"""
function find_owner(indices,global_ids)
find_owner(indices,global_ids,eltype(indices))
end
struct AssemblyCache
neighbors_snd::Base.RefValue{Vector{Int32}}
neighbors_rcv::Base.RefValue{Vector{Int32}}
local_indices_snd::Base.RefValue{JaggedArray{Int32,Int32}}
local_indices_rcv::Base.RefValue{JaggedArray{Int32,Int32}}
end
function Base.copy!(a::AssemblyCache,b::AssemblyCache)
a.neighbors_snd[] = b.neighbors_snd[]
a.neighbors_rcv[] = b.neighbors_rcv[]
a.local_indices_snd[] = b.local_indices_snd[]
a.local_indices_rcv[] = b.local_indices_rcv[]
a
end
function AssemblyCache()
AssemblyCache(
Ref{Vector{Int32}}(),
Ref{Vector{Int32}}(),
Ref{JaggedArray{Int32,Int32}}(),
Ref{JaggedArray{Int32,Int32}}()
)
end
assembly_cache(a) = AssemblyCache()
function empty_assembly_cache()
AssemblyCache(
Ref(Int32[]),
Ref(Int32[]),
Ref(JaggedArray(Int32[],Int32[1])),
Ref(JaggedArray(Int32[],Int32[1])),
)
end
"""
assembly_graph(index_partition;kwargs...)
Return an instance of [`ExchangeGraph`](@ref) representing the communication
graph needed to perform assembly of distributed vectors defined on the index
partition `index_partition`. `kwargs` are delegated to [`ExchangeGraph`](@ref)
in order to find the receiving neighbors from the sending ones.
Equivalent to
neighbors = assembly_neighbors(index_partition;kwargs...)
ExchangeGraph(neighbors...)
"""
function assembly_graph(index_partition;kwargs...)
neighbors_snd,neighbors_rcv = assembly_neighbors(index_partition;kwargs...)
ExchangeGraph(neighbors_snd,neighbors_rcv)
end
"""
neigs_snd, neigs_rcv = assembly_neighbors(index_partition;kwargs...)
Return the ids of the neighbor parts from we send and receive data respectively
in the assembly of distributed vectors defined on the index
partition `index_partition`.
partition `index_partition`. `kwargs` are delegated to [`ExchangeGraph`](@ref)
in order to find the receiving neighbors from the sending ones.
"""
function assembly_neighbors(indices;kwargs...)
cache = map(assembly_cache,indices)
mask = map(cache) do cache
isassigned(cache.neighbors_snd) && isassigned(cache.neighbors_rcv)
end
if ! getany(mask)
neighbors_snd, neighbors_rcv = compute_assembly_neighbors(indices;kwargs...)
map(cache,neighbors_snd,neighbors_rcv) do cache, neigs_snd, neigs_rcv
cache.neighbors_snd[] = neigs_snd
cache.neighbors_rcv[] = neigs_rcv
end
return neighbors_snd, neighbors_rcv
end
neigs_snd, neigs_rcv = map(cache) do cache
cache.neighbors_snd[], cache.neighbors_rcv[]
end |> tuple_of_arrays
neigs_snd, neigs_rcv
end
function compute_assembly_neighbors(indices;kwargs...)
parts_snd = map(indices) do indices
rank = part_id(indices)
local_index_to_owner = local_to_owner(indices)
set = Set{Int32}()
for owner in local_index_to_owner
if owner != rank
push!(set,owner)
end
end
sort(collect(set))
end
graph = ExchangeGraph(parts_snd;kwargs...)
graph.snd, graph.rcv
end
"""
ids_snd, ids_rcv = assembly_local_indices(index_partition)
Return the local ids to be sent and received
in the assembly of distributed vectors defined on the index
partition `index_partition`.
Local values corresponding to the local
indices in `ids_snd[i]` (respectively `ids_rcv[i]`)
are sent to part `neigs_snd[i]` (respectively `neigs_rcv[i]`),
where `neigs_snd, neigs_rcv = assembly_neighbors(index_partition)`.
"""
function assembly_local_indices(index_partition)
neigs = assembly_neighbors(index_partition)
assembly_local_indices(index_partition,neigs...)
end
function assembly_local_indices(indices,neighbors_snd,neighbors_rcv)
cache = map(assembly_cache,indices)
mask = map(cache) do mycache
isassigned(mycache.local_indices_snd) && isassigned(mycache.local_indices_rcv)
end
if ! getany(mask)
new_local_indices_snd, new_local_indices_rcv = compute_assembly_local_indices(indices,neighbors_snd,neighbors_rcv)
map(cache,new_local_indices_snd,new_local_indices_rcv) do mycache, mylocal_indices_snd, mylocal_indices_rcv
mycache.local_indices_snd[] = mylocal_indices_snd
mycache.local_indices_rcv[] = mylocal_indices_rcv
end
end
local_indices_snd, local_indices_rcv = map(cache) do mycache
mycache.local_indices_snd[], mycache.local_indices_rcv[]
end |> tuple_of_arrays
local_indices_snd, local_indices_rcv
end
function compute_assembly_local_indices(indices,neighbors_snd,neighbors_rcv)
parts_snd = neighbors_snd
parts_rcv = neighbors_rcv
local_indices_snd, global_indices_snd = map(indices,parts_snd) do indices,parts_snd
rank = part_id(indices)
local_index_to_owner = local_to_owner(indices)
owner_to_i = Dict(( owner=>i for (i,owner) in enumerate(parts_snd) ))
ptrs = zeros(Int32,length(parts_snd)+1)
for owner in local_index_to_owner
if owner != rank
ptrs[owner_to_i[owner]+1] +=1
end
end
length_to_ptrs!(ptrs)
data_lids = zeros(Int32,ptrs[end]-1)
data_gids = zeros(Int,ptrs[end]-1)
local_to_global_indices = local_to_global(indices)
for (lid,owner) in enumerate(local_index_to_owner)
if owner != rank
p = ptrs[owner_to_i[owner]]
data_lids[p]=lid
data_gids[p]=local_to_global_indices[lid]
ptrs[owner_to_i[owner]] += 1
end
end
rewind_ptrs!(ptrs)
my_local_indices_snd = JaggedArray(data_lids,ptrs)
my_global_indices_snd = JaggedArray(data_gids,ptrs)
my_local_indices_snd, my_global_indices_snd
end |> tuple_of_arrays
graph = ExchangeGraph(parts_snd,parts_rcv)
global_indices_rcv = exchange_fetch(global_indices_snd,graph)
local_indices_rcv = map(global_indices_rcv,indices) do myglobal_indices_rcv,myindices
ptrs = myglobal_indices_rcv.ptrs
data_lids = zeros(Int32,ptrs[end]-1)
global_to_local_indices = global_to_local(myindices)
for (k,gid) in enumerate(myglobal_indices_rcv.data)
data_lids[k] = global_to_local_indices[gid]
end
my_local_indices_rcv = JaggedArray(data_lids,ptrs)
end
local_indices_snd,local_indices_rcv
end
"""
permute_indices(indices,perm)
"""
permute_indices(a,b) = PermutedLocalIndices(a,b)
"""
uniform_partition(ranks,np,n[,ghost[,periodic]])
Generate an `N` dimensional
block partition of the indices in `LinearIndices(np)` with a (roughly) constant block size.
The output is a vector of vectors containing the indices in each component of
the partition. The `eltype` of the result implements the [`AbstractLocalIndices`](@ref)
interface.
# Arguments
- `ranks`: Array containing the distribution of ranks.
- `np::NTuple{N}`: Number of parts per direction.
- `n::NTuple{N}`: Number of global indices per direction.
- `ghost::NTuple{N}=ntuple(i->false,N)`: Use or not ghost indices per direction.
- `periodic::NTuple{N}=ntuple(i->false,N)`: Use or not periodic boundaries per direction.
For convenience, one can also provide scalar inputs instead tuples
to create 1D block partitions. In this case, the argument `np` can be omitted
and it will be computed as `np=length(ranks)`.
# Examples
2D partition of 4x4 indices into 2x2 parts without ghost
```jldoctest
julia> using PartitionedArrays
julia> rank = LinearIndices((4,));
julia> uniform_partition(rank,10)
4-element Vector{PartitionedArrays.LocalIndicesWithConstantBlockSize{1}}:
[1, 2]
[3, 4]
[5, 6, 7]
[8, 9, 10]
julia> uniform_partition(rank,(2,2),(4,4))
4-element Vector{PartitionedArrays.LocalIndicesWithConstantBlockSize{2}}:
[1, 2, 5, 6]
[3, 4, 7, 8]
[9, 10, 13, 14]
[11, 12, 15, 16]
```
"""
function uniform_partition(rank,np,n,args...)
@assert prod(np) == length(rank)
indices = map(rank) do rank
block_with_constant_size(rank,np,n,args...)
end
if length(args) == 0
map(indices) do indices
cache = assembly_cache(indices)
copy!(cache,empty_assembly_cache())
end
else
assembly_neighbors(indices;symmetric=true)
end
indices
end
function uniform_partition(rank,n::Integer)
uniform_partition(rank,length(rank),n)
end
function uniform_partition(rank,n::Integer,ghost::Bool,periodic::Bool=false)
uniform_partition(rank,length(rank),n,ghost,periodic)
end
function uniform_partition(rank,np::Integer,n::Integer) uniform_partition(rank,(np,),(n,)) end
function uniform_partition(rank,np::Integer,n::Integer,ghost::Bool,periodic::Bool=false)
uniform_partition(rank,(np,),(n,),(ghost,),(periodic,))
end
function block_with_constant_size(rank,np,n)
N = length(n)
p = CartesianIndices(np)[rank]
ghost = GhostIndices(prod(n))
LocalIndicesWithConstantBlockSize(p,np,n,ghost)
end
function block_with_constant_size(rank,np,n,ghost,periodic=map(i->false,ghost))
N = length(n)
p = CartesianIndices(np)[rank]
own_ranges = map(local_range,Tuple(p),np,n)
local_ranges = map(local_range,Tuple(p),np,n,ghost,periodic)
owners = map(Tuple(p),own_ranges,local_ranges) do p,or,lr
myowners = zeros(Int32,length(lr))
for i in 1:length(lr)
if lr[i] in or
myowners[i] = p
end
end
if myowners[1] == 0
myowners[1] = p-1
end
if myowners[end] == 0
myowners[end] = p+1
end
myowners
end
n_ghost = 0
cis = CartesianIndices(map(length,local_ranges))
predicate(p,i,owners) = owners[i] == p
for ci in cis
flags = map(predicate,Tuple(p),Tuple(ci),owners)
if !all(flags)
n_ghost += 1
end
end
ghost_to_global = zeros(Int,n_ghost)
ghost_to_owner = zeros(Int32,n_ghost)
n_local = prod(map(length,local_ranges))
perm = zeros(Int32,n_local)
i_ghost = 0
i_own = 0
n_own = prod(map(length,own_ranges))
lis = CircularArray(LinearIndices(n))
local_cis = CartesianIndices(local_ranges)
owner_lis = CircularArray(LinearIndices(np))
for (i,ci) in enumerate(cis)
flags = map(predicate,Tuple(p),Tuple(ci),owners)
if !all(flags)
i_ghost += 1
ghost_to_global[i_ghost] = lis[local_cis[i]]
o = map(getindex,owners,Tuple(ci))
o_ci = CartesianIndex(o)
ghost_to_owner[i_ghost] = owner_lis[o_ci]
perm[i] = i_ghost + n_own
else
i_own += 1
perm[i] = i_own
end
end
ghostids = GhostIndices(prod(n),ghost_to_global,ghost_to_owner)
ids = LocalIndicesWithConstantBlockSize(p,np,n,ghostids)
PermutedLocalIndices(ids,perm)
end
"""
variable_partition(n_own,n_global[;start])
Build a 1D variable-size block partition of the range `1:n`.
The output is a vector of vectors containing the indices in each component of
the partition. The `eltype` of the result implements the [`AbstractLocalIndices`](@ref)
interface.
# Arguments
- `n_own::AbstractArray{<:Integer}`: Array containing the block size for each part.
- `n_global::Integer`: Number of global indices. It should be equal to `sum(n_own)`.
- `start::AbstractArray{Int}=scan(+,n_own,type=:exclusive,init=1)`: First global index in each part.
We ask the user to provide `n_global` and (optionally) `start` since discovering them requires communications.
# Examples
```jldoctest
julia> using PartitionedArrays
julia> rank = LinearIndices((4,));
julia> n_own = [3,2,2,3];
julia> variable_partition(n_own,sum(n_own))
4-element Vector{PartitionedArrays.LocalIndicesWithVariableBlockSize{1}}:
[1, 2, 3]
[4, 5]
[6, 7]
[8, 9, 10]
```
"""
function variable_partition(
n_own,
n_global,
ghost=false,
periodic=false;
start=scan(+,n_own,type=:exclusive,init=one(eltype(n_own))))
rank = linear_indices(n_own)
if ghost == true || periodic == true
error("This case is not yet implemented.")
end
n_parts = length(n_own)
indices = map(rank,n_own,start) do rank,n_own,start
p = CartesianIndex((rank,))
np = (n_parts,)
n = (n_global,)
ranges = ((1:n_own).+(start-1),)
ghost = GhostIndices(n_global)
indices = LocalIndicesWithVariableBlockSize(p,np,n,ranges,ghost)
# This should be changed when including ghost
cache = assembly_cache(indices)
copy!(cache,empty_assembly_cache())
indices
end
indices
end
function local_range(p,np,n,ghost=false,periodic=false)
l = n ÷ np
offset = l * (p-1)
rem = n % np
if rem >= (np-p+1)
l = l + 1
offset = offset + p - (np-rem) - 1
end
start = 1+offset
stop = l+offset
if ghost && np != 1
if periodic || p!=1
start -= 1
end
if periodic || p!=np
stop += 1
end
end
start:stop
end
function boundary_owner(p,np,n,ghost=false,periodic=false)
start = p
stop = p
if ghost && np != 1
if periodic || p!=1
start -= 1
end
if periodic || p!=np
stop += 1
end
end
(start,p,stop)
end
struct VectorFromDict{Tk,Tv} <: AbstractVector{Tv}
dict::Dict{Tk,Tv}
length::Int
end
Base.IndexStyle(::Type{<:VectorFromDict}) = IndexLinear()
Base.size(a::VectorFromDict) = (Int(a.length),)
function Base.getindex(a::VectorFromDict,i::Int)
Tv = eltype(a)
haskey(a.dict,i) || return zero(Tv)
a.dict[i]
end
function Base.setindex!(a::VectorFromDict,v,i::Int)
a.dict[i] = v
v
end
function VectorFromDict(ids,vals,n)
Tk = eltype(ids)
Tv = eltype(vals)
dict = Dict{Tk,Tv}()
@assert length(ids) == length(vals)
for i in 1:length(ids)
dict[ids[i]] = vals[i]
end
VectorFromDict(dict,n)
end
"""
struct OwnIndices
Container for own indices.
# Properties
- `n_global::Int`: Number of global indices
- `owner::Int32`: Id of the part that owns these indices
- `own_to_global::Vector{Int}`: Global ids of the indices owned by this part. `own_to_global[i_own]` is the global id corresponding to the own index number `i_own`.
# Supertype hierarchy
OwnIndices <: Any
"""
struct OwnIndices
n_global::Int
owner::Int32
own_to_global::Vector{Int}
global_to_own::VectorFromDict{Int,Int32}
end
"""
OwnIndices(n_global,owner,own_to_global)
Build an instance of [`OwnIndices`](@ref) from the underlying properties `n_global`,
`owner`, and `own_to_global`. The types of these variables need to match
the type of the properties in [`OwnIndices`](@ref).
"""
function OwnIndices(n_global,owner,own_to_global)
n_own = length(own_to_global)
global_to_own = VectorFromDict(
own_to_global,Int32.(1:n_own),n_global)
OwnIndices(n_global,Int32(owner),own_to_global,global_to_own)
end
"""
struct GhostIndices
Container for ghost indices.
# Properties
- `n_global::Int`: Number of global indices
- `ghost_to_global::Vector{Int}`: Global ids of the ghost indices in this part. `ghost_to_global[i_ghost]` is the global id corresponding to the ghost index number `i_ghost`.
- `ghost_to_owner::Vector{Int32}`: Owners of the ghost ids. `ghost_to_owner[i_ghost]`is the id of the owner of the ghost index number `i_ghost`.
# Supertype hierarchy
GhostIndices <: Any
"""
struct GhostIndices
n_global::Int
ghost_to_global::Vector{Int}
ghost_to_owner::Vector{Int32}
global_to_ghost::VectorFromDict{Int,Int32}
end
"""
GhostIndices(n_global,ghost_to_global,ghost_to_owner)
Build an instance of [`GhostIndices`](@ref) from the underlying fields `n_global`,
`ghost_to_global`, and `ghost_to_owner`.
The types of these variables need to match
the type of the properties in [`GhostIndices`](@ref).
"""
function GhostIndices(n_global,ghost_to_global,ghost_to_owner)
n_ghost = length(ghost_to_global)
@assert length(ghost_to_owner) == n_ghost
global_to_ghost = VectorFromDict(
ghost_to_global,Int32.(1:n_ghost),n_global)
GhostIndices(
n_global,ghost_to_global,ghost_to_owner,global_to_ghost)
end
"""
GhostIndices(n_global)
Build an empty instance of [`GhostIndices`](@ref) for a range of `n_global` indices.
"""
function GhostIndices(n_global)
ghost_to_global = Int[]
ghost_to_owner = Int32[]
GhostIndices(n_global,ghost_to_global,ghost_to_owner)
end
function replace_ghost(indices,gids,owners)
n_global = global_length(indices)
ghost = GhostIndices(n_global,gids,owners)
replace_ghost(indices,ghost)
end
# This is essentially a FillArray
# but we add this to improve stack trace
struct OwnToOwner <: AbstractVector{Int32}
owner::Int32
n_own::Int
end
Base.IndexStyle(::Type{<:OwnToOwner}) = IndexLinear()
Base.size(a::OwnToOwner) = (Int(a.n_own),)
function Base.getindex(a::OwnToOwner,own_id::Int)
a.owner
end
struct GlobalToLocal{A,B,C} <: AbstractVector{Int32}
global_to_own::A
global_to_ghost::VectorFromDict{Int,Int32}
own_to_local::B
ghost_to_local::C
end
Base.size(a::GlobalToLocal) = size(a.global_to_own)
Base.IndexStyle(::Type{<:GlobalToLocal}) = IndexLinear()
function Base.getindex(a::GlobalToLocal,global_id::Int)
own_id = a.global_to_own[global_id]
z = Int32(0)
if own_id != z
return a.own_to_local[own_id]
end
ghost_id = a.global_to_ghost[global_id]
if ghost_id != z
return a.ghost_to_local[ghost_id]
end
return z
end
struct LocalToOwn{A} <: AbstractVector{Int32}
n_own::Int
perm::A
end
Base.size(a::LocalToOwn) = (length(a.perm),)
Base.IndexStyle(::Type{<:LocalToOwn}) = IndexLinear()
function Base.getindex(a::LocalToOwn,local_id::Int)
i = a.perm[local_id]
if i > a.n_own
Int32(0)
else
Int32(i)
end
end
struct LocalToGhost{A} <: AbstractVector{Int32}
n_own::Int
perm::A
end
Base.size(a::LocalToGhost) = (length(a.perm),)
Base.IndexStyle(::Type{<:LocalToGhost}) = IndexLinear()
function Base.getindex(a::LocalToGhost,local_id::Int)
i = a.perm[local_id]
if i > a.n_own
Int32(i-a.n_own)
else
Int32(0)
end
end
struct LocalToGlobal{A,C} <: AbstractVector{Int}
own_to_global::A
ghost_to_global::Vector{Int}
perm::C
end
Base.IndexStyle(::Type{<:LocalToGlobal}) = IndexLinear()
Base.size(a::LocalToGlobal) = (length(a.own_to_global)+length(a.ghost_to_global),)
function Base.getindex(a::LocalToGlobal,local_id::Int)
n_own = length(a.own_to_global)
j = a.perm[local_id]
if j > n_own
a.ghost_to_global[j-n_own]
else
a.own_to_global[j]
end
end
struct LocalToOwner{C} <: AbstractVector{Int32}
own_to_owner::OwnToOwner
ghost_to_owner::Vector{Int32}
perm::C
end
Base.IndexStyle(::Type{<:LocalToOwner}) = IndexLinear()
Base.size(a::LocalToOwner) = (length(a.own_to_owner)+length(a.ghost_to_owner),)
function Base.getindex(a::LocalToOwner,local_id::Int)
n_own = length(a.own_to_owner)
j = a.perm[local_id]
if j > n_own
a.ghost_to_owner[j-n_own]
else
a.own_to_owner[j]
end
end
struct GlobalToOwn{A} <: AbstractVector{Int32}
n_own::Int32
global_to_local::VectorFromDict{Int,Int32}
perm::A
end
Base.IndexStyle(::Type{<:GlobalToOwn}) = IndexLinear()
Base.size(a::GlobalToOwn) = size(a.global_to_local)
function Base.getindex(a::GlobalToOwn,global_i::Int)
local_i = a.global_to_local[global_i]
z = Int32(0)
local_i == z && return z
i = a.perm[local_i]
i > a.n_own && return z
return Int32(i)
end
struct GlobalToGhost{A} <: AbstractVector{Int32}
n_own::Int
global_to_local::VectorFromDict{Int,Int32}
perm::A
end
Base.IndexStyle(::Type{<:GlobalToGhost}) = IndexLinear()
Base.size(a::GlobalToGhost) = size(a.global_to_local)
function Base.getindex(a::GlobalToGhost,global_i::Int)
local_i = a.global_to_local[global_i]
z = Int32(0)
local_i == z && return z
i = a.perm[local_i]
i <= a.n_own && return z
return Int32(i-a.n_own)
end
"""
struct LocalIndices
Container for arbitrary local indices.
# Properties
- `n_global::Int`: Number of global indices.
- `owner::Int32`: Id of the part that stores the local indices
- `local_to_global::Vector{Int}`: Global ids of the local indices in this part. `local_to_global[i_local]` is the global id corresponding to the local index number `i_local`.
- `local_to_owner::Vector{Int32}`: Owners of the local ids. `local_to_owner[i_local]`is the id of the owner of the local index number `i_local`.
# Supertype hierarchy
LocalIndices <: AbstractLocalIndices
"""
struct LocalIndices <: AbstractLocalIndices