forked from dune-project/dune-common
/
remoteindices.hh
1283 lines (1076 loc) · 43.9 KB
/
remoteindices.hh
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
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_REMOTEINDICES_HH
#define DUNE_REMOTEINDICES_HH
#include "indexset.hh"
#include "plocalindex.hh"
#include "mpiparallelparadigm.hh"
#include "threadparallelparadigm.hh"
#include <dune/common/exceptions.hh>
#include <dune/common/poolallocator.hh>
#include <dune/common/sllist.hh>
#include <dune/common/stdstreams.hh>
#include <map>
#include <set>
#include <utility>
#include <iostream>
#include <algorithm>
#include <iterator>
namespace Dune
{
/** @addtogroup Common_Parallel
*
* @{
*/
/**
* @file
* @brief Classes describing a distributed indexset
* @author Marco Agnese, Markus Blatt
*/
template<typename T,typename P,typename A>
class RemoteIndices;
template<typename T1, typename T2>
class RemoteIndex;
template<typename T>
class IndicesSyncer;
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const RemoteIndex<T1,T2>& index);
template<typename T, typename A, bool mode>
class RemoteIndexListModifier;
/** @brief Information about an index residing on another processor. */
template<typename T1, typename T2>
class RemoteIndex
{
template<typename T>
friend class IndicesSyncer;
template<typename T, typename A, typename R>
friend void repairLocalIndexPointers(std::map<int,SLList<std::pair<typename T::GlobalIndex, typename T::LocalIndex::Attribute>,A> >&, R&, const T&);
template<typename T, typename A, bool mode>
friend class RemoteIndexListModifier;
public:
/** @brief the type of the global index. This type has to provide at least a operator< for sorting. */
typedef T1 GlobalIndex;
/**
* @brief The type of the attributes. Normally this will be an enumeration like
* \code
* enum Attributes{owner, border, overlap}
* \endcode
* e.g. OwnerOverlapCopyAttributes.
*/
typedef T2 Attribute;
/** @brief The type of the index pair. */
typedef IndexPair<GlobalIndex,ParallelLocalIndex<Attribute> > PairType;
/**
* @brief Get the attribute of the index on the remote process.
* @return The remote attribute.
*/
const Attribute attribute() const;
/**
* @brief Get the corresponding local index pair.
* @return The corresponding local index pair.
*/
const PairType& localIndexPair() const;
/** @brief Parameterless Constructor. */
RemoteIndex();
/**
* @brief Constructor.
* @param attribute The attribute of the index on the remote processor.
* @param local The corresponding local index.
*/
RemoteIndex(const T2& attribute, const PairType* local);
/**
* @brief Constructor.
* Private as it should only be called from within Indexset.
* @param attribute The attribute of the index on the remote processor.
*/
RemoteIndex(const T2& attribute);
bool operator==(const RemoteIndex& ri) const;
bool operator!=(const RemoteIndex& ri) const;
private:
/** @brief The corresponding local index for this process. */
const PairType* localIndex_;
/** @brief The attribute of the index on the other process. */
char attribute_;
};
template<class T,class P,class A>
std::ostream& operator<<(std::ostream& os, const RemoteIndices<T,P,A>& indices);
class InterfaceBuilder;
template<class T, class A>
class CollectiveIterator;
template<class T>
class IndicesSyncer;
// forward declaration needed for friend declaration.
template<typename T1, typename T2>
class OwnerOverlapCopyCommunication;
/**
* @brief The indices present on remote processes.
*
* To set up communication between the set of processes active in
* the communication every process needs to know which
* indices are also known to other processes and which attributes
* are attached to them on the remote side.
*
* This information is managed by this class. The information can either
* be computed automatically calling rebuild (which requires information
* to be sent in a ring) or set up by hand using the
* RemoteIndexListModifiers returned by function getModifier(int).
*
* @tparam T The type of the parallel index set.
* @tparam P The type of the parallel paradigm to use.
* @tparam A The type of the allocator to use.
*/
template<class T, class P=MPIParadigm, class A=std::allocator<RemoteIndex<typename T::GlobalIndex, typename T::LocalIndex::Attribute> > >
class RemoteIndices
{
public:
/** @brief The type of the index set we use, e.g. ParallelLocalIndexSet. */
typedef T ParallelIndexSet;
/** @brief The type of the parallel paradigm we use, e.g. MPIParadigm. */
typedef P ParallelParadigm;
private:
typedef RemoteIndices<T,P,A> ThisType;
friend class InterfaceBuilder;
friend class IndicesSyncer<ThisType>;
template<typename T1, typename A2, typename R>
friend void repairLocalIndexPointers(std::map<int,SLList<std::pair<typename T1::GlobalIndex, typename T1::LocalIndex::Attribute>,A2> >&, R&, const T1&);
template<class G, class T1, class T2>
friend void fillIndexSetHoles(const G& graph, OwnerOverlapCopyCommunication<T1,T2>& oocomm);
friend std::ostream& operator<<<>(std::ostream&, const RemoteIndices<T,P,A>&);
public:
/** @brief The type of the collective iterator over all remote indices. */
typedef CollectiveIterator<ParallelIndexSet,A> CollectiveIteratorT;
/** @brief The type of the global index. */
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/** @brief The type of the local index. */
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/** @brief The type of the attribute. */
typedef typename LocalIndex::Attribute Attribute;
/** @brief Type of the remote indices we manage. */
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/** @brief The type of the allocator for the remote index list. */
typedef typename A::template rebind<RemoteIndex>::other Allocator;
/** @brief The type of the remote index list. */
typedef SLList<RemoteIndex,Allocator> RemoteIndexList;
/** @brief The type of the map from rank to remote index list. */
typedef std::map<int, std::pair<RemoteIndexList*,RemoteIndexList*> > RemoteIndexMap;
typedef typename RemoteIndexMap::const_iterator const_iterator;
/** @brief The type of the communicator. */
typedef typename ParallelParadigm::CommType CommType;
/**
* @brief Constructor.
* @param parallel The parallel paradigm to use.
* @param source The indexset which represents the global to local mapping at the source of the communication.
* @param destination The indexset to which the communication which represents the global to local mapping at the destination of the communication.
* May be the same as the source indexset.
* @param neighbours Optional: The neighbours the process shares indices with. If this parameter is omitted a ring communication with all indices will take
* place to calculate this information which is O(P).
* @param includeSelf If true, sending from indices of the processor to other indices on the same processor is enabled even if the same indexset is used
* on both the sending and receiving side.
*/
inline RemoteIndices(const ParallelIndexSet& source, const ParallelIndexSet& destination, ParallelParadigm& parallel,
const std::vector<int>& neighbours=std::vector<int>(), bool includeSelf=false);
/** @brief Constructor. Available only for MPI. */
inline RemoteIndices(const ParallelIndexSet& source, const ParallelIndexSet& destination, const typename MPIParadigm::CommType& comm,
const std::vector<int>& neighbours=std::vector<int>(), bool includeSelf=false);
RemoteIndices();
/**
* @brief Tell whether sending from indices of the processor to other indices on the same processor is enabled even if the same indexset is
* used on both the sending and receiving side.
*
* @param includeSelf If true it is enabled.
*/
void setIncludeSelf(bool includeSelf);
/**
* @brief Set the index sets and the parallel paradigm we work with.
*
* @warning All remote indices already setup will be deleted!
*
* @param parallel The parallel paradigm to use.
* @param source The indexset which represents the global to local mapping at the source of the communication.
* @param destination The indexset to which the communication which represents the global to local mapping at the destination of the communication.
* May be the same as the source indexset.
* @param neighbours Optional: The neighbours the process shares indices with. If this parameter is omitted a ring communication with all indices will take
* place to calculate this information which is O(P).
*/
void setIndexSets(const ParallelIndexSet& source, const ParallelIndexSet& destination, ParallelParadigm& parallel,
const std::vector<int>& neighbours=std::vector<int>());
/** @brief Set the index sets. Available only for MPI.*/
void setIndexSets(const ParallelIndexSet& source, const ParallelIndexSet& destination, const typename MPIParadigm::CommType& comm,
const std::vector<int>& neighbours=std::vector<int>());
template<typename C>
void setNeighbours(const C& neighbours)
{
neighbourIds.clear();
neighbourIds.insert(neighbours.begin(), neighbours.end());
}
const std::set<int>& getNeighbours() const
{
return neighbourIds;
}
/** @brief Destructor. */
~RemoteIndices();
/**
* @brief Rebuilds the set of remote indices.
*
* This has to be called whenever the underlying index sets change.
*
* If the template parameter ignorePublic is true all indices will be treated as public.
*/
template<bool ignorePublic>
void rebuild();
bool operator==(const RemoteIndices& ri);
/**
* @brief Checks whether the remote indices are synced with the indexsets.
*
* If they are not synced the remote indices need to be rebuild.
* @return True if they are synced.
*/
inline bool isSynced() const;
inline CommType communicator() const;
/** @brief Get the parallel paradigm used. */
inline ParallelParadigm& parallelParadigm() const;
/**
* @brief Get a modifier for a remote index list.
*
* Sometimes the user knows in advance which indices will be present
* on other processors, too. Then he can set them up using this modifier.
*
* @warning Use with care. If the remote index list is inconsistent
* after the modification the communication might result in a dead lock!
*
* @tparam mode If true the index set corresponding to the remote indices might get modified.
* Therefore the internal pointers to the indices need to be repaired.
* @tparam send If true the remote index information at the sending side will
* be modified, if false the receiving side.
*/
template<bool mode, bool send>
inline RemoteIndexListModifier<ParallelIndexSet,A,mode> getModifier(int process);
/**
* @brief Find an iterator over the remote index lists of a specific process.
* @param proc The identifier of the process.
* @return The iterator the remote index lists postioned at the process.
* If theres is no list for this process, the end iterator is returned.
*/
inline const_iterator find(int proc) const;
/**
* @brief Get an iterator over all remote index lists.
* @return The iterator over all remote index lists postioned at the first process.
*/
inline const_iterator begin() const;
/**
* @brief Get an iterator over all remote index lists.
* @return The iterator over all remote index lists postioned at the end.
*/
inline const_iterator end() const;
/** @brief Get an iterator for colletively iterating over the remote indices of all remote processes. */
template<bool send>
inline CollectiveIteratorT iterator() const;
/** @brief Free the index lists. */
inline void free();
/**
* @brief Get the number of processors we share indices with.
* @return The number of neighbours.
*/
inline int neighbours() const;
/** @brief Get the index set at the source. */
inline const ParallelIndexSet& sourceIndexSet() const;
/** @brief Get the index set at destination. */
inline const ParallelIndexSet& destinationIndexSet() const;
private:
/** copying is forbidden. */
RemoteIndices(const RemoteIndices&)
{}
/** @brief Index set used at the source of the communication. */
const ParallelIndexSet* source_;
/** @brief Index set used at the destination of the communication. */
const ParallelIndexSet* target_;
/** @brief Parallel paradigm used. */
ParallelParadigm* parallel_;
/** @brief Flag which indicates if parallel_ need to be free or not at decosntruction. */
bool freeparallel_;
/** @brief The neighbours we share indices with. If not empty this will speedup rebuild. */
std::set<int> neighbourIds;
/** @brief The sequence number of the source index set when the remote indices where build. */
int sourceSeqNo_;
/** @brief The sequence number of the destination index set when the remote indices where build. */
int destSeqNo_;
/** @brief Whether the public flag was ignored during the build. */
bool publicIgnored;
/** @brief Whether the next build will be the first build ever. */
bool firstBuild;
/*
* @brief If true, sending from indices of the processor to other indices on the same processor is enabled even if
* the same indexset is used on both the sending and receiving side.
*/
bool includeSelf;
/** @brief The index pair type. */
typedef IndexPair<GlobalIndex,LocalIndex> PairType;
/**
* @brief The remote indices. The key is the process id and the values are the pair of remote index lists,
* the first for receiving, the second for sending.
*/
RemoteIndexMap remoteIndices_;
/**
* @brief Count the number of public indices in an index set.
* @param indexSet The index set whose indices we count.
* @return the number of indices marked as public.
*/
inline int noPublic(const ParallelIndexSet& indexSet);
};
/** @} */
/**
* @brief Modifier for adding and/or deleting remote indices from
* the remote index list.
*
* In some cases all the information about the indices also present
* on remote process might already be known. In this case this
* information can be provided to the RemoteIndices via this modifier.
* This prevents the global communication needed by a call to
* RemoteIndices::rebuild.
*
* In some cases it might advisable to run IndicesSyncer::sync afterwards.
*
* @warning Use with care. If the indices are not consistent afterwards
* communication attempts might deadlock!
*/
template<class T, class A, bool mode>
class RemoteIndexListModifier
{
template<typename T1,typename P1,typename A1>
friend class RemoteIndices;
public:
class InvalidPosition : public RangeError
{};
enum {
/**
* @brief If true the index set corresponding to the
* remote indices might get modified.
*
* If for example new indices are added to an index set
* all pointers of the remote indices to the local indices
* become invalid after ParallelIndexSet::endResize() was called.
*/
MODIFYINDEXSET=mode
};
/** @brief Type of the index set we use. */
typedef T ParallelIndexSet;
/** @brief The type of the global index. */
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/** @brief The type of the local index. */
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/** @brief The type of the attribute. */
typedef typename LocalIndex::Attribute Attribute;
/** @brief Type of the remote indices we manage. */
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/** @brief The type of the allocator for the remote index list. */
typedef A Allocator;
/** @brief The type of the remote index list. */
typedef SLList<RemoteIndex,Allocator> RemoteIndexList;
/** @brief The type of the modifying iterator of the remote index list. */
typedef SLListModifyIterator<RemoteIndex,Allocator> ModifyIterator;
/** @brief The type of the remote index list iterator. */
typedef typename RemoteIndexList::const_iterator ConstIterator;
/**
* @brief Insert an index to the list.
*
* Moves to the position where the index fits and inserts it.
* After the insertion only indices with an bigger global index
* than the inserted can be inserted.
*
* This method is only available if MODIFYINDEXSET is false.
*
* @param index The index to insert.
* @exception InvalidPosition Thrown if the index at the current position or
* the one before has bigger global index than the one to be inserted.
*/
void insert(const RemoteIndex& index) throw(InvalidPosition);
/**
* @brief Insert an index to the list.
*
* Moves to the position where the index fits and inserts it.
* After the insertion only indices with an bigger global index
* than the inserted can be inserted.
*
* This method is only available if MODIFYINDEXSET is true.
*
* @param index The index to insert.
* @param global The global index of the remote index.
* @exception InvalidPosition Thrown if the index at the current position or
* the one before has bigger global index than the one to be inserted.
*/
void insert(const RemoteIndex& index, const GlobalIndex& global) throw(InvalidPosition);
/**
* @brief Remove a remote index.
* @param global The global index corresponding to the remote index.
* @return True If there was a corresponding remote index.
* @exception InvalidPostion If there was an insertion or deletion of
* a remote index corresponding to a bigger global index before.
*/
bool remove(const GlobalIndex& global) throw(InvalidPosition);
/**
* @brief Repair the pointers to the local index pairs.
*
* Due to adding new indices or/and deleting indices in the
* index set all pointers to the local index pair might become
* invalid during ParallelIndexSet::endResize().
* This method repairs them.
*
* @exception InvalidIndexSetState Thrown if the underlying
* index set is not in ParallelIndexSetState::GROUND mode (only when
* compiled with DUNE_ISTL_WITH_CHECKING!).
*/
void repairLocalIndexPointers() throw(InvalidIndexSetState);
RemoteIndexListModifier(const RemoteIndexListModifier&);
/**
* @brief Default constructor.
* @warning Object is not usable!
*/
RemoteIndexListModifier() : glist_()
{}
private:
/**
* @brief Create a modifier for a remote index list.
* @param indexSet The set of indices the process knows.
* @param rList The list of remote indices to modify.
*/
RemoteIndexListModifier(const ParallelIndexSet& indexSet, RemoteIndexList& rList);
typedef SLList<GlobalIndex,Allocator> GlobalList;
typedef typename GlobalList::ModifyIterator GlobalModifyIterator;
RemoteIndexList* rList_;
const ParallelIndexSet* indexSet_;
GlobalList glist_;
ModifyIterator iter_;
GlobalModifyIterator giter_;
ConstIterator end_;
bool first_;
GlobalIndex last_;
};
/** @brief A collective iterator for moving over the remote indices for all processes collectively. */
template<class T, class A>
class CollectiveIterator
{
/** @brief Type of the index set we use. */
typedef T ParallelIndexSet;
/** @brief The type of the global index. */
typedef typename ParallelIndexSet::GlobalIndex GlobalIndex;
/** @brief The type of the local index. */
typedef typename ParallelIndexSet::LocalIndex LocalIndex;
/** @brief The type of the attribute. */
typedef typename LocalIndex::Attribute Attribute;
/** @brief The remote index type */
typedef Dune::RemoteIndex<GlobalIndex,Attribute> RemoteIndex;
/** @brief The allocator of the remote indices. */
typedef typename A::template rebind<RemoteIndex>::other Allocator;
/** @brief The type of the remote index list. */
typedef SLList<RemoteIndex,Allocator> RemoteIndexList;
/** @brief The of map for storing the iterators. */
typedef std::map<int,std::pair<typename RemoteIndexList::const_iterator, const typename RemoteIndexList::const_iterator> > Map;
public:
/** @brief The type of the map from rank to remote index list. */
typedef std::map<int, std::pair<RemoteIndexList*,RemoteIndexList*> > RemoteIndexMap;
/**
* @brief Constructor.
* @param map_ The map of the remote indices.
* @param send True if we want iterate over the remote indices used for sending.
*/
inline CollectiveIterator(const RemoteIndexMap& map_, bool send);
/**
* @brief Advances all underlying iterators.
*
* All iterators are advanced until they point to a remote index whose
* global id is bigger or equal to global.
* Iterators pointing to their end are removed.
* @param global The index we search for.
*/
inline void advance(const GlobalIndex& global);
/**
* @brief Advances all underlying iterators.
*
* All iterators are advanced until they point to a remote index whose
* global id is bigger or equal to global.
* Iterators pointing to their end are removed.
* @param global The index we search for.
* @param attribute The attribute we search for.
*/
inline void advance(const GlobalIndex& global, const Attribute& attribute);
CollectiveIterator& operator++();
/** @brief Checks whether there are still iterators in the map. */
inline bool empty();
/**
* @brief Iterator over the valid underlying iterators.
*
* An iterator is valid if it points to a remote index whose
* global id is equal to the one currently examined.
*/
class iterator
{
public:
typedef typename Map::iterator RealIterator;
typedef typename Map::iterator ConstRealIterator;
//! \todo Please doc me!
iterator(const RealIterator& iter, const ConstRealIterator& end, GlobalIndex& index) : iter_(iter), end_(end), index_(index), hasAttribute(false)
{
// move to the first valid entry
while(iter_!=end_ && iter_->second.first->localIndexPair().global()!=index_)
++iter_;
}
iterator(const RealIterator& iter, const ConstRealIterator& end, GlobalIndex index, Attribute attribute) :
iter_(iter), end_(end), index_(index), attribute_(attribute), hasAttribute(true)
{
// move to the first valid entry or the end
while(iter_!=end_ && (iter_->second.first->localIndexPair().global()!=index_ || iter_->second.first->localIndexPair().local().attribute()!=attribute))
++iter_;
}
//! \todo Please doc me!
iterator(const iterator& other) : iter_(other.iter_), end_(other.end_), index_(other.index_)
{}
//! \todo Please doc me!
iterator& operator++()
{
++iter_;
// if entry is not valid move on
while(iter_!=end_ && (iter_->second.first->localIndexPair().global()!=index_ ||
(hasAttribute && iter_->second.first->localIndexPair().local().attribute()!=attribute_)))
++iter_;
assert(iter_==end_ || (iter_->second.first->localIndexPair().global()==index_));
assert(iter_==end_ || !hasAttribute || (iter_->second.first->localIndexPair().local().attribute()==attribute_));
return *this;
}
//! \todo Please doc me!
const RemoteIndex& operator*() const
{
return *(iter_->second.first);
}
//! \todo Please doc me!
int process() const
{
return iter_->first;
}
//! \todo Please doc me!
const RemoteIndex* operator->() const
{
return iter_->second.first.operator->();
}
//! \todo Please doc me!
bool operator==(const iterator& other)
{
return other.iter_==iter_;
}
//! \todo Please doc me!
bool operator!=(const iterator& other)
{
return other.iter_!=iter_;
}
private:
iterator();
RealIterator iter_;
RealIterator end_;
GlobalIndex index_;
Attribute attribute_;
bool hasAttribute;
};
iterator begin();
iterator end();
private:
Map map_;
GlobalIndex index_;
Attribute attribute_;
bool noattribute;
};
template<typename T1,typename T2>
RemoteIndex<T1,T2>::RemoteIndex(const T2& attribute, const PairType* local) : localIndex_(local), attribute_(attribute)
{}
template<typename T1,typename T2>
RemoteIndex<T1,T2>::RemoteIndex(const T2& attribute) : localIndex_(0), attribute_(attribute)
{}
template<typename T1,typename T2>
RemoteIndex<T1,T2>::RemoteIndex() : localIndex_(0), attribute_()
{}
template<typename T1,typename T2>
inline bool RemoteIndex<T1,T2>::operator==(const RemoteIndex& ri) const
{
return localIndex_==ri.localIndex_ && attribute_==ri.attribute;
}
template<typename T1,typename T2>
inline bool RemoteIndex<T1,T2>::operator!=(const RemoteIndex& ri) const
{
return localIndex_!=ri.localIndex_ || attribute_!=ri.attribute_;
}
template<typename T1,typename T2>
inline const T2 RemoteIndex<T1,T2>::attribute() const
{
return T2(attribute_);
}
template<typename T1,typename T2>
inline const IndexPair<T1,ParallelLocalIndex<T2> >& RemoteIndex<T1,T2>::localIndexPair() const
{
return *localIndex_;
}
template<typename T,typename P,typename A>
inline RemoteIndices<T,P,A>::RemoteIndices(const ParallelIndexSet& source, const ParallelIndexSet& destination, ParallelParadigm& parallel,
const std::vector<int>& neighbours, bool includeSelf_) : source_(&source), target_(&destination),
parallel_(¶llel), freeparallel_(false), sourceSeqNo_(-1), destSeqNo_(-1), publicIgnored(false),
firstBuild(true), includeSelf(includeSelf_)
{
setNeighbours(neighbours);
}
template<typename T,typename P,typename A>
inline RemoteIndices<T,P,A>::RemoteIndices(const ParallelIndexSet& source, const ParallelIndexSet& destination, const typename MPIParadigm::CommType& comm,
const std::vector<int>& neighbours, bool includeSelf_) : source_(&source), target_(&destination), freeparallel_(true),
sourceSeqNo_(-1), destSeqNo_(-1), publicIgnored(false), firstBuild(true), includeSelf(includeSelf_)
{
parallel_ = new ParallelParadigm(comm);
setNeighbours(neighbours);
}
template<typename T,typename P,typename A>
void RemoteIndices<T,P,A>::setIncludeSelf(bool b)
{
includeSelf=b;
}
template<typename T,typename P,typename A>
RemoteIndices<T,P,A>::RemoteIndices() : source_(0), target_(0), sourceSeqNo_(-1), destSeqNo_(-1), publicIgnored(false), firstBuild(true)
{}
template<typename T,typename P,typename A>
void RemoteIndices<T,P,A>::setIndexSets(const ParallelIndexSet& source, const ParallelIndexSet& destination, ParallelParadigm& parallel,
const std::vector<int>& neighbours)
{
free();
source_ = &source;
target_ = &destination;
parallel_= ∥
firstBuild = true;
setNeighbours(neighbours);
}
template<typename T,typename P,typename A>
void RemoteIndices<T,P,A>::setIndexSets(const ParallelIndexSet& source, const ParallelIndexSet& destination, const typename MPIParadigm::CommType& comm,
const std::vector<int>& neighbours)
{
free();
if(freeparallel_)
delete parallel_;
source_ = &source;
target_ = &destination;
parallel_= new ParallelParadigm(comm);
firstBuild = true;
setNeighbours(neighbours);
}
template<typename T,typename P,typename A>
const typename RemoteIndices<T,P,A>::ParallelIndexSet& RemoteIndices<T,P,A>::sourceIndexSet() const
{
return *source_;
}
template<typename T,typename P,typename A>
const typename RemoteIndices<T,P,A>::ParallelIndexSet& RemoteIndices<T,P,A>::destinationIndexSet() const
{
return *target_;
}
template<typename T,typename P,typename A>
RemoteIndices<T,P,A>::~RemoteIndices()
{
free();
if(freeparallel_)
delete parallel_;
}
template<typename T,typename P,typename A>
inline int RemoteIndices<T,P,A>::noPublic(const ParallelIndexSet& indexSet)
{
typedef typename ParallelIndexSet::const_iterator const_iterator;
int noPublic=0;
const const_iterator end=indexSet.end();
for(const_iterator index=indexSet.begin(); index!=end; ++index)
if(index->local().isPublic())
noPublic++;
return noPublic;
}
template<typename T,typename P,typename A>
inline void RemoteIndices<T,P,A>::free()
{
typedef typename RemoteIndexMap::iterator Iterator;
Iterator lend = remoteIndices_.end();
for(Iterator lists=remoteIndices_.begin(); lists != lend; ++lists)
{
if(lists->second.first==lists->second.second)
{
// there is only one remote index list
delete lists->second.first;
}
else
{
delete lists->second.first;
delete lists->second.second;
}
}
remoteIndices_.clear();
firstBuild=true;
}
template<typename T,typename P,typename A>
inline int RemoteIndices<T,P,A>::neighbours() const
{
return remoteIndices_.size();
}
template<typename T,typename P,typename A>
template<bool ignorePublic>
inline void RemoteIndices<T,P,A>::rebuild()
{
// test wether a rebuild is needed
if(firstBuild || ignorePublic!=publicIgnored || ! isSynced())
{
free();
parallel_->template buildRemote<ignorePublic,ParallelIndexSet,RemoteIndexList>(source_,target_,remoteIndices_,neighbourIds,includeSelf);
sourceSeqNo_ = source_->seqNo();
destSeqNo_ = target_->seqNo();
firstBuild=false;
publicIgnored=ignorePublic;
}
}
template<typename T,typename P,typename A>
inline bool RemoteIndices<T,P,A>::isSynced() const
{
return sourceSeqNo_==source_->seqNo() && destSeqNo_ ==target_->seqNo();
}
template<typename T,typename P,typename A>
template<bool mode,bool send>
RemoteIndexListModifier<T,A,mode> RemoteIndices<T,P,A>::getModifier(int process)
{
// the user are on their own now therefore we assume they know what they are doing and just set the remote indices to synced status
sourceSeqNo_ = source_->seqNo();
destSeqNo_ = target_->seqNo();
typename RemoteIndexMap::iterator found = remoteIndices_.find(process);
if(found == remoteIndices_.end())
{
if(source_ != target_)
found = remoteIndices_.insert(found, std::make_pair(process, std::make_pair(new RemoteIndexList(), new RemoteIndexList())));
else
{
RemoteIndexList* rlist = new RemoteIndexList();
found = remoteIndices_.insert(found, std::make_pair(process, std::make_pair(rlist, rlist)));
}
}
firstBuild = false;
if(send)
return RemoteIndexListModifier<T,A,mode>(*source_, *(found->second.first));
else
return RemoteIndexListModifier<T,A,mode>(*target_, *(found->second.second));
}
template<typename T,typename P,typename A>
inline typename RemoteIndices<T,P,A>::ParallelParadigm& RemoteIndices<T,P,A>::parallelParadigm() const
{
return *parallel_;
}
template<typename T,typename P,typename A>
inline typename RemoteIndices<T,P,A>::const_iterator RemoteIndices<T,P,A>::find(int proc) const
{
return remoteIndices_.find(proc);
}
template<typename T,typename P,typename A>
inline typename RemoteIndices<T,P,A>::const_iterator RemoteIndices<T,P,A>::begin() const
{
return remoteIndices_.begin();
}
template<typename T,typename P,typename A>
inline typename RemoteIndices<T,P,A>::const_iterator RemoteIndices<T,P,A>::end() const
{
return remoteIndices_.end();
}
template<typename T,typename P,typename A>
bool RemoteIndices<T,P,A>::operator==(const RemoteIndices& ri)
{
if(neighbours()!=ri.neighbours())
return false;
typedef typename std::map<int,std::pair<RemoteIndexList*,RemoteIndexList*> >::const_iterator const_iterator;
const const_iterator rend = remoteIndices_.end();
for(const_iterator rindex = remoteIndices_.begin(), rindex1=ri.remoteIndices_.begin(); rindex!=rend; ++rindex, ++rindex1)
{
if(rindex->first != rindex1->first)
return false;
if(*(rindex->second.first) != *(rindex1->second.first))
return false;
if(*(rindex->second.second) != *(rindex1->second.second))
return false;
}
return true;
}
template<class T, class A, bool mode>
RemoteIndexListModifier<T,A,mode>::RemoteIndexListModifier(const ParallelIndexSet& indexSet, RemoteIndexList& rList) : rList_(&rList),
indexSet_(&indexSet), iter_(rList.beginModify()), end_(rList.end()),
first_(true)
{
if(MODIFYINDEXSET)
{
assert(indexSet_);
for(ConstIterator iter=iter_; iter != end_; ++iter)
glist_.push_back(iter->localIndexPair().global());
giter_ = glist_.beginModify();
}
}
template<typename T, typename A, bool mode>
RemoteIndexListModifier<T,A,mode>::RemoteIndexListModifier(const RemoteIndexListModifier<T,A,mode>& other) : rList_(other.rList_),
indexSet_(other.indexSet_), glist_(other.glist_), iter_(other.iter_),
giter_(other.giter_), end_(other.end_), first_(other.first_), last_(other.last_)
{}
template<typename T, typename A, bool mode>
inline void RemoteIndexListModifier<T,A,mode>::repairLocalIndexPointers() throw(InvalidIndexSetState)
{
if(MODIFYINDEXSET)
{
// repair pointers to local index set
#ifdef DUNE_ISTL_WITH_CHECKING
if(indexSet_->state()!=GROUND)
DUNE_THROW(InvalidIndexSetState, "Index has to be in ground mode for repairing pointers to indices");
#endif
typedef typename ParallelIndexSet::const_iterator IndexIterator;
typedef typename GlobalList::const_iterator GlobalIterator;
typedef typename RemoteIndexList::iterator Iterator;
GlobalIterator giter = glist_.begin();
IndexIterator index = indexSet_->begin();
for(Iterator iter=rList_->begin(); iter != end_; ++iter)
{
while(index->global()<*giter)