-
Notifications
You must be signed in to change notification settings - Fork 51
/
Cabana_CommunicationPlan.hpp
1316 lines (1148 loc) · 52.2 KB
/
Cabana_CommunicationPlan.hpp
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
/****************************************************************************
* Copyright (c) 2018-2023 by the Cabana authors *
* All rights reserved. *
* *
* This file is part of the Cabana library. Cabana is distributed under a *
* BSD 3-clause license. For the licensing terms see the LICENSE file in *
* the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
/*!
\file Cabana_CommunicationPlan.hpp
\brief Multi-node communication patterns
*/
#ifndef CABANA_COMMUNICATIONPLAN_HPP
#define CABANA_COMMUNICATIONPLAN_HPP
#include <Cabana_Utils.hpp>
#include <Kokkos_Core.hpp>
#include <Kokkos_ScatterView.hpp>
#include <mpi.h>
#include <algorithm>
#include <exception>
#include <memory>
#include <numeric>
#include <type_traits>
#include <vector>
namespace Cabana
{
namespace Impl
{
//! \cond Impl
//---------------------------------------------------------------------------//
// Count sends and create steering algorithm tags.
struct CountSendsAndCreateSteeringDuplicated
{
};
struct CountSendsAndCreateSteeringAtomic
{
};
//---------------------------------------------------------------------------//
// Count sends and create steering algorithm selector.
template <class ExecutionSpace>
struct CountSendsAndCreateSteeringAlgorithm;
// CUDA and HIP use atomics.
#ifdef KOKKOS_ENABLE_CUDA
template <>
struct CountSendsAndCreateSteeringAlgorithm<Kokkos::Cuda>
{
using type = CountSendsAndCreateSteeringAtomic;
};
#endif // end KOKKOS_ENABLE_CUDA
#ifdef KOKKOS_ENABLE_HIP
template <>
struct CountSendsAndCreateSteeringAlgorithm<Kokkos::Experimental::HIP>
{
using type = CountSendsAndCreateSteeringAtomic;
};
#endif // end KOKKOS_ENABLE_HIP
#ifdef KOKKOS_ENABLE_SYCL
template <>
struct CountSendsAndCreateSteeringAlgorithm<Kokkos::Experimental::SYCL>
{
using type = CountSendsAndCreateSteeringAtomic;
};
#endif // end KOKKOS_ENABLE_SYCL
#ifdef KOKKOS_ENABLE_OPENMPTARGET
template <>
struct CountSendsAndCreateSteeringAlgorithm<Kokkos::Experimental::OpenMPTarget>
{
using type = CountSendsAndCreateSteeringAtomic;
};
#endif // end KOKKOS_ENABLE_OPENMPTARGET
// The default is to use duplication.
template <class ExecutionSpace>
struct CountSendsAndCreateSteeringAlgorithm
{
using type = CountSendsAndCreateSteeringDuplicated;
};
//---------------------------------------------------------------------------//
// Count sends and generate the steering vector. Atomic version.
template <class ExecutionSpace, class ExportRankView>
auto countSendsAndCreateSteering( ExecutionSpace,
const ExportRankView element_export_ranks,
const int comm_size,
CountSendsAndCreateSteeringAtomic )
-> std::pair<Kokkos::View<int*, typename ExportRankView::memory_space>,
Kokkos::View<typename ExportRankView::size_type*,
typename ExportRankView::memory_space>>
{
using memory_space = typename ExportRankView::memory_space;
using size_type = typename ExportRankView::size_type;
// Create views.
Kokkos::View<int*, memory_space> neighbor_counts( "neighbor_counts",
comm_size );
Kokkos::View<size_type*, memory_space> neighbor_ids(
Kokkos::ViewAllocateWithoutInitializing( "neighbor_ids" ),
element_export_ranks.size() );
// Count the sends and create the steering vector.
// For smaller values of comm_size, use the optimized scratch memory path
// Value of 64 is arbitrary, should be at least 27 to cover 3-d halos?
if ( comm_size <= 64 )
{
constexpr int team_size = 256;
Kokkos::TeamPolicy<ExecutionSpace> team_policy(
( element_export_ranks.size() + team_size - 1 ) / team_size,
team_size );
team_policy = team_policy.set_scratch_size(
0,
Kokkos::PerTeam( sizeof( int ) * ( team_size + 2 * comm_size ) ) );
Kokkos::parallel_for(
"Cabana::CommunicationPlan::countSendsAndCreateSteeringShared",
team_policy,
KOKKOS_LAMBDA(
const typename Kokkos::TeamPolicy<ExecutionSpace>::member_type&
team ) {
// NOTE: `get_shmem` returns shared memory pointers *aligned to
// 8 bytes*, so if `comm_size` is odd we can get erroneously
// padded offsets if we call `get_shmem` separately for each
// shared memory intermediary. Acquiring all the needed scratch
// memory at once then computing pointer offsets by hand avoids
// this issue.
int* scratch = (int*)team.team_shmem().get_shmem(
( team.team_size() + 2 * comm_size ) * sizeof( int ), 0 );
// local neighbor_ids, gives the local offset relative to
// calculated global offset. Size team.team_size() * sizeof(int)
int* local_neighbor_ids = scratch;
// local histogram, `comm_size` in size
int* histo = local_neighbor_ids + team.team_size();
// offset into global array, `comm_size` in size
int* global_offset = histo + comm_size;
// overall element `tid`
const auto tid =
team.team_rank() + team.league_rank() * team.team_size();
// local element
const int local_id = team.team_rank();
// total number of elements, for convenience
const int num_elements = element_export_ranks.size();
// my element export rank
const int my_element_export_rank =
( tid < num_elements ? element_export_ranks( tid ) : -1 );
// cannot outright terminate early b/c threads share work
// if (tid >= num_elements) return;
const bool in_bounds =
tid < num_elements && my_element_export_rank >= 0;
Kokkos::parallel_for(
Kokkos::TeamThreadRange( team, comm_size ),
[&]( const int i ) { histo[i] = 0; } );
// synchronize zeroing
team.team_barrier();
// build local histogram, need to encode num_elements check here
if ( in_bounds )
{
// shared memory atomic add, accumulate into local offset
local_neighbor_ids[local_id] = Kokkos::atomic_fetch_add(
&histo[my_element_export_rank], 1 );
}
// synchronize local histogram build
team.team_barrier();
// reserve space in global array via a loop over neighbor counts
Kokkos::parallel_for(
Kokkos::TeamThreadRange( team, comm_size ),
[&]( const int i )
{
// global memory atomic add, reserves space
global_offset[i] = Kokkos::atomic_fetch_add(
&neighbor_counts( i ), histo[i] );
} );
// synchronize block-stride loop
team.team_barrier();
// and now store to my location
if ( in_bounds )
{
neighbor_ids( tid ) =
global_offset[my_element_export_rank] +
local_neighbor_ids[local_id];
}
} );
}
else
{
// For larger numbers of export ranks (for ex, migration) we use the
// global memory version, though this is a point of future optimization
Kokkos::parallel_for(
"Cabana::CommunicationPlan::countSendsAndCreateSteering",
Kokkos::RangePolicy<ExecutionSpace>( 0,
element_export_ranks.size() ),
KOKKOS_LAMBDA( const size_type i ) {
if ( element_export_ranks( i ) >= 0 )
neighbor_ids( i ) = Kokkos::atomic_fetch_add(
&neighbor_counts( element_export_ranks( i ) ), 1 );
} );
}
Kokkos::fence();
// Return the counts and ids.
return std::make_pair( neighbor_counts, neighbor_ids );
}
//---------------------------------------------------------------------------//
// Count sends and generate the steering vector. Duplicated version.
template <class ExecutionSpace, class ExportRankView>
auto countSendsAndCreateSteering( ExecutionSpace,
const ExportRankView element_export_ranks,
const int comm_size,
CountSendsAndCreateSteeringDuplicated )
-> std::pair<Kokkos::View<int*, typename ExportRankView::memory_space>,
Kokkos::View<typename ExportRankView::size_type*,
typename ExportRankView::memory_space>>
{
using memory_space = typename ExportRankView::memory_space;
using size_type = typename ExportRankView::size_type;
// Create a unique thread token.
Kokkos::Experimental::UniqueToken<
ExecutionSpace, Kokkos::Experimental::UniqueTokenScope::Global>
unique_token;
// Create views.
Kokkos::View<int*, memory_space> neighbor_counts(
Kokkos::ViewAllocateWithoutInitializing( "neighbor_counts" ),
comm_size );
Kokkos::View<size_type*, memory_space> neighbor_ids(
Kokkos::ViewAllocateWithoutInitializing( "neighbor_ids" ),
element_export_ranks.size() );
Kokkos::View<int**, memory_space> neighbor_counts_dup(
"neighbor_counts", unique_token.size(), comm_size );
Kokkos::View<size_type**, memory_space> neighbor_ids_dup(
"neighbor_ids", unique_token.size(), element_export_ranks.size() );
// Compute initial duplicated sends and steering.
Kokkos::parallel_for(
"Cabana::CommunicationPlan::intialCount",
Kokkos::RangePolicy<ExecutionSpace>( 0, element_export_ranks.size() ),
KOKKOS_LAMBDA( const size_type i ) {
if ( element_export_ranks( i ) >= 0 )
{
// Get the thread id.
auto thread_id = unique_token.acquire();
// Do the duplicated fetch-add. If this is a valid element id
// increment the send count for this rank. Add the incremented
// count as the thread-local neighbor id. This is too big by
// one (because we use the prefix increment operator) but we
// want a non-zero value so we can later find which thread
// this element was located on because we are always
// guaranteed a non-zero value. We will subtract this value
// later.
neighbor_ids_dup( thread_id, i ) = ++neighbor_counts_dup(
thread_id, element_export_ranks( i ) );
// Release the thread id.
unique_token.release( thread_id );
}
} );
Kokkos::fence();
// Team policy
using team_policy =
Kokkos::TeamPolicy<ExecutionSpace, Kokkos::Schedule<Kokkos::Dynamic>>;
using index_type = typename team_policy::index_type;
// Compute the send counts for each neighbor rank by reducing across
// the thread duplicates.
Kokkos::parallel_for(
"Cabana::CommunicationPlan::finalCount",
team_policy( neighbor_counts.extent( 0 ), Kokkos::AUTO ),
KOKKOS_LAMBDA( const typename team_policy::member_type& team ) {
// Get the element id.
auto i = team.league_rank();
// Add the thread results.
int thread_counts = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange( team,
neighbor_counts_dup.extent( 0 ) ),
[&]( const index_type thread_id, int& result )
{ result += neighbor_counts_dup( thread_id, i ); },
thread_counts );
neighbor_counts( i ) = thread_counts;
} );
Kokkos::fence();
// Compute the location of each export element in the send buffer of
// its destination rank.
Kokkos::parallel_for(
"Cabana::CommunicationPlan::createSteering",
team_policy( element_export_ranks.size(), Kokkos::AUTO ),
KOKKOS_LAMBDA( const typename team_policy::member_type& team ) {
// Get the element id.
auto i = team.league_rank();
// Only operate on valid elements
if ( element_export_ranks( i ) >= 0 )
{
// Compute the thread id in which we located the element
// during the count phase. Only the thread in which we
// located the element will contribute to the reduction.
index_type dup_thread = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange( team,
neighbor_ids_dup.extent( 0 ) ),
[&]( const index_type thread_id, index_type& result )
{
if ( neighbor_ids_dup( thread_id, i ) > 0 )
result += thread_id;
},
dup_thread );
// Compute the offset of this element in the steering
// vector for its destination rank. Loop through the
// threads up to the thread that found this element in the
// count stage. All thread counts prior to that thread
// will contribute to the offset.
size_type thread_offset = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange( team, dup_thread ),
[&]( const index_type thread_id, size_type& result ) {
result += neighbor_counts_dup(
thread_id, element_export_ranks( i ) );
},
thread_offset );
// Add the thread-local value to the offset where we subtract
// the 1 that we added artificially when we were first
// counting.
neighbor_ids( i ) =
thread_offset + neighbor_ids_dup( dup_thread, i ) - 1;
}
} );
Kokkos::fence();
// Return the counts and ids.
return std::make_pair( neighbor_counts, neighbor_ids );
}
//---------------------------------------------------------------------------//
//! \endcond
} // end namespace Impl
//---------------------------------------------------------------------------//
/*!
\brief Return unique neighbor ranks, with the current rank first.
\param comm MPI communicator.
\param topology MPI neighbor ranks in any order with possible duplication.
\return Unique MPI neighbor ranks, with the current rank first.
*/
inline std::vector<int> getUniqueTopology( MPI_Comm comm,
std::vector<int> topology )
{
auto remove_end = std::remove( topology.begin(), topology.end(), -1 );
std::sort( topology.begin(), remove_end );
auto unique_end = std::unique( topology.begin(), remove_end );
topology.resize( std::distance( topology.begin(), unique_end ) );
// Put this rank first.
int my_rank = -1;
MPI_Comm_rank( comm, &my_rank );
for ( auto& n : topology )
{
if ( n == my_rank )
{
std::swap( n, topology[0] );
break;
}
}
return topology;
}
//---------------------------------------------------------------------------//
/*!
\brief Communication plan base class.
\tparam DeviceType Device type for which the data for this class will be
allocated and where parallel execution will occur.
The communication plan computes how to redistribute elements in a parallel
data structure using MPI. Given a list of data elements on the local MPI
rank and their destination ranks, the communication plan computes which rank
each process is sending and receiving from and how many elements we will
send and receive. In addition, it provides an export steering vector which
describes how to pack the local data to be exported into contiguous send
buffers for each destination rank (in the forward communication plan).
Some nomenclature:
Export - elements we are sending in the forward communication plan.
Import - elements we are receiving in the forward communication plan.
\note If a communication plan does self-sends (i.e. exports and imports data
from its own ranks) then this data is first in the data structure. What this
means is that neighbor 0 is the local rank and the data for that rank that
is being exported will appear first in the steering vector.
*/
template <class MemorySpace>
class CommunicationPlan
{
public:
// FIXME: extracting the self type for backwards compatibility with previous
// template on DeviceType. Should simply be MemorySpace after next release.
//! Memory space.
using memory_space = typename MemorySpace::memory_space;
// FIXME: replace warning with memory space assert after next release.
static_assert( Impl::deprecated( Kokkos::is_device<MemorySpace>() ) );
//! Default device type.
using device_type [[deprecated]] = typename memory_space::device_type;
//! Default execution space.
using execution_space = typename memory_space::execution_space;
// FIXME: extracting the self type for backwards compatibility with previous
// template on DeviceType. Should simply be memory_space::size_type after
// next release.
//! Size type.
using size_type = typename memory_space::memory_space::size_type;
/*!
\brief Constructor.
\param comm The MPI communicator over which the distributor is defined.
*/
CommunicationPlan( MPI_Comm comm )
{
_comm_ptr.reset(
// Duplicate the communicator and store in a std::shared_ptr so that
// all copies point to the same object
[comm]()
{
auto p = std::make_unique<MPI_Comm>();
MPI_Comm_dup( comm, p.get() );
return p.release();
}(),
// Custom deleter to mark the communicator for deallocation
[]( MPI_Comm* p )
{
MPI_Comm_free( p );
delete p;
} );
}
/*!
\brief Get the MPI communicator.
*/
MPI_Comm comm() const { return *_comm_ptr; }
/*!
\brief Get the number of neighbor ranks that this rank will communicate
with.
\return The number of MPI ranks that will exchange data with this rank.
*/
int numNeighbor() const { return _neighbors.size(); }
/*!
\brief Given a local neighbor id get its rank in the MPI communicator.
\param neighbor The local id of the neighbor to get the rank for.
\return The MPI rank of the neighbor with the given local id.
*/
int neighborRank( const int neighbor ) const
{
return _neighbors[neighbor];
}
/*!
\brief Get the number of elements this rank will export to a given
neighbor.
\param neighbor The local id of the neighbor to get the number of
exports for.
\return The number of elements this rank will export to the neighbor with
the given local id.
*/
std::size_t numExport( const int neighbor ) const
{
return _num_export[neighbor];
}
/*!
\brief Get the total number of exports this rank will do.
\return The total number of elements this rank will export to its
neighbors.
*/
std::size_t totalNumExport() const { return _total_num_export; }
/*!
\brief Get the number of elements this rank will import from a given
neighbor.
\param neighbor The local id of the neighbor to get the number of
imports for.
\return The number of elements this rank will import from the neighbor
with the given local id.
*/
std::size_t numImport( const int neighbor ) const
{
return _num_import[neighbor];
}
/*!
\brief Get the total number of imports this rank will do.
\return The total number of elements this rank will import from its
neighhbors.
*/
std::size_t totalNumImport() const { return _total_num_import; }
/*!
\brief Get the number of export elements.
\return The number of export elements.
Whenever the communication plan is applied, this is the total number of
elements expected to be input on the sending ranks (in the forward
communication plan). This will be different than the number returned by
totalNumExport() if some of the export ranks used in the construction
are -1 and therefore will not particpate in an export operation.
*/
std::size_t exportSize() const { return _num_export_element; }
/*!
\brief Get the steering vector for the exports.
\return The steering vector for the exports.
The steering vector places exports in contiguous chunks by destination
rank. The chunks are in consecutive order based on the local neighbor id
(i.e. all elements going to neighbor with local id 0 first, then all
elements going to neighbor with local id 1, etc.).
*/
Kokkos::View<std::size_t*, memory_space> getExportSteering() const
{
return _export_steering;
}
// The functions in the public block below would normally be protected but
// we make them public to allow using private class data in CUDA kernels
// with lambda functions.
public:
/*!
\brief Neighbor and export rank creator. Use this when you already know
which ranks neighbor each other (i.e. every rank already knows who they
will be sending and receiving from) as it will be more efficient. In
this case you already know the topology of the point-to-point
communication but not how much data to send to and receive from the
neighbors.
\param exec_space Kokkos execution space.
\param element_export_ranks The destination rank in the target
decomposition of each locally owned element in the source
decomposition. Each element will have one unique destination to which it
will be exported. This export rank may be any one of the listed neighbor
ranks which can include the calling rank. An export rank of -1 will
signal that this element is *not* to be exported and will be ignored in
the data migration. The input is expected to be a Kokkos view or Cabana
slice in the same memory space as the communication plan.
\param neighbor_ranks List of ranks this rank will send to and receive
from. This list can include the calling rank. This is effectively a
description of the topology of the point-to-point communication
plan. Only the unique elements in this list are used.
\return The location of each export element in the send buffer for its
given neighbor.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
\note For elements that you do not wish to export, use an export rank of
-1 to signal that this element is *not* to be exported and will be
ignored in the data migration. In other words, this element will be
*completely* removed in the new decomposition. If the data is staying on
this rank, just use this rank as the export destination and the data
will be efficiently migrated.
*/
template <class ExecutionSpace, class ViewType>
Kokkos::View<size_type*, memory_space>
createFromExportsAndTopology( ExecutionSpace exec_space,
const ViewType& element_export_ranks,
const std::vector<int>& neighbor_ranks )
{
static_assert( is_accessible_from<memory_space, ExecutionSpace>{}, "" );
// Store the number of export elements.
_num_export_element = element_export_ranks.size();
// Store the unique neighbors (this rank first).
_neighbors = getUniqueTopology( comm(), neighbor_ranks );
int num_n = _neighbors.size();
// Get the size of this communicator.
int comm_size = -1;
MPI_Comm_size( comm(), &comm_size );
// Get the MPI rank we are currently on.
int my_rank = -1;
MPI_Comm_rank( comm(), &my_rank );
// Pick an mpi tag for communication. This object has it's own
// communication space so any mpi tag will do.
const int mpi_tag = 1221;
// Initialize import/export sizes.
_num_export.assign( num_n, 0 );
_num_import.assign( num_n, 0 );
// Count the number of sends this rank will do to other ranks. Keep
// track of which slot we get in our neighbor's send buffer.
auto counts_and_ids = Impl::countSendsAndCreateSteering(
exec_space, element_export_ranks, comm_size,
typename Impl::CountSendsAndCreateSteeringAlgorithm<
ExecutionSpace>::type() );
// Copy the counts to the host.
auto neighbor_counts_host = Kokkos::create_mirror_view_and_copy(
Kokkos::HostSpace(), counts_and_ids.first );
// Get the export counts.
for ( int n = 0; n < num_n; ++n )
_num_export[n] = neighbor_counts_host( _neighbors[n] );
// Post receives for the number of imports we will get.
std::vector<MPI_Request> requests;
requests.reserve( num_n );
for ( int n = 0; n < num_n; ++n )
if ( my_rank != _neighbors[n] )
{
requests.push_back( MPI_Request() );
MPI_Irecv( &_num_import[n], 1, MPI_UNSIGNED_LONG, _neighbors[n],
mpi_tag, comm(), &( requests.back() ) );
}
else
_num_import[n] = _num_export[n];
// Send the number of exports to each of our neighbors.
for ( int n = 0; n < num_n; ++n )
if ( my_rank != _neighbors[n] )
MPI_Send( &_num_export[n], 1, MPI_UNSIGNED_LONG, _neighbors[n],
mpi_tag, comm() );
// Wait on receives.
std::vector<MPI_Status> status( requests.size() );
const int ec =
MPI_Waitall( requests.size(), requests.data(), status.data() );
if ( MPI_SUCCESS != ec )
throw std::logic_error( "Failed MPI Communication" );
// Get the total number of imports/exports.
_total_num_export =
std::accumulate( _num_export.begin(), _num_export.end(), 0 );
_total_num_import =
std::accumulate( _num_import.begin(), _num_import.end(), 0 );
// Barrier before continuing to ensure synchronization.
MPI_Barrier( comm() );
// Return the neighbor ids.
return counts_and_ids.second;
}
/*!
\brief Neighbor and export rank creator. Use this when you already know
which ranks neighbor each other (i.e. every rank already knows who they
will be sending and receiving from) as it will be more efficient. In
this case you already know the topology of the point-to-point
communication but not how much data to send to and receive from the
neighbors.
\param element_export_ranks The destination rank in the target
decomposition of each locally owned element in the source
decomposition. Each element will have one unique destination to which it
will be exported. This export rank may be any one of the listed neighbor
ranks which can include the calling rank. An export rank of -1 will
signal that this element is *not* to be exported and will be ignored in
the data migration. The input is expected to be a Kokkos view or Cabana
slice in the same memory space as the communication plan.
\param neighbor_ranks List of ranks this rank will send to and receive
from. This list can include the calling rank. This is effectively a
description of the topology of the point-to-point communication
plan. Only the unique elements in this list are used.
\return The location of each export element in the send buffer for its
given neighbor.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
\note For elements that you do not wish to export, use an export rank of
-1 to signal that this element is *not* to be exported and will be
ignored in the data migration. In other words, this element will be
*completely* removed in the new decomposition. If the data is staying on
this rank, just use this rank as the export destination and the data
will be efficiently migrated.
*/
template <class ViewType>
Kokkos::View<size_type*, memory_space>
createFromExportsAndTopology( const ViewType& element_export_ranks,
const std::vector<int>& neighbor_ranks )
{
// Use the default execution space.
return createFromExportsAndTopology(
execution_space{}, element_export_ranks, neighbor_ranks );
}
/*!
\brief Export rank creator. Use this when you don't know who you will
receiving from - only who you are sending to. This is less efficient
than if we already knew who our neighbors were because we have to
determine the topology of the point-to-point communication first.
\param exec_space Kokkos execution space.
\param element_export_ranks The destination rank in the target
decomposition of each locally owned element in the source
decomposition. Each element will have one unique destination to which it
will be exported. This export rank may any one of the listed neighbor
ranks which can include the calling rank. An export rank of -1 will
signal that this element is *not* to be exported and will be ignored in
the data migration. The input is expected to be a Kokkos view or Cabana
slice in the same memory space as the communication plan.
\return The location of each export element in the send buffer for its
given neighbor.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
\note For elements that you do not wish to export, use an export rank of
-1 to signal that this element is *not* to be exported and will be
ignored in the data migration. In other words, this element will be
*completely* removed in the new decomposition. If the data is staying on
this rank, just use this rank as the export destination and the data
will be efficiently migrated.
*/
template <class ExecutionSpace, class ViewType>
Kokkos::View<size_type*, memory_space>
createFromExportsOnly( ExecutionSpace exec_space,
const ViewType& element_export_ranks )
{
static_assert( is_accessible_from<memory_space, ExecutionSpace>{}, "" );
// Store the number of export elements.
_num_export_element = element_export_ranks.size();
// Get the size of this communicator.
int comm_size = -1;
MPI_Comm_size( comm(), &comm_size );
// Get the MPI rank we are currently on.
int my_rank = -1;
MPI_Comm_rank( comm(), &my_rank );
// Pick an mpi tag for communication. This object has it's own
// communication space so any mpi tag will do.
const int mpi_tag = 1221;
// Count the number of sends this rank will do to other ranks. Keep
// track of which slot we get in our neighbor's send buffer.
auto counts_and_ids = Impl::countSendsAndCreateSteering(
exec_space, element_export_ranks, comm_size,
typename Impl::CountSendsAndCreateSteeringAlgorithm<
ExecutionSpace>::type() );
// Copy the counts to the host.
auto neighbor_counts_host = Kokkos::create_mirror_view_and_copy(
Kokkos::HostSpace(), counts_and_ids.first );
// Extract the export ranks and number of exports and then flag the
// send ranks.
_neighbors.clear();
_num_export.clear();
_total_num_export = 0;
for ( int r = 0; r < comm_size; ++r )
if ( neighbor_counts_host( r ) > 0 )
{
_neighbors.push_back( r );
_num_export.push_back( neighbor_counts_host( r ) );
_total_num_export += neighbor_counts_host( r );
neighbor_counts_host( r ) = 1;
}
// Get the number of export ranks and initially allocate the import
// sizes.
int num_export_rank = _neighbors.size();
_num_import.assign( num_export_rank, 0 );
// If we are sending to ourself put that one first in the neighbor
// list and assign the number of imports to be the number of exports.
bool self_send = false;
for ( int n = 0; n < num_export_rank; ++n )
if ( _neighbors[n] == my_rank )
{
std::swap( _neighbors[n], _neighbors[0] );
std::swap( _num_export[n], _num_export[0] );
_num_import[0] = _num_export[0];
self_send = true;
break;
}
// Determine how many total import ranks each neighbor has.
int num_import_rank = -1;
std::vector<int> recv_counts( comm_size, 1 );
MPI_Reduce_scatter( neighbor_counts_host.data(), &num_import_rank,
recv_counts.data(), MPI_INT, MPI_SUM, comm() );
if ( self_send )
--num_import_rank;
// Post the expected number of receives and indicate we might get them
// from any rank.
std::vector<std::size_t> import_sizes( num_import_rank );
std::vector<MPI_Request> requests( num_import_rank );
for ( int n = 0; n < num_import_rank; ++n )
MPI_Irecv( &import_sizes[n], 1, MPI_UNSIGNED_LONG, MPI_ANY_SOURCE,
mpi_tag, comm(), &requests[n] );
// Do blocking sends. Dont do any self sends.
int self_offset = ( self_send ) ? 1 : 0;
for ( int n = self_offset; n < num_export_rank; ++n )
MPI_Send( &_num_export[n], 1, MPI_UNSIGNED_LONG, _neighbors[n],
mpi_tag, comm() );
// Wait on non-blocking receives.
std::vector<MPI_Status> status( requests.size() );
const int ec =
MPI_Waitall( requests.size(), requests.data(), status.data() );
if ( MPI_SUCCESS != ec )
throw std::logic_error( "Failed MPI Communication" );
// Compute the total number of imports.
_total_num_import =
std::accumulate( import_sizes.begin(), import_sizes.end(),
( self_send ) ? _num_import[0] : 0 );
// Extract the imports. If we did self sends we already know what
// imports we got from that.
for ( int i = 0; i < num_import_rank; ++i )
{
// Get the message source.
const auto source = status[i].MPI_SOURCE;
// See if the neighbor we received stuff from was someone we also
// sent stuff to.
auto found_neighbor =
std::find( _neighbors.begin(), _neighbors.end(), source );
// If this is a new neighbor (i.e. someone we didn't send anything
// to) record this.
if ( found_neighbor == std::end( _neighbors ) )
{
_neighbors.push_back( source );
_num_import.push_back( import_sizes[i] );
_num_export.push_back( 0 );
}
// Otherwise if we already sent something to this neighbor that
// means we already have a neighbor/export entry. Just assign the
// import entry for that neighbor.
else
{
auto n = std::distance( _neighbors.begin(), found_neighbor );
_num_import[n] = import_sizes[i];
}
}
// Barrier before continuing to ensure synchronization.
MPI_Barrier( comm() );
// Return the neighbor ids.
return counts_and_ids.second;
}
/*!
\brief Export rank creator. Use this when you don't know who you will
receiving from - only who you are sending to. This is less efficient
than if we already knew who our neighbors were because we have to
determine the topology of the point-to-point communication first.
\param element_export_ranks The destination rank in the target
decomposition of each locally owned element in the source
decomposition. Each element will have one unique destination to which it
will be exported. This export rank may any one of the listed neighbor
ranks which can include the calling rank. An export rank of -1 will
signal that this element is *not* to be exported and will be ignored in
the data migration. The input is expected to be a Kokkos view or Cabana
slice in the same memory space as the communication plan.
\return The location of each export element in the send buffer for its
given neighbor.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
\note For elements that you do not wish to export, use an export rank of
-1 to signal that this element is *not* to be exported and will be
ignored in the data migration. In other words, this element will be
*completely* removed in the new decomposition. If the data is staying on
this rank, just use this rank as the export destination and the data
will be efficiently migrated.
*/
template <class ViewType>
Kokkos::View<size_type*, memory_space>
createFromExportsOnly( const ViewType& element_export_ranks )
{
// Use the default execution space.
return createFromExportsOnly( execution_space{}, element_export_ranks );
}
/*!
\brief Create the export steering vector.
Creates an array describing which export element ids are moved to which
location in the send buffer of the communication plan. Ordered such that
if a rank sends to itself then those values come first.
\param neighbor_ids The id of each element in the neighbor send buffers.
\param element_export_ranks The ranks to which we are exporting each
element. We use this to build the steering vector. The input is expected
to be a Kokkos view or Cabana slice in the same memory space as the
communication plan.
*/
template <class PackViewType, class RankViewType>
void createExportSteering( const PackViewType& neighbor_ids,
const RankViewType& element_export_ranks )
{
// passing in element_export_ranks here as a dummy argument.
createSteering( true, neighbor_ids, element_export_ranks,
element_export_ranks );
}
/*!
\brief Create the export steering vector.
Creates an array describing which export element ids are moved to which
location in the contiguous send buffer of the communication plan. Ordered
such that if a rank sends to itself then those values come first.
\param neighbor_ids The id of each element in the neighbor send buffers.
\param element_export_ranks The ranks to which we are exporting each
element. We use this to build the steering vector. The input is expected
to be a Kokkos view or Cabana slice in the same memory space as the
communication plan.
\param element_export_ids The local ids of the elements to be
exported. This corresponds with the export ranks vector and must be the
same length if defined. The input is expected to be a Kokkos view or
Cabana slice in the same memory space as the communication plan.
*/
template <class PackViewType, class RankViewType, class IdViewType>
void createExportSteering( const PackViewType& neighbor_ids,
const RankViewType& element_export_ranks,
const IdViewType& element_export_ids )
{
createSteering( false, neighbor_ids, element_export_ranks,
element_export_ids );
}
//! \cond Impl
// Create the export steering vector.
template <class ExecutionSpace, class PackViewType, class RankViewType,
class IdViewType>
void createSteering( ExecutionSpace, const bool use_iota,
const PackViewType& neighbor_ids,
const RankViewType& element_export_ranks,
const IdViewType& element_export_ids )
{
static_assert( is_accessible_from<memory_space, ExecutionSpace>{}, "" );
if ( !use_iota &&
( element_export_ids.size() != element_export_ranks.size() ) )
throw std::runtime_error( "Export ids and ranks different sizes!" );
// Get the size of this communicator.
int comm_size = -1;
MPI_Comm_size( *_comm_ptr, &comm_size );
// Calculate the steering offsets via exclusive prefix sum for the
// exports.
int num_n = _neighbors.size();
std::vector<std::size_t> offsets( num_n, 0.0 );