// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_BLOCKEDPFACTORY_DEF_HPP_ #define MUELU_BLOCKEDPFACTORY_DEF_HPP_ #include #include #include #include #include #include #include #include #include #include #include "MueLu_BlockedPFactory_decl.hpp" #include "MueLu_FactoryBase.hpp" #include "MueLu_FactoryManager.hpp" #include "MueLu_Monitor.hpp" #include "MueLu_HierarchyUtils.hpp" namespace MueLu { template RCP BlockedPFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); validParamList->set >("A", null, "Generating factory of the matrix A (block matrix)"); validParamList->set("backwards", false, "Forward/backward order"); return validParamList; } template void BlockedPFactory::DeclareInput(Level& fineLevel, Level& coarseLevel) const { Input(fineLevel, "A"); const ParameterList& pL = GetParameterList(); const bool backwards = pL.get("backwards"); const int numFactManagers = FactManager_.size(); for (int k = 0; k < numFactManagers; k++) { int i = (backwards ? numFactManagers - 1 - k : k); const RCP& factManager = FactManager_[i]; SetFactoryManager fineSFM(rcpFromRef(fineLevel), factManager); SetFactoryManager coarseSFM(rcpFromRef(coarseLevel), factManager); if (!restrictionMode_) coarseLevel.DeclareInput("P", factManager->GetFactory("P").get(), this); else coarseLevel.DeclareInput("R", factManager->GetFactory("R").get(), this); } } template void BlockedPFactory::BuildP(Level& /* fineLevel */, Level& /* coarseLevel */) const {} template void BlockedPFactory::Build(Level& fineLevel, Level& coarseLevel) const { FactoryMonitor m(*this, "Build", coarseLevel); RCP Ain = Get >(fineLevel, "A"); RCP A = rcp_dynamic_cast(Ain); TEUCHOS_TEST_FOR_EXCEPTION(A.is_null(), Exceptions::BadCast, "Input matrix A is not a BlockedCrsMatrix."); const int numFactManagers = FactManager_.size(); // Plausibility check TEUCHOS_TEST_FOR_EXCEPTION(A->Rows() != as(numFactManagers), Exceptions::RuntimeError, "Number of block rows [" << A->Rows() << "] does not match the number of SubFactorManagers [" << numFactManagers << "]"); TEUCHOS_TEST_FOR_EXCEPTION(A->Cols() != as(numFactManagers), Exceptions::RuntimeError, "Number of block cols [" << A->Cols() << "] does not match the number of SubFactorManagers [" << numFactManagers << "]"); // Build blocked prolongator std::vector > subBlockP(numFactManagers); std::vector > subBlockPRangeMaps(numFactManagers); std::vector > subBlockPDomainMaps(numFactManagers); std::vector fullRangeMapVector; std::vector fullDomainMapVector; RCP rangeAMapExtractor = A->getRangeMapExtractor(); RCP domainAMapExtractor = A->getDomainMapExtractor(); const ParameterList& pL = GetParameterList(); const bool backwards = pL.get("backwards"); // Build and store the subblocks and the corresponding range and domain // maps. Since we put together the full range and domain map from the // submaps, we do not have to use the maps from blocked A for (int k = 0; k < numFactManagers; k++) { int i = (backwards ? numFactManagers - 1 - k : k); if (restrictionMode_) GetOStream(Runtime1) << "Generating R for block " << k << "/" << numFactManagers << std::endl; else GetOStream(Runtime1) << "Generating P for block " << k << "/" << numFactManagers << std::endl; const RCP& factManager = FactManager_[i]; SetFactoryManager fineSFM(rcpFromRef(fineLevel), factManager); SetFactoryManager coarseSFM(rcpFromRef(coarseLevel), factManager); if (!restrictionMode_) subBlockP[i] = coarseLevel.Get >("P", factManager->GetFactory("P").get()); else subBlockP[i] = coarseLevel.Get >("R", factManager->GetFactory("R").get()); // Check if prolongator/restrictor operators have strided maps TEUCHOS_TEST_FOR_EXCEPTION(subBlockP[i]->IsView("stridedMaps") == false, Exceptions::BadCast, "subBlock P operator [" << i << "] has no strided map information."); // Append strided row map (= range map) to list of range maps. // TAW the row map GIDs extracted here represent the actual row map GIDs. // No support for nested operators! (at least if Thyra style gids are used) RCP strPartialMap = Teuchos::rcp_dynamic_cast(subBlockP[i]->getRowMap("stridedMaps")); std::vector stridedRgData = strPartialMap->getStridingData(); subBlockPRangeMaps[i] = StridedMapFactory::Build( subBlockP[i]->getRangeMap(), // actual global IDs (Thyra or Xpetra) stridedRgData, strPartialMap->getStridedBlockId(), strPartialMap->getOffset()); // subBlockPRangeMaps[i] = subBlockP[i]->getRowMap("stridedMaps"); // Use plain range map to determine the DOF ids ArrayView nodeRangeMap = subBlockPRangeMaps[i]->getLocalElementList(); fullRangeMapVector.insert(fullRangeMapVector.end(), nodeRangeMap.begin(), nodeRangeMap.end()); // Append strided col map (= domain map) to list of range maps. RCP strPartialMap2 = Teuchos::rcp_dynamic_cast(subBlockP[i]->getColMap("stridedMaps")); std::vector stridedRgData2 = strPartialMap2->getStridingData(); subBlockPDomainMaps[i] = StridedMapFactory::Build( subBlockP[i]->getDomainMap(), // actual global IDs (Thyra or Xpetra) stridedRgData2, strPartialMap2->getStridedBlockId(), strPartialMap2->getOffset()); // subBlockPDomainMaps[i] = subBlockP[i]->getColMap("stridedMaps"); // Use plain domain map to determine the DOF ids ArrayView nodeDomainMap = subBlockPDomainMaps[i]->getLocalElementList(); fullDomainMapVector.insert(fullDomainMapVector.end(), nodeDomainMap.begin(), nodeDomainMap.end()); } // check if GIDs for full maps have to be sorted: // For the Thyra mode ordering they do not have to be sorted since the GIDs are // numbered as 0...n1,0...,n2 (starting with zero for each subblock). The MapExtractor // generates unique GIDs during the construction. // For Xpetra style, the GIDs have to be reordered. Such that one obtains a ordered // list of GIDs in an increasing ordering. In Xpetra, the GIDs are all unique through // out all submaps. bool bDoSortRangeGIDs = rangeAMapExtractor->getThyraMode() ? false : true; bool bDoSortDomainGIDs = domainAMapExtractor->getThyraMode() ? false : true; if (bDoSortRangeGIDs) { sort(fullRangeMapVector.begin(), fullRangeMapVector.end()); fullRangeMapVector.erase(std::unique(fullRangeMapVector.begin(), fullRangeMapVector.end()), fullRangeMapVector.end()); } if (bDoSortDomainGIDs) { sort(fullDomainMapVector.begin(), fullDomainMapVector.end()); fullDomainMapVector.erase(std::unique(fullDomainMapVector.begin(), fullDomainMapVector.end()), fullDomainMapVector.end()); } // extract map index base from maps of blocked A GO rangeIndexBase = 0; GO domainIndexBase = 0; if (!restrictionMode_) { // Prolongation mode: just use index base of range and domain map of A rangeIndexBase = A->getRangeMap()->getIndexBase(); domainIndexBase = A->getDomainMap()->getIndexBase(); } else { // Restriction mode: switch range and domain map for blocked restriction operator rangeIndexBase = A->getDomainMap()->getIndexBase(); domainIndexBase = A->getRangeMap()->getIndexBase(); } // Build full range map. // If original range map has striding information, then transfer it to the // new range map RCP stridedRgFullMap = rcp_dynamic_cast(rangeAMapExtractor->getFullMap()); RCP fullRangeMap = Teuchos::null; ArrayView fullRangeMapGIDs(fullRangeMapVector.size() ? &fullRangeMapVector[0] : 0, fullRangeMapVector.size()); if (stridedRgFullMap != Teuchos::null) { std::vector stridedData = stridedRgFullMap->getStridingData(); fullRangeMap = StridedMapFactory::Build( A->getRangeMap()->lib(), Teuchos::OrdinalTraits::invalid(), fullRangeMapGIDs, rangeIndexBase, stridedData, A->getRangeMap()->getComm(), -1, /* the full map vector should always have strided block id -1! */ stridedRgFullMap->getOffset()); } else { fullRangeMap = MapFactory::Build( A->getRangeMap()->lib(), Teuchos::OrdinalTraits::invalid(), fullRangeMapGIDs, rangeIndexBase, A->getRangeMap()->getComm()); } ArrayView fullDomainMapGIDs(fullDomainMapVector.size() ? &fullDomainMapVector[0] : 0, fullDomainMapVector.size()); RCP stridedDoFullMap = rcp_dynamic_cast(domainAMapExtractor->getFullMap()); RCP fullDomainMap = null; if (stridedDoFullMap != null) { TEUCHOS_TEST_FOR_EXCEPTION(stridedDoFullMap == Teuchos::null, Exceptions::BadCast, "MueLu::BlockedPFactory::Build: full map in domain map extractor has no striding information!"); std::vector stridedData2 = stridedDoFullMap->getStridingData(); fullDomainMap = StridedMapFactory::Build( A->getDomainMap()->lib(), Teuchos::OrdinalTraits::invalid(), fullDomainMapGIDs, domainIndexBase, stridedData2, A->getDomainMap()->getComm(), -1, /* the full map vector should always have strided block id -1! */ stridedDoFullMap->getOffset()); } else { fullDomainMap = MapFactory::Build( A->getDomainMap()->lib(), Teuchos::OrdinalTraits::invalid(), fullDomainMapGIDs, domainIndexBase, A->getDomainMap()->getComm()); } // Build map extractors RCP rangeMapExtractor = MapExtractorFactory::Build(fullRangeMap, subBlockPRangeMaps, rangeAMapExtractor->getThyraMode()); RCP domainMapExtractor = MapExtractorFactory::Build(fullDomainMap, subBlockPDomainMaps, domainAMapExtractor->getThyraMode()); RCP P = rcp(new BlockedCrsMatrix(rangeMapExtractor, domainMapExtractor, 10)); for (size_t i = 0; i < subBlockPRangeMaps.size(); i++) for (size_t j = 0; j < subBlockPRangeMaps.size(); j++) if (i == j) { RCP crsOpii = rcp_dynamic_cast(subBlockP[i]); TEUCHOS_TEST_FOR_EXCEPTION(crsOpii == Teuchos::null, Xpetra::Exceptions::BadCast, "Block [" << i << "," << j << "] must be of type CrsMatrixWrap."); P->setMatrix(i, i, crsOpii); } else { P->setMatrix(i, j, Teuchos::null); } P->fillComplete(); // Level Set if (!restrictionMode_) { // Prolongation mode coarseLevel.Set("P", rcp_dynamic_cast(P), this); // Stick the CoarseMap on the level if somebody wants it (useful for repartitioning) coarseLevel.Set("CoarseMap", P->getBlockedDomainMap(), this); } else { // Restriction mode // We do not have to transpose the blocked R operator since the subblocks // on the diagonal are already valid R subblocks coarseLevel.Set("R", Teuchos::rcp_dynamic_cast(P), this); } } } // namespace MueLu #endif /* MUELU_BLOCKEDPFACTORY_DEF_HPP_ */