MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_RebalanceAcFactory_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_REBALANCEACFACTORY_DEF_HPP
11#define MUELU_REBALANCEACFACTORY_DEF_HPP
12
13#include <Xpetra_Matrix.hpp>
14#include <Xpetra_CrsMatrix.hpp>
15#include <Xpetra_CrsMatrixWrap.hpp>
16#include <Xpetra_MatrixFactory.hpp>
17
19
20#include "MueLu_MasterList.hpp"
21#include "MueLu_Monitor.hpp"
22#include "MueLu_PerfUtils.hpp"
23#include "MueLu_RAPFactory.hpp"
24
25namespace MueLu {
26
27template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
29
30template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
32
33template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
35 RCP<ParameterList> validParamList = rcp(new ParameterList());
36
37#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
38 SET_VALID_ENTRY("repartition: use subcommunicators");
39 SET_VALID_ENTRY("repartition: use subcommunicators in place");
40#undef SET_VALID_ENTRY
41
42 validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A for rebalancing");
43 validParamList->set<RCP<const FactoryBase> >("Importer", Teuchos::null, "Generating factory of the importer");
44 validParamList->set<RCP<const FactoryBase> >("InPlaceMap", Teuchos::null, "Generating factory of the InPlaceMap");
45
46 return validParamList;
47}
48
49template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
51 Input(coarseLevel, "A");
52 const Teuchos::ParameterList &pL = GetParameterList();
53 if (pL.isParameter("repartition: use subcommunicators in place") && pL.get<bool>("repartition: use subcommunicators in place") == true) {
54 Input(coarseLevel, "InPlaceMap");
55 } else
56 Input(coarseLevel, "Importer");
57}
58
59template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
61 FactoryMonitor m(*this, "Computing Ac", coarseLevel);
62
63 const Teuchos::ParameterList &pL = GetParameterList();
64 RCP<Matrix> originalAc = Get<RCP<Matrix> >(coarseLevel, "A");
65
66 // This is a short-circuit for if we want to leave A where it is, but restrict its communicator
67 // to something corresponding to a given map. Maxwell1 is the prime customer for this.
68 bool inPlace = pL.get<bool>("repartition: use subcommunicators in place");
69 if (inPlace) {
70 SubFactoryMonitor subM(*this, "Rebalancing existing Ac in-place", coarseLevel);
71 RCP<const Map> newMap = Get<RCP<const Map> >(coarseLevel, "InPlaceMap");
72
73 originalAc->removeEmptyProcessesInPlace(newMap);
74
75 // The "in place" still leaves a dummy matrix here. That needs to go
76 if (newMap.is_null()) originalAc = Teuchos::null;
77
78 Set(coarseLevel, "A", originalAc);
79 return;
80 }
81
82 RCP<const Import> rebalanceImporter = Get<RCP<const Import> >(coarseLevel, "Importer");
83
84 if (rebalanceImporter != Teuchos::null) {
85 RCP<Matrix> rebalancedAc;
86 {
87 SubFactoryMonitor subM(*this, "Rebalancing existing Ac", coarseLevel);
88 RCP<const Map> targetMap = rebalanceImporter->getTargetMap();
89
90 ParameterList XpetraList;
91 if (pL.get<bool>("repartition: use subcommunicators") == true) {
92 GetOStream(Runtime0) << "Replacing maps with a subcommunicator" << std::endl;
93 XpetraList.set("Restrict Communicator", true);
94 }
95 // NOTE: If the communicator is restricted away, Build returns Teuchos::null.
96 XpetraList.set("Timer Label", "MueLu::RebalanceAc-" + Teuchos::toString(coarseLevel.GetLevelID()));
97 {
98 SubFactoryMonitor subM2(*this, "Rebalancing existing Ac: MatrixFactory::Build", coarseLevel);
99 rebalancedAc = MatrixFactory::Build(originalAc, *rebalanceImporter, *rebalanceImporter, targetMap, targetMap, rcp(&XpetraList, false));
100 }
101
102 if (!rebalancedAc.is_null()) {
103 rebalancedAc->SetFixedBlockSize(originalAc->GetFixedBlockSize());
104 std::ostringstream oss;
105 oss << "A_" << coarseLevel.GetLevelID();
106 rebalancedAc->setObjectLabel(oss.str());
107 }
108 Set(coarseLevel, "A", rebalancedAc);
109 }
110 if (!rebalancedAc.is_null() && IsPrint(Statistics2)) {
111 int oldRank = SetProcRankVerbose(rebalancedAc->getRowMap()->getComm()->getRank());
112
113 RCP<ParameterList> params = rcp(new ParameterList());
114 params->set("printLoadBalancingInfo", true);
115 params->set("printCommInfo", true);
116 GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*rebalancedAc, "Ac (rebalanced)", params);
117
118 SetProcRankVerbose(oldRank);
119 }
120
121 } else {
122 // Ac already built by the load balancing process and no load balancing needed
123 GetOStream(Runtime1) << "No rebalancing" << std::endl;
124 Set(coarseLevel, "A", originalAc);
125 }
126
127 if (rebalanceFacts_.begin() != rebalanceFacts_.end()) {
128 SubFactoryMonitor m2(*this, "Rebalance additional data", coarseLevel);
129
130 // call Build of all user-given transfer factories
131 for (std::vector<RCP<const FactoryBase> >::const_iterator it = rebalanceFacts_.begin(); it != rebalanceFacts_.end(); ++it) {
132 GetOStream(Runtime0) << "RebalanceAc: call rebalance factory " << (*it).get() << ": " << (*it)->description() << std::endl;
133 (*it)->CallBuild(coarseLevel);
134 }
135 }
136} // Build()
137
138template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
140 /*TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::rcp_dynamic_cast<const TwoLevelFactoryBase>(factory) == Teuchos::null, Exceptions::BadCast,
141 "MueLu::RAPFactory::AddTransferFactory: Transfer factory is not derived from TwoLevelFactoryBase. "
142 "This is very strange. (Note: you can remove this exception if there's a good reason for)");
143 TEUCHOS_TEST_FOR_EXCEPTION(hasDeclaredInput_, Exceptions::RuntimeError, "MueLu::RAPFactory::AddTransferFactory: Factory is being added after we have already declared input");*/
144 rebalanceFacts_.push_back(factory);
145} // AddRebalanceFactory()
146
147} // namespace MueLu
148
149#endif // MUELU_REBALANCEACFACTORY_DEF_HPP
#define SET_VALID_ENTRY(name)
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
int GetLevelID() const
Return level number.
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
void AddRebalanceFactory(const RCP< const FactoryBase > &factory)
Add rebalancing factory in the end of list of rebalancing factories in RebalanceAcFactory.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
Namespace for MueLu classes and methods.
@ Statistics2
Print even more statistics.
@ Runtime0
One-liner description of what is happening.
@ Runtime1
Description of what is happening (more verbose)