10 #ifndef MUELU_REBALANCEBLOCKRESTRICTIONFACTORY_DEF_HPP_
11 #define MUELU_REBALANCEBLOCKRESTRICTIONFACTORY_DEF_HPP_
13 #include <Teuchos_Tuple.hpp>
16 #include "Xpetra_MultiVectorFactory.hpp"
21 #include <Xpetra_MapFactory.hpp>
22 #include <Xpetra_MapExtractor.hpp>
30 #include "MueLu_HierarchyUtils.hpp"
35 #include "MueLu_PerfUtils.hpp"
39 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
43 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
45 #undef SET_VALID_ENTRY
49 validParamList->
set<
RCP<const FactoryBase> >(
"R", Teuchos::null,
"Factory of the restriction operator that need to be rebalanced (only used if type=Restriction)");
50 validParamList->
set<
RCP<const FactoryBase> >(
"Importer", Teuchos::null,
"Generating factory of the matrix Importer for rebalancing");
51 validParamList->
set<
RCP<const FactoryBase> >(
"SubImporters", Teuchos::null,
"Generating factory of the matrix sub-Importers for rebalancing");
54 return validParamList;
57 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
59 FactManager_.push_back(FactManager);
62 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
64 Input(coarseLevel,
"R");
66 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
67 for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
71 if (!UseSingleSourceImporters_) coarseLevel.
DeclareInput(
"Importer", (*it)->GetFactory(
"Importer").get(),
this);
72 coarseLevel.
DeclareInput(
"Nullspace", (*it)->GetFactory(
"Nullspace").get(),
this);
76 if (UseSingleSourceImporters_) {
77 Input(coarseLevel,
"SubImporters");
81 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
88 originalTransferOp = Get<RCP<Matrix> >(coarseLevel,
"R");
98 bool bRestrictComm =
false;
100 if (pL.
get<
bool>(
"repartition: use subcommunicators") ==
true)
101 bRestrictComm =
true;
110 bool bThyraRangeGIDs = rangeMapExtractor->getThyraMode();
111 bool bThyraDomainGIDs = domainMapExtractor->getThyraMode();
114 std::vector<GO> fullRangeMapVector;
115 std::vector<GO> fullDomainMapVector;
116 std::vector<RCP<const Map> > subBlockRRangeMaps;
117 std::vector<RCP<const Map> > subBlockRDomainMaps;
118 subBlockRRangeMaps.reserve(bOriginalTransferOp->Rows());
119 subBlockRDomainMaps.reserve(bOriginalTransferOp->Cols());
121 std::vector<Teuchos::RCP<Matrix> > subBlockRebR;
122 subBlockRebR.reserve(bOriginalTransferOp->Cols());
124 std::vector<RCP<const Import> > importers = std::vector<RCP<const Import> >(bOriginalTransferOp->Rows(), Teuchos::null);
125 if (UseSingleSourceImporters_) {
126 importers = Get<std::vector<RCP<const Import> > >(coarseLevel,
"SubImporters");
131 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
132 for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
137 if (UseSingleSourceImporters_)
138 rebalanceImporter = importers[curBlockId];
146 TEUCHOS_TEST_FOR_EXCEPTION(bThyraRangeGIDs ==
true && Rii->getRowMap()->getMinAllGlobalIndex() != 0,
148 "MueLu::RebalanceBlockRestrictionFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block range " << curBlockId <<
" start with " << Rii->getRowMap()->getMinAllGlobalIndex() <<
" but should start with 0");
149 TEUCHOS_TEST_FOR_EXCEPTION(bThyraDomainGIDs ==
true && Rii->getColMap()->getMinAllGlobalIndex() != 0,
151 "MueLu::RebalanceBlockRestrictionFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block domain " << curBlockId <<
" start with " << Rii->getColMap()->getMinAllGlobalIndex() <<
" but should start with 0");
154 if (rebalanceImporter != Teuchos::null) {
155 std::stringstream ss;
156 ss <<
"Rebalancing restriction block R(" << curBlockId <<
"," << curBlockId <<
")";
159 SubFactoryMonitor subM(*
this,
"Rebalancing restriction -- fusedImport", coarseLevel);
163 rebRii = MatrixFactory::Build(Rii, *rebalanceImporter, dummy, rebalanceImporter->getTargetMap());
167 params->
set(
"printLoadBalancingInfo",
true);
168 std::stringstream ss2;
169 ss2 <<
"R(" << curBlockId <<
"," << curBlockId <<
") rebalanced:";
174 params->
set(
"printLoadBalancingInfo",
true);
175 std::stringstream ss2;
176 ss2 <<
"R(" << curBlockId <<
"," << curBlockId <<
") not rebalanced:";
181 Teuchos::RCP<const StridedMap> orig_stridedRgMap = Teuchos::rcp_dynamic_cast<
const StridedMap>(rangeMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), rangeMapExtractor->getThyraMode()));
183 if (orig_stridedRgMap != Teuchos::null) {
184 std::vector<size_t> stridingData = orig_stridedRgMap->getStridingData();
186 stridedRgMap = StridedMapFactory::Build(
187 originalTransferOp->getRangeMap()->lib(),
190 rebRii->getRangeMap()->getIndexBase(),
192 originalTransferOp->getRangeMap()->getComm(),
193 orig_stridedRgMap->getStridedBlockId(),
194 orig_stridedRgMap->getOffset());
196 stridedRgMap = Rii->getRangeMap();
198 Teuchos::RCP<const StridedMap> orig_stridedDoMap = Teuchos::rcp_dynamic_cast<
const StridedMap>(domainMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), domainMapExtractor->getThyraMode()));
200 if (orig_stridedDoMap != Teuchos::null) {
201 std::vector<size_t> stridingData = orig_stridedDoMap->getStridingData();
203 stridedDoMap = StridedMapFactory::Build(
204 originalTransferOp->getDomainMap()->lib(),
207 rebRii->getDomainMap()->getIndexBase(),
209 originalTransferOp->getDomainMap()->getComm(),
210 orig_stridedDoMap->getStridedBlockId(),
211 orig_stridedDoMap->getOffset());
213 stridedDoMap = Rii->getDomainMap();
216 stridedRgMap->removeEmptyProcesses();
217 stridedDoMap->removeEmptyProcesses();
220 TEUCHOS_TEST_FOR_EXCEPTION(stridedRgMap == Teuchos::null,
Exceptions::RuntimeError,
"MueLu::RebalanceBlockRestrictionFactory::Build: failed to generate striding information. error.");
221 TEUCHOS_TEST_FOR_EXCEPTION(stridedDoMap == Teuchos::null,
Exceptions::RuntimeError,
"MueLu::RebalanceBlockRestrictionFactory::Build: failed to generate striding information. error.");
224 if (rebRii->IsView(
"stridedMaps")) rebRii->RemoveView(
"stridedMaps");
225 rebRii->CreateView(
"stridedMaps", stridedRgMap, stridedDoMap);
228 subBlockRebR.push_back(rebRii);
232 subBlockRRangeMaps.push_back(rangeMapii);
235 fullRangeMapVector.insert(fullRangeMapVector.end(), nodeRangeMapii.
begin(), nodeRangeMapii.
end());
236 if (bThyraRangeGIDs ==
false)
237 sort(fullRangeMapVector.begin(), fullRangeMapVector.end());
241 subBlockRDomainMaps.push_back(domainMapii);
244 fullDomainMapVector.insert(fullDomainMapVector.end(), nodeDomainMapii.
begin(), nodeDomainMapii.
end());
245 if (bThyraDomainGIDs ==
false)
246 sort(fullDomainMapVector.begin(), fullDomainMapVector.end());
253 if (rebalanceImporter != Teuchos::null) {
254 std::stringstream ss2;
255 ss2 <<
"Rebalancing nullspace block(" << curBlockId <<
"," << curBlockId <<
")";
259 RCP<MultiVector> permutedNullspace = MultiVectorFactory::Build(rebalanceImporter->getTargetMap(), nullspace->getNumVectors());
260 permutedNullspace->doImport(*nullspace, *rebalanceImporter,
Xpetra::INSERT);
264 permutedNullspace->replaceMap(permutedNullspace->getMap()->removeEmptyProcesses());
266 coarseLevel.Set<
RCP<MultiVector> >(
"Nullspace", permutedNullspace, (*it)->GetFactory(
"Nullspace").
get());
271 coarseLevel.Set<
RCP<MultiVector> >(
"Nullspace", nullspace, (*it)->GetFactory(
"Nullspace").
get());
280 GO rangeIndexBase = originalTransferOp->getRangeMap()->getIndexBase();
281 GO domainIndexBase = originalTransferOp->getDomainMap()->getIndexBase();
284 Teuchos::ArrayView<GO> fullRangeMapGIDs(fullRangeMapVector.size() ? &fullRangeMapVector[0] : 0, fullRangeMapVector.size());
287 if (stridedRgFullMap != Teuchos::null) {
288 std::vector<size_t> stridedData = stridedRgFullMap->getStridingData();
290 StridedMapFactory::Build(
291 originalTransferOp->getRangeMap()->lib(),
296 originalTransferOp->getRangeMap()->getComm(),
297 stridedRgFullMap->getStridedBlockId(),
298 stridedRgFullMap->getOffset());
302 originalTransferOp->getRangeMap()->lib(),
306 originalTransferOp->getRangeMap()->getComm());
309 Teuchos::ArrayView<GO> fullDomainMapGIDs(fullDomainMapVector.size() ? &fullDomainMapVector[0] : 0, fullDomainMapVector.size());
312 if (stridedDoFullMap != Teuchos::null) {
313 TEUCHOS_TEST_FOR_EXCEPTION(stridedDoFullMap == Teuchos::null,
Exceptions::BadCast,
"MueLu::BlockedPFactory::Build: full map in domain map extractor has no striding information! error.");
314 std::vector<size_t> stridedData2 = stridedDoFullMap->getStridingData();
316 StridedMapFactory::Build(
317 originalTransferOp->getDomainMap()->lib(),
322 originalTransferOp->getDomainMap()->getComm(),
323 stridedDoFullMap->getStridedBlockId(),
324 stridedDoFullMap->getOffset());
328 originalTransferOp->getDomainMap()->lib(),
332 originalTransferOp->getDomainMap()->getComm());
336 fullRangeMap->removeEmptyProcesses();
337 fullDomainMap->removeEmptyProcesses();
342 MapExtractorFactory::Build(fullRangeMap, subBlockRRangeMaps, bThyraRangeGIDs);
344 MapExtractorFactory::Build(fullDomainMap, subBlockRDomainMaps, bThyraDomainGIDs);
347 for (
size_t i = 0; i < subBlockRRangeMaps.size(); i++) {
349 bRebR->setMatrix(i, i, crsOpii);
352 bRebR->fillComplete();
354 Set(coarseLevel,
"R", Teuchos::rcp_dynamic_cast<Matrix>(bRebR));
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
Exception indicating invalid cast attempted.
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void sort(View &view, const size_t &size)
Print statistics that do not involve significant additional computation.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
#define SET_VALID_ENTRY(name)
Exception throws to report errors in the internal logical of the program.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
An exception safe way to call the method 'Level::SetFactoryManager()'.
void AddFactoryManager(RCP< const FactoryManagerBase > FactManager)
Add a factory manager.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()