10 #ifndef MUELU_REBALANCEBLOCKINTERPOLATIONFACTORY_DEF_HPP_
11 #define MUELU_REBALANCEBLOCKINTERPOLATIONFACTORY_DEF_HPP_
13 #include <Teuchos_Tuple.hpp>
16 #include "Xpetra_MultiVectorFactory.hpp"
21 #include <Xpetra_MapFactory.hpp>
22 #include <Xpetra_MapExtractor.hpp>
31 #include "MueLu_HierarchyUtils.hpp"
34 #include "MueLu_PerfUtils.hpp"
38 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
42 validParamList->
set<
RCP<const FactoryBase> >(
"P", Teuchos::null,
"Factory of the prolongation operator that need to be rebalanced (only used if type=Interpolation)");
43 validParamList->
set<
RCP<const FactoryBase> >(
"A", Teuchos::null,
"Factory for generating the non-rebalanced coarse level A. We need this to make sure the non-rebalanced coarse A is calculated first before rebalancing takes place.");
45 validParamList->
set<
RCP<const FactoryBase> >(
"Coordinates", Teuchos::null,
"Factory for generating the non-rebalanced Coordinates.");
46 validParamList->
set<
RCP<const FactoryBase> >(
"Importer", Teuchos::null,
"Generating factory of the matrix Importer for rebalancing");
47 validParamList->
set<
RCP<const FactoryBase> >(
"SubImporters", Teuchos::null,
"Generating factory of the matrix sub-Importers for rebalancing");
49 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
51 #undef SET_VALID_ENTRY
57 return validParamList;
60 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
62 FactManager_.push_back(FactManager);
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 Input(coarseLevel,
"P");
68 Input(coarseLevel,
"A");
70 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
71 for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
77 coarseLevel.
DeclareInput(
"Importer", (*it)->GetFactory(
"Importer").get(),
this);
78 if ((*it)->hasFactory(
"Coordinates") ==
true)
79 coarseLevel.
DeclareInput(
"Coordinates", (*it)->GetFactory(
"Coordinates").get(),
this);
83 if (FactManager_.size() == 0) {
84 Input(coarseLevel,
"Importer");
85 Input(coarseLevel,
"SubImporters");
86 Input(coarseLevel,
"Coordinates");
90 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
96 bool UseSingleSource = FactManager_.size() == 0;
102 originalTransferOp = Get<RCP<Matrix> >(coarseLevel,
"P");
118 bool bThyraRangeGIDs = rangeMapExtractor->getThyraMode();
119 bool bThyraDomainGIDs = domainMapExtractor->getThyraMode();
122 std::vector<GO> fullRangeMapVector;
123 std::vector<GO> fullDomainMapVector;
124 std::vector<RCP<const Map> > subBlockPRangeMaps;
125 std::vector<RCP<const Map> > subBlockPDomainMaps;
126 subBlockPRangeMaps.reserve(bOriginalTransferOp->Rows());
127 subBlockPDomainMaps.reserve(bOriginalTransferOp->Cols());
129 std::vector<Teuchos::RCP<Matrix> > subBlockRebP;
130 subBlockRebP.reserve(bOriginalTransferOp->Rows());
133 std::vector<RCP<const Import> > importers = std::vector<RCP<const Import> >(bOriginalTransferOp->Rows(), Teuchos::null);
134 std::vector<RCP<xdMV> > newCoordinates(bOriginalTransferOp->Rows());
136 if (UseSingleSource) {
137 importers = Get<std::vector<RCP<const Import> > >(coarseLevel,
"SubImporters");
138 oldCoordinates = Get<RCP<xdBV> >(coarseLevel,
"Coordinates");
143 std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
144 for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
151 rebalanceImporter = importers[curBlockId];
158 TEUCHOS_TEST_FOR_EXCEPTION(Pwii == Teuchos::null,
Xpetra::Exceptions::BadCast,
"MueLu::RebalanceBlockTransferFactory::Build: block " << curBlockId <<
" is not of type CrsMatrixWrap. We need an underlying CsrMatrix to replace domain map and importer!");
162 "MueLu::RebalanceBlockInterpolationFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block range " << curBlockId <<
" start with " << Pii->getRowMap()->getMinAllGlobalIndex() <<
" but should start with 0");
165 "MueLu::RebalanceBlockInterpolationFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block domain " << curBlockId <<
" start with " << Pii->getColMap()->getMinAllGlobalIndex() <<
" but should start with 0");
168 if (rebalanceImporter != Teuchos::null) {
169 std::stringstream ss;
170 ss <<
"Rebalancing prolongator block P(" << curBlockId <<
"," << curBlockId <<
")";
186 SubFactoryMonitor subM(*
this,
"Rebalancing prolongator -- fast map replacement", coarseLevel);
187 newImporter = ImportFactory::Build(rebalanceImporter->getTargetMap(), Pii->getColMap());
188 Pwii->getCrsMatrix()->replaceDomainMapAndImporter(rebalanceImporter->getTargetMap(), newImporter);
192 params->
set(
"printLoadBalancingInfo",
true);
193 std::stringstream ss2;
194 ss2 <<
"P(" << curBlockId <<
"," << curBlockId <<
") rebalanced:";
198 subBlockRebP.push_back(Pii);
203 if (UseSingleSource) {
204 RCP<xdMV> localCoords = oldCoordinates->getMultiVector(curBlockId);
210 permutedLocalCoords->doImport(*localCoords, *coordImporter,
Xpetra::INSERT);
212 newCoordinates[curBlockId] = permutedLocalCoords;
213 }
else if ((*it)->hasFactory(
"Coordinates") ==
true && coarseLevel.IsAvailable(
"Coordinates", (*it)->GetFactory(
"Coordinates").get()) ==
true) {
214 RCP<xdMV> coords = coarseLevel.Get<
RCP<xdMV> >(
"Coordinates", (*it)->GetFactory(
"Coordinates").get());
219 LO nodeNumElts = coords->getMap()->getLocalNumElements();
222 LO myBlkSize = 0, blkSize = 0;
224 if (nodeNumElts > 0) {
227 "MueLu::RebalanceBlockInterpolationFactory::Build: block size. " << rebalanceImporter->getSourceMap()->getLocalNumElements() <<
" not divisable by " << nodeNumElts);
228 myBlkSize = rebalanceImporter->getSourceMap()->getLocalNumElements() / nodeNumElts;
231 MueLu_maxAll(coords->getMap()->getComm(), myBlkSize, blkSize);
235 coordImporter = rebalanceImporter;
241 GO indexBase = origMap->getIndexBase();
244 LO numEntries = OEntries.
size() / blkSize;
246 for (LO i = 0; i < numEntries; i++)
247 Entries[i] = (OEntries[i * blkSize] - indexBase) / blkSize + indexBase;
249 RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm());
250 coordImporter = ImportFactory::Build(origMap, targetMap);
254 permutedCoords->doImport(*coords, *coordImporter,
Xpetra::INSERT);
257 if (pL.
isParameter(
"repartition: use subcommunicators") ==
true && pL.
get<
bool>(
"repartition: use subcommunicators") ==
true)
258 permutedCoords->replaceMap(permutedCoords->getMap()->removeEmptyProcesses());
260 Set(coarseLevel,
"Coordinates", permutedCoords);
265 params->
set(
"printLoadBalancingInfo",
true);
266 std::stringstream ss;
267 ss <<
"P(" << curBlockId <<
"," << curBlockId <<
") not rebalanced:";
270 subBlockRebP.push_back(Pii);
275 if ((*it)->hasFactory(
"Coordinates") ==
true && coarseLevel.IsAvailable(
"Coordinates", (*it)->GetFactory(
"Coordinates").get()) ==
true) {
276 coarseLevel.Set(
"Coordinates", coarseLevel.Get<
RCP<xdMV> >(
"Coordinates", (*it)->GetFactory(
"Coordinates").get()),
this);
282 Teuchos::RCP<const StridedMap> orig_stridedRgMap = Teuchos::rcp_dynamic_cast<
const StridedMap>(rangeMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), rangeMapExtractor->getThyraMode()));
284 if (orig_stridedRgMap != Teuchos::null) {
285 std::vector<size_t> stridingData = orig_stridedRgMap->getStridingData();
287 stridedRgMap = StridedMapFactory::Build(
288 originalTransferOp->getRangeMap()->lib(),
291 Pii->getRangeMap()->getIndexBase(),
293 originalTransferOp->getRangeMap()->getComm(),
294 orig_stridedRgMap->getStridedBlockId(),
295 orig_stridedRgMap->getOffset());
297 stridedRgMap = Pii->getRangeMap();
300 Teuchos::RCP<const StridedMap> orig_stridedDoMap = Teuchos::rcp_dynamic_cast<
const StridedMap>(domainMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), domainMapExtractor->getThyraMode()));
303 if (orig_stridedDoMap != Teuchos::null) {
304 std::vector<size_t> stridingData = orig_stridedDoMap->getStridingData();
306 stridedDoMap = StridedMapFactory::Build(originalTransferOp->getDomainMap()->lib(),
309 Pii->getDomainMap()->getIndexBase(),
311 originalTransferOp->getDomainMap()->getComm(),
312 orig_stridedDoMap->getStridedBlockId(),
313 orig_stridedDoMap->getOffset());
315 stridedDoMap = Pii->getDomainMap();
317 TEUCHOS_TEST_FOR_EXCEPTION(stridedRgMap == Teuchos::null,
Exceptions::RuntimeError,
"MueLu::RebalanceBlockInterpolationFactory::Build: failed to generate striding information. error.");
318 TEUCHOS_TEST_FOR_EXCEPTION(stridedDoMap == Teuchos::null,
Exceptions::RuntimeError,
"MueLu::RebalanceBlockInterpolationFactory::Build: failed to generate striding information. error.");
321 if (Pii->IsView(
"stridedMaps")) Pii->RemoveView(
"stridedMaps");
322 Pii->CreateView(
"stridedMaps", stridedRgMap, stridedDoMap);
326 subBlockPRangeMaps.push_back(rangeMapii);
329 fullRangeMapVector.insert(fullRangeMapVector.end(), nodeRangeMapii.
begin(), nodeRangeMapii.
end());
330 if (bThyraRangeGIDs ==
false)
331 sort(fullRangeMapVector.begin(), fullRangeMapVector.end());
335 subBlockPDomainMaps.push_back(domainMapii);
338 fullDomainMapVector.insert(fullDomainMapVector.end(), nodeDomainMapii.
begin(), nodeDomainMapii.
end());
339 if (bThyraDomainGIDs ==
false)
340 sort(fullDomainMapVector.begin(), fullDomainMapVector.end());
347 GO rangeIndexBase = originalTransferOp->getRangeMap()->getIndexBase();
348 GO domainIndexBase = originalTransferOp->getDomainMap()->getIndexBase();
351 Teuchos::ArrayView<GO> fullRangeMapGIDs(fullRangeMapVector.size() ? &fullRangeMapVector[0] : 0, fullRangeMapVector.size());
354 if (stridedRgFullMap != Teuchos::null) {
355 std::vector<size_t> stridedData = stridedRgFullMap->getStridingData();
357 StridedMapFactory::Build(
358 originalTransferOp->getRangeMap()->lib(),
363 originalTransferOp->getRangeMap()->getComm(),
364 stridedRgFullMap->getStridedBlockId(),
365 stridedRgFullMap->getOffset());
369 originalTransferOp->getRangeMap()->lib(),
373 originalTransferOp->getRangeMap()->getComm());
377 Teuchos::ArrayView<GO> fullDomainMapGIDs(fullDomainMapVector.size() ? &fullDomainMapVector[0] : 0, fullDomainMapVector.size());
380 if (stridedDoFullMap != Teuchos::null) {
381 TEUCHOS_TEST_FOR_EXCEPTION(stridedDoFullMap == Teuchos::null,
Exceptions::BadCast,
"MueLu::BlockedPFactory::Build: full map in domain map extractor has no striding information! error.");
382 std::vector<size_t> stridedData2 = stridedDoFullMap->getStridingData();
384 StridedMapFactory::Build(
385 originalTransferOp->getDomainMap()->lib(),
390 originalTransferOp->getDomainMap()->getComm(),
391 stridedDoFullMap->getStridedBlockId(),
392 stridedDoFullMap->getOffset());
396 originalTransferOp->getDomainMap()->lib(),
400 originalTransferOp->getDomainMap()->getComm());
405 MapExtractorFactory::Build(fullRangeMap, subBlockPRangeMaps, bThyraRangeGIDs);
407 MapExtractorFactory::Build(fullDomainMap, subBlockPDomainMaps, bThyraDomainGIDs);
410 for (
size_t i = 0; i < subBlockPRangeMaps.size(); i++) {
412 TEUCHOS_TEST_FOR_EXCEPTION(crsOpii == Teuchos::null,
Xpetra::Exceptions::BadCast,
"MueLu::RebalanceBlockTransferFactory::Build: block P" << i <<
" is not of type CrsMatrixWrap.");
413 bRebP->setMatrix(i, i, crsOpii);
415 bRebP->fillComplete();
417 Set(coarseLevel,
"P", Teuchos::rcp_dynamic_cast<Matrix>(bRebP));
420 if (UseSingleSource) {
421 RCP<xdBV> bcoarseCoords =
rcp(
new xdBV(rebrangeMapExtractor->getBlockedMap(), newCoordinates));
422 Set(coarseLevel,
"Coordinates", bcoarseCoords);
Exception indicating invalid cast attempted.
#define MueLu_maxAll(rcpComm, in, out)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
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)
bool isParameter(const std::string &name) const
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)
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
Exception throws to report errors in the internal logical of the program.
#define MUELU_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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()