130 typedef Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO> xdMV;
137 if (originalP == Teuchos::null) {
138 Set(coarseLevel,
"P", originalP);
141 int implicit = !pL.get<
bool>(
"repartition: rebalance P and R");
142 int reallyExplicit = pL.get<
bool>(
"repartition: explicit via new copy rebalance P and R");
143 int writeStart = pL.get<
int> (
"write start");
144 int writeEnd = pL.get<
int> (
"write end");
146 if (writeStart == 0 && fineLevel.
GetLevelID() == 0 && writeStart <= writeEnd &&
IsAvailable(fineLevel,
"Coordinates")) {
147 std::string fileName =
"coordinates_level_0.m";
148 RCP<xdMV> fineCoords = fineLevel.
Get< RCP<xdMV> >(
"Coordinates");
149 if (fineCoords != Teuchos::null)
150 Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO>::Write(fileName, *fineCoords);
153 if (writeStart == 0 && fineLevel.
GetLevelID() == 0 && writeStart <= writeEnd &&
IsAvailable(fineLevel,
"BlockNumber")) {
154 std::string fileName =
"BlockNumber_level_0.m";
155 RCP<LocalOrdinalVector> fineBlockNumber = fineLevel.
Get< RCP<LocalOrdinalVector> >(
"BlockNumber");
156 if (fineBlockNumber != Teuchos::null)
157 Xpetra::IO<LO,LO,GO,NO>::Write(fileName, *fineBlockNumber);
166 RCP<ParameterList> params = rcp(
new ParameterList());
168 params->set(
"printLoadBalancingInfo",
true);
169 params->set(
"printCommInfo",
true);
172 std::string transferType = pL.get<std::string>(
"type");
173 if (transferType ==
"Interpolation") {
180 if (implicit || importer.is_null()) {
182 Set(coarseLevel,
"P", originalP);
208 RCP<Matrix> rebalancedP;
209 if (reallyExplicit) {
210 size_t totalMaxPerRow = 0;
211 ArrayRCP<size_t> nnzPerRow(originalP->getRowMap()->getLocalNumElements(), 0);
212 for (
size_t i=0; i<originalP->getRowMap()->getLocalNumElements(); ++i) {
213 nnzPerRow[i] = originalP->getNumEntriesInLocalRow(i);
214 if (nnzPerRow[i] > totalMaxPerRow) totalMaxPerRow = nnzPerRow[i];
217 rebalancedP = MatrixFactory::Build(originalP->getRowMap(), totalMaxPerRow);
220 RCP<Import> trivialImporter = ImportFactory::Build(originalP->getRowMap(), originalP->getRowMap());
221 SubFactoryMonitor m2(*
this,
"Rebalancing prolongator -- import only", coarseLevel);
222 rebalancedP->doImport(*originalP, *trivialImporter, Xpetra::INSERT);
224 rebalancedP->fillComplete(importer->getTargetMap(), originalP->getRangeMap() );
228 rebalancedP = originalP;
229 RCP<const CrsMatrixWrap> crsOp = rcp_dynamic_cast<const CrsMatrixWrap>(originalP);
230 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
232 RCP<CrsMatrix> rebalancedP2 = crsOp->getCrsMatrix();
233 TEUCHOS_TEST_FOR_EXCEPTION(rebalancedP2 == Teuchos::null, std::runtime_error,
"Xpetra::CrsMatrixWrap doesn't have a CrsMatrix");
236 SubFactoryMonitor subM(*
this,
"Rebalancing prolongator -- fast map replacement", coarseLevel);
238 RCP<const Import> newImporter;
241 newImporter = ImportFactory::Build(importer->getTargetMap(), rebalancedP->getColMap());
243 rebalancedP2->replaceDomainMapAndImporter(importer->getTargetMap(), newImporter);
252 if(!rebalancedP.is_null()) {std::ostringstream oss; oss <<
"P_" << coarseLevel.GetLevelID(); rebalancedP->setObjectLabel(oss.str());}
253 Set(coarseLevel,
"P", rebalancedP);
260 if (importer.is_null()) {
262 Set(coarseLevel,
"Nullspace",
Get<RCP<MultiVector> >(coarseLevel,
"Nullspace"));
264 if (pL.isParameter(
"Coordinates") && pL.get< RCP<const FactoryBase> >(
"Coordinates") != Teuchos::null)
266 Set(coarseLevel,
"Coordinates",
Get< RCP<xdMV> >(coarseLevel,
"Coordinates"));
268 if (pL.isParameter(
"BlockNumber") && pL.get< RCP<const FactoryBase> >(
"BlockNumber") != Teuchos::null)
270 Set(coarseLevel,
"BlockNumber",
Get< RCP<LocalOrdinalVector> >(coarseLevel,
"BlockNumber"));
275 if (pL.isParameter(
"Coordinates") &&
276 pL.get< RCP<const FactoryBase> >(
"Coordinates") != Teuchos::null &&
278 RCP<xdMV> coords =
Get<RCP<xdMV> >(coarseLevel,
"Coordinates");
283 LO nodeNumElts = coords->getMap()->getLocalNumElements();
286 LO myBlkSize = 0, blkSize = 0;
288 myBlkSize = importer->getSourceMap()->getLocalNumElements() / nodeNumElts;
289 MueLu_maxAll(coords->getMap()->getComm(), myBlkSize, blkSize);
291 RCP<const Import> coordImporter;
293 coordImporter = importer;
299 RCP<const Map> origMap = coords->getMap();
300 GO indexBase = origMap->getIndexBase();
302 ArrayView<const GO> OEntries = importer->getTargetMap()->getLocalElementList();
303 LO numEntries = OEntries.size()/blkSize;
304 ArrayRCP<GO> Entries(numEntries);
305 for (LO i = 0; i < numEntries; i++)
306 Entries[i] = (OEntries[i*blkSize]-indexBase)/blkSize + indexBase;
308 RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm());
309 coordImporter = ImportFactory::Build(origMap, targetMap);
312 RCP<xdMV> permutedCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO>
::Build(coordImporter->getTargetMap(), coords->getNumVectors());
313 permutedCoords->doImport(*coords, *coordImporter, Xpetra::INSERT);
315 if (pL.isParameter(
"repartition: use subcommunicators") ==
true && pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
316 permutedCoords->replaceMap(permutedCoords->getMap()->removeEmptyProcesses());
318 if (permutedCoords->getMap() == Teuchos::null)
319 permutedCoords = Teuchos::null;
321 Set(coarseLevel,
"Coordinates", permutedCoords);
323 std::string fileName =
"rebalanced_coordinates_level_" +
toString(coarseLevel.GetLevelID()) +
".m";
324 if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedCoords->getMap() != Teuchos::null)
325 Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::magnitudeType,LO,GO,NO>::Write(fileName, *permutedCoords);
328 if (pL.isParameter(
"BlockNumber") &&
329 pL.get< RCP<const FactoryBase> >(
"BlockNumber") != Teuchos::null &&
336 RCP<LocalOrdinalVector> permutedBlockNumber = LocalOrdinalVectorFactory::Build(importer->getTargetMap(),
false);
337 permutedBlockNumber->doImport(*BlockNumber, *importer, Xpetra::INSERT);
339 if (pL.isParameter(
"repartition: use subcommunicators") ==
true && pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
340 permutedBlockNumber->replaceMap(permutedBlockNumber->getMap()->removeEmptyProcesses());
342 if (permutedBlockNumber->getMap() == Teuchos::null)
343 permutedBlockNumber = Teuchos::null;
345 Set(coarseLevel,
"BlockNumber", permutedBlockNumber);
347 std::string fileName =
"rebalanced_BlockNumber_level_" +
toString(coarseLevel.GetLevelID()) +
".m";
348 if (writeStart <= coarseLevel.GetLevelID() && coarseLevel.GetLevelID() <= writeEnd && permutedBlockNumber->getMap() != Teuchos::null)
349 Xpetra::IO<LO,LO,GO,NO>::Write(fileName, *permutedBlockNumber);
358 RCP<MultiVector> permutedNullspace = MultiVectorFactory::Build(importer->getTargetMap(), nullspace->getNumVectors());
359 permutedNullspace->doImport(*nullspace, *importer, Xpetra::INSERT);
361 if (pL.get<
bool>(
"repartition: use subcommunicators") ==
true)
362 permutedNullspace->replaceMap(permutedNullspace->getMap()->removeEmptyProcesses());
364 if (permutedNullspace->getMap() == Teuchos::null)
365 permutedNullspace = Teuchos::null;
367 Set(coarseLevel,
"Nullspace", permutedNullspace);
371 if (pL.get<
bool>(
"transpose: use implicit") ==
false) {
376 if (implicit || importer.is_null()) {
378 Set(coarseLevel,
"R", originalR);
381 RCP<Matrix> rebalancedR;
383 SubFactoryMonitor subM(*
this,
"Rebalancing restriction -- fusedImport", coarseLevel);
386 Teuchos::ParameterList listLabel;
387 listLabel.set(
"Timer Label",
"MueLu::RebalanceR-" + Teuchos::toString(coarseLevel.GetLevelID()));
388 rebalancedR = MatrixFactory::Build(originalR, *importer, dummy, importer->getTargetMap(),Teuchos::rcp(&listLabel,
false));
390 if(!rebalancedR.is_null()) {std::ostringstream oss; oss <<
"R_" << coarseLevel.GetLevelID(); rebalancedR->setObjectLabel(oss.str());}
391 Set(coarseLevel,
"R", rebalancedR);