88 const Teuchos::ParameterList& pL = GetParameterList();
91 bool remapPartitions = pL.get<
bool>(
"repartition: remap parts");
94 RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel,
"A");
95 if (A == Teuchos::null) {
96 Set<RCP<const Import> >(currentLevel,
"Importer", Teuchos::null);
99 RCP<const Map> rowMap = A->getRowMap();
100 GO indexBase = rowMap->getIndexBase();
101 Xpetra::UnderlyingLib lib = rowMap->lib();
103 RCP<const Teuchos::Comm<int> > origComm = rowMap->getComm();
104 RCP<const Teuchos::Comm<int> > comm = origComm;
106 int numProcs = comm->getSize();
108 RCP<const Teuchos::MpiComm<int> > tmpic = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
109 TEUCHOS_TEST_FOR_EXCEPTION(tmpic == Teuchos::null,
Exceptions::RuntimeError,
"Cannot cast base Teuchos::Comm to Teuchos::MpiComm object.");
110 RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm();
113 int numPartitions = Get<int>(currentLevel,
"number of partitions");
118 RCP<GOVector> decomposition = Get<RCP<GOVector> >(currentLevel,
"Partition");
121 if (remapPartitions ==
true && Teuchos::rcp_dynamic_cast<const CloneRepartitionInterface>(GetFactory(
"Partition")) != Teuchos::null) {
125 remapPartitions =
false;
129 if (numPartitions == 1) {
134 GetOStream(
Runtime0) <<
"Only one partition: Skip call to the repartitioner." << std::endl;
135 }
else if (numPartitions == -1) {
137 GetOStream(
Runtime0) <<
"No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
138 Set<RCP<const Import> >(currentLevel,
"Importer", Teuchos::null);
143 const int nodeRepartLevel = pL.get<
int>(
"repartition: node repartition level");
144 if (currentLevel.
GetLevelID() == nodeRepartLevel) {
149 remapPartitions =
false;
189 if (remapPartitions) {
192 bool acceptPartition = pL.get<
bool>(
"repartition: remap accept partition");
193 bool allSubdomainsAcceptPartitions;
194 int localNumAcceptPartition = acceptPartition;
195 int globalNumAcceptPartition;
196 MueLu_sumAll(comm, localNumAcceptPartition, globalNumAcceptPartition);
197 GetOStream(
Statistics2) <<
"Number of ranks that accept partitions: " << globalNumAcceptPartition << std::endl;
198 if (globalNumAcceptPartition < numPartitions) {
199 GetOStream(
Warnings0) <<
"Not enough ranks are willing to accept a partition, allowing partitions on all ranks." << std::endl;
200 acceptPartition =
true;
201 allSubdomainsAcceptPartitions =
true;
202 }
else if (numPartitions > numProcs) {
204 allSubdomainsAcceptPartitions =
true;
206 allSubdomainsAcceptPartitions =
false;
209 DeterminePartitionPlacement(*A, *decomposition, numPartitions, acceptPartition, allSubdomainsAcceptPartitions);
221 int myRank = comm->getRank();
222 ArrayRCP<const GO> decompEntries;
223 if (decomposition->getLocalLength() > 0)
224 decompEntries = decomposition->getData(0);
227 int incorrectRank = -1;
228 for (
int i = 0; i < decompEntries.size(); i++)
229 if (decompEntries[i] >= numProcs || decompEntries[i] < 0) {
230 incorrectRank = myRank;
234 int incorrectGlobalRank = -1;
236 TEUCHOS_TEST_FOR_EXCEPTION(incorrectGlobalRank > -1,
Exceptions::RuntimeError,
"pid " + Teuchos::toString(incorrectGlobalRank) +
" encountered a partition number is that out-of-range");
244 std::tie(myGIDs, numLocalKept) = Util::ConstructGIDs(decomposition);
247 GO numGlobalKept, numGlobalRows = A->getGlobalNumRows();
249 GetOStream(
Statistics2) <<
"Unmoved rows: " << numGlobalKept <<
" / " << numGlobalRows <<
" (" << 100 * Teuchos::as<double>(numGlobalKept) / numGlobalRows <<
"%)" << std::endl;
257 newRowMap = MapFactory ::Build(lib, rowMap->getGlobalNumElements(), myGIDs(), indexBase, origComm);
260 RCP<const Import> rowMapImporter;
262 RCP<const BlockedMap> blockedRowMap = Teuchos::rcp_dynamic_cast<const BlockedMap>(rowMap);
267 if (blockedRowMap.is_null())
268 rowMapImporter = ImportFactory::Build(rowMap, newRowMap);
270 rowMapImporter = ImportFactory::Build(blockedRowMap->getMap(), newRowMap);
275 if (!blockedRowMap.is_null()) {
281 size_t numBlocks = blockedRowMap->getNumMaps();
282 std::vector<RCP<const Import> > subImports(numBlocks);
284 for (
size_t i = 0; i < numBlocks; i++) {
285 RCP<const Map> source = blockedRowMap->getMap(i);
286 RCP<const Map> target = blockedTargetMap->getMap(i);
287 subImports[i] = ImportFactory::Build(source, target);
289 Set(currentLevel,
"SubImporters", subImports);
292 Set(currentLevel,
"Importer", rowMapImporter);
295 bool save_importer = pL.get<
bool>(
"repartition: save importer");
304 if (!rowMapImporter.is_null() && IsPrint(
Statistics2)) {
309 if (pL.get<
bool>(
"repartition: print partition distribution") && IsPrint(
Statistics2)) {
311 GetOStream(
Statistics2) <<
"Partition distribution over cores (ownership is indicated by '+')" << std::endl;
313 char amActive = (myGIDs.size() ? 1 : 0);
314 std::vector<char> areActive(numProcs, 0);
315 MPI_Gather(&amActive, 1, MPI_CHAR, &areActive[0], 1, MPI_CHAR, 0, *rawMpiComm);
317 int rowWidth = std::min(Teuchos::as<int>(ceil(sqrt(numProcs))), 100);
318 for (
int proc = 0; proc < numProcs; proc += rowWidth) {
319 for (
int j = 0; j < rowWidth; j++)
320 if (proc + j < numProcs)
321 GetOStream(
Statistics2) << (areActive[proc + j] ?
"+" :
".");
325 GetOStream(
Statistics2) <<
" " << proc <<
":" << std::min(proc + rowWidth, numProcs) - 1 << std::endl;
344 DeterminePartitionPlacement(
const Matrix& A, GOVector& decomposition, GO numPartitions,
bool willAcceptPartition,
bool allSubdomainsAcceptPartitions)
const {
345 RCP<const Map> rowMap = A.getRowMap();
347 RCP<const Teuchos::Comm<int> > comm = rowMap->getComm()->duplicate();
348 int numProcs = comm->getSize();
350 RCP<const Teuchos::MpiComm<int> > tmpic = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
351 TEUCHOS_TEST_FOR_EXCEPTION(tmpic == Teuchos::null,
Exceptions::RuntimeError,
"Cannot cast base Teuchos::Comm to Teuchos::MpiComm object.");
352 RCP<const Teuchos::OpaqueWrapper<MPI_Comm> > rawMpiComm = tmpic->getRawMpiComm();
354 const Teuchos::ParameterList& pL = GetParameterList();
360 const int maxLocal = pL.get<
int>(
"repartition: remap num values");
361 const int dataSize = 2 * maxLocal;
363 ArrayRCP<GO> decompEntries;
364 if (decomposition.getLocalLength() > 0)
365 decompEntries = decomposition.getDataNonConst(0);
377 std::map<GO, GO> lEdges;
378 if (willAcceptPartition)
379 for (LO i = 0; i < decompEntries.size(); i++)
380 lEdges[decompEntries[i]] += A.getNumEntriesInLocalRow(i);
384 std::multimap<GO, GO> revlEdges;
385 for (
typename std::map<GO, GO>::const_iterator it = lEdges.begin(); it != lEdges.end(); it++)
386 revlEdges.insert(std::make_pair(it->second, it->first));
391 Array<GO> lData(dataSize, -1), gData(numProcs * dataSize);
393 for (
typename std::multimap<GO, GO>::reverse_iterator rit = revlEdges.rbegin(); rit != revlEdges.rend() && numEdges < maxLocal; rit++) {
394 lData[2 * numEdges + 0] = rit->second;
395 lData[2 * numEdges + 1] = rit->first;
401 MPI_Datatype MpiType = Teuchos::Details::MpiTypeTraits<GO>::getType();
402 MPI_Allgather(
static_cast<void*
>(lData.getRawPtr()), dataSize, MpiType,
static_cast<void*
>(gData.getRawPtr()), dataSize, MpiType, *rawMpiComm);
407 Teuchos::Array<Triplet<int, int> > gEdges(numProcs * maxLocal);
408 Teuchos::Array<bool> procWillAcceptPartition(numProcs, allSubdomainsAcceptPartitions);
410 for (LO i = 0; i < gData.size(); i += 2) {
411 int procNo = i / dataSize;
412 GO part = gData[i + 0];
413 GO weight = gData[i + 1];
415 gEdges[k].i = procNo;
417 gEdges[k].v = weight;
418 procWillAcceptPartition[procNo] =
true;
426 std::sort(gEdges.begin(), gEdges.end(), compareTriplets<int, int>);
429 std::map<int, int> match;
430 Teuchos::Array<char> matchedRanks(numProcs, 0), matchedParts(numPartitions, 0);
432 for (
typename Teuchos::Array<
Triplet<int, int> >::const_iterator it = gEdges.begin(); it != gEdges.end(); it++) {
435 if (matchedRanks[rank] == 0 && matchedParts[part] == 0) {
436 matchedRanks[rank] = 1;
437 matchedParts[part] = 1;
442 GetOStream(
Statistics1) <<
"Number of unassigned partitions before cleanup stage: " << (numPartitions - numMatched) <<
" / " << numPartitions << std::endl;
449 if (numPartitions - numMatched > 0) {
450 Teuchos::Array<char> partitionCounts(numPartitions, 0);
451 for (
typename std::map<int, int>::const_iterator it = match.begin(); it != match.end(); it++)
452 partitionCounts[it->first] += 1;
453 for (
int part = 0, matcher = 0; part < numPartitions; part++) {
454 if (partitionCounts[part] == 0) {
456 while (matchedRanks[matcher] || !procWillAcceptPartition[matcher])
459 match[part] = matcher++;
465 TEUCHOS_TEST_FOR_EXCEPTION(numMatched != numPartitions,
Exceptions::RuntimeError,
"MueLu::RepartitionFactory::DeterminePartitionPlacement: Only " << numMatched <<
" partitions out of " << numPartitions <<
" got assigned to ranks.");
468 for (LO i = 0; i < decompEntries.size(); i++)
469 decompEntries[i] = match[decompEntries[i]];