121 const int startLevel = pL.get<
int> (
"repartition: start level");
122 const int nodeRepartLevel = pL.get<
int> (
"repartition: node repartition level");
123 LO minRowsPerProcess = pL.get<LO> (
"repartition: min rows per proc");
124 LO targetRowsPerProcess = pL.get<LO> (
"repartition: target rows per proc");
125 LO minRowsPerThread = pL.get<LO> (
"repartition: min rows per thread");
126 LO targetRowsPerThread = pL.get<LO> (
"repartition: target rows per thread");
127 const double nonzeroImbalance = pL.get<
double>(
"repartition: max imbalance");
128 const bool useMap = pL.get<
bool> (
"repartition: use map");
130 int thread_per_mpi_rank = 1;
131#if defined(KOKKOS_ENABLE_OPENMP)
132 using execution_space =
typename Node::device_type::execution_space;
133 if (std::is_same<execution_space, Kokkos::OpenMP>::value)
134 thread_per_mpi_rank = execution_space().concurrency();
137 if (minRowsPerThread > 0)
139 minRowsPerProcess = minRowsPerThread*thread_per_mpi_rank;
141 if (targetRowsPerThread == 0)
142 targetRowsPerThread = minRowsPerThread;
144 if (targetRowsPerThread > 0)
146 targetRowsPerProcess = targetRowsPerThread*thread_per_mpi_rank;
148 if (targetRowsPerProcess == 0)
149 targetRowsPerProcess = minRowsPerProcess;
152 Set<LO>(currentLevel,
"repartition: heuristic target rows per process",targetRowsPerProcess);
155 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'");
159 RCP<const FactoryBase> Afact;
163 if(!Afact.is_null() && Teuchos::rcp_dynamic_cast<const RAPFactory>(Afact) == Teuchos::null &&
164 Teuchos::rcp_dynamic_cast<const BlockedRAPFactory>(Afact) == Teuchos::null &&
165 Teuchos::rcp_dynamic_cast<const SubBlockAFactory>(Afact) == Teuchos::null) {
167 "MueLu::RepartitionHeuristicFactory::Build: The generation factory for A must " \
168 "be a RAPFactory or a SubBlockAFactory providing the non-rebalanced matrix information! " \
169 "It specifically must not be of type Rebalance(Blocked)AcFactory or similar. " \
170 "Please check the input. Make also sure that \"number of partitions\" is provided to " \
171 "the Interface class and the RepartitionFactory instance. Instead, we have a "<<Afact->description() << std::endl;
175 map = A->getRowMap();
187 if (currentLevel.
GetLevelID() == nodeRepartLevel && map->getComm()->getSize() > 1) {
189 TEUCHOS_TEST_FOR_EXCEPTION(NodeComm.is_null(),
Exceptions::RuntimeError,
"MueLu::RepartitionHeuristicFactory::Build(): NodeComm is null.");
192 if(NodeComm()->getSize() != map->getComm()->getSize()) {
194 int nodeRank = NodeComm->getRank();
197 int isZero = (nodeRank == 0);
199 Teuchos::reduceAll(*map->getComm(), Teuchos::REDUCE_SUM, isZero, Teuchos::outArg(numNodes));
200 Set(currentLevel,
"number of partitions", numNodes);
208 "\n current level = " << Teuchos::toString(currentLevel.
GetLevelID()) <<
209 ", first level where repartitioning can happen is " + Teuchos::toString(startLevel) << std::endl;
212 Set(currentLevel,
"number of partitions", -1);
217 RCP<const Teuchos::Comm<int> > origComm = map->getComm();
218 RCP<const Teuchos::Comm<int> > comm = origComm;
226 if (comm->getSize() == 1 && Teuchos::rcp_dynamic_cast<const RAPFactory>(Afact) != Teuchos::null) {
228 "\n comm size = 1" << std::endl;
230 Set(currentLevel,
"number of partitions", -1);
234 int numActiveProcesses = 0;
235 MueLu_sumAll(comm, Teuchos::as<int>((map->getLocalNumElements() > 0) ? 1 : 0), numActiveProcesses);
237 if (numActiveProcesses == 1) {
239 "\n # processes with rows = " << Teuchos::toString(numActiveProcesses) << std::endl;
241 Set(currentLevel,
"number of partitions", 1);
246 bool test3 =
false, test4 =
false;
247 std::string msg3, msg4;
251 if (minRowsPerProcess > 0) {
252 LO numMyRows = Teuchos::as<LO>(map->getLocalNumElements()), minNumRows, LOMAX = Teuchos::OrdinalTraits<LO>::max();
253 LO haveFewRows = (numMyRows < minRowsPerProcess ? 1 : 0), numWithFewRows = 0;
255 MueLu_minAll(comm, (numMyRows > 0 ? numMyRows : LOMAX), minNumRows);
260 if (numWithFewRows > 0)
263 msg3 =
"\n min # rows per proc = " + Teuchos::toString(minNumRows) +
", min allowable = " + Teuchos::toString(minRowsPerProcess);
271 GO minNnz, maxNnz, numMyNnz = Teuchos::as<GO>(A->getLocalNumEntries());
273 MueLu_minAll(comm, (numMyNnz > 0 ? numMyNnz : maxNnz), minNnz);
274 double imbalance = Teuchos::as<double>(maxNnz)/minNnz;
276 if (imbalance > nonzeroImbalance)
279 msg4 =
"\n nonzero imbalance = " + Teuchos::toString(imbalance) +
", max allowable = " + Teuchos::toString(nonzeroImbalance);
283 if (!test3 && !test4) {
287 Set(currentLevel,
"number of partitions", -1);
304 const auto globalNumRows = Teuchos::as<GO>(map->getGlobalNumElements());
305 int numPartitions = 1;
306 if (globalNumRows >= targetRowsPerProcess) {
308 numPartitions = std::max(Teuchos::as<int>(globalNumRows / targetRowsPerProcess), 1);
310 numPartitions = std::min(numPartitions, comm->getSize());
312 Set(currentLevel,
"number of partitions", numPartitions);