46 #ifndef MUELU_HYBRIDAGGREGATIONFACTORY_DEF_HPP_
47 #define MUELU_HYBRIDAGGREGATIONFACTORY_DEF_HPP_
49 #include <Xpetra_Matrix.hpp>
50 #include <Xpetra_Map.hpp>
51 #include <Xpetra_Vector.hpp>
52 #include <Xpetra_MultiVectorFactory.hpp>
53 #include <Xpetra_VectorFactory.hpp>
58 #include "MueLu_InterfaceAggregationAlgorithm.hpp"
59 #include "MueLu_OnePtAggregationAlgorithm.hpp"
60 #include "MueLu_PreserveDirichletAggregationAlgorithm.hpp"
61 #include "MueLu_IsolatedNodeAggregationAlgorithm.hpp"
63 #include "MueLu_AggregationPhase1Algorithm.hpp"
64 #include "MueLu_AggregationPhase2aAlgorithm.hpp"
65 #include "MueLu_AggregationPhase2bAlgorithm.hpp"
66 #include "MueLu_AggregationPhase3Algorithm.hpp"
69 #include "MueLu_AggregationStructuredAlgorithm.hpp"
70 #include "MueLu_UncoupledIndexManager.hpp"
77 #include "MueLu_Aggregates.hpp"
80 #include "MueLu_Utilities.hpp"
81 #include "MueLu_AmalgamationInfo.hpp"
86 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
91 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
94 RCP<ParameterList> validParamList = rcp(
new ParameterList());
96 typedef Teuchos::StringToIntegralParameterEntryValidator<int> validatorType;
97 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
103 validParamList->getEntry(
"aggregation: ordering").setValidator(
104 rcp(
new validatorType(Teuchos::tuple<std::string>(
"natural",
"graph",
"random"),
"aggregation: ordering")));
111 SET_VALID_ENTRY(
"aggregation: error on nodes with no on-rank neighbors");
121 #undef SET_VALID_ENTRY
125 validParamList->set< RCP<const FactoryBase> >(
"Graph",
null,
"Generating factory of the graph");
126 validParamList->set< RCP<const FactoryBase> >(
"DofsPerNode",
null,
"Generating factory for variable \'DofsPerNode\', usually the same as for \'Graph\'");
128 validParamList->set<
std::string> (
"OnePt aggregate map name",
"",
129 "Name of input map for single node aggregates. (default='')");
130 validParamList->set<
std::string> (
"OnePt aggregate map factory",
"",
131 "Generating factory of (DOF) map for single node aggregates.");
134 validParamList->set<
std::string> (
"Interface aggregate map name",
"",
135 "Name of input map for interface aggregates. (default='')");
136 validParamList->set<
std::string> (
"Interface aggregate map factory",
"",
137 "Generating factory of (DOF) map for interface aggregates.");
138 validParamList->set<RCP<const FactoryBase> > (
"interfacesDimensions", Teuchos::null,
139 "Describes the dimensions of all the interfaces on this rank.");
140 validParamList->set<RCP<const FactoryBase> > (
"nodeOnInterface", Teuchos::null,
141 "List the LIDs of the nodes on any interface.");
145 validParamList->set<RCP<const FactoryBase> >(
"numDimensions", Teuchos::null,
146 "Number of spatial dimension provided by CoordinatesTransferFactory.");
147 validParamList->set<RCP<const FactoryBase> >(
"lNodesPerDim", Teuchos::null,
148 "Number of nodes per spatial dimmension provided by CoordinatesTransferFactory.");
152 validParamList->set<RCP<const FactoryBase> > (
"aggregationRegionType", Teuchos::null,
153 "Type of aggregation to use on the region (\"structured\" or \"uncoupled\")");
155 return validParamList;
158 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
161 Input(currentLevel,
"Graph");
163 ParameterList pL = GetParameterList();
176 "Aggregation region type was not provided by the user!");
183 "numDimensions was not provided by the user on level0!");
190 "lNodesPerDim was not provided by the user on level0!");
193 Input(currentLevel,
"aggregationRegionType");
194 Input(currentLevel,
"numDimensions");
195 Input(currentLevel,
"lNodesPerDim");
201 Input(currentLevel,
"DofsPerNode");
204 if (pL.get<
bool>(
"aggregation: use interface aggregation") ==
true){
211 "interfacesDimensions was not provided by the user on level0!");
218 "nodeOnInterface was not provided by the user on level0!");
221 Input(currentLevel,
"interfacesDimensions");
222 Input(currentLevel,
"nodeOnInterface");
228 if (mapOnePtName.length() > 0) {
230 if (mapOnePtFactName ==
"" || mapOnePtFactName ==
"NoFactory") {
233 RCP<const FactoryBase> mapOnePtFact = GetFactory(mapOnePtFactName);
234 currentLevel.
DeclareInput(mapOnePtName, mapOnePtFact.get());
239 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
244 RCP<Teuchos::FancyOStream> out;
245 if(
const char* dbg = std::getenv(
"MUELU_HYBRIDAGGREGATION_DEBUG")) {
246 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
247 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
249 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
252 *out <<
"Entering hybrid aggregation" << std::endl;
254 ParameterList pL = GetParameterList();
255 bDefinitionPhase_ =
false;
257 if (pL.get<
int>(
"aggregation: max agg size") == -1)
258 pL.set(
"aggregation: max agg size", INT_MAX);
261 RCP<const FactoryBase> graphFact = GetFactory(
"Graph");
264 RCP<const GraphBase> graph = Get< RCP<GraphBase> >(currentLevel,
"Graph");
265 RCP<const Map> fineMap = graph->GetDomainMap();
266 const int myRank = fineMap->getComm()->getRank();
267 const int numRanks = fineMap->getComm()->getSize();
269 out->setProcRankAndSize(graph->GetImportMap()->getComm()->getRank(),
270 graph->GetImportMap()->getComm()->getSize());
273 RCP<Aggregates> aggregates = rcp(
new Aggregates(*graph));
274 aggregates->setObjectLabel(
"HB");
277 const LO numRows = graph->GetNodeNumVertices();
278 std::vector<unsigned> aggStat(numRows,
READY);
282 if(currentLevel.GetLevelID() == 0) {
287 regionType = Get< std::string >(currentLevel,
"aggregationRegionType");
290 int numDimensions = 0;
291 if(currentLevel.GetLevelID() == 0) {
293 numDimensions = currentLevel.Get<
int>(
"numDimensions",
NoFactory::get());
296 numDimensions = Get<int>(currentLevel,
"numDimensions");
301 Teuchos::Array<LO> coarseRate;
303 coarseRate = Teuchos::fromStringToArray<LO>(coarseningRate);
304 }
catch(
const Teuchos::InvalidArrayStringRepresentation& e) {
305 GetOStream(
Errors,-1) <<
" *** \"aggregation: coarsening rate\" must be a string convertible into an array! *** "
309 TEUCHOS_TEST_FOR_EXCEPTION((coarseRate.size() > 1) && (coarseRate.size() < numDimensions),
311 "\"aggregation: coarsening rate\" must have at least as many"
312 " components as the number of spatial dimensions in the problem.");
315 LO numNonAggregatedNodes = numRows;
316 if (regionType ==
"structured") {
322 const int interpolationOrder = pL.get<
int>(
"aggregation: coarsening order");
323 Array<LO> lFineNodesPerDir(3);
324 if(currentLevel.GetLevelID() == 0) {
326 lFineNodesPerDir = currentLevel.Get<Array<LO> >(
"lNodesPerDim",
NoFactory::get());
329 lFineNodesPerDir = Get<Array<LO> >(currentLevel,
"lNodesPerDim");
333 for(
int dim = numDimensions; dim < 3; ++dim) {
334 lFineNodesPerDir[dim] = 1;
338 RCP<MueLu::IndexManager<LO,GO,NO> > geoData;
349 TEUCHOS_TEST_FOR_EXCEPTION(fineMap->getNodeNumElements()
350 !=
static_cast<size_t>(geoData->getNumLocalFineNodes()),
352 "The local number of elements in the graph's map is not equal to "
353 "the number of nodes given by: lNodesPerDim!");
355 aggregates->SetIndexManager(geoData);
356 aggregates->SetNumAggregates(geoData->getNumLocalCoarseNodes());
358 Set(currentLevel,
"lCoarseNodesPerDim", geoData->getLocalCoarseNodesPerDir());
362 if (regionType ==
"uncoupled"){
366 if (pL.get<
bool>(
"aggregation: allow user-specified singletons") ==
true) algos_.push_back(rcp(
new OnePtAggregationAlgorithm (graphFact)));
372 *out <<
" Build interface aggregates" << std::endl;
374 if (pL.get<
bool>(
"aggregation: use interface aggregation") ==
true) {
375 BuildInterfaceAggregates(currentLevel, aggregates, aggStat, numNonAggregatedNodes,
379 *out <<
"Treat Dirichlet BC" << std::endl;
381 ArrayRCP<const bool> dirichletBoundaryMap = graph->GetBoundaryNodeMap();
382 if (dirichletBoundaryMap != Teuchos::null)
383 for (LO i = 0; i < numRows; i++)
384 if (dirichletBoundaryMap[i] ==
true)
389 RCP<Map> OnePtMap = Teuchos::null;
390 if (mapOnePtName.length()) {
392 if (mapOnePtFactName ==
"" || mapOnePtFactName ==
"NoFactory") {
393 OnePtMap = currentLevel.Get<RCP<Map> >(mapOnePtName,
NoFactory::get());
395 RCP<const FactoryBase> mapOnePtFact = GetFactory(mapOnePtFactName);
396 OnePtMap = currentLevel.Get<RCP<Map> >(mapOnePtName, mapOnePtFact.get());
400 LO nDofsPerNode = Get<LO>(currentLevel,
"DofsPerNode");
401 GO indexBase = graph->GetDomainMap()->getIndexBase();
402 if (OnePtMap != Teuchos::null) {
403 for (LO i = 0; i < numRows; i++) {
405 GO grid = (graph->GetDomainMap()->getGlobalElement(i)-indexBase) * nDofsPerNode + indexBase;
406 for (LO kr = 0; kr < nDofsPerNode; kr++)
407 if (OnePtMap->isNodeGlobalElement(grid + kr))
413 Array<LO> lCoarseNodesPerDir(3,-1);
414 Set(currentLevel,
"lCoarseNodesPerDim", lCoarseNodesPerDir);
417 aggregates->AggregatesCrossProcessors(
false);
419 *out <<
"Run all the algorithms on the local rank" << std::endl;
420 for (
size_t a = 0; a < algos_.size(); a++) {
423 *out << regionType <<
" | Executing phase " << a << std::endl;
425 int oldRank = algos_[a]->SetProcRankVerbose(this->GetProcRankVerbose());
426 algos_[a]->BuildAggregates(pL, *graph, *aggregates, aggStat, numNonAggregatedNodes);
427 algos_[a]->SetProcRankVerbose(oldRank);
428 *out << regionType <<
" | Done Executing phase " << a << std::endl;
431 *out <<
"Compute statistics on aggregates" << std::endl;
432 aggregates->ComputeAggregateSizes(
true);
434 Set(currentLevel,
"Aggregates", aggregates);
435 Set(currentLevel,
"numDimensions", numDimensions);
436 Set(currentLevel,
"aggregationRegionTypeCoarse", regionType);
438 GetOStream(
Statistics1) << aggregates->description() << std::endl;
439 *out <<
"HybridAggregation done!" << std::endl;
442 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
445 std::vector<unsigned>& aggStat, LO& numNonAggregatedNodes,
446 Array<LO> coarseRate)
const {
447 FactoryMonitor m(*
this,
"BuildInterfaceAggregates", currentLevel);
449 RCP<Teuchos::FancyOStream> out;
450 if(
const char* dbg = std::getenv(
"MUELU_HYBRIDAGGREGATION_DEBUG")) {
451 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
452 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
454 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
458 if(coarseRate.size() == 1) {coarseRate.resize(3, coarseRate[0]);}
459 ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
460 ArrayRCP<LO> procWinner = aggregates->GetProcWinner() ->getDataNonConst(0);
461 Array<LO> interfacesDimensions = Get<Array<LO> >(currentLevel,
"interfacesDimensions");
462 Array<LO> nodesOnInterfaces = Get<Array<LO> >(currentLevel,
"nodeOnInterface");
463 const int numInterfaces = interfacesDimensions.size() / 3;
464 const int myRank = aggregates->GetMap()->getComm()->getRank();
467 Array<LO> coarseInterfacesDimensions(interfacesDimensions.size());
468 Array<LO> nodesOnCoarseInterfaces;
470 LO endRate, totalNumCoarseNodes = 0, numCoarseNodes;
471 for(
int interfaceIdx = 0; interfaceIdx < numInterfaces; ++interfaceIdx) {
473 for(
int dim = 0; dim < 3; ++dim) {
474 endRate = interfacesDimensions[3*interfaceIdx + dim] % coarseRate[dim];
475 if(interfacesDimensions[3*interfaceIdx + dim] == 1) {
476 coarseInterfacesDimensions[3*interfaceIdx + dim] = 1;
478 coarseInterfacesDimensions[3*interfaceIdx + dim]
479 = (interfacesDimensions[3*interfaceIdx + dim] - endRate - 1) / coarseRate[dim] + 2;
481 numCoarseNodes *= coarseInterfacesDimensions[3*interfaceIdx + dim];
483 totalNumCoarseNodes += numCoarseNodes;
485 nodesOnCoarseInterfaces.resize(totalNumCoarseNodes, -1);
488 Array<LO> endRate(3);
489 LO interfaceOffset = 0, aggregateCount = 0, coarseNodeCount = 0;
490 for(
int interfaceIdx = 0; interfaceIdx < numInterfaces; ++interfaceIdx) {
491 ArrayView<LO> fineNodesPerDim = interfacesDimensions(3*interfaceIdx, 3);
492 ArrayView<LO> coarseNodesPerDim = coarseInterfacesDimensions(3*interfaceIdx, 3);
493 LO numInterfaceNodes = 1, numCoarseNodes = 1;
494 for(
int dim = 0; dim < 3; ++dim) {
495 numInterfaceNodes *= fineNodesPerDim[dim];
496 numCoarseNodes *= coarseNodesPerDim[dim];
497 endRate[dim] = fineNodesPerDim[dim] % coarseRate[dim];
499 ArrayView<LO> interfaceNodes = nodesOnInterfaces(interfaceOffset, numInterfaceNodes);
501 interfaceOffset += numInterfaceNodes;
503 LO rem, rate, fineNodeIdx;
504 Array<LO> nodeIJK(3), coarseIJK(3), rootIJK(3);
507 for(LO coarseNodeIdx = 0; coarseNodeIdx < numCoarseNodes; ++coarseNodeIdx) {
508 coarseIJK[2] = coarseNodeIdx / (coarseNodesPerDim[0]*coarseNodesPerDim[1]);
509 rem = coarseNodeIdx % (coarseNodesPerDim[0]*coarseNodesPerDim[1]);
510 coarseIJK[1] = rem / coarseNodesPerDim[0];
511 coarseIJK[0] = rem % coarseNodesPerDim[0];
513 for(LO dim = 0; dim < 3; ++dim) {
514 if(coarseIJK[dim] == coarseNodesPerDim[dim] - 1) {
515 nodeIJK[dim] = fineNodesPerDim[dim] - 1;
517 nodeIJK[dim] = coarseIJK[dim]*coarseRate[dim];
520 fineNodeIdx = (nodeIJK[2]*fineNodesPerDim[1] + nodeIJK[1])*fineNodesPerDim[0] + nodeIJK[0];
522 if(aggStat[interfaceNodes[fineNodeIdx]] ==
READY) {
523 vertex2AggId[interfaceNodes[fineNodeIdx]] = aggregateCount;
524 procWinner[interfaceNodes[fineNodeIdx]] = myRank;
525 aggStat[interfaceNodes[fineNodeIdx]] =
AGGREGATED;
527 --numNonAggregatedNodes;
529 nodesOnCoarseInterfaces[coarseNodeCount] = vertex2AggId[interfaceNodes[fineNodeIdx]];
536 for(LO nodeIdx = 0; nodeIdx < numInterfaceNodes; ++nodeIdx) {
539 if(aggStat[interfaceNodes[nodeIdx]] ==
AGGREGATED) {
continue;}
541 nodeIJK[2] = nodeIdx / (fineNodesPerDim[0]*fineNodesPerDim[1]);
542 rem = nodeIdx % (fineNodesPerDim[0]*fineNodesPerDim[1]);
543 nodeIJK[1] = rem / fineNodesPerDim[0];
544 nodeIJK[0] = rem % fineNodesPerDim[0];
546 for(
int dim = 0; dim < 3; ++dim) {
547 coarseIJK[dim] = nodeIJK[dim] / coarseRate[dim];
548 rem = nodeIJK[dim] % coarseRate[dim];
549 if(nodeIJK[dim] < fineNodesPerDim[dim] - endRate[dim]) {
550 rate = coarseRate[dim];
554 if(rem > (rate / 2)) {++coarseIJK[dim];}
557 for(LO dim = 0; dim < 3; ++dim) {
558 if(coarseIJK[dim] == coarseNodesPerDim[dim] - 1) {
559 nodeIJK[dim] = fineNodesPerDim[dim] - 1;
561 nodeIJK[dim] = coarseIJK[dim]*coarseRate[dim];
564 fineNodeIdx = (nodeIJK[2]*fineNodesPerDim[1] + nodeIJK[1])*fineNodesPerDim[0] + nodeIJK[0];
566 vertex2AggId[interfaceNodes[nodeIdx]] = vertex2AggId[interfaceNodes[fineNodeIdx]];
567 procWinner[interfaceNodes[nodeIdx]] = myRank;
568 aggStat[interfaceNodes[nodeIdx]] =
AGGREGATED;
569 --numNonAggregatedNodes;
574 aggregates->SetNumAggregates(aggregateCount);
577 Set(currentLevel,
"coarseInterfacesDimensions", coarseInterfacesDimensions);
578 Set(currentLevel,
"nodeOnCoarseInterface", nodesOnCoarseInterfaces);