MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_RebalanceMapFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef MUELU_REBALANCEMAPFACTORY_DEF_HPP_
11 #define MUELU_REBALANCEMAPFACTORY_DEF_HPP_
12 
14 
15 #include <Teuchos_Utils.hpp>
16 
17 #include "MueLu_Exceptions.hpp"
18 #include "MueLu_Level.hpp"
19 #include "MueLu_MasterList.hpp"
20 #include "MueLu_Monitor.hpp"
21 
22 namespace MueLu {
23 
24 template <class LocalOrdinal, class GlobalOrdinal, class Node>
26  RCP<ParameterList> validParamList = rcp(new ParameterList());
27 
28 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
29  SET_VALID_ENTRY("repartition: use subcommunicators");
30 #undef SET_VALID_ENTRY
31 
32  // Information about map that is to be rebalanced
33  validParamList->set<std::string>("Map name", "", "Name of map to rebalanced.");
34  validParamList->set<RCP<const FactoryBase> >("Map factory", MueLu::NoFactory::getRCP(), "Generating factory of map to be rebalanced.");
35 
36  // Importer object with rebalancing information
37  validParamList->set<RCP<const FactoryBase> >("Importer", Teuchos::null, "Factory of the importer object used for the rebalancing");
38 
39  return validParamList;
40 }
41 
42 template <class LocalOrdinal, class GlobalOrdinal, class Node>
44  const Teuchos::ParameterList &pL = GetParameterList();
45  std::string mapName = pL.get<std::string>("Map name");
46  Teuchos::RCP<const FactoryBase> mapFactory = GetFactory("Map factory");
47  currentLevel.DeclareInput(mapName, mapFactory.get(), this);
48 
49  Input(currentLevel, "Importer");
50 } // DeclareInput()
51 
52 template <class LocalOrdinal, class GlobalOrdinal, class Node>
54  FactoryMonitor m(*this, "Build", level);
55 
56  // Teuchos::RCP<Teuchos::FancyOStream> fos = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
57 
58  // extract data from Level object
59  const Teuchos::ParameterList &pL = GetParameterList();
60  std::string mapName = pL.get<std::string>("Map name");
61  Teuchos::RCP<const FactoryBase> mapFactory = GetFactory("Map factory");
62 
63  RCP<const Import> rebalanceImporter = Get<RCP<const Import> >(level, "Importer");
64 
65  if (rebalanceImporter != Teuchos::null) {
66  // input map (not rebalanced)
67  RCP<const Map> map = level.Get<RCP<const Map> >(mapName, mapFactory.get());
68 
69  // create vector based on input map
70  // Note, that the map can be a part only of the full map stored in rebalanceImporter.getSourceMap()
71  RCP<Vector> v = VectorFactory::Build(map);
72  v->putScalar(1.0);
73 
74  // create a new vector based on the full rebalanceImporter.getSourceMap()
75  // import the partial map information to the full source map
76  RCP<const Import> blowUpImporter = ImportFactory::Build(map, rebalanceImporter->getSourceMap());
77  RCP<Vector> pv = VectorFactory::Build(rebalanceImporter->getSourceMap());
78  pv->doImport(*v, *blowUpImporter, Xpetra::INSERT);
79 
80  // do rebalancing using rebalanceImporter
81  RCP<Vector> ptv = VectorFactory::Build(rebalanceImporter->getTargetMap());
82  ptv->doImport(*pv, *rebalanceImporter, Xpetra::INSERT);
83 
84  if (pL.get<bool>("repartition: use subcommunicators") == true)
85  ptv->replaceMap(ptv->getMap()->removeEmptyProcesses());
86 
87  // reconstruct rebalanced partial map
88  Teuchos::ArrayRCP<const Scalar> ptvData = ptv->getData(0);
89  std::vector<GlobalOrdinal> localGIDs; // vector with GIDs that are stored on current proc
90 
91  for (size_t k = 0; k < ptv->getLocalLength(); k++) {
92  if (ptvData[k] == 1.0) {
93  localGIDs.push_back(ptv->getMap()->getGlobalElement(k));
94  }
95  }
96 
97  const Teuchos::ArrayView<const GlobalOrdinal> localGIDs_view(&localGIDs[0], localGIDs.size());
98 
99  Teuchos::RCP<const Map> localGIDsMap = MapFactory::Build(
100  map->lib(),
102  localGIDs_view,
103  0, ptv->getMap()->getComm()); // use correct communicator here!
104 
105  // store rebalanced partial map using the same name and generating factory as the original map
106  // in the level class
107  level.Set(mapName, localGIDsMap, mapFactory.get());
108  }
109 } // Build()
110 
111 } // end namespace MueLu
112 
113 #endif /* MUELU_REBALANCEMAPFACTORY_DEF_HPP_ */
T & get(const std::string &name, T def_value)
#define SET_VALID_ENTRY(name)
Timer to be used in factories. Similar to Monitor but with additional timers.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Build(Level &level) const
Build an object with this factory.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
static const RCP< const NoFactory > getRCP()
Static Get() functions.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()