243 RCP<Teuchos::FancyOStream> out;
244 if(
const char* dbg = std::getenv(
"MUELU_HYBRIDAGGREGATION_DEBUG")) {
245 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
246 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
248 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
251 *out <<
"Entering hybrid aggregation" << std::endl;
256 if (pL.get<
int>(
"aggregation: max agg size") == -1)
257 pL.set(
"aggregation: max agg size", INT_MAX);
260 RCP<const FactoryBase> graphFact =
GetFactory(
"Graph");
264 RCP<const Map> fineMap = graph->GetDomainMap();
265 const int myRank = fineMap->getComm()->getRank();
266 const int numRanks = fineMap->getComm()->getSize();
268 out->setProcRankAndSize(graph->GetImportMap()->getComm()->getRank(),
269 graph->GetImportMap()->getComm()->getSize());
272 RCP<Aggregates> aggregates = rcp(
new Aggregates(*graph));
273 aggregates->setObjectLabel(
"HB");
276 const LO numRows = graph->GetNodeNumVertices();
277 std::vector<unsigned> aggStat(numRows,
READY);
280 std::string regionType;
283 regionType = currentLevel.
Get<std::string>(
"aggregationRegionType",
NoFactory::get());
289 int numDimensions = 0;
295 numDimensions =
Get<int>(currentLevel,
"numDimensions");
299 std::string coarseningRate = pL.get<std::string>(
"aggregation: coarsening rate");
300 Teuchos::Array<LO> coarseRate;
302 coarseRate = Teuchos::fromStringToArray<LO>(coarseningRate);
303 }
catch(
const Teuchos::InvalidArrayStringRepresentation& e) {
304 GetOStream(
Errors,-1) <<
" *** \"aggregation: coarsening rate\" must be a string convertible into an array! *** "
308 TEUCHOS_TEST_FOR_EXCEPTION((coarseRate.size() > 1) && (coarseRate.size() < numDimensions),
310 "\"aggregation: coarsening rate\" must have at least as many"
311 " components as the number of spatial dimensions in the problem.");
314 LO numNonAggregatedNodes = numRows;
315 if (regionType ==
"structured") {
321 const int interpolationOrder = pL.get<
int>(
"aggregation: coarsening order");
322 Array<LO> lFineNodesPerDir(3);
325 lFineNodesPerDir = currentLevel.
Get<Array<LO> >(
"lNodesPerDim",
NoFactory::get());
328 lFineNodesPerDir =
Get<Array<LO> >(currentLevel,
"lNodesPerDim");
332 for(
int dim = numDimensions; dim < 3; ++dim) {
333 lFineNodesPerDir[dim] = 1;
337 RCP<MueLu::IndexManager<LO,GO,NO> > geoData;
348 TEUCHOS_TEST_FOR_EXCEPTION(fineMap->getLocalNumElements()
349 !=
static_cast<size_t>(geoData->getNumLocalFineNodes()),
351 "The local number of elements in the graph's map is not equal to "
352 "the number of nodes given by: lNodesPerDim!");
354 aggregates->SetIndexManager(geoData);
355 aggregates->SetNumAggregates(geoData->getNumLocalCoarseNodes());
357 Set(currentLevel,
"lCoarseNodesPerDim", geoData->getLocalCoarseNodesPerDir());
361 if (regionType ==
"uncoupled"){
371 *out <<
" Build interface aggregates" << std::endl;
373 if (pL.get<
bool>(
"aggregation: use interface aggregation") ==
true) {
378 *out <<
"Treat Dirichlet BC" << std::endl;
380 ArrayRCP<const bool> dirichletBoundaryMap = graph->GetBoundaryNodeMap();
381 if (dirichletBoundaryMap != Teuchos::null)
382 for (LO i = 0; i < numRows; i++)
383 if (dirichletBoundaryMap[i] ==
true)
387 std::string mapOnePtName = pL.get<std::string>(
"OnePt aggregate map name");
388 RCP<Map> OnePtMap = Teuchos::null;
389 if (mapOnePtName.length()) {
390 std::string mapOnePtFactName = pL.get<std::string>(
"OnePt aggregate map factory");
391 if (mapOnePtFactName ==
"" || mapOnePtFactName ==
"NoFactory") {
394 RCP<const FactoryBase> mapOnePtFact =
GetFactory(mapOnePtFactName);
395 OnePtMap = currentLevel.
Get<RCP<Map> >(mapOnePtName, mapOnePtFact.get());
399 LO nDofsPerNode =
Get<LO>(currentLevel,
"DofsPerNode");
400 GO indexBase = graph->GetDomainMap()->getIndexBase();
401 if (OnePtMap != Teuchos::null) {
402 for (LO i = 0; i < numRows; i++) {
404 GO grid = (graph->GetDomainMap()->getGlobalElement(i)-indexBase) * nDofsPerNode + indexBase;
405 for (LO kr = 0; kr < nDofsPerNode; kr++)
406 if (OnePtMap->isNodeGlobalElement(grid + kr))
412 Array<LO> lCoarseNodesPerDir(3,-1);
413 Set(currentLevel,
"lCoarseNodesPerDim", lCoarseNodesPerDir);
416 aggregates->AggregatesCrossProcessors(
false);
418 *out <<
"Run all the algorithms on the local rank" << std::endl;
419 for (
size_t a = 0; a <
algos_.size(); a++) {
420 std::string phase =
algos_[a]->description();
422 *out << regionType <<
" | Executing phase " << a << std::endl;
425 algos_[a]->BuildAggregates(pL, *graph, *aggregates, aggStat, numNonAggregatedNodes);
426 algos_[a]->SetProcRankVerbose(oldRank);
427 *out << regionType <<
" | Done Executing phase " << a << std::endl;
430 *out <<
"Compute statistics on aggregates" << std::endl;
431 aggregates->ComputeAggregateSizes(
true);
433 Set(currentLevel,
"Aggregates", aggregates);
434 Set(currentLevel,
"numDimensions", numDimensions);
435 Set(currentLevel,
"aggregationRegionTypeCoarse", regionType);
438 *out <<
"HybridAggregation done!" << std::endl;
444 std::vector<unsigned>& aggStat, LO& numNonAggregatedNodes,
445 Array<LO> coarseRate)
const {
446 FactoryMonitor m(*
this,
"BuildInterfaceAggregates", currentLevel);
448 RCP<Teuchos::FancyOStream> out;
449 if(
const char* dbg = std::getenv(
"MUELU_HYBRIDAGGREGATION_DEBUG")) {
450 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
451 out->setShowAllFrontMatter(
false).setShowProcRank(
true);
453 out = Teuchos::getFancyOStream(rcp(
new Teuchos::oblackholestream()));
457 if(coarseRate.size() == 1) {coarseRate.resize(3, coarseRate[0]);}
458 ArrayRCP<LO> vertex2AggId = aggregates->GetVertex2AggId()->getDataNonConst(0);
459 ArrayRCP<LO> procWinner = aggregates->GetProcWinner() ->getDataNonConst(0);
460 Array<LO> interfacesDimensions =
Get<Array<LO> >(currentLevel,
"interfacesDimensions");
461 Array<LO> nodesOnInterfaces =
Get<Array<LO> >(currentLevel,
"nodeOnInterface");
462 const int numInterfaces = interfacesDimensions.size() / 3;
463 const int myRank = aggregates->GetMap()->getComm()->getRank();
466 Array<LO> coarseInterfacesDimensions(interfacesDimensions.size());
467 Array<LO> nodesOnCoarseInterfaces;
469 LO endRate, totalNumCoarseNodes = 0, numCoarseNodes;
470 for(
int interfaceIdx = 0; interfaceIdx < numInterfaces; ++interfaceIdx) {
472 for(
int dim = 0; dim < 3; ++dim) {
473 endRate = (interfacesDimensions[3*interfaceIdx + dim] - 1) % coarseRate[dim];
474 if(interfacesDimensions[3*interfaceIdx + dim] == 1) {
475 coarseInterfacesDimensions[3*interfaceIdx + dim] = 1;
477 coarseInterfacesDimensions[3*interfaceIdx + dim]
478 = (interfacesDimensions[3*interfaceIdx+dim]-1) / coarseRate[dim] + 2;
479 if(endRate==0){ coarseInterfacesDimensions[3*interfaceIdx + dim]--;}
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]-1) % 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);