-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathInterfaces.jl
More file actions
2857 lines (2476 loc) · 72.8 KB
/
Interfaces.jl
File metadata and controls
2857 lines (2476 loc) · 72.8 KB
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 AbstractBackend end
Abstract type representing a message passing model used to run
a distributed computation.
At this moment, these specializations are available
- [`SequentialBackend`](@ref)
- [`MPIBackend`](@ref)
"""
abstract type AbstractBackend end
# Should return a AbstractPData{Int}
"""
get_part_ids(b::AbstractBackend,nparts::Integer) -> AbstractPData{Int}
get_part_ids(b::AbstractBackend,nparts::Tuple) -> AbstractPData{Int}
Return a partitioned data consisting of `nparts`,
where each part stores its part id. The concrete type of
the returned object depends on the back-end `b`. If `nparts` is a tuple
a Cartesian partition is considered with `nparts[i]` parts in direction `i`.
"""
function get_part_ids(b::AbstractBackend,nparts::Integer)
@abstractmethod
end
function get_part_ids(b::AbstractBackend,nparts::Tuple)
get_part_ids(b,prod(nparts))
end
# This can be overwritten to add a finally clause
function prun(driver::Function,b::AbstractBackend,nparts)
part = get_part_ids(b,nparts)
driver(part)
end
# Data distributed in parts of type T
"""
abstract type AbstractPData{T,N} end
Abstract type representing a data partitioned into parts of type `T` where `N`
is the tensor order of the partition. I.e., `N==1` for linear partitions and
`N>1` for Cartesian partitions.
At this moment, these specializations are available
- [`SequentialData`](@ref)
- [`MPIData`](@ref)
"""
abstract type AbstractPData{T,N} end
"""
size(a::AbstractPData) -> Tuple
Number of parts per direction in the partitioned data `a`.
"""
Base.size(a::AbstractPData) = @abstractmethod
"""
length(a::AbstractPData) -> Int
Same as `num_parts(a)`.
"""
Base.length(a::AbstractPData) = prod(size(a))
"""
num_parts(a::AbstractPData) -> Int
Total number of parts in `a`.
"""
num_parts(a::AbstractPData) = length(a)
"""
get_backend(a::AbstractPData) -> AbstractBackend
Get the back-end associated with `a`.
"""
get_backend(a::AbstractPData) = @abstractmethod
Base.iterate(a::AbstractPData) = @abstractmethod
Base.iterate(a::AbstractPData,state) = @abstractmethod
get_part_ids(a::AbstractPData) = get_part_ids(get_backend(a),size(a))
"""
map_parts(f, xs::AbstractPData...) -> AbstractPData
Call function `f` on the data `xs` owned by the current process.
Internally this unwraps the arguments `xs`, calls `f` with the underlying data,
and rewraps the result as `AbstractPData` of the same type.
# Example
In this example (using the `MPIBackend`):
- `A`, `B`, `X`, and `Y` are `::MPIData`,
- `a`, `b`, `x`, and `y` are `::Vector` (the underlying data of the `MPIData`)
```julia
X, Y = map_parts(A, B) do a, b
x = a + 2 * b
y = a - 5 * b
return x, y
end
```
"""
map_parts(task,a::AbstractPData...) = @abstractmethod
i_am_main(::AbstractPData) = @abstractmethod
Base.eltype(a::AbstractPData{T}) where T = T
Base.eltype(::Type{<:AbstractPData{T}}) where T = T
Base.ndims(a::AbstractPData{T,N}) where {T,N} = N
Base.ndims(::Type{<:AbstractPData{T,N}}) where {T,N} = N
Base.copy(a::AbstractPData) = map_parts(copy,a)
#function map_parts(task,a...)
# map_parts(task,map(AbstractPData,a)...)
#end
#
#AbstractPData(a::AbstractPData) = a
const MAIN = 1
function map_main(f,args::AbstractPData...)
parts = get_part_ids(first(args))
map_parts(parts,args...) do part,args...
if part == MAIN
f(args...)
else
nothing
end
end
end
# import the main part to the main scope
# in MPI this will broadcast the main part to all procs
get_main_part(a::AbstractPData) = get_part(a,MAIN)
# This one is safe to use only when all parts contain the same value, e.g. the result of a gather_all call.
get_part(a::AbstractPData) = @abstractmethod
get_part(a::AbstractPData,part::Integer) = @abstractmethod
# rcv can contain vectors or tables
gather!(rcv::AbstractPData,snd::AbstractPData) = @abstractmethod
gather_all!(rcv::AbstractPData,snd::AbstractPData) = @abstractmethod
function allocate_gather(snd::AbstractPData)
np = num_parts(snd)
parts = get_part_ids(snd)
rcv = map_parts(parts,snd) do part, snd
T = typeof(snd)
if part == MAIN
Vector{T}(undef,np)
else
Vector{T}(undef,0)
end
end
rcv
end
function allocate_gather(snd::AbstractPData{<:AbstractVector})
l = map_parts(length,snd)
l_main = gather(l)
parts = get_part_ids(snd)
rcv = map_parts(parts,l_main,snd) do part, l, snd
if part == MAIN
ptrs = counts_to_ptrs(l)
ndata = ptrs[end]-1
data = Vector{eltype(snd)}(undef,ndata)
Table(data,ptrs)
else
ptrs = Vector{Int32}(undef,1)
data = Vector{eltype(snd)}(undef,0)
Table(data,ptrs)
end
end
rcv
end
function gather(snd::AbstractPData)
rcv = allocate_gather(snd)
gather!(rcv,snd)
rcv
end
function allocate_gather_all(snd::AbstractPData)
np = num_parts(snd)
rcv = map_parts(snd) do snd
T = typeof(snd)
Vector{T}(undef,np)
end
rcv
end
function allocate_gather_all(snd::AbstractPData{<:AbstractVector})
l = map_parts(length,snd)
l_all = gather_all(l)
parts = get_part_ids(snd)
rcv = map_parts(parts,l_all,snd) do part, l, snd
ptrs = counts_to_ptrs(l)
ndata = ptrs[end]-1
data = Vector{eltype(snd)}(undef,ndata)
Table(data,ptrs)
end
rcv
end
function gather_all(snd::AbstractPData)
rcv = allocate_gather_all(snd)
gather_all!(rcv,snd)
rcv
end
# The back-end need to support these cases:
# i.e. AbstractPData{AbstractVector{<:Number}} and AbstractPData{AbstractVector{<:AbstractVector{<:Number}}}
function scatter(snd::AbstractPData)
@abstractmethod
end
# AKA broadcast
function emit(snd::AbstractPData)
np = num_parts(snd)
parts = get_part_ids(snd)
snd2 = map_parts(parts,snd) do part, snd
T = typeof(snd)
if part == MAIN
v = Vector{T}(undef,np)
fill!(v,snd)
else
v = Vector{T}(undef,0)
end
v
end
scatter(snd2)
end
function reduce_main(op,snd::AbstractPData;init)
a = gather(snd)
map_parts(i->reduce(op,i;init=init),a)
end
function reduce_all(args...;kwargs...)
b = reduce_main(args...;kwargs...)
emit(b)
end
function Base.reduce(op,a::AbstractPData;init)
b = reduce_main(op,a;init=init)
get_main_part(b)
end
function Base.sum(a::AbstractPData)
reduce(+,a,init=zero(eltype(a)))
end
# inclusive prefix reduction
function iscan(op,a::AbstractPData;init)
b = iscan_main(op,a,init=init)
scatter(b)
end
function iscan(op,::typeof(reduce),a::AbstractPData;init)
b,n = iscan_main(op,reduce,a,init=init)
scatter(b), get_main_part(n)
end
function iscan_all(op,a::AbstractPData;init)
b = iscan_main(op,a,init=init)
emit(b)
end
function iscan_all(op,::typeof(reduce),a::AbstractPData;init)
b,n = iscan_main(op,reduce,a,init=init)
emit(b), get_main_part(n)
end
function iscan_main(op,a;init)
b = gather(a)
map_parts(b) do b
_iscan_local!(op,b,init)
end
b
end
function iscan_main(op,::typeof(reduce),a;init)
b = gather(a)
n = map_parts(b) do b
reduce(op,b,init=init)
end
map_parts(b) do b
_iscan_local!(op,b,init)
end
b,n
end
function _iscan_local!(op,b,init)
if length(b)!=0
b[1] = op(init,b[1])
end
@inbounds for i in 1:(length(b)-1)
b[i+1] = op(b[i],b[i+1])
end
b
end
# exclusive prefix reduction
function xscan(op,a::AbstractPData;init)
b = xscan_main(op,a,init=init)
scatter(b)
end
function xscan(op,::typeof(reduce),a::AbstractPData;init)
b,n = xscan_main(op,reduce,a,init=init)
scatter(b), get_main_part(n)
end
function xscan_all(op,a::AbstractPData;init)
b = xscan_main(op,a,init=init)
emit(b)
end
function xscan_all(op,::typeof(reduce),a::AbstractPData;init)
b,n = xscan_main(op,reduce,a,init=init)
emit(b), get_main_part(n)
end
function xscan_main(op,a::AbstractPData;init)
b = gather(a)
map_parts(b) do b
_xscan_local!(op,b,init)
end
b
end
function xscan_main(op,::typeof(reduce),a::AbstractPData;init)
b = gather(a)
n = map_parts(b) do b
reduce(op,b,init=init)
end
map_parts(b) do b
_xscan_local!(op,b,init)
end
b,n
end
function _xscan_local!(op,b,init)
@inbounds for i in (length(b)-1):-1:1
b[i+1] = b[i]
end
if length(b) != 0
b[1] = init
end
@inbounds for i in 1:(length(b)-1)
b[i+1] = op(b[i],b[i+1])
end
end
# TODO improve the mechanism for waiting
# Non-blocking in-place exchange
# In this version, sending a number per part is enough
# We have another version below to send a vector of numbers per part (compressed in a Table)
# Starts a non-blocking exchange. It returns a AbstractPData of Julia Tasks. Calling schedule and wait on these
# tasks will wait until the exchange is done in the corresponding part
# (i.e., at this point it is save to read/write the buffers again).
function async_exchange!(
data_rcv::AbstractPData,
data_snd::AbstractPData,
parts_rcv::AbstractPData,
parts_snd::AbstractPData,
t_in::AbstractPData)
@abstractmethod
end
function async_exchange!(
data_rcv::AbstractPData,
data_snd::AbstractPData,
parts_rcv::AbstractPData,
parts_snd::AbstractPData)
t_in = _empty_tasks(parts_rcv)
async_exchange!(data_rcv,data_snd,parts_rcv,parts_snd,t_in)
end
function _empty_tasks(a)
map_parts(a) do a
@task nothing
end
end
# Non-blocking allocating exchange
# the returned data_rcv cannot be consumed in a part until the corresponding task in t is done.
function async_exchange(
data_snd::AbstractPData,
parts_rcv::AbstractPData,
parts_snd::AbstractPData,
t_in::AbstractPData=_empty_tasks(parts_rcv))
data_rcv = map_parts(data_snd,parts_rcv) do data_snd, parts_rcv
similar(data_snd,eltype(data_snd),length(parts_rcv))
end
t_out = async_exchange!(data_rcv,data_snd,parts_rcv,parts_snd,t_in)
data_rcv, t_out
end
# Non-blocking in-place exchange variable length (compressed in a Table)
function async_exchange!(
data_rcv::AbstractPData{<:Table},
data_snd::AbstractPData{<:Table},
parts_rcv::AbstractPData,
parts_snd::AbstractPData,
t_in::AbstractPData)
@abstractmethod
end
# Non-blocking allocating exchange variable length (compressed in a Table)
function async_exchange(
data_snd::AbstractPData{<:Table},
parts_rcv::AbstractPData,
parts_snd::AbstractPData,
t_in::AbstractPData)
# Allocate empty data
data_rcv = map_parts(empty_table,data_snd)
n_snd = map_parts(parts_snd) do parts_snd
Int[]
end
# wait data_snd to be in a correct state and
# Count how many we snd to each part
t1 = map_parts(n_snd,data_snd,t_in) do n_snd,data_snd,t_in
@task begin
wait(schedule(t_in))
resize!(n_snd,length(data_snd))
for i in 1:length(n_snd)
n_snd[i] = data_snd.ptrs[i+1] - data_snd.ptrs[i]
end
end
end
# Count how many we rcv from each part
n_rcv, t2 = async_exchange(n_snd,parts_rcv,parts_snd,t1)
# Wait n_rcv to be in a correct state and
# resize data_rcv to the correct size
t3 = map_parts(n_rcv,t2,data_rcv) do n_rcv,t2,data_rcv
@task begin
wait(schedule(t2))
resize!(data_rcv.ptrs,length(n_rcv)+1)
for i in 1:length(n_rcv)
data_rcv.ptrs[i+1] = n_rcv[i]
end
length_to_ptrs!(data_rcv.ptrs)
ndata = data_rcv.ptrs[end]-1
resize!(data_rcv.data,ndata)
end
end
# Do the actual exchange
t4 = async_exchange!(data_rcv,data_snd,parts_rcv,parts_snd,t3)
data_rcv, t4
end
# Blocking in-place exchange
function exchange!(args...;kwargs...)
t = async_exchange!(args...;kwargs...)
map_parts(schedule,t)
map_parts(wait,t)
first(args)
end
# Blocking allocating exchange
function exchange(args...;kwargs...)
data_rcv, t = async_exchange(args...;kwargs...)
map_parts(schedule,t)
map_parts(wait,t)
data_rcv
end
# Discover snd parts from rcv assuming that snd is a subset of neighbors
# Assumes that neighbors_snd is a symmetric communication graph
# if neighbors_rcv not provided
function discover_parts_snd(
parts_rcv::AbstractPData,
neighbors_snd::AbstractPData,
neighbors_rcv::AbstractPData=neighbors_snd)
@assert num_parts(parts_rcv) == num_parts(neighbors_rcv)
parts = get_part_ids(parts_rcv)
# Tell the neighbors whether I want to receive data from them
data_rcv = map_parts(parts,neighbors_rcv,parts_rcv) do part, neighbors_rcv, parts_rcv
dict_snd = Dict(( n=>Int32(-1) for n in neighbors_rcv))
for i in parts_rcv
dict_snd[i] = part
end
[ dict_snd[n] for n in neighbors_rcv ]
end
data_snd = exchange(data_rcv,neighbors_snd,neighbors_rcv)
# build parts_snd
parts_snd = map_parts(data_snd) do data_snd
k = findall(j->j>0,data_snd)
data_snd[k]
end
parts_snd
end
const ERROR_DISCOVER_PARTS_SND = Ref(false)
function _error_message_on_main_task_discover_parts_snd(data::AbstractPData)
ERROR_DISCOVER_PARTS_SND[] || return nothing
error_msg =
"""
[PartitionedArrays.jl] Using a non-scalable implementation
to discover reciprocal parts in sparse communication kernel among nearest
neighbours. This might cause trouble when running the code at medium/large scales.
You can avoid this using the Exchanger constructor with a superset of
the actual receivers/senders
"""
@unreachable error_msg
nothing
end
# If neighbors not provided, we need to gather in main
function discover_parts_snd(parts_rcv::AbstractPData)
_error_message_on_main_task_discover_parts_snd(parts_rcv)
parts_rcv_main = gather(parts_rcv)
parts_snd_main = map_parts(_parts_rcv_to_parts_snd,parts_rcv_main)
parts_snd = scatter(parts_snd_main)
parts_snd
end
# This also works in part != MAIN since it is able to deal
# with an empty table (the result is also an empty table in this case)
function _parts_rcv_to_parts_snd(parts_rcv::Table)
I = Int32[]
J = Int32[]
np = length(parts_rcv)
for p in 1:np
kini = parts_rcv.ptrs[p]
kend = parts_rcv.ptrs[p+1]-1
for k in kini:kend
push!(I,p)
push!(J,parts_rcv.data[k])
end
end
graph = sparse(I,J,I,np,np)
ptrs = similar(parts_rcv.ptrs)
fill!(ptrs,zero(eltype(ptrs)))
for (i,j,_) in nziterator(graph)
ptrs[j+1] += 1
end
length_to_ptrs!(ptrs)
ndata = ptrs[end]-1
data = similar(parts_rcv.data,eltype(parts_rcv.data),ndata)
for (i,j,_) in nziterator(graph)
data[ptrs[j]] = i
ptrs[j] += 1
end
rewind_ptrs!(ptrs)
parts_snd = Table(data,ptrs)
end
function discover_parts_snd(parts_rcv::AbstractPData,::Nothing)
discover_parts_snd(parts_rcv)
end
function discover_parts_snd(parts_rcv::AbstractPData,::Nothing,::Nothing)
discover_parts_snd(parts_rcv)
end
function discover_parts_snd(parts_rcv::AbstractPData,neighbors::AbstractPData,::Nothing)
discover_parts_snd(parts_rcv,neighbors)
end
abstract type AbstractIndexSet end
num_lids(a::AbstractIndexSet) = length(a.lid_to_part)
num_oids(a::AbstractIndexSet) = length(a.oid_to_lid)
num_hids(a::AbstractIndexSet) = length(a.hid_to_lid)
get_part(a::AbstractIndexSet) = a.part
get_lid_to_gid(a::AbstractIndexSet) = a.lid_to_gid
get_lid_to_part(a::AbstractIndexSet) = a.lid_to_part
get_oid_to_lid(a::AbstractIndexSet) = a.oid_to_lid
get_hid_to_lid(a::AbstractIndexSet) = a.hid_to_lid
get_lid_to_ohid(a::AbstractIndexSet) = a.lid_to_ohid
get_gid_to_lid(a::AbstractIndexSet) = a.gid_to_lid
function add_gid!(a::AbstractIndexSet,gid::Integer,part::Integer)
if (part != a.part) && (!haskey(a.gid_to_lid,gid))
_add_gid_ghost!(a,gid,part)
end
a
end
function add_gid!(gid_to_part::AbstractArray,a::AbstractIndexSet,gid::Integer)
if !haskey(a.gid_to_lid,gid)
part = gid_to_part[gid]
_add_gid_ghost!(a,gid,part)
end
a
end
#TODO use resize + setindex instead of push! when possible
@inline function _add_gid_ghost!(a,gid,part)
lid = Int32(num_lids(a)+1)
hid = Int32(num_hids(a)+1)
push!(a.lid_to_gid,gid)
push!(a.lid_to_part,part)
push!(a.hid_to_lid,lid)
push!(a.lid_to_ohid,-hid)
a.gid_to_lid[gid] = lid
end
function add_gids!(
a::AbstractIndexSet,
i_to_gid::AbstractVector{<:Integer},
i_to_part::AbstractVector{<:Integer})
for i in 1:length(i_to_gid)
gid = i_to_gid[i]
part = i_to_part[i]
add_gid!(a,gid,part)
end
a
end
function add_gids!(
gid_to_part::AbstractArray,
a::AbstractIndexSet,
gids::AbstractVector{<:Integer})
for gid in gids
add_gid!(gid_to_part,a,gid)
end
a
end
function to_lids!(ids::AbstractArray{<:Integer},a::AbstractIndexSet)
for i in eachindex(ids)
gid = ids[i]
lid = a.gid_to_lid[gid]
ids[i] = lid
end
ids
end
function to_gids!(ids::AbstractArray{<:Integer},a::AbstractIndexSet)
for i in eachindex(ids)
lid = ids[i]
gid = a.lid_to_gid[lid]
ids[i] = gid
end
ids
end
function oids_are_equal(a::AbstractIndexSet,b::AbstractIndexSet)
view(a.lid_to_gid,a.oid_to_lid) == view(b.lid_to_gid,b.oid_to_lid)
end
function hids_are_equal(a::AbstractIndexSet,b::AbstractIndexSet)
view(a.lid_to_gid,a.hid_to_lid) == view(b.lid_to_gid,b.hid_to_lid)
end
function lids_are_equal(a::AbstractIndexSet,b::AbstractIndexSet)
a.lid_to_gid == b.lid_to_gid
end
function find_lid_map(a::AbstractIndexSet,b::AbstractIndexSet)
alid_to_blid = fill(Int32(-1),num_lids(a))
for blid in 1:num_lids(b)
gid = b.lid_to_gid[blid]
alid = a.gid_to_lid[gid]
alid_to_blid[alid] = blid
end
alid_to_blid
end
# The given ids are assumed to be a sub-set of the lids
function touched_hids(a::AbstractIndexSet,gids::AbstractVector{<:Integer})
i = 0
hid_touched = fill(false,num_hids(a))
for gid in gids
lid = a.gid_to_lid[gid]
ohid = a.lid_to_ohid[lid]
hid = - ohid
if ohid < 0 && !hid_touched[hid]
hid_touched[hid] = true
i += 1
end
end
i_to_hid = Vector{Int32}(undef,i)
i = 0
hid_touched .= false
for gid in gids
lid = a.gid_to_lid[gid]
ohid = a.lid_to_ohid[lid]
hid = - ohid
if ohid < 0 && !hid_touched[hid]
hid_touched[hid] = true
i += 1
i_to_hid[i] = hid
end
end
i_to_hid
end
struct Exchanger{B,C}
parts_rcv::B
parts_snd::B
lids_rcv::C
lids_snd::C
function Exchanger(
parts_rcv::AbstractPData{<:AbstractVector{<:Integer}},
parts_snd::AbstractPData{<:AbstractVector{<:Integer}},
lids_rcv::AbstractPData{<:Table{<:Integer}},
lids_snd::AbstractPData{<:Table{<:Integer}})
B = typeof(parts_rcv)
C = typeof(lids_rcv)
new{B,C}(parts_rcv,parts_snd,lids_rcv,lids_snd)
end
end
function Base.copy(a::Exchanger)
Exchanger(
copy(a.parts_rcv),
copy(a.parts_snd),
copy(a.lids_rcv),
copy(a.lids_snd))
end
function Exchanger(
ids::AbstractPData{<:AbstractIndexSet},
neighbors_snd=nothing,
neighbors_rcv=nothing;
reuse_parts_rcv=false)
parts = get_part_ids(ids)
parts_rcv = map_parts(parts,ids) do part, ids
parts_rcv = Dict((owner=>true for owner in ids.lid_to_part if owner!=part))
sort(collect(keys(parts_rcv)))
end
lids_rcv, gids_rcv = map_parts(parts,ids,parts_rcv) do part,ids,parts_rcv
owner_to_i = Dict(( owner=>i for (i,owner) in enumerate(parts_rcv) ))
ptrs = zeros(Int32,length(parts_rcv)+1)
for owner in ids.lid_to_part
if owner != part
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)
for (lid,owner) in enumerate(ids.lid_to_part)
if owner != part
p = ptrs[owner_to_i[owner]]
data_lids[p]=lid
data_gids[p]=ids.lid_to_gid[lid]
ptrs[owner_to_i[owner]] += 1
end
end
rewind_ptrs!(ptrs)
lids_rcv = Table(data_lids,ptrs)
gids_rcv = Table(data_gids,ptrs)
lids_rcv, gids_rcv
end
if reuse_parts_rcv
parts_snd = parts_rcv
else
parts_snd = discover_parts_snd(parts_rcv,neighbors_snd,neighbors_rcv)
end
gids_snd = exchange(gids_rcv,parts_snd,parts_rcv)
lids_snd = map_parts(ids,gids_snd) do ids,gids_snd
ptrs = gids_snd.ptrs
data_lids = zeros(Int32,ptrs[end]-1)
for (k,gid) in enumerate(gids_snd.data)
data_lids[k] = ids.gid_to_lid[gid]
end
lids_snd = Table(data_lids,ptrs)
lids_snd
end
Exchanger(parts_rcv,parts_snd,lids_rcv,lids_snd)
end
function empty_exchanger(a::AbstractPData)
parts_rcv = map_parts(i->Int32[],a)
parts_snd = map_parts(i->Int32[],a)
lids_rcv = map_parts(i->Table(Vector{Int32}[]),a)
lids_snd = map_parts(i->Table(Vector{Int32}[]),a)
Exchanger(parts_rcv,parts_snd,lids_rcv,lids_snd)
end
function Base.reverse(a::Exchanger)
Exchanger(a.parts_snd,a.parts_rcv,a.lids_snd,a.lids_rcv)
end
function allocate_rcv_buffer(::Type{T},a::Exchanger) where T
data_rcv = map_parts(a.lids_rcv) do lids_rcv
ptrs = lids_rcv.ptrs
data = zeros(T,ptrs[end]-1)
Table(data,ptrs)
end
data_rcv
end
function allocate_snd_buffer(::Type{T},a::Exchanger) where T
data_snd = map_parts(a.lids_snd) do lids_snd
ptrs = lids_snd.ptrs
data = zeros(T,ptrs[end]-1)
Table(data,ptrs)
end
data_snd
end
function async_exchange!(
values::AbstractPData{<:AbstractVector{T}},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv)) where T
async_exchange!(_replace,values,exchanger,t0)
end
function async_exchange!(
values_rcv::AbstractPData{<:AbstractVector{T}},
values_snd::AbstractPData{<:AbstractVector{T}},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv)) where T
async_exchange!(_replace,values_rcv,values_snd,exchanger,t0)
end
_replace(x,y) = y
function async_exchange!(
combine_op,
values::AbstractPData{<:AbstractVector{Trcv}},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv)) where {Trcv,Tsnd}
async_exchange!(combine_op,values,values,exchanger,t0)
end
function async_exchange!(
combine_op,
values_rcv::AbstractPData{<:AbstractVector{Trcv}},
values_snd::AbstractPData{<:AbstractVector{Tsnd}},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv)) where {Trcv,Tsnd}
# Allocate buffers
data_rcv = allocate_rcv_buffer(Trcv,exchanger)
data_snd = allocate_snd_buffer(Tsnd,exchanger)
# Fill snd buffer
t1 = map_parts(t0,values_snd,data_snd,exchanger.lids_snd) do t0,values_snd,data_snd,lids_snd
@task begin
wait(schedule(t0))
for p in 1:length(lids_snd.data)
lid = lids_snd.data[p]
data_snd.data[p] = values_snd[lid]
end
end
end
# communicate
t2 = async_exchange!(
data_rcv,
data_snd,
exchanger.parts_rcv,
exchanger.parts_snd,
t1)
# Fill values_rcv from rcv buffer
# asynchronously
t3 = map_parts(t2,values_rcv,data_rcv,exchanger.lids_rcv) do t2,values_rcv,data_rcv,lids_rcv
@task begin
wait(schedule(t2))
for p in 1:length(lids_rcv.data)
lid = lids_rcv.data[p]
values_rcv[lid] = combine_op(values_rcv[lid],data_rcv.data[p])
end
end
end
t3
end
function async_exchange!(
values::AbstractPData{<:Table},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv))
async_exchange!(_replace,values,exchanger,t0)
end
function async_exchange!(
combine_op,
values::AbstractPData{<:Table},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv))
data, ptrs = map_parts(t->(t.data,t.ptrs),values)
t_exchanger = _table_exchanger(exchanger,ptrs)
async_exchange!(combine_op,data,t_exchanger,t0)
end
function async_exchange!(
combine_op,
values_rcv::AbstractPData{<:Table},
values_snd::AbstractPData{<:Table},
exchanger::Exchanger,
t0::AbstractPData=_empty_tasks(exchanger.parts_rcv))
@notimplemented
end
function _table_exchanger(exchanger,values)
lids_rcv = _table_lids_snd(exchanger.lids_rcv,values)
lids_snd = _table_lids_snd(exchanger.lids_snd,values)
parts_rcv = exchanger.parts_rcv
parts_snd = exchanger.parts_snd
Exchanger(parts_rcv,parts_snd,lids_rcv,lids_snd)
end
function _table_lids_snd(lids_snd,tptrs)
k_snd = map_parts(tptrs,lids_snd) do tptrs,lids_snd
ptrs = similar(lids_snd.ptrs)
fill!(ptrs,zero(eltype(ptrs)))
np = length(ptrs)-1
for p in 1:np
iini = lids_snd.ptrs[p]
iend = lids_snd.ptrs[p+1]-1
for i in iini:iend
d = lids_snd.data[i]
ptrs[p+1] += tptrs[d+1]-tptrs[d]
end
end
length_to_ptrs!(ptrs)
ndata = ptrs[end]-1
data = similar(lids_snd.data,eltype(lids_snd.data),ndata)
for p in 1:np
iini = lids_snd.ptrs[p]
iend = lids_snd.ptrs[p+1]-1
for i in iini:iend
d = lids_snd.data[i]
jini = tptrs[d]
jend = tptrs[d+1]-1
for j in jini:jend
data[ptrs[p]] = j
ptrs[p] += 1
end
end
end
rewind_ptrs!(ptrs)
Table(data,ptrs)
end
k_snd
end
# mutable is needed to correctly implement add_gids!
mutable struct PRange{A,B,C} <: AbstractUnitRange{Int}
ngids::Int
partition::A
exchanger::B
gid_to_part::C
ghost::Bool
function PRange(
ngids::Integer,
partition::AbstractPData{<:AbstractIndexSet},
exchanger::Exchanger,
gid_to_part::Union{AbstractPData{<:AbstractArray{<:Integer}},Nothing}=nothing,
ghost::Bool=true)
A = typeof(partition)
B = typeof(exchanger)
C = typeof(gid_to_part)
new{A,B,C}(