MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_RebalanceTransferFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef MUELU_REBALANCETRANSFERFACTORY_DEF_HPP
11 #define MUELU_REBALANCETRANSFERFACTORY_DEF_HPP
12 
13 #include <sstream>
14 #include <Teuchos_Tuple.hpp>
15 
16 #include "Xpetra_MultiVector.hpp"
17 #include "Xpetra_MultiVectorFactory.hpp"
18 #include "Xpetra_Vector.hpp"
19 #include "Xpetra_VectorFactory.hpp"
20 #include <Xpetra_Matrix.hpp>
21 #include <Xpetra_MapFactory.hpp>
22 #include <Xpetra_MatrixFactory.hpp>
23 #include <Xpetra_Import.hpp>
24 #include <Xpetra_ImportFactory.hpp>
25 #include <Xpetra_IO.hpp>
26 
28 
29 #include "MueLu_Level.hpp"
30 #include "MueLu_MasterList.hpp"
31 #include "MueLu_Monitor.hpp"
32 #include "MueLu_PerfUtils.hpp"
33 
34 namespace MueLu {
35 
36 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
38 
39 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
41 
42 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
44  RCP<ParameterList> validParamList = rcp(new ParameterList());
45 
46 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
47  SET_VALID_ENTRY("repartition: rebalance P and R");
48  SET_VALID_ENTRY("repartition: explicit via new copy rebalance P and R");
49  SET_VALID_ENTRY("repartition: rebalance Nullspace");
50  SET_VALID_ENTRY("transpose: use implicit");
51  SET_VALID_ENTRY("repartition: use subcommunicators");
52 #undef SET_VALID_ENTRY
53 
54  {
55  typedef Teuchos::StringValidator validatorType;
56  RCP<validatorType> typeValidator = rcp(new validatorType(Teuchos::tuple<std::string>("Interpolation", "Restriction")));
57  validParamList->set("type", "Interpolation", "Type of the transfer operator that need to be rebalanced (Interpolation or Restriction)", typeValidator);
58  }
59 
60  validParamList->set<RCP<const FactoryBase> >("P", null, "Factory of the prolongation operator that need to be rebalanced (only used if type=Interpolation)");
61  validParamList->set<RCP<const FactoryBase> >("R", null, "Factory of the restriction operator that need to be rebalanced (only used if type=Restriction)");
62  validParamList->set<RCP<const FactoryBase> >("Nullspace", null, "Factory of the nullspace that need to be rebalanced (only used if type=Interpolation)");
63  validParamList->set<RCP<const FactoryBase> >("Coordinates", null, "Factory of the coordinates that need to be rebalanced (only used if type=Interpolation)");
64  validParamList->set<RCP<const FactoryBase> >("BlockNumber", null, "Factory of the block ids that need to be rebalanced (only used if type=Interpolation)");
65  validParamList->set<RCP<const FactoryBase> >("Importer", null, "Factory of the importer object used for the rebalancing");
66  validParamList->set<int>("write start", -1, "First level at which coordinates should be written to file");
67  validParamList->set<int>("write end", -1, "Last level at which coordinates should be written to file");
68 
69  // TODO validation: "P" parameter valid only for type="Interpolation" and "R" valid only for type="Restriction". Like so:
70  // if (paramList.isEntry("type") && paramList.get("type) == "Interpolation) {
71  // validParamList->set< RCP<const FactoryBase> >("P", Teuchos::null, "Factory of the prolongation operator that need to be rebalanced (only used if type=Interpolation)");
72 
73  return validParamList;
74 }
75 
76 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
78  const ParameterList& pL = GetParameterList();
79 
80  if (pL.get<std::string>("type") == "Interpolation") {
81  Input(coarseLevel, "P");
82  if (pL.get<bool>("repartition: rebalance Nullspace"))
83  Input(coarseLevel, "Nullspace");
84  if (pL.get<RCP<const FactoryBase> >("Coordinates") != Teuchos::null)
85  Input(coarseLevel, "Coordinates");
86  if (pL.get<RCP<const FactoryBase> >("BlockNumber") != Teuchos::null)
87  Input(coarseLevel, "BlockNumber");
88 
89  } else {
90  if (pL.get<bool>("transpose: use implicit") == false)
91  Input(coarseLevel, "R");
92  }
93 
94  Input(coarseLevel, "Importer");
95 }
96 
97 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
99  FactoryMonitor m(*this, "Build", coarseLevel);
101 
102  const ParameterList& pL = GetParameterList();
103 
104  RCP<Matrix> originalP = Get<RCP<Matrix> >(coarseLevel, "P");
105  // If we don't have a valid P (e.g., # global aggregates is 0), skip this rebalancing. This level will
106  // ultimately be removed in MueLu_Hierarchy_defs.h via a resize()
107  if (originalP == Teuchos::null) {
108  Set(coarseLevel, "P", originalP);
109  return;
110  }
111  int implicit = !pL.get<bool>("repartition: rebalance P and R");
112  int reallyExplicit = pL.get<bool>("repartition: explicit via new copy rebalance P and R");
113  int writeStart = pL.get<int>("write start");
114  int writeEnd = pL.get<int>("write end");
115 
116  if (writeStart == 0 && fineLevel.GetLevelID() == 0 && writeStart <= writeEnd && IsAvailable(fineLevel, "Coordinates")) {
117  std::string fileName = "coordinates_level_0.m";
118  RCP<xdMV> fineCoords = fineLevel.Get<RCP<xdMV> >("Coordinates");
119  if (fineCoords != Teuchos::null)
120  Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Write(fileName, *fineCoords);
121  }
122 
123  if (writeStart == 0 && fineLevel.GetLevelID() == 0 && writeStart <= writeEnd && IsAvailable(fineLevel, "BlockNumber")) {
124  std::string fileName = "BlockNumber_level_0.m";
125  RCP<LocalOrdinalVector> fineBlockNumber = fineLevel.Get<RCP<LocalOrdinalVector> >("BlockNumber");
126  if (fineBlockNumber != Teuchos::null)
127  Xpetra::IO<SC, LO, GO, NO>::WriteLOMV(fileName, *fineBlockNumber);
128  }
129 
130  RCP<const Import> importer = Get<RCP<const Import> >(coarseLevel, "Importer");
131  if (implicit) {
132  // Save the importer, we'll need it for solve
133  coarseLevel.Set("Importer", importer, NoFactory::get());
134  }
135 
136  RCP<ParameterList> params = rcp(new ParameterList());
137  if (IsPrint(Statistics2)) {
138  params->set("printLoadBalancingInfo", true);
139  params->set("printCommInfo", true);
140  }
141 
142  std::string transferType = pL.get<std::string>("type");
143  if (transferType == "Interpolation") {
144  originalP = Get<RCP<Matrix> >(coarseLevel, "P");
145 
146  {
147  // This line must be after the Get call
148  SubFactoryMonitor m1(*this, "Rebalancing prolongator", coarseLevel);
149 
150  if (implicit || importer.is_null()) {
151  GetOStream(Runtime0) << "Using original prolongator" << std::endl;
152  Set(coarseLevel, "P", originalP);
153 
154  } else {
155  // There are two version of an explicit rebalanced P and R.
156  // The !reallyExplicit way, is sufficient for all MueLu purposes
157  // with the exception of the CombinePFactory that needs true domain
158  // and column maps.
159  // !reallyExplicit:
160  // Rather than calling fillComplete (which would entail creating a new
161  // column map), it's sufficient to replace the domain map and importer.
162  // Note that this potentially violates the assumption that in the
163  // column map, local IDs appear before any off-rank IDs.
164  //
165  // reallyExplicit:
166  // P transfers from coarse grid to the fine grid. Here, we change
167  // the domain map (coarse) of Paccording to the new partition. The
168  // range map (fine) is kept unchanged.
169  //
170  // The domain map of P must match the range map of R. To change the
171  // domain map of P, P needs to be fillCompleted again with the new
172  // domain map. To achieve this, P is copied into a new matrix that
173  // is not fill-completed. The doImport() operation is just used
174  // here to make a copy of P: the importer is trivial and there is
175  // no data movement involved. The reordering actually happens during
176  // fillComplete() with domainMap == importer->getTargetMap().
177 
178  RCP<Matrix> rebalancedP;
179  if (reallyExplicit) {
180  size_t totalMaxPerRow = 0;
181  ArrayRCP<size_t> nnzPerRow(originalP->getRowMap()->getLocalNumElements(), 0);
182  for (size_t i = 0; i < originalP->getRowMap()->getLocalNumElements(); ++i) {
183  nnzPerRow[i] = originalP->getNumEntriesInLocalRow(i);
184  if (nnzPerRow[i] > totalMaxPerRow) totalMaxPerRow = nnzPerRow[i];
185  }
186 
187  rebalancedP = MatrixFactory::Build(originalP->getRowMap(), totalMaxPerRow);
188 
189  {
190  RCP<Import> trivialImporter = ImportFactory::Build(originalP->getRowMap(), originalP->getRowMap());
191  SubFactoryMonitor m2(*this, "Rebalancing prolongator -- import only", coarseLevel);
192  rebalancedP->doImport(*originalP, *trivialImporter, Xpetra::INSERT);
193  }
194  rebalancedP->fillComplete(importer->getTargetMap(), originalP->getRangeMap());
195 
196  } else {
197  rebalancedP = originalP;
198  RCP<const CrsMatrixWrap> crsOp = rcp_dynamic_cast<const CrsMatrixWrap>(originalP);
199  TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null, Exceptions::BadCast, "Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
200 
201  RCP<CrsMatrix> rebalancedP2 = crsOp->getCrsMatrix();
202  TEUCHOS_TEST_FOR_EXCEPTION(rebalancedP2 == Teuchos::null, std::runtime_error, "Xpetra::CrsMatrixWrap doesn't have a CrsMatrix");
203 
204  {
205  SubFactoryMonitor subM(*this, "Rebalancing prolongator -- fast map replacement", coarseLevel);
206 
207  RCP<const Import> newImporter;
208  {
209  SubFactoryMonitor subM2(*this, "Import construction", coarseLevel);
210  newImporter = ImportFactory::Build(importer->getTargetMap(), rebalancedP->getColMap());
211  }
212  rebalancedP2->replaceDomainMapAndImporter(importer->getTargetMap(), newImporter);
213  }
214  }
216  // TODO FIXME somehow we have to transfer the striding information of the permuted domain/range maps.
217  // That is probably something for an external permutation factory
218  // if (originalP->IsView("stridedMaps"))
219  // rebalancedP->CreateView("stridedMaps", originalP);
221  if (!rebalancedP.is_null()) {
222  std::ostringstream oss;
223  oss << "P_" << coarseLevel.GetLevelID();
224  rebalancedP->setObjectLabel(oss.str());
225  }
226  Set(coarseLevel, "P", rebalancedP);
227 
228  if (IsPrint(Statistics2))
229  GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*rebalancedP, "P (rebalanced)", params);
230  }
231  }
232 
233  if (importer.is_null()) {
234  if (IsAvailable(coarseLevel, "Nullspace"))
235  Set(coarseLevel, "Nullspace", Get<RCP<MultiVector> >(coarseLevel, "Nullspace"));
236 
237  if (pL.isParameter("Coordinates") && pL.get<RCP<const FactoryBase> >("Coordinates") != Teuchos::null)
238  if (IsAvailable(coarseLevel, "Coordinates"))
239  Set(coarseLevel, "Coordinates", Get<RCP<xdMV> >(coarseLevel, "Coordinates"));
240 
241  if (pL.isParameter("BlockNumber") && pL.get<RCP<const FactoryBase> >("BlockNumber") != Teuchos::null)
242  if (IsAvailable(coarseLevel, "BlockNumber"))
243  Set(coarseLevel, "BlockNumber", Get<RCP<LocalOrdinalVector> >(coarseLevel, "BlockNumber"));
244 
245  return;
246  }
247 
248  if (pL.isParameter("Coordinates") &&
249  pL.get<RCP<const FactoryBase> >("Coordinates") != Teuchos::null &&
250  IsAvailable(coarseLevel, "Coordinates")) {
251  RCP<xdMV> coords = Get<RCP<xdMV> >(coarseLevel, "Coordinates");
252 
253  // This line must be after the Get call
254  SubFactoryMonitor subM(*this, "Rebalancing coordinates", coarseLevel);
255 
256  LO nodeNumElts = coords->getMap()->getLocalNumElements();
257 
258  // If a process has no matrix rows, then we can't calculate blocksize using the formula below.
259  LO myBlkSize = 0, blkSize = 0;
260  if (nodeNumElts > 0)
261  myBlkSize = importer->getSourceMap()->getLocalNumElements() / nodeNumElts;
262  MueLu_maxAll(coords->getMap()->getComm(), myBlkSize, blkSize);
263 
264  RCP<const Import> coordImporter;
265  if (blkSize == 1) {
266  coordImporter = importer;
267 
268  } else {
269  // NOTE: there is an implicit assumption here: we assume that dof any node are enumerated consequently
270  // Proper fix would require using decomposition similar to how we construct importer in the
271  // RepartitionFactory
272  RCP<const Map> origMap = coords->getMap();
273  GO indexBase = origMap->getIndexBase();
274 
275  ArrayView<const GO> OEntries = importer->getTargetMap()->getLocalElementList();
276  LO numEntries = OEntries.size() / blkSize;
277  ArrayRCP<GO> Entries(numEntries);
278  for (LO i = 0; i < numEntries; i++)
279  Entries[i] = (OEntries[i * blkSize] - indexBase) / blkSize + indexBase;
280 
281  RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm());
282  coordImporter = ImportFactory::Build(origMap, targetMap);
283  }
284 
285  RCP<xdMV> permutedCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Build(coordImporter->getTargetMap(), coords->getNumVectors());
286  permutedCoords->doImport(*coords, *coordImporter, Xpetra::INSERT);
287 
288  if (pL.isParameter("repartition: use subcommunicators") == true && pL.get<bool>("repartition: use subcommunicators") == true)
289  permutedCoords->replaceMap(permutedCoords->getMap()->removeEmptyProcesses());
290 
291  if (permutedCoords->getMap() == Teuchos::null)
292  permutedCoords = Teuchos::null;
293 
294  Set(coarseLevel, "Coordinates", permutedCoords);
295 
296  std::string fileName = "rebalanced_coordinates_level_" + toString(coarseLevel.GetLevelID()) + ".m";
297  if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedCoords->getMap() != Teuchos::null)
298  Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Write(fileName, *permutedCoords);
299  }
300 
301  if (pL.isParameter("BlockNumber") &&
302  pL.get<RCP<const FactoryBase> >("BlockNumber") != Teuchos::null &&
303  IsAvailable(coarseLevel, "BlockNumber")) {
304  RCP<LocalOrdinalVector> BlockNumber = Get<RCP<LocalOrdinalVector> >(coarseLevel, "BlockNumber");
305 
306  // This line must be after the Get call
307  SubFactoryMonitor subM(*this, "Rebalancing BlockNumber", coarseLevel);
308 
309  RCP<LocalOrdinalVector> permutedBlockNumber = LocalOrdinalVectorFactory::Build(importer->getTargetMap(), false);
310  permutedBlockNumber->doImport(*BlockNumber, *importer, Xpetra::INSERT);
311 
312  if (pL.isParameter("repartition: use subcommunicators") == true && pL.get<bool>("repartition: use subcommunicators") == true)
313  permutedBlockNumber->replaceMap(permutedBlockNumber->getMap()->removeEmptyProcesses());
314 
315  if (permutedBlockNumber->getMap() == Teuchos::null)
316  permutedBlockNumber = Teuchos::null;
317 
318  Set(coarseLevel, "BlockNumber", permutedBlockNumber);
319 
320  std::string fileName = "rebalanced_BlockNumber_level_" + toString(coarseLevel.GetLevelID()) + ".m";
321  if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedBlockNumber->getMap() != Teuchos::null)
322  Xpetra::IO<SC, LO, GO, NO>::WriteLOMV(fileName, *permutedBlockNumber);
323  }
324 
325  if (IsAvailable(coarseLevel, "Nullspace")) {
326  RCP<MultiVector> nullspace = Get<RCP<MultiVector> >(coarseLevel, "Nullspace");
327 
328  // This line must be after the Get call
329  SubFactoryMonitor subM(*this, "Rebalancing nullspace", coarseLevel);
330 
331  RCP<MultiVector> permutedNullspace = MultiVectorFactory::Build(importer->getTargetMap(), nullspace->getNumVectors());
332  permutedNullspace->doImport(*nullspace, *importer, Xpetra::INSERT);
333 
334  if (pL.get<bool>("repartition: use subcommunicators") == true)
335  permutedNullspace->replaceMap(permutedNullspace->getMap()->removeEmptyProcesses());
336 
337  if (permutedNullspace->getMap() == Teuchos::null)
338  permutedNullspace = Teuchos::null;
339 
340  Set(coarseLevel, "Nullspace", permutedNullspace);
341  }
342 
343  } else {
344  if (pL.get<bool>("transpose: use implicit") == false) {
345  RCP<Matrix> originalR = Get<RCP<Matrix> >(coarseLevel, "R");
346 
347  SubFactoryMonitor m2(*this, "Rebalancing restrictor", coarseLevel);
348 
349  if (implicit || importer.is_null()) {
350  GetOStream(Runtime0) << "Using original restrictor" << std::endl;
351  Set(coarseLevel, "R", originalR);
352 
353  } else {
354  RCP<Matrix> rebalancedR;
355  {
356  SubFactoryMonitor subM(*this, "Rebalancing restriction -- fusedImport", coarseLevel);
357 
358  RCP<Map> dummy; // meaning: use originalR's domain map.
359  Teuchos::ParameterList listLabel;
360  listLabel.set("Timer Label", "MueLu::RebalanceR-" + Teuchos::toString(coarseLevel.GetLevelID()));
361  rebalancedR = MatrixFactory::Build(originalR, *importer, dummy, importer->getTargetMap(), Teuchos::rcp(&listLabel, false));
362  }
363  if (!rebalancedR.is_null()) {
364  std::ostringstream oss;
365  oss << "R_" << coarseLevel.GetLevelID();
366  rebalancedR->setObjectLabel(oss.str());
367  }
368  Set(coarseLevel, "R", rebalancedR);
369 
371  // TODO FIXME somehow we have to transfer the striding information of the permuted domain/range maps.
372  // That is probably something for an external permutation factory
373  // if (originalR->IsView("stridedMaps"))
374  // rebalancedR->CreateView("stridedMaps", originalR);
376 
377  if (IsPrint(Statistics2))
378  GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*rebalancedR, "R (rebalanced)", params);
379  }
380  }
381  }
382 }
383 
384 } // namespace MueLu
385 
386 #endif // MUELU_REBALANCETRANSFERFACTORY_DEF_HPP
Exception indicating invalid cast attempted.
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level-&gt;Get&lt; RCP&lt;Matrix&gt; &gt;(&quot;A&quot;, factory) if factory == NULL =&gt; use default factory.
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
#define MueLu_maxAll(rcpComm, in, out)
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
LocalOrdinal LO
One-liner description of what is happening.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static const NoFactory * get()
virtual ~RebalanceTransferFactory()
Destructor.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Print even more statistics.
bool isParameter(const std::string &name) const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
#define SET_VALID_ENTRY(name)
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
RebalanceTransferFactory()
Constructor.
Node NO
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:51
std::string toString(const T &t)
bool is_null() const