MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
49 #include <Xpetra_MultiVectorFactory.hpp>
50 #include <Xpetra_VectorFactory.hpp>
51 
53 
54 #include "MueLu_Level.hpp"
55 #include "MueLu_Exceptions.hpp"
56 #include "MueLu_Monitor.hpp"
57 
58 namespace MueLu {
59 
60 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62  Teuchos::RCP<ParameterList> validParamList = rcp(new ParameterList());
63  validParamList->set<Teuchos::RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
64  validParamList->set<Teuchos::RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
65  validParamList->set<Teuchos::RCP<const FactoryBase> >("Partition", Teuchos::null, "Factory generating the Partition array (e.g. ZoltanInterface)");
66 
67  return validParamList;
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  if (currentLevel.IsAvailable("number of partitions")) {
86  GetOStream(Warnings0) << "Using user-provided \"number of partitions\", the performance is unknown" << std::endl;
87  }
88 
89  // ======================================================================================================
90  // Construct decomposition vector
91  // ======================================================================================================
92  RCP<GOVector> decomposition = Teuchos::null;
93 
94  // extract decomposition vector
95  decomposition = Get<RCP<GOVector> >(currentLevel, "Partition");
96  ArrayRCP<const GO> decompEntries = decomposition->getData(0);
97 
98  if (decomposition.is_null()) {
99  GetOStream(Warnings0) << "No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
100  Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
101  return;
102  }
103 
104  // create new decomposition vector
106  ArrayRCP<GO> retDecompEntries = ret->getDataNonConst(0);
107 
108  // block size of output vector
109  LocalOrdinal blkSize = 1;
110 
111  // check for blocking/striding information
112  if (A->IsView("stridedMaps") &&
113  Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
114  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
115  RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
116  TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null, Exceptions::BadCast, "MueLu::CloneRepartitionInterface::Build: cast to strided row map failed.");
117  LocalOrdinal stridedBlock = strMap->getStridedBlockId();
118  if (stridedBlock == -1)
119  blkSize = strMap->getFixedBlockSize();
120  else {
121  std::vector<size_t> strInfo = strMap->getStridingData();
122  blkSize = strInfo[stridedBlock];
123  }
124  oldView = A->SwitchToView(oldView);
125  GetOStream(Statistics1) << "CloneRepartitionInterface::Build():"
126  << " found blockdim=" << blkSize << " from strided maps." << std::endl;
127  } else {
128  GetOStream(Statistics1) << "CloneRepartitionInterface::Build(): no striding information available. Use blockdim=" << blkSize << " (DofsPerNode)." << std::endl;
129  blkSize = A->GetFixedBlockSize();
130  }
131 
132  // plausibility check!
133  size_t inLocalLength = decomposition->getLocalLength();
134  size_t outLocalLength = A->getRowMap()->getLocalNumElements();
135 
136  // only for non-strided maps
137  size_t numLocalNodes = outLocalLength / blkSize;
138  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 << ")");
139 
140  if (numLocalNodes > 0) {
141  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 << ")");
142  LocalOrdinal inBlkSize = Teuchos::as<LocalOrdinal>(inLocalLength / numLocalNodes);
143  // TEUCHOS_TEST_FOR_EXCEPTION(blkSize != inBlkSize, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: input block size = " << inBlkSize << " outpub block size = " << blkSize << ". They should be the same.");
144 
145  for (LO i = 0; i < Teuchos::as<LO>(numLocalNodes); i++) {
146  for (LO j = 0; j < blkSize; j++) {
147  retDecompEntries[i * blkSize + j] = Teuchos::as<GO>(decompEntries[i * inBlkSize]);
148  }
149  }
150  } // end if numLocalNodes > 0
151  Set(currentLevel, "Partition", ret);
152 } // Build()
153 
154 } // namespace MueLu
155 
156 #endif /* PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_ */
Important warning messages (one line)
Exception indicating invalid cast attempted.
MueLu::DefaultLocalOrdinal LocalOrdinal
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
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
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
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
std::string viewLabel_t
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
void print(std::ostream &out, const VerbLevel verbLevel=Default) const
Printing method.
Exception throws to report errors in the internal logical of the program.
void Build(Level &level) const
Build an object with this factory.
bool is_null() const