3#ifndef DUNE_ISTL_MATRIXREDISTRIBUTE_HH
4#define DUNE_ISTL_MATRIXREDISTRIBUTE_HH
18 struct RedistributeInformation
25 void redistribute(
const D& from, D& to)
const
32 void redistributeBackward(D& from,
const D& to)
const
41 void setNoRows(std::size_t size)
46 void setNoCopyRows(std::size_t size)
51 void setNoBackwardsCopyRows(std::size_t size)
56 std::size_t getRowSize(std::size_t index)
const
62 std::size_t getCopyRowSize(std::size_t index)
const
68 std::size_t getBackwardsCopyRowSize(std::size_t index)
const
77 template<
typename T,
typename T1>
78 class RedistributeInformation<OwnerOverlapCopyCommunication<T,T1> >
81 typedef OwnerOverlapCopyCommunication<T,T1> Comm;
83 RedistributeInformation()
84 : interface(), setup_(false)
87 RedistributeInterface& getInterface()
92 void checkInterface(
const IS& source,
93 const IS& target, MPI_Comm comm)
95 RemoteIndices<IS> *ri=
new RemoteIndices<IS>(source, target, comm);
96 ri->template rebuild<true>();
98 typename OwnerOverlapCopyCommunication<int>::OwnerSet flags;
100 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
102 inf.build(*ri, flags, flags);
108 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
110 std::cout<<
"Interfaces do not match!"<<std::endl;
111 std::cout<<rank<<
": redist interface new :"<<inf<<std::endl;
112 std::cout<<rank<<
": redist interface :"<<interface<<std::endl;
132 template<
class GatherScatter,
class D>
133 void redistribute(
const D& from, D& to)
const
135 BufferedCommunicator communicator;
136 communicator.template build<D>(from,to, interface);
137 communicator.template forward<GatherScatter>(from, to);
140 template<
class GatherScatter,
class D>
141 void redistributeBackward(D& from,
const D& to)
const
144 BufferedCommunicator communicator;
145 communicator.template build<D>(from,to, interface);
146 communicator.template backward<GatherScatter>(from, to);
151 void redistribute(
const D& from, D& to)
const
153 redistribute<CopyGatherScatter<D> >(from,to);
156 void redistributeBackward(D& from,
const D& to)
const
158 redistributeBackward<CopyGatherScatter<D> >(from,to);
165 void reserve(std::size_t size)
168 std::size_t& getRowSize(std::size_t index)
170 return rowSize[index];
173 std::size_t getRowSize(std::size_t index)
const
175 return rowSize[index];
178 std::size_t& getCopyRowSize(std::size_t index)
180 return copyrowSize[index];
183 std::size_t getCopyRowSize(std::size_t index)
const
185 return copyrowSize[index];
188 std::size_t& getBackwardsCopyRowSize(std::size_t index)
190 return backwardscopyrowSize[index];
193 std::size_t getBackwardsCopyRowSize(std::size_t index)
const
195 return backwardscopyrowSize[index];
198 void setNoRows(std::size_t rows)
200 rowSize.resize(rows, 0);
203 void setNoCopyRows(std::size_t rows)
205 copyrowSize.resize(rows, 0);
208 void setNoBackwardsCopyRows(std::size_t rows)
210 backwardscopyrowSize.resize(rows, 0);
214 std::vector<std::size_t> rowSize;
215 std::vector<std::size_t> copyrowSize;
216 std::vector<std::size_t> backwardscopyrowSize;
217 RedistributeInterface interface;
229 template<
class M,
class RI>
233 typedef typename M::size_type value_type;
234 typedef typename M::size_type size_type;
242 : matrix(m_), rowsize(rowsize_)
258 template<
class M,
class I>
261 typedef typename M::size_type size_type;
270 : matrix(m_), idxset(idxset_), aggidxset(aggidxset_), rowsize()
281 const std::vector<typename M::size_type>& rowsize_)
282 : matrix(m_), idxset(idxset_), aggidxset(aggidxset_), sparsity(aggidxset_.size()), rowsize(&rowsize_)
300 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
302 for(IIter i= aggidxset.begin(), end=aggidxset.end(); i!=end; ++i) {
303 if(!OwnerSet::contains(i->local().attribute())) {
305 std::cout<<rank<<
" Inserting diagonal for"<<i->local()<<std::endl;
307 sparsity[i->local()].insert(i->local());
310 nnz+=sparsity[i->local()].size();
312 assert( aggidxset.size()==sparsity.size());
315 m.setSize(aggidxset.size(), aggidxset.size(), nnz);
316 m.setBuildMode(M::row_wise);
317 typename M::CreateIterator citer=m.createbegin();
323 typedef typename std::vector<std::set<size_type> >::const_iterator Iter;
324 for(Iter i=sparsity.begin(), end=sparsity.end(); i!=end; ++i, ++citer)
326 typedef typename std::set<size_type>::const_iterator SIter;
327 for(SIter si=i->begin(), send=i->end(); si!=send; ++si)
330 if(i->find(idx)==i->end()) {
331 const typename I::IndexPair* gi=global.
pair(idx);
333 std::cout<<rank<<
": row "<<idx<<
" is missing a diagonal entry! global="<<gi->global()<<
" attr="<<gi->local().attribute()<<
" "<<
334 OwnerSet::contains(gi->local().attribute())<<
335 " row size="<<i->size()<<std::endl;
357 for (
unsigned int i = 0; i != sparsity.size(); ++i) {
358 if (add_sparsity[i].size() != 0) {
359 typedef std::set<size_type> Set;
361 std::insert_iterator<Set> tmp_insert (tmp_set, tmp_set.begin());
362 std::set_union(add_sparsity[i].begin(), add_sparsity[i].end(),
363 sparsity[i].begin(), sparsity[i].end(), tmp_insert);
364 sparsity[i].swap(tmp_set);
373 std::vector<std::set<size_type> > sparsity;
374 const std::vector<size_type>* rowsize;
377 template<
class M,
class I>
378 struct CommPolicy<CommMatrixSparsityPattern<M,I> >
380 typedef CommMatrixSparsityPattern<M,I>
Type;
391 static typename M::size_type
getSize(
const Type& t, std::size_t i)
394 return t.matrix[i].size();
397 assert((*t.rowsize)[i]>0);
398 return (*t.rowsize)[i];
409 template<
class M,
class I>
428 std::vector<size_t>& rowsize_)
442 if(!OwnerSet::contains(i->local().attribute())) {
444 typedef typename M::ColIterator CIter;
445 for(CIter c=
matrix[i->local()].begin(), cend=
matrix[i->local()].end();
449 if(c.index()==i->local()) {
450 typedef typename M::block_type::RowIterator RIter;
451 for(RIter r=c->begin(), rend=c->end();
468 template<
class M,
class I>
477 typedef std::pair<typename I::GlobalIndex,typename M::block_type>
IndexedType;
482 static std::size_t
getSize(
const Type& t, std::size_t i)
485 return t.matrix[i].size();
488 assert((*t.rowsize)[i]>0);
489 return (*t.rowsize)[i];
494 template<
class M,
class I,
class RI>
495 struct MatrixRowSizeGatherScatter
497 typedef CommMatrixRowSize<M,RI> Container;
499 static const typename M::size_type gather(
const Container& cont, std::size_t i)
501 return cont.matrix[i].size();
503 static void scatter(Container& cont,
const typename M::size_type& rowsize, std::size_t i)
506 cont.rowsize.getRowSize(i)=rowsize;
511 template<
class M,
class I,
class RI>
512 struct MatrixCopyRowSizeGatherScatter
514 typedef CommMatrixRowSize<M,RI> Container;
516 static const typename M::size_type gather(
const Container& cont, std::size_t i)
518 return cont.matrix[i].size();
520 static void scatter(Container& cont,
const typename M::size_type& rowsize, std::size_t i)
523 if (rowsize > cont.rowsize.getCopyRowSize(i))
524 cont.rowsize.getCopyRowSize(i)=rowsize;
529 template<
class M,
class I>
530 struct MatrixSparsityPatternGatherScatter
532 typedef typename I::GlobalIndex GlobalIndex;
533 typedef CommMatrixSparsityPattern<M,I> Container;
534 typedef typename M::ConstColIterator ColIter;
537 static GlobalIndex numlimits;
539 static const GlobalIndex& gather(
const Container& cont, std::size_t i, std::size_t j)
542 col=cont.matrix[i].begin();
543 else if (col!=cont.matrix[i].end())
550 if (col==cont.matrix[i].end()) {
551 numlimits = std::numeric_limits<GlobalIndex>::max();
555 const typename I::IndexPair* index=cont.idxset.pair(col.index());
558 if ( index->local().attribute() != 2)
559 return index->global();
561 numlimits = std::numeric_limits<GlobalIndex>::max();
566 static void scatter(Container& cont,
const GlobalIndex& gi, std::size_t i, std::size_t j)
570 if (gi != std::numeric_limits<GlobalIndex>::max()) {
571 const typename I::IndexPair& ip=cont.aggidxset.at(gi);
572 assert(ip.global()==gi);
573 std::size_t column = ip.local();
574 cont.sparsity[i].insert(column);
577 if(!OwnerSet::contains(ip.local().attribute()))
579 cont.sparsity[column].insert(i);
585 typedef typename Container::LookupIndexSet GlobalLookup;
586 typedef typename GlobalLookup::IndexPair IndexPair;
589 GlobalLookup lookup(cont.aggidxset);
590 const IndexPair* pi=lookup.pair(i);
592 if(OwnerSet::contains(pi->local().attribute())) {
594 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
595 std::cout<<rank<<cont.aggidxset<<std::endl;
596 std::cout<<rank<<
": row "<<i<<
" (global="<<gi <<
") not in index set for owner index "<<pi->global()<<std::endl;
604 template<
class M,
class I>
605 typename MatrixSparsityPatternGatherScatter<M,I>::ColIter MatrixSparsityPatternGatherScatter<M,I>::col;
607 template<
class M,
class I>
608 typename MatrixSparsityPatternGatherScatter<M,I>::GlobalIndex MatrixSparsityPatternGatherScatter<M,I>::numlimits;
611 template<
class M,
class I>
612 struct MatrixRowGatherScatter
614 typedef typename I::GlobalIndex GlobalIndex;
615 typedef CommMatrixRow<M,I> Container;
616 typedef typename M::ConstColIterator ColIter;
617 typedef typename std::pair<GlobalIndex,typename M::block_type> Data;
619 static Data datastore;
620 static GlobalIndex numlimits;
622 static const Data& gather(
const Container& cont, std::size_t i, std::size_t j)
625 col=cont.matrix[i].begin();
626 else if (col!=cont.matrix[i].end())
632 if (col==cont.matrix[i].end()) {
633 numlimits = std::numeric_limits<GlobalIndex>::max();
634 datastore = std::make_pair(numlimits,*col);
639 const typename I::IndexPair* index=cont.idxset.pair(col.index());
643 if ( index->local().attribute() != 2)
644 datastore = std::make_pair(index->global(),*col);
646 numlimits = std::numeric_limits<GlobalIndex>::max();
647 datastore = std::make_pair(numlimits,*col);
652 static void scatter(Container& cont,
const Data& data, std::size_t i, std::size_t j)
656 if (data.first != std::numeric_limits<GlobalIndex>::max()) {
657 typename M::size_type column=cont.aggidxset.at(data.first).local();
658 cont.matrix[i][column]=data.second;
668 template<
class M,
class I>
669 typename MatrixRowGatherScatter<M,I>::ColIter MatrixRowGatherScatter<M,I>::col;
671 template<
class M,
class I>
672 typename MatrixRowGatherScatter<M,I>::Data MatrixRowGatherScatter<M,I>::datastore;
674 template<
class M,
class I>
675 typename MatrixRowGatherScatter<M,I>::GlobalIndex MatrixRowGatherScatter<M,I>::numlimits;
677 template<
typename M,
typename C>
678 void redistributeSparsityPattern(M& origMatrix, M& newMatrix, C& origComm, C& newComm,
679 RedistributeInformation<C>& ri)
681 typename C::CopySet copyflags;
682 typename C::OwnerSet ownerflags;
683 typedef typename C::ParallelIndexSet IndexSet;
684 typedef RedistributeInformation<C> RI;
685 std::vector<typename M::size_type> rowsize(newComm.indexSet().size(), 0);
686 std::vector<typename M::size_type> copyrowsize(newComm.indexSet().size(), 0);
687 std::vector<typename M::size_type> backwardscopyrowsize(origComm.indexSet().size(), 0);
690 CommMatrixRowSize<M,RI> commRowSize(origMatrix, ri);
691 ri.template redistribute<MatrixRowSizeGatherScatter<M,IndexSet,RI> >(commRowSize,commRowSize);
693 origComm.buildGlobalLookup();
695 for (std::size_t i=0; i < newComm.indexSet().size(); i++) {
696 rowsize[i] = ri.getRowSize(i);
699 CommMatrixSparsityPattern<M,IndexSet>
700 origsp(origMatrix, origComm.globalLookup(), newComm.indexSet());
701 CommMatrixSparsityPattern<M,IndexSet>
702 newsp(origMatrix, origComm.globalLookup(), newComm.indexSet(), rowsize);
704 ri.template redistribute<MatrixSparsityPatternGatherScatter<M,IndexSet> >(origsp,newsp);
708 RemoteIndices<IndexSet> *ris =
new RemoteIndices<IndexSet>(origComm.indexSet(),
710 origComm.communicator());
711 ris->template rebuild<true>();
713 ri.getInterface().free();
714 ri.getInterface().build(*ris,copyflags,ownerflags);
717 CommMatrixRowSize<M,RI> commRowSize_copy(origMatrix, ri);
718 ri.template redistribute<MatrixCopyRowSizeGatherScatter<M,IndexSet,RI> >(commRowSize_copy,
721 for (std::size_t i=0; i < newComm.indexSet().size(); i++) {
722 copyrowsize[i] = ri.getCopyRowSize(i);
725 ri.redistributeBackward(backwardscopyrowsize,copyrowsize);
726 for (std::size_t i=0; i < origComm.indexSet().size(); i++) {
727 ri.getBackwardsCopyRowSize(i) = backwardscopyrowsize[i];
731 CommMatrixSparsityPattern<M,IndexSet> origsp_copy(origMatrix,
732 origComm.globalLookup(),
734 backwardscopyrowsize);
735 CommMatrixSparsityPattern<M,IndexSet> newsp_copy(origMatrix, origComm.globalLookup(),
736 newComm.indexSet(), copyrowsize);
737 ri.template redistribute<MatrixSparsityPatternGatherScatter<M,IndexSet> >(origsp_copy,
740 newsp.completeSparsityPattern(newsp_copy.sparsity);
741 newsp.storeSparsityPattern(newMatrix);
744 newsp.storeSparsityPattern(newMatrix);
746#ifdef DUNE_ISTL_WITH_CHECKING
749 typedef typename M::ConstRowIterator RIter;
750 for(RIter row=newMatrix.begin(), rend=newMatrix.end(); row != rend; ++row) {
751 typedef typename M::ConstColIterator CIter;
752 for(CIter col=row->begin(), cend=row->end(); col!=cend; ++col)
755 newMatrix[col.index()][row.index()];
757 std::cerr<<newComm.communicator().rank()<<
": entry ("
758 <<col.index()<<
","<<row.index()<<
") missing! for symmetry!"<<std::endl;
767 DUNE_THROW(ISTLError,
"Matrix not symmetric!");
771 template<
typename M,
typename C>
772 void redistributeMatrixEntries(M& origMatrix, M& newMatrix, C& origComm, C& newComm,
773 RedistributeInformation<C>& ri)
775 typedef typename C::ParallelIndexSet IndexSet;
776 typename C::OwnerSet ownerflags;
777 std::vector<typename M::size_type> rowsize(newComm.indexSet().size(), 0);
778 std::vector<typename M::size_type> copyrowsize(newComm.indexSet().size(), 0);
779 std::vector<typename M::size_type> backwardscopyrowsize(origComm.indexSet().size(), 0);
781 for (std::size_t i=0; i < newComm.indexSet().size(); i++) {
782 rowsize[i] = ri.getRowSize(i);
784 copyrowsize[i] = ri.getCopyRowSize(i);
788 for (std::size_t i=0; i < origComm.indexSet().size(); i++)
790 backwardscopyrowsize[i] = ri.getBackwardsCopyRowSize(i);
795 CommMatrixRow<M,IndexSet> origrow_copy(origMatrix, origComm.globalLookup(),
796 newComm.indexSet(), backwardscopyrowsize);
797 CommMatrixRow<M,IndexSet> newrow_copy(newMatrix, origComm.globalLookup(),
798 newComm.indexSet(),copyrowsize);
799 ri.template redistribute<MatrixRowGatherScatter<M,IndexSet> >(origrow_copy,
801 ri.getInterface().free();
802 RemoteIndices<IndexSet> *ris =
new RemoteIndices<IndexSet>(origComm.indexSet(),
804 origComm.communicator());
805 ris->template rebuild<true>();
806 ri.getInterface().build(*ris,ownerflags,ownerflags);
809 CommMatrixRow<M,IndexSet>
810 origrow(origMatrix, origComm.globalLookup(), newComm.indexSet());
811 CommMatrixRow<M,IndexSet>
812 newrow(newMatrix, origComm.globalLookup(), newComm.indexSet(),rowsize);
813 ri.template redistribute<MatrixRowGatherScatter<M,IndexSet> >(origrow,newrow);
815 newrow.setOverlapRowsToDirichlet();
816 if(newMatrix.N()>0&&newMatrix.N()<20)
817 printmatrix(std::cout, newMatrix,
"redist",
"row");
836 template<
typename M,
typename C>
838 RedistributeInformation<C>& ri)
840 ri.setNoRows(newComm.indexSet().size());
841 ri.setNoCopyRows(newComm.indexSet().size());
842 ri.setNoBackwardsCopyRows(origComm.indexSet().size());
843 redistributeSparsityPattern(origMatrix, newMatrix, origComm, newComm, ri);
844 redistributeMatrixEntries(origMatrix, newMatrix, origComm, newComm, ri);
A constant random access iterator for the Dune::ArrayList class.
Definition: arraylist.hh:379
A set consisting only of one item.
Definition: enumset.hh:60
Decorates an index set with the possibility to find a global index that is mapped to a specific local...
Definition: indexset.hh:506
derive error class from the base class in common
Definition: istlexception.hh:16
Default exception class for range errors.
Definition: exceptions.hh:279
A few common exception classes.
const IndexPair * pair(const std::size_t &local) const
Get the index pair corresponding to a local index.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:243
void printmatrix(std::ostream &s, const M &A, std::string title, std::string rowtext, int width=10, int precision=2)
Print a generic block matrix.
Definition: io.hh:259
Provides a map between global and local indices.
Dune namespace.
Definition: alignment.hh:10
void redistributeMatrix(M &origMatrix, M &newMatrix, C &origComm, C &newComm, RedistributeInformation< C > &ri)
Redistribute a matrix according to given domain decompositions.
Definition: matrixredistribute.hh:837
Classes providing communication interfaces for overlapping Schwarz methods.
Functionality for redistributing a parallel index set using graph partitioning.
Utility class to communicate and set the row sizes of a redistributed matrix.
Definition: matrixredistribute.hh:231
CommMatrixRowSize(const M &m_, RI &rowsize_)
Constructor.
Definition: matrixredistribute.hh:241
Utility class for comunicating the matrix entries.
Definition: matrixredistribute.hh:411
std::vector< size_t > * rowsize
row size information for the receiving side.
Definition: matrixredistribute.hh:465
M & matrix
The matrix to communicate the values of.
Definition: matrixredistribute.hh:459
CommMatrixRow(M &m_, const Dune::GlobalLookupIndexSet< I > &idxset_, const I &aggidxset_, std::vector< size_t > &rowsize_)
Constructor.
Definition: matrixredistribute.hh:427
const Dune::GlobalLookupIndexSet< I > & idxset
Index set for the original matrix.
Definition: matrixredistribute.hh:461
void setOverlapRowsToDirichlet()
Sets the non-owner rows correctly as Dirichlet boundaries.
Definition: matrixredistribute.hh:436
const I & aggidxset
Index set for the redistributed matrix.
Definition: matrixredistribute.hh:463
CommMatrixRow(M &m_, const Dune::GlobalLookupIndexSet< I > &idxset_, const I &aggidxset_)
Constructor.
Definition: matrixredistribute.hh:420
Utility class to communicate and build the sparsity pattern of a redistributed matrix.
Definition: matrixredistribute.hh:260
void storeSparsityPattern(M &m)
Creates and stores the sparsity pattern of the redistributed matrix.
Definition: matrixredistribute.hh:291
void completeSparsityPattern(std::vector< std::set< size_type > > add_sparsity)
Completes the sparsity pattern of the redistributed matrix with data from copy rows for the novlp cas...
Definition: matrixredistribute.hh:355
CommMatrixSparsityPattern(const M &m_, const Dune::GlobalLookupIndexSet< I > &idxset_, const I &aggidxset_)
Constructor for the original side.
Definition: matrixredistribute.hh:269
CommMatrixSparsityPattern(const M &m_, const Dune::GlobalLookupIndexSet< I > &idxset_, const I &aggidxset_, const std::vector< typename M::size_type > &rowsize_)
Constructor for the redistruted side.
Definition: matrixredistribute.hh:280
Default policy used for communicating an indexed type.
Definition: communicator.hh:120
V::value_type IndexedType
The type we get at each index with operator[].
Definition: communicator.hh:139
static int getSize(const V &, int index)
Get the number of primitve elements at that index.
SizeOne IndexedTypeFlag
Whether the indexed type has variable size or there is always one value at each index.
Definition: communicator.hh:145
V Type
The type the policy is for.
Definition: communicator.hh:132
@ nonoverlapping
Category for on overlapping solvers.
Definition: solvercategory.hh:23
Flag for marking indexed data structures where the data at each index may be a variable multiple of a...
Definition: communicator.hh:110
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18