MueLu  Version of the Day
MueLu_CloneRepartitionInterface_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 PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_
47 #define PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_
48 
50 
51 #include "MueLu_Level.hpp"
52 #include "MueLu_Exceptions.hpp"
53 #include "MueLu_Monitor.hpp"
54 #include "MueLu_Utilities.hpp"
55 
56 
57 namespace MueLu {
58 
59  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
61  Teuchos::RCP<ParameterList> validParamList = rcp(new ParameterList());
62  validParamList->set< Teuchos::RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
63  validParamList->set< Teuchos::RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
64  validParamList->set< Teuchos::RCP<const FactoryBase> >("Partition", Teuchos::null, "Factory generating the Partition array (e.g. ZoltanInterface)");
65 
66  return validParamList;
67  }
68 
69 
70  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
72  Input(currentLevel, "A");
73  Input(currentLevel, "Partition");
74  } //DeclareInput()
75 
76  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
78  FactoryMonitor m(*this, "Build", currentLevel);
79  currentLevel.print(GetOStream(Statistics0,0));
80  // extract blocked operator A from current level
81  Teuchos::RCP<Matrix> A = Get< Teuchos::RCP<Matrix> > (currentLevel, "A");
82  Teuchos::RCP<const Teuchos::Comm< int > > comm = A->getRowMap()->getComm();
83 
84  // number of Partitions only used for a shortcut.
85  GO numPartitions = 0;
86  if (currentLevel.IsAvailable("number of partitions")) {
87  numPartitions = currentLevel.Get<GO>("number of partitions");
88  GetOStream(Warnings0) << "Using user-provided \"number of partitions\", the performance is unknown" << std::endl;
89 
90  }
91 
92  // ======================================================================================================
93  // Construct decomposition vector
94  // ======================================================================================================
95  RCP<GOVector> decomposition = Teuchos::null;
96 
97  // extract decomposition vector
98  decomposition = Get<RCP<GOVector> >(currentLevel, "Partition");
99  ArrayRCP<const GO> decompEntries = decomposition->getData(0);
100 
101  if (decomposition.is_null()) {
102  GetOStream(Warnings0) << "No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
103  Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
104  return;
105  }
106 
107  // create new decomposition vector
109  ArrayRCP<GO> retDecompEntries = ret->getDataNonConst(0);
110 
111  // block size of output vector
112  LocalOrdinal blkSize = 1;
113 
114  // check for blocking/striding information
115  if(A->IsView("stridedMaps") &&
116  Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
117  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
118  RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
119  TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null,Exceptions::BadCast,"MueLu::CloneRepartitionInterface::Build: cast to strided row map failed.");
120  LocalOrdinal stridedBlock = strMap->getStridedBlockId();
121  if (stridedBlock == -1)
122  blkSize = strMap->getFixedBlockSize();
123  else {
124  std::vector<size_t> strInfo = strMap->getStridingData();
125  blkSize = strInfo[stridedBlock];
126  }
127  oldView = A->SwitchToView(oldView);
128  GetOStream(Statistics1) << "CloneRepartitionInterface::Build():" << " found blockdim=" << blkSize << " from strided maps."<< std::endl;
129  } else {
130  GetOStream(Statistics1) << "CloneRepartitionInterface::Build(): no striding information available. Use blockdim=" << blkSize << " (DofsPerNode)." << std::endl;
131  blkSize = A->GetFixedBlockSize();
132  }
133 
134  // plausibility check!
135  size_t inLocalLength = decomposition->getLocalLength();
136  size_t outLocalLength = A->getRowMap()->getNodeNumElements();
137 
138  // only for non-strided maps
139  size_t numLocalNodes = outLocalLength / blkSize;
140  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(outLocalLength % blkSize) != 0, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: inconsistent number of local DOFs (" << outLocalLength << ") and degrees of freedoms (" << blkSize <<")");
141 
142  if (numLocalNodes > 0) {
143  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(inLocalLength % numLocalNodes) != 0, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: inconsistent number of local DOFs (" << inLocalLength << ") and number of local nodes (" << numLocalNodes << ")");
144  LocalOrdinal inBlkSize = Teuchos::as<LocalOrdinal>(inLocalLength / numLocalNodes);
145  //TEUCHOS_TEST_FOR_EXCEPTION(blkSize != inBlkSize, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: input block size = " << inBlkSize << " outpub block size = " << blkSize << ". They should be the same.");
146 
147  for(LO i = 0; i<Teuchos::as<LO>(numLocalNodes); i++) {
148  for(LO j = 0; j < blkSize; j++) {
149  retDecompEntries[i*blkSize + j] = Teuchos::as<GO>(decompEntries[i*inBlkSize]);
150  }
151  }
152  } // end if numLocalNodes > 0
153  Set(currentLevel, "Partition", ret);
154  } //Build()
155 
156 
157 
158 } //namespace MueLu
159 
160 #endif /* PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_ */
Important warning messages (one line)
Exception indicating invalid cast attempted.
GlobalOrdinal GO
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)
Print more statistics.
LocalOrdinal LO
Namespace for MueLu classes and methods.
bool is_null() 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
void print(std::ostream &out, const VerbLevel verbLevel=Default) const
Printing method.
std::string viewLabel_t
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
Exception throws to report errors in the internal logical of the program.
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.
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.