MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_RepartitionFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_REPARTITIONFACTORY_DEF_HPP
47 #define MUELU_REPARTITIONFACTORY_DEF_HPP
48 
49 #include <algorithm>
50 #include <iostream>
51 #include <sstream>
52 
53 #include "MueLu_RepartitionFactory_decl.hpp" // TMP JG NOTE: before other includes, otherwise I cannot test the fwd declaration in _def
54 
55 #ifdef HAVE_MPI
57 #include <Teuchos_CommHelpers.hpp>
59 
60 #include <Xpetra_Map.hpp>
61 #include <Xpetra_MapFactory.hpp>
62 #include <Xpetra_MultiVectorFactory.hpp>
63 #include <Xpetra_VectorFactory.hpp>
64 #include <Xpetra_Import.hpp>
65 #include <Xpetra_ImportFactory.hpp>
66 #include <Xpetra_Export.hpp>
67 #include <Xpetra_ExportFactory.hpp>
68 #include <Xpetra_Matrix.hpp>
69 #include <Xpetra_MatrixFactory.hpp>
70 
71 #include "MueLu_Utilities.hpp"
72 
73 #include "MueLu_CloneRepartitionInterface.hpp"
74 
75 #include "MueLu_Level.hpp"
76 #include "MueLu_MasterList.hpp"
77 #include "MueLu_Monitor.hpp"
78 #include "MueLu_PerfUtils.hpp"
79 
80 namespace MueLu {
81 
82 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84  RCP<ParameterList> validParamList = rcp(new ParameterList());
85 
86 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
87  SET_VALID_ENTRY("repartition: print partition distribution");
88  SET_VALID_ENTRY("repartition: remap parts");
89  SET_VALID_ENTRY("repartition: remap num values");
90  SET_VALID_ENTRY("repartition: remap accept partition");
91  SET_VALID_ENTRY("repartition: node repartition level");
92 #undef SET_VALID_ENTRY
93 
94  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
95  validParamList->set<RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
96  validParamList->set<RCP<const FactoryBase> >("Partition", Teuchos::null, "Factory of the partition");
97 
98  return validParamList;
99 }
100 
101 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103  Input(currentLevel, "A");
104  Input(currentLevel, "number of partitions");
105  Input(currentLevel, "Partition");
106 }
107 
108 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
110  FactoryMonitor m(*this, "Build", currentLevel);
111 
112  const Teuchos::ParameterList& pL = GetParameterList();
113  // Access parameters here to make sure that we set the parameter entry flag to "used" even in case of short-circuit evaluation.
114  // TODO (JG): I don't really know if we want to do this.
115  bool remapPartitions = pL.get<bool>("repartition: remap parts");
116 
117  // TODO: We only need a CrsGraph. This class does not have to be templated on Scalar types.
118  RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel, "A");
119  if (A == Teuchos::null) {
120  Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
121  return;
122  }
123  RCP<const Map> rowMap = A->getRowMap();
124  GO indexBase = rowMap->getIndexBase();
125  Xpetra::UnderlyingLib lib = rowMap->lib();
126 
127  RCP<const Teuchos::Comm<int> > origComm = rowMap->getComm();
128  RCP<const Teuchos::Comm<int> > comm = origComm;
129 
130  int myRank = comm->getRank();
131  int numProcs = comm->getSize();
132 
133  RCP<const Teuchos::MpiComm<int> > tmpic = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
134  TEUCHOS_TEST_FOR_EXCEPTION(tmpic == Teuchos::null, Exceptions::RuntimeError, "Cannot cast base Teuchos::Comm to Teuchos::MpiComm object.");
135  RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm();
136 
138  int numPartitions = Get<int>(currentLevel, "number of partitions");
139 
140  // ======================================================================================================
141  // Construct decomposition vector
142  // ======================================================================================================
143  RCP<GOVector> decomposition = Get<RCP<GOVector> >(currentLevel, "Partition");
144 
145  // check which factory provides "Partition"
146  if (remapPartitions == true && Teuchos::rcp_dynamic_cast<const CloneRepartitionInterface>(GetFactory("Partition")) != Teuchos::null) {
147  // if "Partition" is provided by a CloneRepartitionInterface class we have to switch of remapPartitions
148  // as we can assume the processor Ids in Partition to be the expected ones. If we would do remapping we
149  // would get different processors for the different blocks which screws up matrix-matrix multiplication.
150  remapPartitions = false;
151  }
152 
153  // check special cases
154  if (numPartitions == 1) {
155  // Trivial case: decomposition is the trivial one, all zeros. We skip the call to Zoltan_Interface
156  // (this is mostly done to avoid extra output messages, as even if we didn't skip there is a shortcut
157  // in Zoltan[12]Interface).
158  // TODO: We can probably skip more work in this case (like building all extra data structures)
159  GetOStream(Runtime0) << "Only one partition: Skip call to the repartitioner." << std::endl;
160  } else if (numPartitions == -1) {
161  // No repartitioning necessary: decomposition should be Teuchos::null
162  GetOStream(Runtime0) << "No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
163  Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
164  return;
165  }
166 
167  // If we're doing node away, we need to be sure to get the mapping to the NodeComm's rank 0.
168  const int nodeRepartLevel = pL.get<int>("repartition: node repartition level");
169  if (currentLevel.GetLevelID() == nodeRepartLevel) {
170  // NodePartitionInterface returns the *ranks* of the guy who gets the info, not the *partition number*
171  // In a sense, we've already done remap here.
172 
173  // FIXME: We need a low-comm import construction
174  remapPartitions = false;
175  }
176 
177  // ======================================================================================================
178  // Remap if necessary
179  // ======================================================================================================
180  // From a user perspective, we want user to not care about remapping, thinking of it as only a performance feature.
181  // There are two problems, however.
182  // (1) Next level aggregation depends on the order of GIDs in the vector, if one uses "natural" or "random" orderings.
183  // This also means that remapping affects next level aggregation, despite the fact that the _set_ of GIDs for
184  // each partition is the same.
185  // (2) Even with the fixed order of GIDs, the remapping may influence the aggregation for the next-next level.
186  // Let us consider the following example. Lets assume that when we don't do remapping, processor 0 would have
187  // GIDs {0,1,2}, and processor 1 GIDs {3,4,5}, and if we do remapping processor 0 would contain {3,4,5} and
188  // processor 1 {0,1,2}. Now, when we run repartitioning algorithm on the next level (say Zoltan1 RCB), it may
189  // be dependent on whether whether it is [{0,1,2}, {3,4,5}] or [{3,4,5}, {0,1,2}]. Specifically, the tie-breaking
190  // algorithm can resolve these differently. For instance, running
191  // mpirun -np 5 ./MueLu_ScalingTestParamList.exe --xml=easy_sa.xml --nx=12 --ny=12 --nz=12
192  // with
193  // <ParameterList name="MueLu">
194  // <Parameter name="coarse: max size" type="int" value="1"/>
195  // <Parameter name="repartition: enable" type="bool" value="true"/>
196  // <Parameter name="repartition: min rows per proc" type="int" value="2"/>
197  // <ParameterList name="level 1">
198  // <Parameter name="repartition: remap parts" type="bool" value="false/true"/>
199  // </ParameterList>
200  // </ParameterList>
201  // produces different repartitioning for level 2.
202  // This different repartitioning may then escalate into different aggregation for the next level.
203  //
204  // We fix (1) by fixing the order of GIDs in a vector by sorting the resulting vector.
205  // Fixing (2) is more complicated.
206  // FIXME: Fixing (2) in Zoltan may not be enough, as we may use some arbitration in MueLu,
207  // for instance with CoupledAggregation. What we really need to do is to use the same order of processors containing
208  // the same order of GIDs. To achieve that, the newly created subcommunicator must be conforming with the order. For
209  // instance, if we have [{0,1,2}, {3,4,5}], we create a subcommunicator where processor 0 gets rank 0, and processor 1
210  // gets rank 1. If, on the other hand, we have [{3,4,5}, {0,1,2}], we assign rank 1 to processor 0, and rank 0 to processor 1.
211  // This rank permutation requires help from Epetra/Tpetra, both of which have no such API in place.
212  // One should also be concerned that if we had such API in place, rank 0 in subcommunicator may no longer be rank 0 in
213  // MPI_COMM_WORLD, which may lead to issues for logging.
214  if (remapPartitions) {
215  SubFactoryMonitor m1(*this, "DeterminePartitionPlacement", currentLevel);
216 
217  bool acceptPartition = pL.get<bool>("repartition: remap accept partition");
218  bool allSubdomainsAcceptPartitions;
219  int localNumAcceptPartition = acceptPartition;
220  int globalNumAcceptPartition;
221  MueLu_sumAll(comm, localNumAcceptPartition, globalNumAcceptPartition);
222  GetOStream(Statistics2) << "Number of ranks that accept partitions: " << globalNumAcceptPartition << std::endl;
223  if (globalNumAcceptPartition < numPartitions) {
224  GetOStream(Warnings0) << "Not enough ranks are willing to accept a partition, allowing partitions on all ranks." << std::endl;
225  acceptPartition = true;
226  allSubdomainsAcceptPartitions = true;
227  } else if (numPartitions > numProcs) {
228  // We are trying to repartition to a larger communicator.
229  allSubdomainsAcceptPartitions = true;
230  } else {
231  allSubdomainsAcceptPartitions = false;
232  }
233 
234  DeterminePartitionPlacement(*A, *decomposition, numPartitions, acceptPartition, allSubdomainsAcceptPartitions);
235  }
236 
237  // ======================================================================================================
238  // Construct importer
239  // ======================================================================================================
240  // At this point, the following is true:
241  // * Each processors owns 0 or 1 partitions
242  // * If a processor owns a partition, that partition number is equal to the processor rank
243  // * The decomposition vector contains the partitions ids that the corresponding GID belongs to
244 
245  ArrayRCP<const GO> decompEntries;
246  if (decomposition->getLocalLength() > 0)
247  decompEntries = decomposition->getData(0);
248 
249 #ifdef HAVE_MUELU_DEBUG
250  // Test range of partition ids
251  int incorrectRank = -1;
252  for (int i = 0; i < decompEntries.size(); i++)
253  if (decompEntries[i] >= numProcs || decompEntries[i] < 0) {
254  incorrectRank = myRank;
255  break;
256  }
257 
258  int incorrectGlobalRank = -1;
259  MueLu_maxAll(comm, incorrectRank, incorrectGlobalRank);
260  TEUCHOS_TEST_FOR_EXCEPTION(incorrectGlobalRank > -1, Exceptions::RuntimeError, "pid " + Teuchos::toString(incorrectGlobalRank) + " encountered a partition number is that out-of-range");
261 #endif
262 
263  Array<GO> myGIDs;
264  myGIDs.reserve(decomposition->getLocalLength());
265 
266  // Step 0: Construct mapping
267  // part number -> GIDs I own which belong to this part
268  // NOTE: my own part GIDs are not part of the map
269  typedef std::map<GO, Array<GO> > map_type;
270  map_type sendMap;
271  for (LO i = 0; i < decompEntries.size(); i++) {
272  GO id = decompEntries[i];
273  GO GID = rowMap->getGlobalElement(i);
274 
275  if (id == myRank)
276  myGIDs.push_back(GID);
277  else
278  sendMap[id].push_back(GID);
279  }
280  decompEntries = Teuchos::null;
281 
282  if (IsPrint(Statistics2)) {
283  GO numLocalKept = myGIDs.size(), numGlobalKept, numGlobalRows = A->getGlobalNumRows();
284  MueLu_sumAll(comm, numLocalKept, numGlobalKept);
285  GetOStream(Statistics2) << "Unmoved rows: " << numGlobalKept << " / " << numGlobalRows << " (" << 100 * Teuchos::as<double>(numGlobalKept) / numGlobalRows << "%)" << std::endl;
286  }
287 
288  int numSend = sendMap.size(), numRecv;
289 
290  // Arrayify map keys
291  Array<GO> myParts(numSend), myPart(1);
292  int cnt = 0;
293  myPart[0] = myRank;
294  for (typename map_type::const_iterator it = sendMap.begin(); it != sendMap.end(); it++)
295  myParts[cnt++] = it->first;
296 
297  // Step 1: Find out how many processors send me data
298  // partsIndexBase starts from zero, as the processors ids start from zero
299  {
300  SubFactoryMonitor m1(*this, "Mapping Step 1", currentLevel);
301  GO partsIndexBase = 0;
302  RCP<Map> partsIHave = MapFactory ::Build(lib, Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), myParts(), partsIndexBase, comm);
303  RCP<Map> partsIOwn = MapFactory ::Build(lib, numProcs, myPart(), partsIndexBase, comm);
304  RCP<Export> partsExport = ExportFactory::Build(partsIHave, partsIOwn);
305 
308  if (numSend) {
309  ArrayRCP<GO> partsISendData = partsISend->getDataNonConst(0);
310  for (int i = 0; i < numSend; i++)
311  partsISendData[i] = 1;
312  }
313  (numPartsIRecv->getDataNonConst(0))[0] = 0;
314 
315  numPartsIRecv->doExport(*partsISend, *partsExport, Xpetra::ADD);
316  numRecv = (numPartsIRecv->getData(0))[0];
317  }
318 
319  // Step 2: Get my GIDs from everybody else
320  MPI_Datatype MpiType = Teuchos::Details::MpiTypeTraits<GO>::getType();
321  int msgTag = 12345; // TODO: use Comm::dup for all internal messaging
322 
323  // Post sends
324  Array<MPI_Request> sendReqs(numSend);
325  cnt = 0;
326  for (typename map_type::iterator it = sendMap.begin(); it != sendMap.end(); it++)
327  MPI_Isend(static_cast<void*>(it->second.getRawPtr()), it->second.size(), MpiType, Teuchos::as<GO>(it->first), msgTag, *rawMpiComm, &sendReqs[cnt++]);
328 
329  map_type recvMap;
330  size_t totalGIDs = myGIDs.size();
331  for (int i = 0; i < numRecv; i++) {
332  MPI_Status status;
333  MPI_Probe(MPI_ANY_SOURCE, msgTag, *rawMpiComm, &status);
334 
335  // Get rank and number of elements from status
336  int fromRank = status.MPI_SOURCE, count;
337  MPI_Get_count(&status, MpiType, &count);
338 
339  recvMap[fromRank].resize(count);
340  MPI_Recv(static_cast<void*>(recvMap[fromRank].getRawPtr()), count, MpiType, fromRank, msgTag, *rawMpiComm, &status);
341 
342  totalGIDs += count;
343  }
344 
345  // Do waits on send requests
346  if (numSend) {
347  Array<MPI_Status> sendStatuses(numSend);
348  MPI_Waitall(numSend, sendReqs.getRawPtr(), sendStatuses.getRawPtr());
349  }
350 
351  // Merge GIDs
352  myGIDs.reserve(totalGIDs);
353  for (typename map_type::const_iterator it = recvMap.begin(); it != recvMap.end(); it++) {
354  int offset = myGIDs.size(), len = it->second.size();
355  if (len) {
356  myGIDs.resize(offset + len);
357  memcpy(myGIDs.getRawPtr() + offset, it->second.getRawPtr(), len * sizeof(GO));
358  }
359  }
360  // NOTE 2: The general sorting algorithm could be sped up by using the knowledge that original myGIDs and all received chunks
361  // (i.e. it->second) are sorted. Therefore, a merge sort would work well in this situation.
362  std::sort(myGIDs.begin(), myGIDs.end());
363 
364  // Step 3: Construct importer
365  RCP<Map> newRowMap;
366  {
367  SubFactoryMonitor m1(*this, "Map construction", currentLevel);
368  newRowMap = MapFactory ::Build(lib, rowMap->getGlobalNumElements(), myGIDs(), indexBase, origComm);
369  }
370 
371  RCP<const Import> rowMapImporter;
372 
373  RCP<const BlockedMap> blockedRowMap = Teuchos::rcp_dynamic_cast<const BlockedMap>(rowMap);
374 
375  {
376  SubFactoryMonitor m1(*this, "Import construction", currentLevel);
377  // Generate a nonblocked rowmap if we need one
378  if (blockedRowMap.is_null())
379  rowMapImporter = ImportFactory::Build(rowMap, newRowMap);
380  else {
381  rowMapImporter = ImportFactory::Build(blockedRowMap->getMap(), newRowMap);
382  }
383  }
384 
385  // If we're running BlockedCrs we should chop up the newRowMap into a newBlockedRowMap here (and do likewise for importers)
386  if (!blockedRowMap.is_null()) {
387  SubFactoryMonitor m1(*this, "Blocking newRowMap and Importer", currentLevel);
388  RCP<const BlockedMap> blockedTargetMap = MueLu::UtilitiesBase<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GeneratedBlockedTargetMap(*blockedRowMap, *rowMapImporter);
389 
390  // NOTE: This code qualifies as "correct but not particularly performant" If this needs to be sped up, we can probably read data from the existing importer to
391  // build sub-importers rather than generating new ones ex nihilo
392  size_t numBlocks = blockedRowMap->getNumMaps();
393  std::vector<RCP<const Import> > subImports(numBlocks);
394 
395  for (size_t i = 0; i < numBlocks; i++) {
396  RCP<const Map> source = blockedRowMap->getMap(i);
397  RCP<const Map> target = blockedTargetMap->getMap(i);
398  subImports[i] = ImportFactory::Build(source, target);
399  }
400  Set(currentLevel, "SubImporters", subImports);
401  }
402 
403  Set(currentLevel, "Importer", rowMapImporter);
404 
405  // ======================================================================================================
406  // Print some data
407  // ======================================================================================================
408  if (!rowMapImporter.is_null() && IsPrint(Statistics2)) {
409  // int oldRank = SetProcRankVerbose(rebalancedAc->getRowMap()->getComm()->getRank());
410  GetOStream(Statistics2) << PerfUtils::PrintImporterInfo(rowMapImporter, "Importer for rebalancing");
411  // SetProcRankVerbose(oldRank);
412  }
413  if (pL.get<bool>("repartition: print partition distribution") && IsPrint(Statistics2)) {
414  // Print the grid of processors
415  GetOStream(Statistics2) << "Partition distribution over cores (ownership is indicated by '+')" << std::endl;
416 
417  char amActive = (myGIDs.size() ? 1 : 0);
418  std::vector<char> areActive(numProcs, 0);
419  MPI_Gather(&amActive, 1, MPI_CHAR, &areActive[0], 1, MPI_CHAR, 0, *rawMpiComm);
420 
421  int rowWidth = std::min(Teuchos::as<int>(ceil(sqrt(numProcs))), 100);
422  for (int proc = 0; proc < numProcs; proc += rowWidth) {
423  for (int j = 0; j < rowWidth; j++)
424  if (proc + j < numProcs)
425  GetOStream(Statistics2) << (areActive[proc + j] ? "+" : ".");
426  else
427  GetOStream(Statistics2) << " ";
428 
429  GetOStream(Statistics2) << " " << proc << ":" << std::min(proc + rowWidth, numProcs) - 1 << std::endl;
430  }
431  }
432 
433 } // Build
434 
435 //----------------------------------------------------------------------
436 template <typename T, typename W>
437 struct Triplet {
438  T i, j;
439  W v;
440 };
441 template <typename T, typename W>
442 static bool compareTriplets(const Triplet<T, W>& a, const Triplet<T, W>& b) {
443  return (a.v > b.v); // descending order
444 }
445 
446 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
447 void RepartitionFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
448  DeterminePartitionPlacement(const Matrix& A, GOVector& decomposition, GO numPartitions, bool willAcceptPartition, bool allSubdomainsAcceptPartitions) const {
449  RCP<const Map> rowMap = A.getRowMap();
450 
451  RCP<const Teuchos::Comm<int> > comm = rowMap->getComm()->duplicate();
452  int numProcs = comm->getSize();
453 
454  RCP<const Teuchos::MpiComm<int> > tmpic = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
455  TEUCHOS_TEST_FOR_EXCEPTION(tmpic == Teuchos::null, Exceptions::RuntimeError, "Cannot cast base Teuchos::Comm to Teuchos::MpiComm object.");
456  RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm();
457 
458  const Teuchos::ParameterList& pL = GetParameterList();
459 
460  // maxLocal is a constant which determins the number of largest edges which are being exchanged
461  // The idea is that we do not want to construct the full bipartite graph, but simply a subset of
462  // it, which requires less communication. By selecting largest local edges we hope to achieve
463  // similar results but at a lower cost.
464  const int maxLocal = pL.get<int>("repartition: remap num values");
465  const int dataSize = 2 * maxLocal;
466 
467  ArrayRCP<GO> decompEntries;
468  if (decomposition.getLocalLength() > 0)
469  decompEntries = decomposition.getDataNonConst(0);
470 
471  // Step 1: Sort local edges by weight
472  // Each edge of a bipartite graph corresponds to a triplet (i, j, v) where
473  // i: processor id that has some piece of part with part_id = j
474  // j: part id
475  // v: weight of the edge
476  // We set edge weights to be the total number of nonzeros in rows on this processor which
477  // correspond to this part_id. The idea is that when we redistribute matrix, this weight
478  // is a good approximation of the amount of data to move.
479  // We use two maps, original which maps a partition id of an edge to the corresponding weight,
480  // and a reverse one, which is necessary to sort by edges.
481  std::map<GO, GO> lEdges;
482  if (willAcceptPartition)
483  for (LO i = 0; i < decompEntries.size(); i++)
484  lEdges[decompEntries[i]] += A.getNumEntriesInLocalRow(i);
485 
486  // Reverse map, so that edges are sorted by weight.
487  // This results in multimap, as we may have edges with the same weight
488  std::multimap<GO, GO> revlEdges;
489  for (typename std::map<GO, GO>::const_iterator it = lEdges.begin(); it != lEdges.end(); it++)
490  revlEdges.insert(std::make_pair(it->second, it->first));
491 
492  // Both lData and gData are arrays of data which we communicate. The data is stored
493  // in pairs, so that data[2*i+0] is the part index, and data[2*i+1] is the corresponding edge weight.
494  // We do not store processor id in data, as we can compute that by looking on the offset in the gData.
495  Array<GO> lData(dataSize, -1), gData(numProcs * dataSize);
496  int numEdges = 0;
497  for (typename std::multimap<GO, GO>::reverse_iterator rit = revlEdges.rbegin(); rit != revlEdges.rend() && numEdges < maxLocal; rit++) {
498  lData[2 * numEdges + 0] = rit->second; // part id
499  lData[2 * numEdges + 1] = rit->first; // edge weight
500  numEdges++;
501  }
502 
503  // Step 2: Gather most edges
504  // Each processors contributes maxLocal edges by providing maxLocal pairs <part id, weight>, which is of size dataSize
505  MPI_Datatype MpiType = Teuchos::Details::MpiTypeTraits<GO>::getType();
506  MPI_Allgather(static_cast<void*>(lData.getRawPtr()), dataSize, MpiType, static_cast<void*>(gData.getRawPtr()), dataSize, MpiType, *rawMpiComm);
507 
508  // Step 3: Construct mapping
509 
510  // Construct the set of triplets
511  Teuchos::Array<Triplet<int, int> > gEdges(numProcs * maxLocal);
512  Teuchos::Array<bool> procWillAcceptPartition(numProcs, allSubdomainsAcceptPartitions);
513  size_t k = 0;
514  for (LO i = 0; i < gData.size(); i += 2) {
515  int procNo = i / dataSize; // determine the processor by its offset (since every processor sends the same amount)
516  GO part = gData[i + 0];
517  GO weight = gData[i + 1];
518  if (part != -1) { // skip nonexistent edges
519  gEdges[k].i = procNo;
520  gEdges[k].j = part;
521  gEdges[k].v = weight;
522  procWillAcceptPartition[procNo] = true;
523  k++;
524  }
525  }
526  gEdges.resize(k);
527 
528  // Sort edges by weight
529  // NOTE: compareTriplets is actually a reverse sort, so the edges weight is in decreasing order
530  std::sort(gEdges.begin(), gEdges.end(), compareTriplets<int, int>);
531 
532  // Do matching
533  std::map<int, int> match;
534  Teuchos::Array<char> matchedRanks(numProcs, 0), matchedParts(numPartitions, 0);
535  int numMatched = 0;
536  for (typename Teuchos::Array<Triplet<int, int> >::const_iterator it = gEdges.begin(); it != gEdges.end(); it++) {
537  GO rank = it->i;
538  GO part = it->j;
539  if (matchedRanks[rank] == 0 && matchedParts[part] == 0) {
540  matchedRanks[rank] = 1;
541  matchedParts[part] = 1;
542  match[part] = rank;
543  numMatched++;
544  }
545  }
546  GetOStream(Statistics1) << "Number of unassigned partitions before cleanup stage: " << (numPartitions - numMatched) << " / " << numPartitions << std::endl;
547 
548  // Step 4: Assign unassigned partitions if necessary.
549  // We do that through desperate matching for remaining partitions:
550  // We select the lowest rank that can still take a partition.
551  // The reason it is done this way is that we don't need any extra communication, as we don't
552  // need to know which parts are valid.
553  if (numPartitions - numMatched > 0) {
554  Teuchos::Array<char> partitionCounts(numPartitions, 0);
555  for (typename std::map<int, int>::const_iterator it = match.begin(); it != match.end(); it++)
556  partitionCounts[it->first] += 1;
557  for (int part = 0, matcher = 0; part < numPartitions; part++) {
558  if (partitionCounts[part] == 0) {
559  // Find first non-matched rank that accepts partitions
560  while (matchedRanks[matcher] || !procWillAcceptPartition[matcher])
561  matcher++;
562 
563  match[part] = matcher++;
564  numMatched++;
565  }
566  }
567  }
568 
569  TEUCHOS_TEST_FOR_EXCEPTION(numMatched != numPartitions, Exceptions::RuntimeError, "MueLu::RepartitionFactory::DeterminePartitionPlacement: Only " << numMatched << " partitions out of " << numPartitions << " got assigned to ranks.");
570 
571  // Step 5: Permute entries in the decomposition vector
572  for (LO i = 0; i < decompEntries.size(); i++)
573  decompEntries[i] = match[decompEntries[i]];
574 }
575 
576 } // namespace MueLu
577 
578 #endif // ifdef HAVE_MPI
579 
580 #endif // MUELU_REPARTITIONFACTORY_DEF_HPP
Important warning messages (one line)
void Build(Level &currentLevel) const
Build an object with this factory.
void reserve(size_type n)
#define MueLu_sumAll(rcpComm, in, out)
#define MueLu_maxAll(rcpComm, in, out)
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print more statistics.
LocalOrdinal LO
One-liner description of what is happening.
size_type size() const
#define SET_VALID_ENTRY(name)
static std::string PrintImporterInfo(RCP< const Import > importer, const std::string &msgTag)
Print even more statistics.
static bool compareTriplets(const Triplet< T, W > &a, const Triplet< T, W > &b)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static RCP< const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > > GeneratedBlockedTargetMap(const Xpetra::BlockedMap< LocalOrdinal, GlobalOrdinal, Node > &sourceBlockedMap, const Xpetra::Import< LocalOrdinal, GlobalOrdinal, Node > &Importer)
void resize(size_type new_size, const value_type &x=value_type())
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
iterator end()
void DeclareInput(Level &currentLevel) const
Determines the data that RepartitionFactory needs, and the factories that generate that data...
void push_back(const value_type &x)
size_type size() const
Exception throws to report errors in the internal logical of the program.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
iterator begin()
std::string toString(const T &t)