// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_REBALANCEMAPFACTORY_DEF_HPP_ #define MUELU_REBALANCEMAPFACTORY_DEF_HPP_ #include "MueLu_RebalanceMapFactory_decl.hpp" #include #include "MueLu_Exceptions.hpp" #include "MueLu_Level.hpp" #include "MueLu_MasterList.hpp" #include "MueLu_Monitor.hpp" namespace MueLu { template RCP RebalanceMapFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name)) SET_VALID_ENTRY("repartition: use subcommunicators"); #undef SET_VALID_ENTRY // Information about map that is to be rebalanced validParamList->set("Map name", "", "Name of map to rebalanced."); validParamList->set >("Map factory", MueLu::NoFactory::getRCP(), "Generating factory of map to be rebalanced."); // Importer object with rebalancing information validParamList->set >("Importer", Teuchos::null, "Factory of the importer object used for the rebalancing"); return validParamList; } template void RebalanceMapFactory::DeclareInput(Level ¤tLevel) const { const Teuchos::ParameterList &pL = GetParameterList(); std::string mapName = pL.get("Map name"); Teuchos::RCP mapFactory = GetFactory("Map factory"); currentLevel.DeclareInput(mapName, mapFactory.get(), this); Input(currentLevel, "Importer"); } // DeclareInput() template void RebalanceMapFactory::Build(Level &level) const { FactoryMonitor m(*this, "Build", level); // Teuchos::RCP fos = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout)); // extract data from Level object const Teuchos::ParameterList &pL = GetParameterList(); std::string mapName = pL.get("Map name"); Teuchos::RCP mapFactory = GetFactory("Map factory"); RCP rebalanceImporter = Get >(level, "Importer"); if (rebalanceImporter != Teuchos::null) { // input map (not rebalanced) RCP map = level.Get >(mapName, mapFactory.get()); // create vector based on input map // Note, that the map can be a part only of the full map stored in rebalanceImporter.getSourceMap() RCP v = VectorFactory::Build(map); v->putScalar(1.0); // create a new vector based on the full rebalanceImporter.getSourceMap() // import the partial map information to the full source map RCP blowUpImporter = ImportFactory::Build(map, rebalanceImporter->getSourceMap()); RCP pv = VectorFactory::Build(rebalanceImporter->getSourceMap()); pv->doImport(*v, *blowUpImporter, Xpetra::INSERT); // do rebalancing using rebalanceImporter RCP ptv = VectorFactory::Build(rebalanceImporter->getTargetMap()); ptv->doImport(*pv, *rebalanceImporter, Xpetra::INSERT); if (pL.get("repartition: use subcommunicators") == true) ptv->replaceMap(ptv->getMap()->removeEmptyProcesses()); // reconstruct rebalanced partial map Teuchos::ArrayRCP ptvData = ptv->getData(0); std::vector localGIDs; // vector with GIDs that are stored on current proc for (size_t k = 0; k < ptv->getLocalLength(); k++) { if (ptvData[k] == 1.0) { localGIDs.push_back(ptv->getMap()->getGlobalElement(k)); } } const Teuchos::ArrayView localGIDs_view(&localGIDs[0], localGIDs.size()); Teuchos::RCP localGIDsMap = MapFactory::Build( map->lib(), Teuchos::OrdinalTraits::invalid(), localGIDs_view, 0, ptv->getMap()->getComm()); // use correct communicator here! // store rebalanced partial map using the same name and generating factory as the original map // in the level class level.Set(mapName, localGIDsMap, mapFactory.get()); } } // Build() } // end namespace MueLu #endif /* MUELU_REBALANCEMAPFACTORY_DEF_HPP_ */