-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathCabana_Halo.hpp
913 lines (764 loc) · 36.1 KB
/
Cabana_Halo.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
/****************************************************************************
* 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_Halo.hpp
\brief Multi-node particle scatter/gather
*/
#ifndef CABANA_HALO_HPP
#define CABANA_HALO_HPP
#include <Cabana_AoSoA.hpp>
#include <Cabana_CommunicationPlan.hpp>
#include <Cabana_Slice.hpp>
#include <Kokkos_Core.hpp>
#include <Kokkos_Profiling_ScopedRegion.hpp>
#include <mpi.h>
#include <exception>
#include <vector>
namespace Cabana
{
//---------------------------------------------------------------------------//
/*!
\brief A communication plan for scattering and gathering of ghosted data.
\tparam MemorySpace Kokkos memory space in which data for this class will be
allocated.
The halo allows for scatter and gather operations between locally-owned and
ghosted data. All data in the Halo (e.g. export and import data) is from the
point of view of the forward *GATHER* operation such that, for example, the
number of exports is the number of exports in the gather and the number of
imports is the number of imports in the gather. The reverse *SCATTER*
operation sends the ghosted data back the the uniquely-owned decomposition
and resolves collisions. Based on input for the forward communication plan
(where local data will be sent) the local number of ghosts is computed. Some
nomenclature:
Export - the local data we uniquely own that we will send to other ranks for
those ranks to be used as ghosts. Export is used in the context of the
forward communication plan (the gather).
Import - the ghost data that we get from other ranks. The rank we get a
ghost from is the unique owner of that data. Import is used in the context
of the forward communication plan (the gather).
*/
template <class MemorySpace>
class Halo : public CommunicationPlan<MemorySpace>
{
public:
/*!
\brief Neighbor and export rank constructor. Use this when you already
know which ranks neighbor each other (i.e. every rank already knows who
they will be exporting to 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 and receive
from the neighbors.
\tparam IdViewType The container type for the export element ids. This
container type can be either a Kokkos View or a Cabana Slice.
\tparam RankViewType The container type for the export element
ranks. This container type can be either a Kokkos View or a Cabana
Slice.
\param comm The MPI communicator over which the halo is defined.
\param num_local The number of locally-owned elements on this rank.
\param element_export_ids The local ids of the elements that will be
exported to other ranks to be used as ghosts. Element ids may be
repeated in this list if they are sent to multiple destinations. Must be
the same length as element_export_ranks. The input is expected to be a
Kokkos view or Cabana slice in the same memory space as the
communication plan.
\param element_export_ranks The ranks to which we will send each element
in element_export_ids. In this case each rank must be one of the
neighbor ranks. Must be the same length as element_export_ids. A rank is
allowed to send to itself. 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. The elements in this list must be unique.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
*/
template <class IdViewType, class RankViewType>
Halo( MPI_Comm comm, const std::size_t num_local,
const IdViewType& element_export_ids,
const RankViewType& element_export_ranks,
const std::vector<int>& neighbor_ranks )
: CommunicationPlan<MemorySpace>( comm )
, _num_local( num_local )
{
if ( element_export_ids.size() != element_export_ranks.size() )
throw std::runtime_error( "Export ids and ranks different sizes!" );
auto neighbor_ids = this->createFromExportsAndTopology(
element_export_ranks, neighbor_ranks );
this->createExportSteering( neighbor_ids, element_export_ranks,
element_export_ids );
}
/*!
\brief Export rank constructor. 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.
\tparam IdViewType The container type for the export element ids. This
container type can be either a Kokkos View or a Cabana Slice.
\tparam RankViewType The container type for the export element
ranks. This container type can be either a Kokkos View or a Cabana
Slice.
\param comm The MPI communicator over which the halo is defined.
\param num_local The number of locally-owned elements on this rank.
\param element_export_ids The local ids of the elements that will be
sent to other ranks to be used as ghosts. Element ids may be repeated in
this list if they are sent to multiple destinations. Must be the same
length as element_export_ranks. The input is expected to be a Kokkos
view or Cabana slice in the same memory space as the communication plan.
\param element_export_ranks The ranks to which we will export each element
in element_export_ids. Must be the same length as
element_export_ids. The neighbor ranks will be determined from this
list. A rank is allowed to send to itself. The input is expected to be a
Kokkos view or Cabana slice in the same memory space as the
communication plan.
\note Calling this function completely updates the state of this object
and invalidates the previous state.
*/
template <class IdViewType, class RankViewType>
Halo( MPI_Comm comm, const std::size_t num_local,
const IdViewType& element_export_ids,
const RankViewType& element_export_ranks )
: CommunicationPlan<MemorySpace>( comm )
, _num_local( num_local )
{
if ( element_export_ids.size() != element_export_ranks.size() )
throw std::runtime_error( "Export ids and ranks different sizes!" );
auto neighbor_ids = this->createFromExportsOnly( element_export_ranks );
this->createExportSteering( neighbor_ids, element_export_ranks,
element_export_ids );
}
/*!
\brief Get the number of elements locally owned by this rank.
\return THe number of elements locally owned by this rank.
*/
std::size_t numLocal() const { return _num_local; }
/*!
\brief Get the number of ghost elements this rank. Use this to resize a
data structure for scatter/gather operations. For use with scatter
gather, a data structure should be of size numLocal() + numGhost().
\return The number of ghosted elements on this rank.
*/
std::size_t numGhost() const { return this->totalNumImport(); }
private:
std::size_t _num_local;
};
//---------------------------------------------------------------------------//
//! \cond Impl
template <typename>
struct is_halo_impl : public std::false_type
{
};
template <typename MemorySpace>
struct is_halo_impl<Halo<MemorySpace>> : public std::true_type
{
};
//! \endcond
//! Halo static type checker.
template <class T>
struct is_halo : public is_halo_impl<typename std::remove_cv<T>::type>::type
{
};
/*!
\brief Ensure the particle size matches the total halo (local and ghost) size.
\param halo The halo that will be used for the gather. Used to query import
and export sizes.
\param particles The particle data (either AoSoA or slice). Used to query the
total size.
*/
template <class Halo, class ParticleData>
bool haloCheckValidSize(
const Halo& halo, const ParticleData& particles,
typename std::enable_if<( is_halo<Halo>::value ), int>::type* = 0 )
{
// Check that the data is the right size.
return ( particles.size() == halo.numLocal() + halo.numGhost() );
}
template <class HaloType, class AoSoAType, class SFINAE = void>
class Gather;
//---------------------------------------------------------------------------//
/*!
\brief Synchronously gather data from the local decomposition to the ghosts
using the halo forward communication plan. AoSoA version. This is a
uniquely-owned to multiply-owned communication.
A gather sends data from a locally owned elements to one or many ranks on
which they exist as ghosts. A locally owned element may be sent to as many
ranks as desired to be used as a ghost on those ranks. The value of the
element in the locally owned decomposition will be the value assigned to the
element in the ghosted decomposition.
*/
template <class HaloType, class AoSoAType>
class Gather<HaloType, AoSoAType,
typename std::enable_if<is_aosoa<AoSoAType>::value>::type>
: public CommunicationData<HaloType, CommunicationDataAoSoA<AoSoAType>>
{
public:
static_assert( is_halo<HaloType>::value, "" );
//! Base type.
using base_type =
CommunicationData<HaloType, CommunicationDataAoSoA<AoSoAType>>;
//! Communication plan type (Halo)
using plan_type = typename base_type::plan_type;
//! Kokkos execution space.
using execution_space = typename base_type::execution_space;
//! Kokkos memory space.
using memory_space = typename base_type::memory_space;
//! Communication data type.
using data_type = typename base_type::data_type;
//! Communication buffer type.
using buffer_type = typename base_type::buffer_type;
/*!
\param halo The Halo to be used for the gather.
\param aosoa The AoSoA on which to perform the gather. The AoSoA should
have a size equivalent to halo.numGhost() + halo.numLocal(). The locally
owned elements are expected to appear first (i.e. in the first
halo.numLocal() elements) and the ghosted elements are expected to appear
second (i.e. in the next halo.numGhost() elements()).
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
Gather( HaloType halo, AoSoAType aosoa, const double overallocation = 1.0 )
: base_type( halo, aosoa, overallocation )
{
reserve( _halo, aosoa );
}
//! Total gather send size for this rank.
auto totalSend() { return _halo.totalNumExport(); }
//! Total gather receive size for this rank.
auto totalReceive() { return _halo.totalNumImport(); }
/*!
\brief Perform the gather operation.
*/
template <class ExecutionSpace>
void apply( ExecutionSpace )
{
Kokkos::Profiling::ScopedRegion region( "Cabana::gather" );
// Get the buffers and particle data (local copies for lambdas below).
auto send_buffer = this->getSendBuffer();
auto recv_buffer = this->getReceiveBuffer();
auto aosoa = this->getData();
// Get the steering vector for the sends.
auto steering = _halo.getExportSteering();
// Gather from the local data into a tuple-contiguous send buffer.
auto gather_send_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
send_buffer( i ) = aosoa.getTuple( steering( i ) );
};
Kokkos::RangePolicy<ExecutionSpace> send_policy( 0, _send_size );
Kokkos::parallel_for( "Cabana::gather::gather_send_buffer", send_policy,
gather_send_buffer_func );
Kokkos::fence();
// The halo has it's own communication space so choose any mpi tag.
const int mpi_tag = 2345;
// Post non-blocking receives.
int num_n = _halo.numNeighbor();
std::vector<MPI_Request> requests( num_n );
std::pair<std::size_t, std::size_t> recv_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
recv_range.second = recv_range.first + _halo.numImport( n );
auto recv_subview = Kokkos::subview( recv_buffer, recv_range );
MPI_Irecv( recv_subview.data(),
recv_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm(),
&( requests[n] ) );
recv_range.first = recv_range.second;
}
// Do blocking sends.
std::pair<std::size_t, std::size_t> send_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
send_range.second = send_range.first + _halo.numExport( n );
auto send_subview = Kokkos::subview( send_buffer, send_range );
MPI_Send( send_subview.data(),
send_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm() );
send_range.first = send_range.second;
}
// Wait on non-blocking receives.
std::vector<MPI_Status> status( num_n );
const int ec =
MPI_Waitall( requests.size(), requests.data(), status.data() );
if ( MPI_SUCCESS != ec )
throw std::logic_error( "Failed MPI Communication" );
// Extract the receive buffer into the ghosted elements.
std::size_t num_local = _halo.numLocal();
auto extract_recv_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
std::size_t ghost_idx = i + num_local;
aosoa.setTuple( ghost_idx, recv_buffer( i ) );
};
Kokkos::RangePolicy<ExecutionSpace> recv_policy( 0, _recv_size );
Kokkos::parallel_for( "Cabana::gather::extract_recv_buffer",
recv_policy, extract_recv_buffer_func );
Kokkos::fence();
// Barrier before completing to ensure synchronization.
MPI_Barrier( _halo.comm() );
}
void apply() override { apply( execution_space{} ); }
/*!
\brief Reserve new buffers as needed and update the halo and AoSoA data.
\param halo The Halo to be used for the gather.
\param aosoa The AoSoA on which to perform the gather.
*/
void reserve( const HaloType& halo, AoSoAType& aosoa )
{
if ( !haloCheckValidSize( halo, aosoa ) )
throw std::runtime_error( "AoSoA is the wrong size for gather!" );
this->reserveImpl( halo, aosoa, totalSend(), totalReceive() );
}
/*!
\brief Reserve new buffers as needed and update the halo and AoSoA data.
\param halo The Halo to be used for the gather.
\param aosoa The AoSoA on which to perform the gather.
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
void reserve( const HaloType& halo, AoSoAType& aosoa,
const double overallocation )
{
if ( !haloCheckValidSize( halo, aosoa ) )
throw std::runtime_error( "AoSoA is the wrong size for gather!" );
this->reserveImpl( halo, aosoa, totalSend(), totalReceive(),
overallocation );
}
private:
plan_type _halo = base_type::_comm_plan;
using base_type::_recv_size;
using base_type::_send_size;
};
/*!
\brief Synchronously gather data from the local decomposition to the ghosts
using the halo forward communication plan. AoSoA version. This is a
uniquely-owned to multiply-owned communication.
A gather sends data from a locally owned elements to one or many ranks on
which they exist as ghosts. A locally owned element may be sent to as many
ranks as desired to be used as a ghost on those ranks. The value of the
element in the locally owned decomposition will be the value assigned to the
element in the ghosted decomposition.
*/
template <class HaloType, class SliceType>
class Gather<HaloType, SliceType,
typename std::enable_if<is_slice<SliceType>::value>::type>
: public CommunicationData<HaloType, CommunicationDataSlice<SliceType>>
{
public:
static_assert( is_halo<HaloType>::value, "" );
//! Base type.
using base_type =
CommunicationData<HaloType, CommunicationDataSlice<SliceType>>;
//! Communication plan type (Halo)
using plan_type = typename base_type::plan_type;
//! Kokkos execution space.
using execution_space = typename base_type::execution_space;
//! Kokkos memory space.
using memory_space = typename base_type::memory_space;
//! Communication data type.
using data_type = typename base_type::data_type;
//! Communication buffer type.
using buffer_type = typename base_type::buffer_type;
/*!
\param halo The Halo to be used for the gather.
\param slice The slice on which to perform the gather. The slice should
have a size equivalent to halo.numGhost() + halo.numLocal(). The locally
owned elements are expected to appear first (i.e. in the first
halo.numLocal() elements) and the ghosted elements are expected to appear
second (i.e. in the next halo.numGhost() elements()).
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
Gather( HaloType halo, SliceType slice, const double overallocation = 1.0 )
: base_type( halo, slice, overallocation )
{
reserve( _halo, slice );
}
//! Total gather send size for this rank.
auto totalSend() { return _halo.totalNumExport(); }
//! Total gather receive size for this rank.
auto totalReceive() { return _halo.totalNumImport(); }
/*!
\brief Perform the gather operation.
*/
template <class ExecutionSpace>
void apply( ExecutionSpace )
{
Kokkos::Profiling::ScopedRegion region( "Cabana::gather" );
// Get the buffers (local copies for lambdas below).
auto send_buffer = this->getSendBuffer();
auto recv_buffer = this->getReceiveBuffer();
auto slice = this->getData();
// Get the number of components in the slice.
std::size_t num_comp = this->getSliceComponents();
// Get the raw slice data.
auto slice_data = slice.data();
// Get the steering vector for the sends.
auto steering = _halo.getExportSteering();
// Gather from the local data into a tuple-contiguous send buffer.
auto gather_send_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
auto s = SliceType::index_type::s( steering( i ) );
auto a = SliceType::index_type::a( steering( i ) );
std::size_t slice_offset = s * slice.stride( 0 ) + a;
for ( std::size_t n = 0; n < num_comp; ++n )
send_buffer( i, n ) =
slice_data[slice_offset + n * SliceType::vector_length];
};
Kokkos::RangePolicy<ExecutionSpace> send_policy( 0, _send_size );
Kokkos::parallel_for( "Cabana::gather::gather_send_buffer", send_policy,
gather_send_buffer_func );
Kokkos::fence();
// The halo has it's own communication space so choose any mpi tag.
const int mpi_tag = 2345;
// Post non-blocking receives.
int num_n = _halo.numNeighbor();
std::vector<MPI_Request> requests( num_n );
std::pair<std::size_t, std::size_t> recv_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
recv_range.second = recv_range.first + _halo.numImport( n );
auto recv_subview =
Kokkos::subview( recv_buffer, recv_range, Kokkos::ALL );
MPI_Irecv( recv_subview.data(),
recv_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm(),
&( requests[n] ) );
recv_range.first = recv_range.second;
}
// Do blocking sends.
std::pair<std::size_t, std::size_t> send_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
send_range.second = send_range.first + _halo.numExport( n );
auto send_subview =
Kokkos::subview( send_buffer, send_range, Kokkos::ALL );
MPI_Send( send_subview.data(),
send_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm() );
send_range.first = send_range.second;
}
// Wait on non-blocking receives.
std::vector<MPI_Status> status( num_n );
const int ec =
MPI_Waitall( requests.size(), requests.data(), status.data() );
if ( MPI_SUCCESS != ec )
throw std::logic_error( "Failed MPI Communication" );
// Extract the receive buffer into the ghosted elements.
std::size_t num_local = _halo.numLocal();
auto extract_recv_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
std::size_t ghost_idx = i + num_local;
auto s = SliceType::index_type::s( ghost_idx );
auto a = SliceType::index_type::a( ghost_idx );
std::size_t slice_offset = s * slice.stride( 0 ) + a;
for ( std::size_t n = 0; n < num_comp; ++n )
slice_data[slice_offset + SliceType::vector_length * n] =
recv_buffer( i, n );
};
Kokkos::RangePolicy<ExecutionSpace> recv_policy( 0, _recv_size );
Kokkos::parallel_for( "Cabana::gather::extract_recv_buffer",
recv_policy, extract_recv_buffer_func );
Kokkos::fence();
// Barrier before completing to ensure synchronization.
MPI_Barrier( _halo.comm() );
}
void apply() override { apply( execution_space{} ); }
/*!
\brief Reserve new buffers as needed and update the halo and slice data.
\param halo The Halo to be used for the gather.
\param slice The slice on which to perform the gather.
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
void reserve( const HaloType& halo, const SliceType& slice,
const double overallocation )
{
if ( !haloCheckValidSize( halo, slice ) )
throw std::runtime_error( "AoSoA is the wrong size for gather!" );
this->reserveImpl( halo, slice, totalSend(), totalReceive(),
overallocation );
}
/*!
\brief Reserve new buffers as needed and update the halo and slice data.
\param halo The Halo to be used for the gather.
\param slice The slice on which to perform the gather.
*/
void reserve( const HaloType& halo, const SliceType& slice )
{
if ( !haloCheckValidSize( halo, slice ) )
throw std::runtime_error( "AoSoA is the wrong size for gather!" );
this->reserveImpl( halo, slice, totalSend(), totalReceive() );
}
private:
plan_type _halo = base_type::_comm_plan;
using base_type::_recv_size;
using base_type::_send_size;
};
//---------------------------------------------------------------------------//
/*!
\brief Create the gather.
\param halo The halo to use for the gather.
\param data The data on which to perform the gather. The slice should have a
size equivalent to halo.numGhost() + halo.numLocal(). The locally owned
elements are expected to appear first (i.e. in the first halo.numLocal()
elements) and the ghosted elements are expected to appear second (i.e. in the
next halo.numGhost() elements()).
\param overallocation An optional factor to keep extra space in the buffers to
avoid frequent resizing.
\return Gather
*/
template <class HaloType, class ParticleDataType>
auto createGather( const HaloType& halo, const ParticleDataType& data,
const double overallocation = 1.0 )
{
return Gather<HaloType, ParticleDataType>( halo, data, overallocation );
}
//---------------------------------------------------------------------------//
/*!
\brief Synchronously gather data from the local decomposition to the
ghosts using the halo forward communication plan. Slice version. This is a
uniquely-owned to multiply-owned communication.
\note This routine allocates send and receive buffers internally. This is
often not performant due to frequent buffer reallocations - consider creating
and reusing Gather instead.
\param halo The halo to use for the gather.
\param data The data on which to perform the gather. The slice should
have a size equivalent to halo.numGhost() + halo.numLocal(). The locally
owned elements are expected to appear first (i.e. in the first
halo.numLocal() elements) and the ghosted elements are expected to appear
second (i.e. in the next halo.numGhost() elements()).
*/
template <class HaloType, class ParticleDataType>
void gather( const HaloType& halo, ParticleDataType& data )
{
auto gather = createGather( halo, data );
gather.apply();
}
/**********
* SCATTER *
**********/
//---------------------------------------------------------------------------//
/*!
\brief Synchronously scatter data from the ghosts to the local decomposition
of a slice using the halo reverse communication plan. This is a multiply-owned
to uniquely owned communication.
In a scatter operation results from ghosted values on other processors are
scattered back to the owning processor of the ghost and the value associated
with the ghost is summed into the locally owned value the ghost represents.
If a locally owned element is ghosted on multiple ranks, then multiple
contributions will be made to the sum, one for each rank.
*/
template <class HaloType, class SliceType>
class Scatter
: public CommunicationData<HaloType, CommunicationDataSlice<SliceType>>
{
static_assert( is_halo<HaloType>::value, "" );
public:
//! Base type.
using base_type =
CommunicationData<HaloType, CommunicationDataSlice<SliceType>>;
//! Communication plan type (Halo).
using plan_type = typename base_type::plan_type;
//! Kokkos execution space.
using execution_space = typename base_type::execution_space;
//! Kokkos memory space.
using memory_space = typename base_type::memory_space;
//! Communication data type.
using data_type = typename base_type::data_type;
//! Communication buffer type.
using buffer_type = typename base_type::buffer_type;
/*!
\param halo The Halo to be used for the gather.
\param slice The slice on which to perform the gather. The slice should
have a size equivalent to halo.numGhost() + halo.numLocal(). The locally
owned elements are expected to appear first (i.e. in the first
halo.numLocal() elements) and the ghosted elements are expected to appear
second (i.e. in the next halo.numGhost() elements()).
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
Scatter( HaloType halo, SliceType slice, const double overallocation = 1.0 )
: base_type( halo, slice, overallocation )
{
reserve( _halo, slice );
}
//! Total scatter send size for this rank.
auto totalSend() { return _halo.totalNumImport(); }
//! Total scatter receive size for this rank.
auto totalReceive() { return _halo.totalNumExport(); }
/*!
\brief Perform the scatter operation.
*/
template <class ExecutionSpace>
void apply( ExecutionSpace )
{
Kokkos::Profiling::ScopedRegion region( "Cabana::scatter" );
// Get the buffers (local copies for lambdas below).
auto send_buffer = this->getSendBuffer();
auto recv_buffer = this->getReceiveBuffer();
auto slice = this->getData();
// Get the number of components in the slice.
std::size_t num_comp = this->getSliceComponents();
// Get the raw slice data. Wrap in a 1D Kokkos View so we can unroll the
// components of each slice element.
Kokkos::View<data_type*, memory_space,
Kokkos::MemoryTraits<Kokkos::Unmanaged>>
slice_data( slice.data(), slice.numSoA() * slice.stride( 0 ) );
// Extract the send buffer from the ghosted elements.
std::size_t num_local = _halo.numLocal();
auto extract_send_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
std::size_t ghost_idx = i + num_local;
auto s = SliceType::index_type::s( ghost_idx );
auto a = SliceType::index_type::a( ghost_idx );
std::size_t slice_offset = s * slice.stride( 0 ) + a;
for ( std::size_t n = 0; n < num_comp; ++n )
send_buffer( i, n ) =
slice_data( slice_offset + SliceType::vector_length * n );
};
Kokkos::RangePolicy<ExecutionSpace> send_policy( 0, _send_size );
Kokkos::parallel_for( "Cabana::scatter::extract_send_buffer",
send_policy, extract_send_buffer_func );
Kokkos::fence();
// The halo has it's own communication space so choose any mpi tag.
const int mpi_tag = 2345;
// Post non-blocking receives.
int num_n = _halo.numNeighbor();
std::vector<MPI_Request> requests( num_n );
std::pair<std::size_t, std::size_t> recv_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
recv_range.second = recv_range.first + _halo.numExport( n );
auto recv_subview =
Kokkos::subview( recv_buffer, recv_range, Kokkos::ALL );
MPI_Irecv( recv_subview.data(),
recv_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm(),
&( requests[n] ) );
recv_range.first = recv_range.second;
}
// Do blocking sends.
std::pair<std::size_t, std::size_t> send_range = { 0, 0 };
for ( int n = 0; n < num_n; ++n )
{
send_range.second = send_range.first + _halo.numImport( n );
auto send_subview =
Kokkos::subview( send_buffer, send_range, Kokkos::ALL );
MPI_Send( send_subview.data(),
send_subview.size() * sizeof( data_type ), MPI_BYTE,
_halo.neighborRank( n ), mpi_tag, _halo.comm() );
send_range.first = send_range.second;
}
// Wait on non-blocking receives.
std::vector<MPI_Status> status( num_n );
const int ec =
MPI_Waitall( requests.size(), requests.data(), status.data() );
if ( MPI_SUCCESS != ec )
throw std::logic_error( "Failed MPI Communication" );
// Get the steering vector for the sends.
auto steering = _halo.getExportSteering();
// Scatter the ghosts in the receive buffer into the local values.
auto scatter_recv_buffer_func = KOKKOS_LAMBDA( const std::size_t i )
{
auto s = SliceType::index_type::s( steering( i ) );
auto a = SliceType::index_type::a( steering( i ) );
std::size_t slice_offset = s * slice.stride( 0 ) + a;
for ( std::size_t n = 0; n < num_comp; ++n )
Kokkos::atomic_add(
&slice_data( slice_offset + SliceType::vector_length * n ),
recv_buffer( i, n ) );
};
Kokkos::RangePolicy<ExecutionSpace> recv_policy( 0, _recv_size );
Kokkos::parallel_for( "Cabana::scatter::scatter_recv_buffer",
recv_policy, scatter_recv_buffer_func );
Kokkos::fence();
// Barrier before completing to ensure synchronization.
MPI_Barrier( _halo.comm() );
}
void apply() override { apply( execution_space{} ); }
/*!
\brief Reserve new buffers as needed and update the halo and slice data.
Reallocation only occurs if there is not enough space in the buffers.
\param halo The Halo to be used for the scatter.
\param slice The slice on which to perform the scatter.
\param overallocation An optional factor to keep extra space in the
buffers to avoid frequent resizing.
*/
void reserve( const HaloType& halo, const SliceType& slice,
const double overallocation )
{
if ( !haloCheckValidSize( halo, slice ) )
throw std::runtime_error( "AoSoA is the wrong size for scatter!" );
this->reserveImpl( halo, slice, totalSend(), totalReceive(),
overallocation );
}
/*!
\brief Reserve new buffers as needed and update the halo and slice data.
\param halo The Halo to be used for the scatter.
\param slice The slice on which to perform the scatter.
*/
void reserve( const HaloType& halo, const SliceType& slice )
{
if ( !haloCheckValidSize( halo, slice ) )
throw std::runtime_error( "AoSoA is the wrong size for scatter!" );
this->reserveImpl( halo, slice, totalSend(), totalReceive() );
}
private:
plan_type _halo = base_type::_comm_plan;
using base_type::_recv_size;
using base_type::_send_size;
};
/*!
\brief Create the scatter.
\param halo The halo to use for the scatter.
\param slice The slice on which to perform the scatter. The slice should have
a size equivalent to halo.numGhost() + halo.numLocal(). The locally owned
elements are expected to appear first (i.e. in the first halo.numLocal()
elements) and the ghosted elements are expected to appear second (i.e. in the
next halo.numGhost() elements()).
\param overallocation An optional factor to keep extra space in the buffers to
avoid frequent resizing.
\return Scatter
*/
template <class HaloType, class SliceType>
auto createScatter( const HaloType& halo, const SliceType& slice,
const double overallocation = 1.0,
typename std::enable_if<( is_halo<HaloType>::value &&
is_slice<SliceType>::value ),
int>::type* = 0 )
{
return Scatter<HaloType, SliceType>( halo, slice, overallocation );
}
//---------------------------------------------------------------------------//
/*!
\brief Synchronously scatter data from the ghosts to the local decomposition
of a slice using the halo reverse communication plan. This is a
multiply-owned to uniquely owned communication.
\note This routine allocates send and receive buffers internally. This is
often not performant due to frequent buffer reallocations - consider creating
and reusing Gather instead.
\param halo The halo to use for the scatter.
\param slice The Slice on which to perform the scatter. The Slice should have
a size equivalent to halo.numGhost() + halo.numLocal(). The locally owned
elements are expected to appear first (i.e. in the first halo.numLocal()
elements) and the ghosted elements are expected to appear second (i.e. in
the next halo.numGhost() elements()).
*/
template <class HaloType, class SliceType>
void scatter( const HaloType& halo, SliceType& slice,
typename std::enable_if<( is_halo<HaloType>::value &&
is_slice<SliceType>::value ),
int>::type* = 0 )
{
auto scatter = createScatter( halo, slice );
scatter.apply();
}
//---------------------------------------------------------------------------//
} // end namespace Cabana
#endif // end CABANA_HALO_HPP