103 typedef Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> xdMV;
110 if (originalP == Teuchos::null) {
111 Set(coarseLevel,
"P", originalP);
114 int implicit = !pL.get<
bool>(
"repartition: rebalance P and R");
115 int reallyExplicit = pL.get<
bool>(
"repartition: explicit via new copy rebalance P and R");
116 int writeStart = pL.get<
int>(
"write start");
117 int writeEnd = pL.get<
int>(
"write end");
119 if (writeStart == 0 && fineLevel.
GetLevelID() == 0 && writeStart <= writeEnd &&
IsAvailable(fineLevel,
"Coordinates")) {
120 std::string fileName =
"coordinates_level_0.m";
121 RCP<xdMV> fineCoords = fineLevel.
Get<RCP<xdMV> >(
"Coordinates");
122 if (fineCoords != Teuchos::null)
123 Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Write(fileName, *fineCoords);
126 if (writeStart == 0 && fineLevel.
GetLevelID() == 0 && writeStart <= writeEnd &&
IsAvailable(fineLevel,
"BlockNumber")) {
127 std::string fileName =
"BlockNumber_level_0.m";
128 RCP<LocalOrdinalVector> fineBlockNumber = fineLevel.
Get<RCP<LocalOrdinalVector> >(
"BlockNumber");
129 if (fineBlockNumber != Teuchos::null)
130 Xpetra::IO<SC, LO, GO, NO>::WriteLOMV(fileName, *fineBlockNumber);
139 RCP<ParameterList> params = rcp(
new ParameterList());
141 params->set(
"printLoadBalancingInfo",
true);
142 params->set(
"printCommInfo",
true);
145 std::string transferType = pL.get<std::string>(
"type");
146 if (transferType ==
"Interpolation") {
153 if (implicit || importer.is_null()) {
155 Set(coarseLevel,
"P", originalP);
181 RCP<Matrix> rebalancedP;
182 if (reallyExplicit) {
183 size_t totalMaxPerRow = 0;
184 ArrayRCP<size_t> nnzPerRow(originalP->getRowMap()->getLocalNumElements(), 0);
185 for (
size_t i = 0; i < originalP->getRowMap()->getLocalNumElements(); ++i) {
186 nnzPerRow[i] = originalP->getNumEntriesInLocalRow(i);
187 if (nnzPerRow[i] > totalMaxPerRow) totalMaxPerRow = nnzPerRow[i];
190 rebalancedP = MatrixFactory::Build(originalP->getRowMap(), totalMaxPerRow);
193 RCP<Import> trivialImporter = ImportFactory::Build(originalP->getRowMap(), originalP->getRowMap());
194 SubFactoryMonitor m2(*
this,
"Rebalancing prolongator -- import only", coarseLevel);
195 rebalancedP->doImport(*originalP, *trivialImporter, Xpetra::INSERT);
197 rebalancedP->fillComplete(importer->getTargetMap(), originalP->getRangeMap());
200 rebalancedP = originalP;
201 RCP<const CrsMatrixWrap> crsOp = rcp_dynamic_cast<const CrsMatrixWrap>(originalP);
202 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
204 RCP<CrsMatrix> rebalancedP2 = crsOp->getCrsMatrix();
205 TEUCHOS_TEST_FOR_EXCEPTION(rebalancedP2 == Teuchos::null, std::runtime_error,
"Xpetra::CrsMatrixWrap doesn't have a CrsMatrix");
208 SubFactoryMonitor subM(*
this,
"Rebalancing prolongator -- fast map replacement", coarseLevel);
210 RCP<const Import> newImporter;
213 newImporter = ImportFactory::Build(importer->getTargetMap(), rebalancedP->getColMap());
215 rebalancedP2->replaceDomainMapAndImporter(importer->getTargetMap(), newImporter);
224 if (!rebalancedP.is_null()) {
225 std::ostringstream oss;
226 oss <<
"P_" << coarseLevel.GetLevelID();
227 rebalancedP->setObjectLabel(oss.str());
229 Set(coarseLevel,
"P", rebalancedP);
236 if (importer.is_null()) {
238 Set(coarseLevel,
"Nullspace",
Get<RCP<MultiVector> >(coarseLevel,
"Nullspace"));
240 if (pL.isParameter(
"Coordinates") && pL.get<RCP<const FactoryBase> >(
"Coordinates") != Teuchos::null)
242 Set(coarseLevel,
"Coordinates",
Get<RCP<xdMV> >(coarseLevel,
"Coordinates"));
244 if (pL.isParameter(
"Material") && pL.get<RCP<const FactoryBase> >(
"Material") != Teuchos::null)
246 Set(coarseLevel,
"Material",
Get<RCP<MultiVector> >(coarseLevel,
"Material"));
248 if (pL.isParameter(
"BlockNumber") && pL.get<RCP<const FactoryBase> >(
"BlockNumber") != Teuchos::null)
250 Set(coarseLevel,
"BlockNumber",
Get<RCP<LocalOrdinalVector> >(coarseLevel,
"BlockNumber"));
255 if (pL.isParameter(
"Coordinates") &&
256 pL.get<RCP<const FactoryBase> >(
"Coordinates") != Teuchos::null &&
258 RCP<xdMV> coords =
Get<RCP<xdMV> >(coarseLevel,
"Coordinates");
263 LO nodeNumElts = coords->getMap()->getLocalNumElements();
266 LO myBlkSize = 0, blkSize = 0;
268 myBlkSize = importer->getSourceMap()->getLocalNumElements() / nodeNumElts;
269 MueLu_maxAll(coords->getMap()->getComm(), myBlkSize, blkSize);
271 RCP<const Import> coordImporter;
273 coordImporter = importer;
279 RCP<const Map> origMap = coords->getMap();
280 GO indexBase = origMap->getIndexBase();
282 ArrayView<const GO> OEntries = importer->getTargetMap()->getLocalElementList();
283 LO numEntries = OEntries.size() / blkSize;
284 ArrayRCP<GO> Entries(numEntries);
285 for (LO i = 0; i < numEntries; i++)
286 Entries[i] = (OEntries[i * blkSize] - indexBase) / blkSize + indexBase;
288 RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm());
289 coordImporter = ImportFactory::Build(origMap, targetMap);
292 RCP<xdMV> permutedCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>
::Build(coordImporter->getTargetMap(), coords->getNumVectors());
293 permutedCoords->doImport(*coords, *coordImporter, Xpetra::INSERT);
295 if (pL.isParameter(
"repartition: use subcommunicators") ==
true && pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
296 permutedCoords->replaceMap(permutedCoords->getMap()->removeEmptyProcesses());
298 if (permutedCoords->getMap() == Teuchos::null)
299 permutedCoords = Teuchos::null;
301 Set(coarseLevel,
"Coordinates", permutedCoords);
303 std::string fileName =
"rebalanced_coordinates_level_" +
toString(coarseLevel.GetLevelID()) +
".m";
304 if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedCoords->getMap() != Teuchos::null)
305 Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Write(fileName, *permutedCoords);
314 RCP<MultiVector> permutedMaterial = MultiVectorFactory::Build(importer->getTargetMap(), material->getNumVectors());
315 permutedMaterial->doImport(*material, *importer, Xpetra::INSERT);
317 if (pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
318 permutedMaterial->replaceMap(permutedMaterial->getMap()->removeEmptyProcesses());
320 if (permutedMaterial->getMap() == Teuchos::null)
321 permutedMaterial = Teuchos::null;
323 Set(coarseLevel,
"Material", permutedMaterial);
326 if (pL.isParameter(
"BlockNumber") &&
327 pL.get<RCP<const FactoryBase> >(
"BlockNumber") != Teuchos::null &&
334 RCP<LocalOrdinalVector> permutedBlockNumber = LocalOrdinalVectorFactory::Build(importer->getTargetMap(),
false);
335 permutedBlockNumber->doImport(*BlockNumber, *importer, Xpetra::INSERT);
337 if (pL.isParameter(
"repartition: use subcommunicators") ==
true && pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
338 permutedBlockNumber->replaceMap(permutedBlockNumber->getMap()->removeEmptyProcesses());
340 if (permutedBlockNumber->getMap() == Teuchos::null)
341 permutedBlockNumber = Teuchos::null;
343 Set(coarseLevel,
"BlockNumber", permutedBlockNumber);
345 std::string fileName =
"rebalanced_BlockNumber_level_" +
toString(coarseLevel.GetLevelID()) +
".m";
346 if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedBlockNumber->getMap() != Teuchos::null)
347 Xpetra::IO<SC, LO, GO, NO>::WriteLOMV(fileName, *permutedBlockNumber);
356 RCP<MultiVector> permutedNullspace = MultiVectorFactory::Build(importer->getTargetMap(), nullspace->getNumVectors());
357 permutedNullspace->doImport(*nullspace, *importer, Xpetra::INSERT);
359 if (pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
360 permutedNullspace->replaceMap(permutedNullspace->getMap()->removeEmptyProcesses());
362 if (permutedNullspace->getMap() == Teuchos::null)
363 permutedNullspace = Teuchos::null;
365 Set(coarseLevel,
"Nullspace", permutedNullspace);
369 if (pL.get<
bool>(
"transpose: use implicit") ==
false) {
374 if (implicit || importer.is_null()) {
376 Set(coarseLevel,
"R", originalR);
379 RCP<Matrix> rebalancedR;
381 SubFactoryMonitor subM(*
this,
"Rebalancing restriction -- fusedImport", coarseLevel);
384 Teuchos::ParameterList listLabel;
385 listLabel.set(
"Timer Label",
"MueLu::RebalanceR-" + Teuchos::toString(coarseLevel.GetLevelID()));
386 rebalancedR = MatrixFactory::Build(originalR, *importer, dummy, importer->getTargetMap(), Teuchos::rcp(&listLabel,
false));
388 if (!rebalancedR.is_null()) {
389 std::ostringstream oss;
390 oss <<
"R_" << coarseLevel.GetLevelID();
391 rebalancedR->setObjectLabel(oss.str());
393 Set(coarseLevel,
"R", rebalancedR);