3 #ifndef DUNE_ISTL_REPARTITION_HH
4 #define DUNE_ISTL_REPARTITION_HH
20 #include <dune/common/timer.hh>
21 #include <dune/common/unused.hh>
22 #include <dune/common/enumset.hh>
23 #include <dune/common/stdstreams.hh>
24 #include <dune/common/parallel/mpitraits.hh>
25 #include <dune/common/parallel/communicator.hh>
26 #include <dune/common/parallel/indexset.hh>
27 #include <dune/common/parallel/indicessyncer.hh>
28 #include <dune/common/parallel/remoteindices.hh>
57 template<
class G,
class T1,
class T2>
61 typedef typename IndexSet::LocalIndex::Attribute Attribute;
63 IndexSet& indexSet = oocomm.
indexSet();
67 typedef typename G::ConstVertexIterator VertexIterator;
70 std::size_t sum=0, needed = graph.noVertices()-indexSet.size();
71 std::vector<std::size_t> neededall(oocomm.
communicator().size(), 0);
73 MPI_Allgather(&needed, 1, MPITraits<std::size_t>::getType() , &(neededall[0]), 1, MPITraits<std::size_t>::getType(), oocomm.
communicator());
83 typedef typename IndexSet::const_iterator Iterator;
86 for(Iterator it = indexSet.begin(); it != end; ++it)
87 maxgi=std::max(maxgi,it->global());
96 maxgi=maxgi+neededall[i];
99 std::map<int,SLList<std::pair<T1,Attribute> > > globalIndices;
100 storeGlobalIndicesOfRemoteIndices(globalIndices, oocomm.
remoteIndices());
101 indexSet.beginResize();
103 for(VertexIterator vertex = graph.begin(), vend=graph.end(); vertex != vend; ++vertex) {
104 const typename IndexSet::IndexPair* pair=lookup.pair(*vertex);
112 indexSet.endResize();
114 repairLocalIndexPointers(globalIndices, oocomm.
remoteIndices(), indexSet);
119 std::cout<<
"Holes are filled!"<<std::endl;
127 class ParmetisDuneIndexMap
132 #if PARMETIS_MAJOR_VERSION > 3
133 typedef idx_t idxtype;
136 #endif // PARMETIS_MAJOR_VERSION > 3
138 typedef std::size_t idxtype;
139 #endif // #if HAVE_PARMETIS
141 template<
class Graph,
class OOComm>
142 ParmetisDuneIndexMap(
const Graph& graph,
const OOComm& com);
143 int toParmetis(
int i)
const
147 int toLocalParmetis(
int i)
const
151 int operator[](
int i)
const
155 int toDune(
int i)
const
159 std::vector<int>::size_type numOfOwnVtx()
const
176 template<
class G,
class OOComm>
177 ParmetisDuneIndexMap::ParmetisDuneIndexMap(
const G& graph,
const OOComm& oocomm)
180 int npes=oocomm.communicator().size(), mype=oocomm.communicator().rank();
182 typedef typename OOComm::ParallelIndexSet::const_iterator Iterator;
183 typedef typename OOComm::OwnerSet OwnerSet;
186 Iterator end = oocomm.indexSet().end();
187 for(Iterator index = oocomm.indexSet().begin(); index != end; ++index) {
188 if (OwnerSet::contains(index->local().attribute())) {
193 std::vector<int> globalNumOfVtx(npes);
195 MPI_Allgather(&numOfOwnVtx, 1, MPI_INT, &(globalNumOfVtx[0]), 1, MPI_INT, oocomm.communicator());
199 for(
int i=0; i<npes; i++) {
201 base += globalNumOfVtx[i];
209 typedef typename G::ConstVertexIterator VertexIterator;
211 std::cout << oocomm.communicator().rank()<<
" vtxDist: ";
212 for(
int i=0; i<= npes; ++i)
214 std::cout<<std::endl;
221 VertexIterator vend = graph.end();
222 for(VertexIterator vertex = graph.begin(); vertex != vend; ++vertex) {
223 const typename OOComm::ParallelIndexSet::IndexPair* index=oocomm.globalLookup().pair(*vertex);
225 if (OwnerSet::contains(index->local().attribute())) {
238 std::cout <<oocomm.communicator().rank()<<
": before ";
241 std::cout<<std::endl;
245 std::cout <<oocomm.communicator().rank()<<
": after ";
248 std::cout<<std::endl;
260 template<
class Flags,
class IS>
263 std::map<int,int> sizes;
265 typedef typename IS::const_iterator IIter;
266 for(IIter i=idxset.begin(), end=idxset.end(); i!=end; ++i)
267 if(Flags::contains(i->local().attribute()))
268 ++sizes[toPart[i->local()]];
271 typedef std::map<int,int>::const_iterator MIter;
272 for(MIter i=sizes.begin(), end=sizes.end(); i!=end; ++i)
273 interfaces()[i->first].first.reserve(i->second);
276 typedef typename IS::const_iterator IIter;
277 for(IIter i=idxset.begin(), end=idxset.end(); i!=end; ++i)
278 if(Flags::contains(i->local().attribute()))
279 interfaces()[toPart[i->local()]].first.add(i->local());
284 interfaces()[proc].second.reserve(size);
288 interfaces()[proc].second.add(idx);
290 template<
typename TG>
293 typedef typename std::vector<std::pair<TG,int> >::const_iterator VIter;
295 for(VIter idx=indices.begin(); idx!= indices.end(); ++idx) {
296 interfaces()[idx->second].second.add(i++);
308 typedef ParmetisDuneIndexMap :: idxtype idxtype ;
320 void createSendBuf(std::vector<GI>& ownerVec, std::set<GI>& overlapVec, std::set<int>& neighbors,
char *sendBuf,
int buffersize, MPI_Comm comm) {
322 std::size_t s=ownerVec.size();
326 MPI_Pack(&s, 1, MPITraits<std::size_t>::getType(), sendBuf, buffersize, &pos, comm);
327 MPI_Pack(&(ownerVec[0]), s, MPITraits<GI>::getType(), sendBuf, buffersize, &pos, comm);
328 s = overlapVec.size();
329 MPI_Pack(&s, 1, MPITraits<std::size_t>::getType(), sendBuf, buffersize, &pos, comm);
330 typedef typename std::set<GI>::iterator Iter;
331 for(Iter i=overlapVec.begin(), end= overlapVec.end(); i != end; ++i)
332 MPI_Pack(const_cast<GI*>(&(*i)), 1, MPITraits<GI>::getType(), sendBuf, buffersize, &pos, comm);
335 MPI_Pack(&s, 1, MPITraits<std::size_t>::getType(), sendBuf, buffersize, &pos, comm);
336 typedef typename std::set<int>::iterator IIter;
338 for(IIter i=neighbors.begin(), end= neighbors.end(); i != end; ++i)
339 MPI_Pack(const_cast<int*>(&(*i)), 1, MPI_INT, sendBuf, buffersize, &pos, comm);
350 void saveRecvBuf(
char *recvBuf,
int bufferSize, std::vector<std::pair<GI,int> >& ownerVec,
351 std::set<GI>& overlapVec, std::set<int>& neighbors, RedistributeInterface& inf,
int from, MPI_Comm comm) {
355 MPI_Unpack(recvBuf, bufferSize, &pos, &size, 1, MPITraits<std::size_t>::getType(), comm);
356 inf.reserveSpaceForReceiveInterface(from, size);
357 ownerVec.reserve(ownerVec.size()+size);
358 for(; size!=0; --size) {
360 MPI_Unpack(recvBuf, bufferSize, &pos, &gi, 1, MPITraits<GI>::getType(), comm);
361 ownerVec.push_back(std::make_pair(gi,from));
364 MPI_Unpack(recvBuf, bufferSize, &pos, &size, 1, MPITraits<std::size_t>::getType(), comm);
365 typename std::set<GI>::iterator ipos = overlapVec.begin();
366 Dune::dverb <<
"unpacking "<<size<<
" overlap"<<std::endl;
367 for(; size!=0; --size) {
369 MPI_Unpack(recvBuf, bufferSize, &pos, &gi, 1, MPITraits<GI>::getType(), comm);
370 ipos=overlapVec.insert(ipos, gi);
373 MPI_Unpack(recvBuf, bufferSize, &pos, &size, 1, MPITraits<std::size_t>::getType(), comm);
374 Dune::dverb <<
"unpacking "<<size<<
" neighbors"<<std::endl;
375 typename std::set<int>::iterator npos = neighbors.begin();
376 for(; size!=0; --size) {
378 MPI_Unpack(recvBuf, bufferSize, &pos, &n, 1, MPI_INT, comm);
379 npos=neighbors.insert(npos, n);
397 void getDomain(
const MPI_Comm& comm, T *part,
int numOfOwnVtx,
int nparts,
int *myDomain, std::vector<int> &domainMapping) {
399 MPI_Comm_size(comm, &npes);
400 MPI_Comm_rank(comm, &mype);
407 std::vector<int> domain(nparts);
408 std::vector<int> assigned(npes);
410 for (i=0; i<nparts; i++) {
411 domainMapping[i] = -1;
414 for (i=0; i<npes; i++) {
418 for (i=0; i<numOfOwnVtx; i++) {
422 int *domainMatrix =
new int[npes * nparts];
424 for(i=0; i<npes*nparts; i++) {
429 int *buf =
new int[nparts];
430 for (i=0; i<nparts; i++) {
432 domainMatrix[mype*nparts+i] = domain[i];
435 int src = (mype-1+npes)%npes;
436 int dest = (mype+1)%npes;
438 for (i=0; i<npes-1; i++) {
439 MPI_Sendrecv_replace(buf, nparts, MPI_INT, dest, 0, src, 0, comm, &status);
441 pe = ((mype-1-i)+npes)%npes;
442 for(j=0; j<nparts; j++) {
444 domainMatrix[pe*nparts+j] = buf[j];
452 int maxOccurance = 0;
454 for(i=0; i<nparts; i++) {
455 for(j=0; j<npes; j++) {
457 if (assigned[j]==0) {
458 if (maxOccurance < domainMatrix[j*nparts+i]) {
459 maxOccurance = domainMatrix[j*nparts+i];
467 domainMapping[i] = pe;
478 delete[] domainMatrix;
485 bool operator()(
const T& t1,
const T& t2)
const
503 void mergeVec(std::vector<std::pair<GI, int> >& ownerVec, std::set<GI>& overlapSet) {
505 typedef typename std::vector<std::pair<GI,int> >::const_iterator VIter;
508 if(ownerVec.size()>0)
510 VIter old=ownerVec.begin();
511 for(VIter i=old+1, end=ownerVec.end(); i != end; old=i++)
513 if(i->first==old->first)
515 std::cerr<<
"Value at indes"<<old-ownerVec.begin()<<
" is the same as at index "
516 <<i-ownerVec.begin()<<
" ["<<old->first<<
","<<old->second<<
"]==["
517 <<i->first<<
","<<i->second<<
"]"<<std::endl;
525 typedef typename std::set<GI>::iterator SIter;
526 VIter v=ownerVec.begin(), vend=ownerVec.end();
527 for(SIter s=overlapSet.begin(), send=overlapSet.end(); s!=send;)
529 while(v!=vend && v->first<*s) ++v;
530 if(v!=vend && v->first==*s) {
535 overlapSet.erase(tmp);
555 template<
class OwnerSet,
class Graph,
class IS,
class GI>
556 void getNeighbor(
const Graph& g, std::vector<int>& part,
557 typename Graph::VertexDescriptor vtx,
const IS& indexSet,
558 int toPe, std::set<GI>& neighbor, std::set<int>& neighborProcs) {
559 typedef typename Graph::ConstEdgeIterator Iter;
560 for(Iter edge=g.beginEdges(vtx), end=g.endEdges(vtx); edge!=end; ++edge)
562 const typename IS::IndexPair* pindex = indexSet.pair(edge.target());
564 if(part[pindex->local()]!=toPe || !OwnerSet::contains(pindex->local().attribute()))
567 neighbor.insert(pindex->global());
568 neighborProcs.insert(part[pindex->local()]);
573 template<
class T,
class I>
574 void my_push_back(std::vector<T>& ownerVec,
const I& index,
int proc)
576 DUNE_UNUSED_PARAMETER(proc);
577 ownerVec.push_back(index);
580 template<
class T,
class I>
581 void my_push_back(std::vector<std::pair<T,int> >& ownerVec,
const I& index,
int proc)
583 ownerVec.push_back(std::make_pair(index,proc));
586 void reserve(std::vector<T>&, RedistributeInterface&,
int)
589 void reserve(std::vector<std::pair<T,int> >& ownerVec, RedistributeInterface& redist,
int proc)
591 redist.reserveSpaceForReceiveInterface(proc, ownerVec.size());
612 template<
class OwnerSet,
class G,
class IS,
class T,
class GI>
613 void getOwnerOverlapVec(
const G& graph, std::vector<int>& part, IS& indexSet,
614 int myPe,
int toPe, std::vector<T>& ownerVec, std::set<GI>& overlapSet,
615 RedistributeInterface& redist, std::set<int>& neighborProcs) {
616 DUNE_UNUSED_PARAMETER(myPe);
618 typedef typename IS::const_iterator Iterator;
619 for(Iterator index = indexSet.begin(); index != indexSet.end(); ++index) {
621 if(OwnerSet::contains(index->local().attribute()))
623 if(part[index->local()]==toPe)
625 getNeighbor<OwnerSet>(graph, part, index->local(), indexSet,
626 toPe, overlapSet, neighborProcs);
627 my_push_back(ownerVec, index->global(), toPe);
631 reserve(ownerVec, redist, toPe);
642 template<
class F,
class IS>
643 inline bool isOwner(IS& indexSet,
int index) {
645 const typename IS::IndexPair* pindex=indexSet.pair(index);
648 return F::contains(pindex->local().attribute());
652 class BaseEdgeFunctor
655 BaseEdgeFunctor(idxtype* adj,
const ParmetisDuneIndexMap& data)
660 void operator()(
const T& edge)
680 :
public BaseEdgeFunctor
682 EdgeFunctor(idxtype* adj,
const ParmetisDuneIndexMap& data, std::size_t)
683 : BaseEdgeFunctor(adj, data)
686 idxtype* getWeights()
693 template<
class G,
class V,
class E,
class VM,
class EM>
694 class EdgeFunctor<
Dune::Amg::PropertiesGraph<G,V,E,VM,EM> >
695 :
public BaseEdgeFunctor
698 EdgeFunctor(idxtype* adj,
const ParmetisDuneIndexMap& data, std::size_t s)
699 : BaseEdgeFunctor(adj, data)
701 weight_=
new idxtype[s];
705 void operator()(
const T& edge)
707 weight_[index()]=edge.properties().depends() ? 3 : 1;
708 BaseEdgeFunctor::operator()(edge);
710 idxtype* getWeights()
739 template<
class F,
class G,
class IS,
class EW>
740 void getAdjArrays(G& graph, IS& indexSet, idxtype *xadj,
746 typedef typename G::ConstVertexIterator VertexIterator;
748 typedef typename IS::const_iterator Iterator;
750 VertexIterator vend = graph.end();
753 for(VertexIterator vertex = graph.begin(); vertex != vend; ++vertex) {
754 if (isOwner<F>(indexSet,*vertex)) {
756 typedef typename G::ConstEdgeIterator EdgeIterator;
757 EdgeIterator eend = vertex.end();
758 xadj[j] = ew.index();
760 for(EdgeIterator edge = vertex.begin(); edge != eend; ++edge) {
765 xadj[j] = ew.index();
769 template<
class G,
class T1,
class T2>
773 RedistributeInterface& redistInf,
776 #ifndef METIS_VER_MAJOR
780 void METIS_PartGraphKway(
int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
781 idxtype *adjwgt,
int *wgtflag,
int *numflag,
int *nparts,
782 int *options,
int *edgecut, idxtype *part);
784 void METIS_PartGraphRecursive(
int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
785 idxtype *adjwgt,
int *wgtflag,
int *numflag,
int *nparts,
786 int *options,
int *edgecut, idxtype *part);
789 #endif // HAVE_PARMETIS
791 template<
class S,
class T>
794 for(T *cur=array, *end=array+l; cur!=end; ++cur)
798 inline bool isValidGraph(std::size_t noVtx, std::size_t gnoVtx, idxtype noEdges, idxtype* xadj,
799 idxtype* adjncy,
bool checkSymmetry)
803 for(idxtype vtx=0; vtx<(idxtype)noVtx; ++vtx) {
804 if(xadj[vtx]>noEdges||xadj[vtx]<0) {
805 std::cerr <<
"Check graph: xadj["<<vtx<<
"]="<<xadj[vtx]<<
" (>"
806 <<noEdges<<
") out of range!"<<std::endl;
809 if(xadj[vtx+1]>noEdges||xadj[vtx+1]<0) {
810 std::cerr <<
"Check graph: xadj["<<vtx+1<<
"]="<<xadj[vtx+1]<<
" (>"
811 <<noEdges<<
") out of range!"<<std::endl;
815 for(idxtype i=xadj[vtx]; i< xadj[vtx+1]; ++i) {
816 if(adjncy[i]<0||((std::size_t)adjncy[i])>gnoVtx) {
817 std::cerr<<
" Edge "<<adjncy[i]<<
" out of range ["<<0<<
","<<noVtx<<
")"
823 for(idxtype i=xadj[vtx]; i< xadj[vtx+1]; ++i) {
824 idxtype target=adjncy[i];
827 for(idxtype j=xadj[target]; j< xadj[target+1]; ++j)
831 std::cerr<<
"Edge ("<<target<<
","<<vtx<<
") "<<i<<
" time"<<std::endl;
840 template<
class M,
class T1,
class T2>
848 std::cout<<
"Repartitioning from "<<oocomm.
communicator().size()
849 <<
" to "<<nparts<<
" parts"<<std::endl;
853 int* part =
new int[1];
856 idxtype* part =
new idxtype[1];
868 typedef typename RemoteIndices::const_iterator
883 idxtype *xadj=
new idxtype[2];
884 idxtype *vtxdist=
new idxtype[oocomm.
communicator().size()+1];
885 idxtype *adjncy=
new idxtype[noNeighbours];
897 xadj[1]=noNeighbours;
927 typedef typename RemoteIndices::const_iterator NeighbourIterator;
929 idxtype* adjp=adjncy;
932 vwgt =
new idxtype[1];
935 adjwgt =
new idxtype[noNeighbours];
936 idxtype* adjwp=adjwgt;
941 if(n->first != rank) {
951 noNeighbours, xadj, adjncy,
false));
953 idxtype wgtflag=0, numflag=0;
958 float *tpwgts =
new float[nparts];
959 for(
int i=0; i<nparts; ++i)
960 tpwgts[i]=1.0/nparts;
961 int options[5] ={ 0,1,15,0,0};
964 Dune::dinfo<<rank<<
" vtxdist: ";
966 Dune::dinfo<<std::endl<<rank<<
" xadj: ";
968 Dune::dinfo<<std::endl<<rank<<
" adjncy: ";
972 Dune::dinfo<<std::endl<<rank<<
" vwgt: ";
974 Dune::dinfo<<std::endl<<rank<<
" adwgt: ";
977 Dune::dinfo<<std::endl;
980 std::cout<<
"Creating comm graph took "<<time.elapsed()<<std::endl;
983 #ifdef PARALLEL_PARTITION
990 ParMETIS_V3_PartKway(vtxdist, xadj, adjncy,
991 vwgt, adjwgt, &wgtflag,
992 &numflag, &ncon, &nparts, tpwgts, &ubvec, options, &edgecut, part,
995 std::cout<<
"ParMETIS took "<<time.elapsed()<<std::endl;
999 std::size_t gnoedges=0;
1002 Dune::dverb<<
"noNeighbours: "<<noNeighbours<<std::endl;
1004 MPI_Allgather(&noNeighbours,1,MPI_INT,noedges,1, MPI_INT,oocomm.
communicator());
1007 std::cout<<
"Gathering noedges took "<<time1.elapsed()<<std::endl;
1010 idxtype noVertices = vtxdist[oocomm.
communicator().size()];
1013 idxtype *gadjncy = 0;
1014 idxtype *gadjwgt = 0;
1022 std::size_t localNoVtx=vtxdist[rank+1]-vtxdist[rank];
1027 Dune::dinfo<<
"noedges: ";
1029 Dune::dinfo<<std::endl;
1037 noxs[i]=vtxdist[i+1]-vtxdist[i]+1;
1038 novs[i]=vtxdist[i+1]-vtxdist[i];
1041 idxtype *so= vtxdist;
1043 for(
int *xcurr = xdispl, *vcurr = vdispl, *end=vdispl+oocomm.
communicator().size();
1044 vcurr!=end; ++vcurr, ++xcurr, ++so, ++offset) {
1046 *xcurr = offset + *so;
1052 for(
int *curr=noedges, *end=noedges+oocomm.
communicator().size()-1;
1053 curr!=end; ++curr) {
1058 Dune::dinfo<<
"displ: ";
1060 Dune::dinfo<<std::endl;
1064 for(
int *curr=noedges, *end=noedges+oocomm.
communicator().size();
1069 Dune::dinfo<<
"gxadjlen: "<<gxadjlen<<
" noVertices: "<<noVertices
1070 <<
" gnoedges: "<<gnoedges<<std::endl;
1071 gxadj =
new idxtype[gxadjlen];
1072 gpart =
new idxtype[noVertices];
1074 gvwgt =
new idxtype[noVertices];
1075 gadjwgt =
new idxtype[gnoedges];
1077 gadjncy =
new idxtype[gnoedges];
1081 std::cout<<
"Preparing global graph took "<<time1.elapsed()<<std::endl;
1085 MPI_Allgatherv(xadj,2,MPITraits<idxtype>::getType(),
1086 gxadj,noxs,xdispl,MPITraits<idxtype>::getType(),
1088 MPI_Allgatherv(adjncy,noNeighbours,MPITraits<idxtype>::getType(),
1089 gadjncy,noedges,displ,MPITraits<idxtype>::getType(),
1092 MPI_Allgatherv(adjwgt,noNeighbours,MPITraits<idxtype>::getType(),
1093 gadjwgt,noedges,displ,MPITraits<idxtype>::getType(),
1095 MPI_Allgatherv(vwgt,localNoVtx,MPITraits<idxtype>::getType(),
1096 gvwgt,novs,vdispl,MPITraits<idxtype>::getType(),
1100 std::cout<<
"Gathering global graph data took "<<time1.elapsed()<<std::endl;
1110 idxtype increment = vtxdist[1];
1111 idxtype *start=gxadj+1;
1114 int lprev = vtxdist[i]-vtxdist[i-1];
1115 int l = vtxdist[i+1]-vtxdist[i];
1117 assert((start+l+offset)-gxadj<=static_cast<idxtype>(gxadjlen));
1118 increment = *(start-1);
1119 std::transform(start+offset, start+l+offset, start, std::bind2nd(std::plus<idxtype>(), increment));
1121 Dune::dinfo<<std::endl<<
"shifted xadj:";
1123 Dune::dinfo<<std::endl<<
" gadjncy: ";
1126 Dune::dinfo<<std::endl<<
" gvwgt: ";
1128 Dune::dinfo<<std::endl<<
"adjwgt: ";
1130 Dune::dinfo<<std::endl;
1134 std::cout<<
"Postprocesing global graph data took "<<time1.elapsed()<<std::endl;
1138 gxadj, gadjncy,
true));
1142 std::cout<<
"Creating grah one 1 process took "<<time.elapsed()<<std::endl;
1144 options[0]=0; options[1]=1; options[2]=1; options[3]=3; options[4]=3;
1145 #if METIS_VER_MAJOR >= 5
1147 idxtype moptions[METIS_NOPTIONS];
1148 METIS_SetDefaultOptions(moptions);
1149 moptions[METIS_OPTION_NUMBERING] = numflag;
1150 METIS_PartGraphRecursive(&noVertices, &ncon, gxadj, gadjncy, gvwgt, NULL, gadjwgt,
1151 &nparts, NULL, NULL, moptions, &edgecut, gpart);
1154 METIS_PartGraphRecursive(&noVertices, gxadj, gadjncy, gvwgt, gadjwgt, &wgtflag,
1155 &numflag, &nparts, options, &edgecut, gpart);
1159 std::cout<<
"METIS took "<<time.elapsed()<<std::endl;
1162 Dune::dinfo<<std::endl<<
"part:";
1173 MPI_Scatter(gpart, 1, MPITraits<idxtype>::getType(), part, 1,
1174 MPITraits<idxtype>::getType(), 0, comm);
1198 Dune::dinfo<<
" repart "<<rank <<
" -> "<< part[0]<<std::endl;
1200 std::vector<int> realpart(mat.N(), part[0]);
1206 std::cout<<
"Scattering repartitioning took "<<time.elapsed()<<std::endl;
1214 std::cout<<
"Filling index set took "<<time.elapsed()<<std::endl;
1222 std::cout<<
"Average no neighbours was "<<noNeighbours<<std::endl;
1227 std::cout<<
"Building index sets took "<<time.elapsed()<<std::endl;
1249 template<
class G,
class T1,
class T2>
1262 std::cout<<
"Filling holes took "<<time.elapsed()<<std::endl;
1269 double t1=0.0, t2=0.0, t3=0.0, t4=0.0, tSum=0.0;
1297 typedef typename OOComm::OwnerSet OwnerSet;
1302 ParmetisDuneIndexMap indexMap(graph,oocomm);
1304 idxtype *part =
new idxtype[indexMap.numOfOwnVtx()];
1306 std::size_t *part =
new std::size_t[indexMap.numOfOwnVtx()];
1308 for(std::size_t i=0; i < indexMap.numOfOwnVtx(); ++i)
1313 std::cerr<<
"ParMETIS not activated. Will repartition to 1 domain instead of requested "
1314 <<nparts<<
" domains."<<std::endl;
1321 idxtype *xadj =
new idxtype[indexMap.numOfOwnVtx()+1];
1322 idxtype *adjncy =
new idxtype[graph.noEdges()];
1323 EdgeFunctor<G> ef(adjncy, indexMap, graph.noEdges());
1324 getAdjArrays<OwnerSet>(graph, oocomm.
globalLookup(), xadj, ef);
1330 idxtype numflag=0, wgtflag=0, options[3], edgecut=0, ncon=1;
1332 float *tpwgts =
new float[nparts];
1333 for(
int i=0; i<nparts; ++i)
1334 tpwgts[i]=1.0/nparts;
1343 wgtflag = (ef.getWeights()!=NULL) ? 1 : 0;
1351 std::cout<<std::endl;
1352 std::cout<<
"Testing ParMETIS_V3_PartKway with options[1-2] = {"
1353 <<options[1]<<
" "<<options[2]<<
"}, Ncon: "
1354 <<ncon<<
", Nparts: "<<nparts<<std::endl;
1367 std::cout<<
"Preparing for parmetis took "<<time.elapsed()<<std::endl;
1374 ParMETIS_V3_PartKway(indexMap.vtxDist(), xadj, adjncy,
1375 NULL, ef.getWeights(), &wgtflag,
1376 &numflag, &ncon, &nparts, tpwgts, ubvec, options, &edgecut, part, &
const_cast<MPI_Comm&
>(comm));
1387 std::cout<<std::endl;
1388 std::cout<<
"ParMETIS_V3_PartKway reported a cut of "<<edgecut<<std::endl;
1389 std::cout<<std::endl;
1391 std::cout<<mype<<
": PARMETIS-Result: ";
1392 for(
int i=0; i < indexMap.vtxDist()[mype+1]-indexMap.vtxDist()[mype]; ++i) {
1393 std::cout<<part[i]<<
" ";
1395 std::cout<<std::endl;
1396 std::cout<<
"Testing ParMETIS_V3_PartKway with options[1-2] = {"
1397 <<options[1]<<
" "<<options[2]<<
"}, Ncon: "
1398 <<ncon<<
", Nparts: "<<nparts<<std::endl;
1411 std::cout<<
"Parmetis took "<<time.elapsed()<<std::endl;
1418 for(std::size_t i=0; i<indexMap.numOfOwnVtx(); ++i)
1428 std::vector<int> domainMapping(nparts);
1430 getDomain(comm, part, indexMap.numOfOwnVtx(), nparts, &myDomain, domainMapping);
1435 std::cout<<mype<<
": myDomain: "<<myDomain<<std::endl;
1436 std::cout<<mype<<
": DomainMapping: ";
1437 for(
int j=0; j<nparts; j++) {
1438 std::cout<<
" do: "<<j<<
" pe: "<<domainMapping[j]<<
" ";
1440 std::cout<<std::endl;
1447 std::vector<int> setPartition(oocomm.
indexSet().size(), -1);
1449 typedef typename OOComm::ParallelIndexSet::const_iterator Iterator;
1451 for(Iterator index = oocomm.
indexSet().begin(); index != oocomm.
indexSet().end(); ++index)
1452 if(OwnerSet::contains(index->local().attribute())) {
1453 setPartition[index->local()]=domainMapping[part[i++]];
1461 static_cast<int>(SolverCategory::nonoverlapping))
1468 std::cout<<
"Creating indexsets took "<<time.elapsed()<<std::endl;
1475 template<
class G,
class T1,
class T2>
1483 typedef typename OOComm::OwnerSet OwnerSet;
1517 std::set<int> recvFrom;
1523 typedef typename std::vector<int>::const_iterator VIter;
1527 std::set<int> tsendTo;
1528 for(VIter i=setPartition.begin(), iend = setPartition.end(); i!=iend; ++i)
1531 noSendTo = tsendTo.size();
1532 sendTo =
new int[noSendTo];
1533 typedef std::set<int>::const_iterator iterator;
1535 for(iterator i=tsendTo.begin(); i != tsendTo.end(); ++i, ++idx)
1541 int* gsendToDispl =
new int[oocomm.
communicator().size()+1];
1543 MPI_Allgather(&noSendTo, 1, MPI_INT, gnoSend, 1,
1547 int totalNoRecv = 0;
1548 for(
int i=0; i<npes; ++i)
1549 totalNoRecv += gnoSend[i];
1551 int *gsendTo =
new int[totalNoRecv];
1555 for(
int i=0; i<npes; ++i)
1556 gsendToDispl[i+1]=gsendToDispl[i]+gnoSend[i];
1559 MPI_Allgatherv(sendTo, noSendTo, MPI_INT, gsendTo, gnoSend, gsendToDispl,
1563 for(
int proc=0; proc < npes; ++proc)
1564 for(
int i=gsendToDispl[proc]; i < gsendToDispl[proc+1]; ++i)
1565 if(gsendTo[i]==mype)
1566 recvFrom.insert(proc);
1568 bool existentOnNextLevel = recvFrom.size()>0;
1572 delete[] gsendToDispl;
1577 if(recvFrom.size()) {
1578 std::cout<<mype<<
": recvFrom: ";
1579 typedef typename std::set<int>::const_iterator siter;
1580 for(siter i=recvFrom.begin(); i!= recvFrom.end(); ++i) {
1585 std::cout<<std::endl<<std::endl;
1586 std::cout<<mype<<
": sendTo: ";
1587 for(
int i=0; i<noSendTo; i++) {
1588 std::cout<<sendTo[i]<<
" ";
1590 std::cout<<std::endl<<std::endl;
1595 std::cout<<
" Communicating the receive information took "<<
1596 time.elapsed()<<std::endl;
1610 typedef typename OOComm::ParallelIndexSet::GlobalIndex GI;
1611 typedef std::vector<GI> GlobalVector;
1612 std::vector<std::pair<GI,int> > myOwnerVec;
1613 std::set<GI> myOverlapSet;
1614 GlobalVector sendOwnerVec;
1615 std::set<GI> sendOverlapSet;
1616 std::set<int> myNeighbors;
1621 char **sendBuffers=
new char*[noSendTo];
1622 MPI_Request *requests =
new MPI_Request[noSendTo];
1625 for(
int i=0; i < noSendTo; ++i) {
1627 sendOwnerVec.clear();
1628 sendOverlapSet.clear();
1631 std::set<int> neighbors;
1632 getOwnerOverlapVec<OwnerSet>(graph, setPartition, oocomm.
globalLookup(),
1633 mype, sendTo[i], sendOwnerVec, sendOverlapSet, redistInf,
1639 MPI_Pack_size(1, MPITraits<std::size_t>::getType(), oocomm.
communicator(), &buffersize);
1640 MPI_Pack_size(sendOwnerVec.size(), MPITraits<GI>::getType(), oocomm.
communicator(), &tsize);
1642 MPI_Pack_size(1, MPITraits<std::size_t>::getType(), oocomm.
communicator(), &tsize);
1644 MPI_Pack_size(sendOverlapSet.size(), MPITraits<GI>::getType(), oocomm.
communicator(), &tsize);
1645 buffersize += tsize;
1646 MPI_Pack_size(1, MPITraits<std::size_t>::getType(), oocomm.
communicator(), &tsize);
1647 buffersize += tsize;
1648 MPI_Pack_size(neighbors.size(), MPI_INT, oocomm.
communicator(), &tsize);
1649 buffersize += tsize;
1651 sendBuffers[i] =
new char[buffersize];
1654 std::cout<<mype<<
" sending "<<sendOwnerVec.size()<<
" owner and "<<
1655 sendOverlapSet.size()<<
" overlap to "<<sendTo[i]<<
" buffersize="<<buffersize<<std::endl;
1657 createSendBuf(sendOwnerVec, sendOverlapSet, neighbors, sendBuffers[i], buffersize, oocomm.
communicator());
1658 MPI_Issend(sendBuffers[i], buffersize, MPI_PACKED, sendTo[i], 99, oocomm.
communicator(), requests+i);
1664 std::cout<<
" Creating sends took "<<
1665 time.elapsed()<<std::endl;
1670 int noRecv = recvFrom.size();
1671 int oldbuffersize=0;
1676 MPI_Probe(MPI_ANY_SOURCE, 99, oocomm.
communicator(), &stat);
1678 MPI_Get_count(&stat, MPI_PACKED, &buffersize);
1680 if(oldbuffersize<buffersize) {
1683 recvBuf =
new char[buffersize];
1684 oldbuffersize = buffersize;
1686 MPI_Recv(recvBuf, buffersize, MPI_PACKED, stat.MPI_SOURCE, 99, oocomm.
communicator(), &stat);
1687 saveRecvBuf(recvBuf, buffersize, myOwnerVec, myOverlapSet, myNeighbors, redistInf,
1697 MPI_Status *statuses =
new MPI_Status[noSendTo];
1698 int send = MPI_Waitall(noSendTo, requests, statuses);
1701 if(send==MPI_ERR_IN_STATUS) {
1702 std::cerr<<mype<<
": Error in sending :"<<std::endl;
1704 for(
int i=0; i< noSendTo; i++)
1705 if(statuses[i].MPI_ERROR!=MPI_SUCCESS) {
1708 MPI_Error_string(statuses[i].MPI_ERROR, message, &messageLength);
1709 std::cerr<<
" source="<<statuses[i].MPI_SOURCE<<
" message: ";
1710 for(
int i=0; i< messageLength; i++)
1711 std::cout<<message[i];
1713 std::cerr<<std::endl;
1719 std::cout<<
" Receiving and saving took "<<
1720 time.elapsed()<<std::endl;
1724 for(
int i=0; i < noSendTo; ++i)
1725 delete[] sendBuffers[i];
1727 delete[] sendBuffers;
1742 if (!existentOnNextLevel) {
1744 color= MPI_UNDEFINED;
1746 MPI_Comm outputComm;
1754 std::vector<int> tneighbors;
1755 tneighbors.reserve(myNeighbors.size());
1757 typename OOComm::ParallelIndexSet& outputIndexSet = outcomm->indexSet();
1759 MPI_Allgather(&newrank, 1, MPI_INT, newranks, 1,
1761 typedef typename std::set<int>::const_iterator IIter;
1765 for(IIter i=myNeighbors.begin(), end=myNeighbors.end();
1767 assert(newranks[*i]>=0);
1768 std::cout<<*i<<
"->"<<newranks[*i]<<
" ";
1769 tneighbors.push_back(newranks[*i]);
1771 std::cout<<std::endl;
1773 for(IIter i=myNeighbors.begin(), end=myNeighbors.end();
1775 tneighbors.push_back(newranks[*i]);
1779 myNeighbors.clear();
1784 std::cout<<
" Calculating new neighbours ("<<tneighbors.size()<<
") took "<<
1785 time.elapsed()<<std::endl;
1790 outputIndexSet.beginResize();
1793 std::sort(myOwnerVec.begin(), myOwnerVec.end(), SortFirst());
1797 typedef typename OOComm::ParallelIndexSet::LocalIndex LocalIndex;
1798 typedef typename std::vector<std::pair<GI,int> >::const_iterator VPIter;
1800 for(VPIter g=myOwnerVec.begin(), end =myOwnerVec.end(); g!=end; ++g, ++i ) {
1801 outputIndexSet.add(g->first,LocalIndex(i, OwnerOverlapCopyAttributeSet::owner,
true));
1808 std::cout<<
" Adding owner indices took "<<
1809 time.elapsed()<<std::endl;
1818 mergeVec(myOwnerVec, myOverlapSet);
1822 myOwnerVec.swap(myOwnerVec);
1827 std::cout<<
" Merging indices took "<<
1828 time.elapsed()<<std::endl;
1834 typedef typename std::set<GI>::const_iterator SIter;
1835 for(SIter g=myOverlapSet.begin(), end=myOverlapSet.end(); g!=end; ++g, i++) {
1836 outputIndexSet.add(*g,LocalIndex(i, OwnerOverlapCopyAttributeSet::copy,
true));
1838 myOverlapSet.clear();
1839 outputIndexSet.endResize();
1841 #ifdef DUNE_ISTL_WITH_CHECKING
1843 typedef typename OOComm::ParallelIndexSet::const_iterator Iterator;
1844 Iterator end = outputIndexSet.end();
1845 for(Iterator index = outputIndexSet.begin(); index != end; ++index) {
1846 if (OwnerSet::contains(index->local().attribute())) {
1857 Iterator index=outputIndexSet.begin();
1860 for(Iterator old = outputIndexSet.begin(); index != end; old=index++) {
1861 if(old->global()>index->global())
1862 DUNE_THROW(
ISTLError,
"Index set's globalindex not sorted correctly");
1869 std::cout<<
" Adding overlap indices took "<<
1870 time.elapsed()<<std::endl;
1875 if(color != MPI_UNDEFINED) {
1876 outcomm->remoteIndices().setNeighbours(tneighbors);
1877 outcomm->remoteIndices().template rebuild<true>();
1887 std::cout<<
" Storing indexsets took "<<
1888 time.elapsed()<<std::endl;
1894 tSum = t1 + t2 + t3 + t4;
1895 std::cout<<std::endl
1896 <<mype<<
": WTime for step 1): "<<t1
1904 return color!=MPI_UNDEFINED;
1908 template<
class G,
class P,
class T1,
class T2,
class R>
1914 if(nparts!=oocomm.size())
1915 DUNE_THROW(NotImplemented,
"only available for MPI programs");
1919 template<
class G,
class P,
class T1,
class T2,
class R>
1925 if(nparts!=oocomm.size())
1926 DUNE_THROW(NotImplemented,
"only available for MPI programs");
void buildSendInterface(const std::vector< int > &toPart, const IS &idxset)
Definition: repartition.hh:261
const ParmetisDuneIndexMap & data_
Definition: repartition.hh:675
SolverCategory::Category getSolverCategory() const
Get Solver Category.
Definition: owneroverlapcopy.hh:300
A class setting up standard communication for a two-valued attribute set with owner/overlap/copy sema...
Definition: owneroverlapcopy.hh:172
void copyOwnerToAll(const T &source, T &dest) const
Communicate values from owner data points to all other data points.
Definition: owneroverlapcopy.hh:316
std::size_t i_
Definition: repartition.hh:673
idxtype * adj_
Definition: repartition.hh:674
Definition: basearray.hh:19
void copyCopyToAll(const T &source, T &dest) const
Communicate values from copy data points to all other data points.
Definition: owneroverlapcopy.hh:333
derive error class from the base class in common
Definition: istlexception.hh:16
Matrix & mat
Definition: matrixmatrix.hh:343
const ParallelIndexSet & indexSet() const
Get the underlying parallel index set.
Definition: owneroverlapcopy.hh:464
std::vector< int > duneToParmetis
Definition: repartition.hh:170
void buildReceiveInterface(std::vector< std::pair< TG, int > > &indices)
Definition: repartition.hh:291
Dune::GlobalLookupIndexSet< ParallelIndexSet > GlobalLookupIndexSet
The type of the reverse lookup of indices.
Definition: owneroverlapcopy.hh:458
The (undirected) graph of a matrix.
Definition: graph.hh:48
const CollectiveCommunication< MPI_Comm > & communicator() const
Definition: owneroverlapcopy.hh:304
bool graphRepartition(const G &graph, Dune::OwnerOverlapCopyCommunication< T1, T2 > &oocomm, idxtype nparts, Dune::OwnerOverlapCopyCommunication< T1, T2 > *&outcomm, RedistributeInterface &redistInf, bool verbose=false)
execute a graph repartition for a giving graph and indexset.
Definition: repartition.hh:1250
const GlobalLookupIndexSet & globalLookup() const
Definition: owneroverlapcopy.hh:528
int globalOwnerVertices
Definition: repartition.hh:167
void buildGlobalLookup()
Definition: owneroverlapcopy.hh:497
void fillIndexSetHoles(const G &graph, Dune::OwnerOverlapCopyCommunication< T1, T2 > &oocomm)
Fills the holes in an index set.
Definition: repartition.hh:58
int base_
Definition: repartition.hh:169
Definition: repartition.hh:253
const RemoteIndices & remoteIndices() const
Get the underlying remote indices.
Definition: owneroverlapcopy.hh:473
void freeGlobalLookup()
Definition: owneroverlapcopy.hh:522
bool isValidGraph(std::size_t noVtx, std::size_t gnoVtx, idxtype noEdges, idxtype *xadj, idxtype *adjncy, bool checkSymmetry)
Definition: repartition.hh:798
void reserveSpaceForReceiveInterface(int proc, int size)
Definition: repartition.hh:282
bool commGraphRepartition(const M &mat, Dune::OwnerOverlapCopyCommunication< T1, T2 > &oocomm, idxtype nparts, Dune::OwnerOverlapCopyCommunication< T1, T2 > *&outcomm, RedistributeInterface &redistInf, bool verbose=false)
Definition: repartition.hh:841
Dune::RemoteIndices< PIS > RemoteIndices
The type of the remote indices.
Definition: owneroverlapcopy.hh:454
Dune::ParallelIndexSet< GlobalIdType, LI, 512 > ParallelIndexSet
The type of the parallel index set.
Definition: owneroverlapcopy.hh:451
void setCommunicator(MPI_Comm comm)
Definition: repartition.hh:256
std::vector< int > parmetisToDune
Definition: repartition.hh:171
Provides classes for building the matrix graph.
void addReceiveIndex(int proc, std::size_t idx)
Definition: repartition.hh:286
Definition: example.cc:34
void print_carray(S &os, T *array, std::size_t l)
Definition: repartition.hh:792
Classes providing communication interfaces for overlapping Schwarz methods.
std::vector< idxtype > vtxDist_
Definition: repartition.hh:173
bool buildCommunication(const G &graph, std::vector< int > &realparts, Dune::OwnerOverlapCopyCommunication< T1, T2 > &oocomm, Dune::OwnerOverlapCopyCommunication< T1, T2 > *&outcomm, RedistributeInterface &redistInf, bool verbose=false)
Definition: repartition.hh:1476
Definition: owneroverlapcopy.hh:60
~RedistributeInterface()
Definition: repartition.hh:300