MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_RebalanceBlockInterpolationFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_REBALANCEBLOCKINTERPOLATIONFACTORY_DEF_HPP_
47 #define MUELU_REBALANCEBLOCKINTERPOLATIONFACTORY_DEF_HPP_
48 
49 #include <Teuchos_Tuple.hpp>
50 
51 #include "Xpetra_MultiVector.hpp"
52 #include "Xpetra_MultiVectorFactory.hpp"
53 #include "Xpetra_Vector.hpp"
54 #include "Xpetra_VectorFactory.hpp"
55 #include <Xpetra_Matrix.hpp>
57 #include <Xpetra_MapFactory.hpp>
58 #include <Xpetra_MapExtractor.hpp>
60 #include <Xpetra_MatrixFactory.hpp>
61 #include <Xpetra_Import.hpp>
62 #include <Xpetra_ImportFactory.hpp>
63 
65 
67 #include "MueLu_HierarchyUtils.hpp"
68 #include "MueLu_Level.hpp"
69 #include "MueLu_Monitor.hpp"
70 #include "MueLu_PerfUtils.hpp"
71 
72 namespace MueLu {
73 
74 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
76  RCP<ParameterList> validParamList = rcp(new ParameterList());
77 
78  validParamList->set<RCP<const FactoryBase> >("P", Teuchos::null, "Factory of the prolongation operator that need to be rebalanced (only used if type=Interpolation)");
79  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.");
80 
81  validParamList->set<RCP<const FactoryBase> >("Coordinates", Teuchos::null, "Factory for generating the non-rebalanced Coordinates.");
82  validParamList->set<RCP<const FactoryBase> >("Importer", Teuchos::null, "Generating factory of the matrix Importer for rebalancing");
83  validParamList->set<RCP<const FactoryBase> >("SubImporters", Teuchos::null, "Generating factory of the matrix sub-Importers for rebalancing");
84 
85 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
86  // SET_VALID_ENTRY("repartition: use subcommunicators");
87 #undef SET_VALID_ENTRY
88 
89  // TODO validation: "P" parameter valid only for type="Interpolation" and "R" valid only for type="Restriction". Like so:
90  // if (paramList.isEntry("type") && paramList.get("type) == "Interpolation) {
91  // validParamList->set< RCP<const FactoryBase> >("P", Teuchos::null, "Factory of the prolongation operator that need to be rebalanced (only used if type=Interpolation)");
92 
93  return validParamList;
94 }
95 
96 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
98  FactManager_.push_back(FactManager);
99 }
100 
101 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103  Input(coarseLevel, "P");
104  Input(coarseLevel, "A"); // we request the non-rebalanced coarse level A since we have to make sure it is calculated before rebalancing starts!
105 
106  std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
107  for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
108  SetFactoryManager fineSFM(rcpFromRef(fineLevel), *it);
109  SetFactoryManager coarseSFM(rcpFromRef(coarseLevel), *it);
110 
111  // Request Importer and Coordinates (if defined in xml file)
112  // Note, that we have to use the Level::DeclareInput routine in order to use the FactoryManager *it (rather than the main factory manager)
113  coarseLevel.DeclareInput("Importer", (*it)->GetFactory("Importer").get(), this);
114  if ((*it)->hasFactory("Coordinates") == true)
115  coarseLevel.DeclareInput("Coordinates", (*it)->GetFactory("Coordinates").get(), this);
116  }
117 
118  // Use the non-manager path if the maps / importers are generated in one place
119  if (FactManager_.size() == 0) {
120  Input(coarseLevel, "Importer");
121  Input(coarseLevel, "SubImporters");
122  Input(coarseLevel, "Coordinates");
123  }
124 }
125 
126 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
128  FactoryMonitor m(*this, "Build", coarseLevel);
131 
132  bool UseSingleSource = FactManager_.size() == 0;
133  // RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
134 
135  Teuchos::RCP<Matrix> nonrebCoarseA = Get<RCP<Matrix> >(coarseLevel, "A");
136 
137  Teuchos::RCP<Matrix> originalTransferOp = Teuchos::null;
138  originalTransferOp = Get<RCP<Matrix> >(coarseLevel, "P");
139 
141  Teuchos::rcp_dynamic_cast<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(originalTransferOp);
142  TEUCHOS_TEST_FOR_EXCEPTION(bOriginalTransferOp == Teuchos::null, Exceptions::BadCast, "MueLu::RebalanceBlockTransferFactory::Build: input matrix P or R is not of type BlockedCrsMatrix! error.");
143 
144  RCP<const MapExtractor> rangeMapExtractor = bOriginalTransferOp->getRangeMapExtractor();
145  RCP<const MapExtractor> domainMapExtractor = bOriginalTransferOp->getDomainMapExtractor();
146 
147  // check if GIDs for full maps have to be sorted:
148  // For the Thyra mode ordering they do not have to be sorted since the GIDs are
149  // numbered as 0...n1,0...,n2 (starting with zero for each subblock). The MapExtractor
150  // generates unique GIDs during the construction.
151  // For Xpetra style, the GIDs have to be reordered. Such that one obtains a ordered
152  // list of GIDs in an increasing ordering. In Xpetra, the GIDs are all unique through
153  // out all submaps.
154  bool bThyraRangeGIDs = rangeMapExtractor->getThyraMode();
155  bool bThyraDomainGIDs = domainMapExtractor->getThyraMode();
156 
157  // declare variables for maps of blocked rebalanced prolongation operator
158  std::vector<GO> fullRangeMapVector; // contains all range GIDs on current processor
159  std::vector<GO> fullDomainMapVector; // contains all domain GIDs on current processor
160  std::vector<RCP<const Map> > subBlockPRangeMaps;
161  std::vector<RCP<const Map> > subBlockPDomainMaps;
162  subBlockPRangeMaps.reserve(bOriginalTransferOp->Rows()); // reserve size for block P operators
163  subBlockPDomainMaps.reserve(bOriginalTransferOp->Cols()); // reserve size for block P operators
164 
165  std::vector<Teuchos::RCP<Matrix> > subBlockRebP;
166  subBlockRebP.reserve(bOriginalTransferOp->Rows());
167 
168  // For use in single-source mode only
169  std::vector<RCP<const Import> > importers = std::vector<RCP<const Import> >(bOriginalTransferOp->Rows(), Teuchos::null);
170  std::vector<RCP<xdMV> > newCoordinates(bOriginalTransferOp->Rows());
171  RCP<xdBV> oldCoordinates;
172  if (UseSingleSource) {
173  importers = Get<std::vector<RCP<const Import> > >(coarseLevel, "SubImporters");
174  oldCoordinates = Get<RCP<xdBV> >(coarseLevel, "Coordinates");
175  }
176 
177  int curBlockId = 0;
178  Teuchos::RCP<const Import> rebalanceImporter = Teuchos::null;
179  std::vector<Teuchos::RCP<const FactoryManagerBase> >::const_iterator it;
180  for (it = FactManager_.begin(); it != FactManager_.end(); ++it) {
181  // begin SubFactoryManager environment
182  SetFactoryManager fineSFM(rcpFromRef(fineLevel), *it);
183  SetFactoryManager coarseSFM(rcpFromRef(coarseLevel), *it);
184 
185  // TAW: use the Level::Get routine in order to access the data declared in (*it) factory manager (rather than the main factory manager)
186  if (UseSingleSource)
187  rebalanceImporter = importers[curBlockId];
188  else
189  rebalanceImporter = coarseLevel.Get<Teuchos::RCP<const Import> >("Importer", (*it)->GetFactory("Importer").get());
190 
191  // extract diagonal matrix block
192  Teuchos::RCP<Matrix> Pii = bOriginalTransferOp->getMatrix(curBlockId, curBlockId);
193  Teuchos::RCP<CrsMatrixWrap> Pwii = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(Pii);
194  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!");
195 
196  MUELU_TEST_FOR_EXCEPTION(bThyraRangeGIDs == true && Pii->getRowMap()->getMinAllGlobalIndex() != 0,
198  "MueLu::RebalanceBlockInterpolationFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block range " << curBlockId << " start with " << Pii->getRowMap()->getMinAllGlobalIndex() << " but should start with 0");
199  MUELU_TEST_FOR_EXCEPTION(bThyraDomainGIDs == true && Pii->getColMap()->getMinAllGlobalIndex() != 0,
201  "MueLu::RebalanceBlockInterpolationFactory::Build: inconsistent Thyra GIDs. Thyra global ids for block domain " << curBlockId << " start with " << Pii->getColMap()->getMinAllGlobalIndex() << " but should start with 0");
202 
203  // rebalance P11
204  if (rebalanceImporter != Teuchos::null) {
205  std::stringstream ss;
206  ss << "Rebalancing prolongator block P(" << curBlockId << "," << curBlockId << ")";
207  SubFactoryMonitor m1(*this, ss.str(), coarseLevel);
208 
209  // P is the transfer operator from the coarse grid to the fine grid.
210  // P must transfer the data from the newly reordered coarse A to the (unchanged) fine A.
211  // This means that the domain map (coarse) of P must be changed according to the new partition. The range map (fine) is kept unchanged.
212  //
213  // The domain map of P must match the range map of R.
214  // See also note below about domain/range map of R and its implications for P.
215  //
216  // To change the domain map of P, P needs to be fillCompleted again with the new domain map.
217  // To achieve this, P is copied into a new matrix that is not fill-completed.
218  // The doImport() operation is just used here to make a copy of P: the importer is trivial and there is no data movement involved.
219  // The reordering actually happens during the fillComplete() with domainMap == rebalanceImporter->getTargetMap().
220  RCP<const Import> newImporter;
221  {
222  SubFactoryMonitor subM(*this, "Rebalancing prolongator -- fast map replacement", coarseLevel);
223  newImporter = ImportFactory::Build(rebalanceImporter->getTargetMap(), Pii->getColMap());
224  Pwii->getCrsMatrix()->replaceDomainMapAndImporter(rebalanceImporter->getTargetMap(), newImporter);
225  }
226 
227  RCP<ParameterList> params = rcp(new ParameterList());
228  params->set("printLoadBalancingInfo", true);
229  std::stringstream ss2;
230  ss2 << "P(" << curBlockId << "," << curBlockId << ") rebalanced:";
231  GetOStream(Statistics0) << PerfUtils::PrintMatrixInfo(*Pii, ss2.str(), params);
232 
233  // store rebalanced P block
234  subBlockRebP.push_back(Pii);
235 
236  // rebalance coordinates
237  // TAW: Note, that each sub-block manager overwrites the Coordinates. So far we only support one set of Coordinates
238  // for a multiphysics problem (i.e., we only support volume coupled problems with the same mesh)
239  if (UseSingleSource) {
240  RCP<xdMV> localCoords = oldCoordinates->getMultiVector(curBlockId);
241 
242  // FIXME: This should be extended to work with blocking
243  RCP<const Import> coordImporter = rebalanceImporter;
244 
245  RCP<xdMV> permutedLocalCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Build(coordImporter->getTargetMap(), localCoords->getNumVectors());
246  permutedLocalCoords->doImport(*localCoords, *coordImporter, Xpetra::INSERT);
247 
248  newCoordinates[curBlockId] = permutedLocalCoords;
249  } else if ((*it)->hasFactory("Coordinates") == true && coarseLevel.IsAvailable("Coordinates", (*it)->GetFactory("Coordinates").get()) == true) {
250  RCP<xdMV> coords = coarseLevel.Get<RCP<xdMV> >("Coordinates", (*it)->GetFactory("Coordinates").get());
251 
252  // This line must be after the Get call
253  SubFactoryMonitor subM(*this, "Rebalancing coordinates", coarseLevel);
254 
255  LO nodeNumElts = coords->getMap()->getLocalNumElements();
256 
257  // If a process has no matrix rows, then we can't calculate blocksize using the formula below.
258  LO myBlkSize = 0, blkSize = 0;
259 
260  if (nodeNumElts > 0) {
261  MUELU_TEST_FOR_EXCEPTION(rebalanceImporter->getSourceMap()->getLocalNumElements() % nodeNumElts != 0,
263  "MueLu::RebalanceBlockInterpolationFactory::Build: block size. " << rebalanceImporter->getSourceMap()->getLocalNumElements() << " not divisable by " << nodeNumElts);
264  myBlkSize = rebalanceImporter->getSourceMap()->getLocalNumElements() / nodeNumElts;
265  }
266 
267  MueLu_maxAll(coords->getMap()->getComm(), myBlkSize, blkSize);
268 
269  RCP<const Import> coordImporter = Teuchos::null;
270  if (blkSize == 1) {
271  coordImporter = rebalanceImporter;
272  } else {
273  // NOTE: there is an implicit assumption here: we assume that dof any node are enumerated consequently
274  // Proper fix would require using decomposition similar to how we construct importer in the
275  // RepartitionFactory
276  RCP<const Map> origMap = coords->getMap();
277  GO indexBase = origMap->getIndexBase();
278 
279  ArrayView<const GO> OEntries = rebalanceImporter->getTargetMap()->getLocalElementList();
280  LO numEntries = OEntries.size() / blkSize;
281  ArrayRCP<GO> Entries(numEntries);
282  for (LO i = 0; i < numEntries; i++)
283  Entries[i] = (OEntries[i * blkSize] - indexBase) / blkSize + indexBase;
284 
285  RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm());
286  coordImporter = ImportFactory::Build(origMap, targetMap);
287  }
288 
289  RCP<xdMV> permutedCoords = Xpetra::MultiVectorFactory<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LO, GO, NO>::Build(coordImporter->getTargetMap(), coords->getNumVectors());
290  permutedCoords->doImport(*coords, *coordImporter, Xpetra::INSERT);
291 
292  const ParameterList &pL = GetParameterList();
293  if (pL.isParameter("repartition: use subcommunicators") == true && pL.get<bool>("repartition: use subcommunicators") == true)
294  permutedCoords->replaceMap(permutedCoords->getMap()->removeEmptyProcesses());
295 
296  Set(coarseLevel, "Coordinates", permutedCoords);
297  }
298  } // end rebalance P(1,1)
299  else {
300  RCP<ParameterList> params = rcp(new ParameterList());
301  params->set("printLoadBalancingInfo", true);
302  std::stringstream ss;
303  ss << "P(" << curBlockId << "," << curBlockId << ") not rebalanced:";
304  GetOStream(Statistics0) << PerfUtils::PrintMatrixInfo(*Pii, ss.str(), params);
305  // store rebalanced P block
306  subBlockRebP.push_back(Pii);
307 
308  // Store Coordinates on coarse level (generated by this)
309  // TAW: Note, that each sub-block manager overwrites the Coordinates. So far we only support one set of Coordinates
310  // for a multiphysics problem (i.e., we only support volume coupled problems with the same mesh)
311  if ((*it)->hasFactory("Coordinates") == true && coarseLevel.IsAvailable("Coordinates", (*it)->GetFactory("Coordinates").get()) == true) {
312  coarseLevel.Set("Coordinates", coarseLevel.Get<RCP<xdMV> >("Coordinates", (*it)->GetFactory("Coordinates").get()), this);
313  }
314  }
315 
316  // fix striding information for rebalanced diagonal block Pii
317  // RCP<const Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> > rgPMapExtractor = bOriginalTransferOp->getRangeMapExtractor(); // original map extractor
318  Teuchos::RCP<const StridedMap> orig_stridedRgMap = Teuchos::rcp_dynamic_cast<const StridedMap>(rangeMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), rangeMapExtractor->getThyraMode()));
319  Teuchos::RCP<const Map> stridedRgMap = Teuchos::null;
320  if (orig_stridedRgMap != Teuchos::null) {
321  std::vector<size_t> stridingData = orig_stridedRgMap->getStridingData();
322  Teuchos::ArrayView<const GlobalOrdinal> nodeRangeMapii = Pii->getRangeMap()->getLocalElementList();
323  stridedRgMap = StridedMapFactory::Build(
324  originalTransferOp->getRangeMap()->lib(),
326  nodeRangeMapii,
327  Pii->getRangeMap()->getIndexBase(),
328  stridingData,
329  originalTransferOp->getRangeMap()->getComm(),
330  orig_stridedRgMap->getStridedBlockId(),
331  orig_stridedRgMap->getOffset());
332  } else
333  stridedRgMap = Pii->getRangeMap();
334 
335  // RCP<const Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> > doPMapExtractor = bOriginalTransferOp->getDomainMapExtractor(); // original map extractor
336  Teuchos::RCP<const StridedMap> orig_stridedDoMap = Teuchos::rcp_dynamic_cast<const StridedMap>(domainMapExtractor->getMap(Teuchos::as<size_t>(curBlockId), domainMapExtractor->getThyraMode()));
337 
338  Teuchos::RCP<const Map> stridedDoMap = Teuchos::null;
339  if (orig_stridedDoMap != Teuchos::null) {
340  std::vector<size_t> stridingData = orig_stridedDoMap->getStridingData();
341  Teuchos::ArrayView<const GlobalOrdinal> nodeDomainMapii = Pii->getDomainMap()->getLocalElementList();
342  stridedDoMap = StridedMapFactory::Build(originalTransferOp->getDomainMap()->lib(),
344  nodeDomainMapii,
345  Pii->getDomainMap()->getIndexBase(),
346  stridingData,
347  originalTransferOp->getDomainMap()->getComm(),
348  orig_stridedDoMap->getStridedBlockId(),
349  orig_stridedDoMap->getOffset());
350  } else
351  stridedDoMap = Pii->getDomainMap();
352 
353  TEUCHOS_TEST_FOR_EXCEPTION(stridedRgMap == Teuchos::null, Exceptions::RuntimeError, "MueLu::RebalanceBlockInterpolationFactory::Build: failed to generate striding information. error.");
354  TEUCHOS_TEST_FOR_EXCEPTION(stridedDoMap == Teuchos::null, Exceptions::RuntimeError, "MueLu::RebalanceBlockInterpolationFactory::Build: failed to generate striding information. error.");
355 
356  // replace stridedMaps view in diagonal sub block
357  if (Pii->IsView("stridedMaps")) Pii->RemoveView("stridedMaps");
358  Pii->CreateView("stridedMaps", stridedRgMap, stridedDoMap);
359 
360  // append strided row map (= range map) to list of range maps.
361  Teuchos::RCP<const Map> rangeMapii = Pii->getRowMap("stridedMaps"); // strided range map
362  subBlockPRangeMaps.push_back(rangeMapii);
363  Teuchos::ArrayView<const GlobalOrdinal> nodeRangeMapii = Pii->getRangeMap()->getLocalElementList();
364  // append the GIDs in the end. Do not sort if we have Thyra style GIDs
365  fullRangeMapVector.insert(fullRangeMapVector.end(), nodeRangeMapii.begin(), nodeRangeMapii.end());
366  if (bThyraRangeGIDs == false)
367  sort(fullRangeMapVector.begin(), fullRangeMapVector.end());
368 
369  // append strided col map (= domain map) to list of range maps.
370  Teuchos::RCP<const Map> domainMapii = Pii->getColMap("stridedMaps"); // strided domain map
371  subBlockPDomainMaps.push_back(domainMapii);
372  Teuchos::ArrayView<const GlobalOrdinal> nodeDomainMapii = Pii->getDomainMap()->getLocalElementList();
373  // append the GIDs in the end. Do not sort if we have Thyra style GIDs
374  fullDomainMapVector.insert(fullDomainMapVector.end(), nodeDomainMapii.begin(), nodeDomainMapii.end());
375  if (bThyraDomainGIDs == false)
376  sort(fullDomainMapVector.begin(), fullDomainMapVector.end());
377 
378  curBlockId++; // increase block id index
379 
380  } // end SubFactoryManager environment
381 
382  // extract map index base from maps of blocked P
383  GO rangeIndexBase = originalTransferOp->getRangeMap()->getIndexBase();
384  GO domainIndexBase = originalTransferOp->getDomainMap()->getIndexBase();
385 
386  RCP<const Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> > rangePMapExtractor = bOriginalTransferOp->getRangeMapExtractor(); // original map extractor
387  Teuchos::ArrayView<GO> fullRangeMapGIDs(fullRangeMapVector.size() ? &fullRangeMapVector[0] : 0, fullRangeMapVector.size());
388  Teuchos::RCP<const StridedMap> stridedRgFullMap = Teuchos::rcp_dynamic_cast<const StridedMap>(rangePMapExtractor->getFullMap());
389  Teuchos::RCP<const Map> fullRangeMap = Teuchos::null;
390  if (stridedRgFullMap != Teuchos::null) {
391  std::vector<size_t> stridedData = stridedRgFullMap->getStridingData();
392  fullRangeMap =
393  StridedMapFactory::Build(
394  originalTransferOp->getRangeMap()->lib(),
396  fullRangeMapGIDs,
397  rangeIndexBase,
398  stridedData,
399  originalTransferOp->getRangeMap()->getComm(),
400  stridedRgFullMap->getStridedBlockId(),
401  stridedRgFullMap->getOffset());
402  } else {
403  fullRangeMap =
404  MapFactory::Build(
405  originalTransferOp->getRangeMap()->lib(),
407  fullRangeMapGIDs,
408  rangeIndexBase,
409  originalTransferOp->getRangeMap()->getComm());
410  }
411 
412  RCP<const Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> > domainAMapExtractor = bOriginalTransferOp->getDomainMapExtractor();
413  Teuchos::ArrayView<GO> fullDomainMapGIDs(fullDomainMapVector.size() ? &fullDomainMapVector[0] : 0, fullDomainMapVector.size());
414  Teuchos::RCP<const StridedMap> stridedDoFullMap = Teuchos::rcp_dynamic_cast<const StridedMap>(domainAMapExtractor->getFullMap());
415  Teuchos::RCP<const Map> fullDomainMap = Teuchos::null;
416  if (stridedDoFullMap != Teuchos::null) {
417  TEUCHOS_TEST_FOR_EXCEPTION(stridedDoFullMap == Teuchos::null, Exceptions::BadCast, "MueLu::BlockedPFactory::Build: full map in domain map extractor has no striding information! error.");
418  std::vector<size_t> stridedData2 = stridedDoFullMap->getStridingData();
419  fullDomainMap =
420  StridedMapFactory::Build(
421  originalTransferOp->getDomainMap()->lib(),
423  fullDomainMapGIDs,
424  domainIndexBase,
425  stridedData2,
426  originalTransferOp->getDomainMap()->getComm(),
427  stridedDoFullMap->getStridedBlockId(),
428  stridedDoFullMap->getOffset());
429  } else {
430  fullDomainMap =
431  MapFactory::Build(
432  originalTransferOp->getDomainMap()->lib(),
434  fullDomainMapGIDs,
435  domainIndexBase,
436  originalTransferOp->getDomainMap()->getComm());
437  }
438 
439  // build map extractors
440  Teuchos::RCP<const MapExtractor> rebrangeMapExtractor =
441  MapExtractorFactory::Build(fullRangeMap, subBlockPRangeMaps, bThyraRangeGIDs);
442  Teuchos::RCP<const MapExtractor> rebdomainMapExtractor =
443  MapExtractorFactory::Build(fullDomainMap, subBlockPDomainMaps, bThyraDomainGIDs);
444 
445  Teuchos::RCP<BlockedCrsMatrix> bRebP = Teuchos::rcp(new BlockedCrsMatrix(rebrangeMapExtractor, rebdomainMapExtractor, 10));
446  for (size_t i = 0; i < subBlockPRangeMaps.size(); i++) {
447  Teuchos::RCP<CrsMatrixWrap> crsOpii = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(subBlockRebP[i]);
448  TEUCHOS_TEST_FOR_EXCEPTION(crsOpii == Teuchos::null, Xpetra::Exceptions::BadCast, "MueLu::RebalanceBlockTransferFactory::Build: block P" << i << " is not of type CrsMatrixWrap.");
449  bRebP->setMatrix(i, i, crsOpii);
450  }
451  bRebP->fillComplete();
452 
453  Set(coarseLevel, "P", Teuchos::rcp_dynamic_cast<Matrix>(bRebP));
454 
455  // Finish up the coordinates (single source only)
456  if (UseSingleSource) {
457  RCP<xdBV> bcoarseCoords = rcp(new xdBV(rebrangeMapExtractor->getBlockedMap(), newCoordinates));
458  Set(coarseLevel, "Coordinates", bcoarseCoords);
459  }
460 
461 } // Build
462 
463 } // namespace MueLu
464 
465 #endif /* MUELU_REBALANCEBLOCKINTERPOLATIONFACTORY_DEF_HPP_ */
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.
iterator begin() const
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
LocalOrdinal LO
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.
Definition: MueLu_Level.hpp:99
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.
iterator end() const
Node NO
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 &#39;Level::SetFactoryManager()&#39;.
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()