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 #include "MueLu_Utilities.hpp"
58 
59 
60 namespace MueLu {
61 
62  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
64  Teuchos::RCP<ParameterList> validParamList = rcp(new ParameterList());
65  validParamList->set< Teuchos::RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
66  validParamList->set< Teuchos::RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
67  validParamList->set< Teuchos::RCP<const FactoryBase> >("Partition", Teuchos::null, "Factory generating the Partition array (e.g. ZoltanInterface)");
68 
69  return validParamList;
70  }
71 
72 
73  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
75  Input(currentLevel, "A");
76  Input(currentLevel, "Partition");
77  } //DeclareInput()
78 
79  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
81  FactoryMonitor m(*this, "Build", currentLevel);
82  currentLevel.print(GetOStream(Statistics0,0));
83  // extract blocked operator A from current level
84  Teuchos::RCP<Matrix> A = Get< Teuchos::RCP<Matrix> > (currentLevel, "A");
85  Teuchos::RCP<const Teuchos::Comm< int > > comm = A->getRowMap()->getComm();
86 
87  // number of Partitions only used for a shortcut.
88  if (currentLevel.IsAvailable("number of partitions")) {
89  GetOStream(Warnings0) << "Using user-provided \"number of partitions\", the performance is unknown" << std::endl;
90 
91  }
92 
93  // ======================================================================================================
94  // Construct decomposition vector
95  // ======================================================================================================
96  RCP<GOVector> decomposition = Teuchos::null;
97 
98  // extract decomposition vector
99  decomposition = Get<RCP<GOVector> >(currentLevel, "Partition");
100  ArrayRCP<const GO> decompEntries = decomposition->getData(0);
101 
102  if (decomposition.is_null()) {
103  GetOStream(Warnings0) << "No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
104  Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
105  return;
106  }
107 
108  // create new decomposition vector
109  Teuchos::RCP<GOVector> ret = Xpetra::VectorFactory<GO, LO, GO, NO>::Build(A->getRowMap(), false);
110  ArrayRCP<GO> retDecompEntries = ret->getDataNonConst(0);
111 
112  // block size of output vector
113  LocalOrdinal blkSize = 1;
114 
115  // check for blocking/striding information
116  if(A->IsView("stridedMaps") &&
117  Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
118  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
119  RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
120  TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null,Exceptions::BadCast,"MueLu::CloneRepartitionInterface::Build: cast to strided row map failed.");
121  LocalOrdinal stridedBlock = strMap->getStridedBlockId();
122  if (stridedBlock == -1)
123  blkSize = strMap->getFixedBlockSize();
124  else {
125  std::vector<size_t> strInfo = strMap->getStridingData();
126  blkSize = strInfo[stridedBlock];
127  }
128  oldView = A->SwitchToView(oldView);
129  GetOStream(Statistics1) << "CloneRepartitionInterface::Build():" << " found blockdim=" << blkSize << " from strided maps."<< std::endl;
130  } else {
131  GetOStream(Statistics1) << "CloneRepartitionInterface::Build(): no striding information available. Use blockdim=" << blkSize << " (DofsPerNode)." << std::endl;
132  blkSize = A->GetFixedBlockSize();
133  }
134 
135  // plausibility check!
136  size_t inLocalLength = decomposition->getLocalLength();
137  size_t outLocalLength = A->getRowMap()->getNodeNumElements();
138 
139  // only for non-strided maps
140  size_t numLocalNodes = outLocalLength / blkSize;
141  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 <<")");
142 
143  if (numLocalNodes > 0) {
144  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 << ")");
145  LocalOrdinal inBlkSize = Teuchos::as<LocalOrdinal>(inLocalLength / numLocalNodes);
146  //TEUCHOS_TEST_FOR_EXCEPTION(blkSize != inBlkSize, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: input block size = " << inBlkSize << " outpub block size = " << blkSize << ". They should be the same.");
147 
148  for(LO i = 0; i<Teuchos::as<LO>(numLocalNodes); i++) {
149  for(LO j = 0; j < blkSize; j++) {
150  retDecompEntries[i*blkSize + j] = Teuchos::as<GO>(decompEntries[i*inBlkSize]);
151  }
152  }
153  } // end if numLocalNodes > 0
154  Set(currentLevel, "Partition", ret);
155  } //Build()
156 
157 
158 
159 } //namespace MueLu
160 
161 #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.
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
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