89 const int startLevel = pL.get<
int>(
"repartition: start level");
90 const int nodeRepartLevel = pL.get<
int>(
"repartition: node repartition level");
91 LO minRowsPerProcess = pL.get<LO>(
"repartition: min rows per proc");
92 LO targetRowsPerProcess = pL.get<LO>(
"repartition: target rows per proc");
93 LO minRowsPerThread = pL.get<LO>(
"repartition: min rows per thread");
94 LO targetRowsPerThread = pL.get<LO>(
"repartition: target rows per thread");
95 const double nonzeroImbalance = pL.get<
double>(
"repartition: max imbalance");
96 const bool useMap = pL.get<
bool>(
"repartition: use map");
98 int thread_per_mpi_rank = 1;
99#if defined(KOKKOS_ENABLE_OPENMP)
100 using execution_space =
typename Node::device_type::execution_space;
101 if (std::is_same<execution_space, Kokkos::OpenMP>::value)
102 thread_per_mpi_rank = execution_space().concurrency();
105 if (minRowsPerThread > 0)
107 minRowsPerProcess = minRowsPerThread * thread_per_mpi_rank;
109 if (targetRowsPerThread == 0)
110 targetRowsPerThread = minRowsPerThread;
112 if (targetRowsPerThread > 0)
114 targetRowsPerProcess = targetRowsPerThread * thread_per_mpi_rank;
116 if (targetRowsPerProcess == 0)
117 targetRowsPerProcess = minRowsPerProcess;
120 Set<LO>(currentLevel,
"repartition: heuristic target rows per process", targetRowsPerProcess);
123 TEUCHOS_TEST_FOR_EXCEPTION(nodeRepartLevel >= startLevel,
Exceptions::RuntimeError,
"MueLu::RepartitionHeuristicFactory::Build(): If 'repartition: node repartition level' is set, it must be less than or equal to 'repartition: start level'");
126 RCP<const FactoryBase> Afact;
130 if (!Afact.is_null() && Teuchos::rcp_dynamic_cast<const RAPFactory>(Afact) == Teuchos::null &&
131 Teuchos::rcp_dynamic_cast<const BlockedRAPFactory>(Afact) == Teuchos::null &&
132 Teuchos::rcp_dynamic_cast<const SubBlockAFactory>(Afact) == Teuchos::null) {
133 GetOStream(
Warnings) <<
"MueLu::RepartitionHeuristicFactory::Build: The generation factory for A must "
134 "be a RAPFactory or a SubBlockAFactory providing the non-rebalanced matrix information! "
135 "It specifically must not be of type Rebalance(Blocked)AcFactory or similar. "
136 "Please check the input. Make also sure that \"number of partitions\" is provided to "
137 "the Interface class and the RepartitionFactory instance. Instead, we have a "
138 << Afact->description() << std::endl;
142 map = A->getRowMap();
154 if (currentLevel.
GetLevelID() == nodeRepartLevel && map->getComm()->getSize() > 1) {
156 TEUCHOS_TEST_FOR_EXCEPTION(NodeComm.is_null(),
Exceptions::RuntimeError,
"MueLu::RepartitionHeuristicFactory::Build(): NodeComm is null.");
159 if (NodeComm()->getSize() != map->getComm()->getSize()) {
161 int nodeRank = NodeComm->getRank();
164 int isZero = (nodeRank == 0);
166 Teuchos::reduceAll(*map->getComm(), Teuchos::REDUCE_SUM, isZero, Teuchos::outArg(numNodes));
167 Set(currentLevel,
"number of partitions", numNodes);
175 <<
"\n current level = " << Teuchos::toString(currentLevel.
GetLevelID()) <<
", first level where repartitioning can happen is " + Teuchos::toString(startLevel) << std::endl;
178 Set(currentLevel,
"number of partitions", -1);
183 RCP<const Teuchos::Comm<int> > origComm = map->getComm();
184 RCP<const Teuchos::Comm<int> > comm = origComm;
192 if (comm->getSize() == 1 && Teuchos::rcp_dynamic_cast<const RAPFactory>(Afact) != Teuchos::null) {
194 <<
"\n comm size = 1" << std::endl;
196 Set(currentLevel,
"number of partitions", -1);
200 int numActiveProcesses = 0;
201 MueLu_sumAll(comm, Teuchos::as<int>((map->getLocalNumElements() > 0) ? 1 : 0), numActiveProcesses);
203 if (numActiveProcesses == 1) {
205 <<
"\n # processes with rows = " << Teuchos::toString(numActiveProcesses) << std::endl;
207 Set(currentLevel,
"number of partitions", 1);
212 bool test3 =
false, test4 =
false;
213 std::string msg3, msg4;
217 if (minRowsPerProcess > 0) {
218 LO numMyRows = Teuchos::as<LO>(map->getLocalNumElements()), minNumRows, LOMAX = Teuchos::OrdinalTraits<LO>::max();
219 LO haveFewRows = (numMyRows < minRowsPerProcess ? 1 : 0), numWithFewRows = 0;
221 MueLu_minAll(comm, (numMyRows > 0 ? numMyRows : LOMAX), minNumRows);
226 if (numWithFewRows > 0)
229 msg3 =
"\n min # rows per proc = " + Teuchos::toString(minNumRows) +
", min allowable = " + Teuchos::toString(minRowsPerProcess);
237 GO minNnz, maxNnz, numMyNnz = Teuchos::as<GO>(A->getLocalNumEntries());
239 MueLu_minAll(comm, (numMyNnz > 0 ? numMyNnz : maxNnz), minNnz);
240 double imbalance = Teuchos::as<double>(maxNnz) / minNnz;
242 if (imbalance > nonzeroImbalance)
245 msg4 =
"\n nonzero imbalance = " + Teuchos::toString(imbalance) +
", max allowable = " + Teuchos::toString(nonzeroImbalance);
249 if (!test3 && !test4) {
253 Set(currentLevel,
"number of partitions", -1);
270 const auto globalNumRows = Teuchos::as<GO>(map->getGlobalNumElements());
271 int numPartitions = 1;
272 if (globalNumRows >= targetRowsPerProcess) {
274 numPartitions = std::max(Teuchos::as<int>(globalNumRows / targetRowsPerProcess), 1);
276 numPartitions = std::min(numPartitions, comm->getSize());
278 Set(currentLevel,
"number of partitions", numPartitions);