MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_RepartitionInterface_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 /*
11  * MueLu_RepartitionInterface_def.hpp
12  *
13  * Created on: 5 Sep 2013
14  * Author: wiesner
15  */
16 
17 #ifndef MUELU_REPARTITIONINTERFACE_DEF_HPP_
18 #define MUELU_REPARTITIONINTERFACE_DEF_HPP_
19 
21 
22 #include "MueLu_Level.hpp"
23 #include "MueLu_Exceptions.hpp"
24 #include "MueLu_Monitor.hpp"
25 
26 namespace MueLu {
27 
28 template <class LocalOrdinal, class GlobalOrdinal, class Node>
30  RCP<ParameterList> validParamList = rcp(new ParameterList());
31  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
32  validParamList->set<RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
33  validParamList->set<RCP<const FactoryBase> >("AmalgamatedPartition", Teuchos::null, "(advanced) Factory generating the AmalgamatedPartition (e.g. an IsorropiaInterface)");
34 
35  return validParamList;
36 }
37 
38 template <class LocalOrdinal, class GlobalOrdinal, class Node>
40  Input(currentLevel, "A");
41  Input(currentLevel, "number of partitions");
42  Input(currentLevel, "AmalgamatedPartition");
43 } // DeclareInput()
44 
45 template <class LocalOrdinal, class GlobalOrdinal, class Node>
47  FactoryMonitor m(*this, "Build", level);
48 
49  RCP<Matrix> A = Get<RCP<Matrix> >(level, "A");
50  RCP<Xpetra::Vector<GO, LO, GO, NO> > amalgPartition = Get<RCP<Xpetra::Vector<GO, LO, GO, NO> > >(level, "AmalgamatedPartition");
51  int numParts = Get<int>(level, "number of partitions");
52 
53  RCP<const Map> rowMap = A->getRowMap();
54 
55  // standard case: use matrix info and amalgamated rebalancing info to create "Partition" vector
56  RCP<const Teuchos::Comm<int> > comm = A->getRowMap()->getComm();
57 
58  // Short cut: if we only need one partition, then create a dummy partition vector
59  if (numParts == 1 || numParts == -1) {
60  // Single processor, decomposition is trivial: all zeros
62  Set(level, "Partition", decomposition);
63  return;
64  } /* else if (numParts == -1) {
65  // No repartitioning
66  RCP<Xpetra::Vector<GO,LO,GO,NO> > decomposition = Teuchos::null; //Xpetra::VectorFactory<GO, LO, GO, NO>::Build(rowMap, true);
67  //decomposition->putScalar(Teuchos::as<Scalar>(comm->getRank()));
68  Set(level, "Partition", decomposition);
69  return;
70  }*/
71 
72  ArrayRCP<GO> amalgPartitionData = amalgPartition->getDataNonConst(0);
73  RCP<const Map> nodeMap = amalgPartition->getMap();
74 
75  // extract amalgamation information from matrix A
76  LO blockdim = 1; // block dim for fixed size blocks
77  LO blockid = -1; // block id in strided map
78  LO nStridedOffset = 0; // DOF offset for strided block id "blockid" (default = 0)
79  LO stridedblocksize = blockdim; // size of strided block id "blockid" (default = fullblocksize, only if blockid!=-1 stridedblocksize <= fullblocksize)
80 
81  // 1) check for blocking/striding information
82  // fill above variables
83  if (A->IsView("stridedMaps") &&
84  Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
85  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
86  RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
87  TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null, Exceptions::BadCast, "MueLu::RepartitionInterface::Build: cast to strided row map failed.");
88  blockdim = strMap->getFixedBlockSize();
89  blockid = strMap->getStridedBlockId();
90  if (blockid > -1) {
91  std::vector<size_t> stridingInfo = strMap->getStridingData();
92  for (size_t j = 0; j < Teuchos::as<size_t>(blockid); j++)
93  nStridedOffset += stridingInfo[j];
94  stridedblocksize = Teuchos::as<LocalOrdinal>(stridingInfo[blockid]);
95 
96  } else {
97  stridedblocksize = blockdim;
98  }
99  oldView = A->SwitchToView(oldView);
100  // GetOStream(Statistics0, -1) << "RepartitionInterface::Build():" << " found blockdim=" << blockdim << " from strided maps (blockid=" << blockid << ", strided block size=" << stridedblocksize << "). offset=" << offset << std::endl;
101  } else
102  GetOStream(Statistics0, -1) << "RepartitionInterface::Build(): no striding information available. Use blockdim=1 with offset=0" << std::endl;
103 
104  // vector which stores final (unamalgamated) repartitioning
106  ArrayRCP<GO> decompEntries = decomposition->getDataNonConst(0);
107 
108  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<int>(nodeMap->getLocalNumElements()) * stridedblocksize != Teuchos::as<int>(rowMap->getLocalNumElements()), Exceptions::RuntimeError, "Inconsistency between nodeMap and dofMap: we are supporting block maps only. No support for general strided maps, yet!");
109 
110  // RCP<std::map<GO,std::vector<GO> > > nodegid2dofgids = amalgInfo->GetGlobalAmalgamationParams();
111 
112  // fill vector with information about partitioning
113  // TODO: we assume simple block maps here
114  // TODO: adapt this to usage of nodegid2dofgids
115  for (size_t i = 0; i < nodeMap->getLocalNumElements(); i++) {
116  // not fully sure about this. We're filling local ids in the decomposition vector with
117  // the results stored in array. The decomposition vector is created using the rowMap of A
118 
119  // transform local node id to global node id.
120  // GO gNodeId = nodeMap->getGlobalElement(i);
121 
122  // extract global DOF ids that belong to gNodeId
123  /*std::vector<GlobalOrdinal> DOFs = (*nodegid2dofgids)[gNodeId];
124  for(size_t j=0; j<stridedblocksize; j++) {
125  decompEntries[i*stridedblocksize + j] = myRank;
126  }*/
127  for (LO j = 0; j < stridedblocksize /*DOFs.size()*/; j++) {
128  // transform global DOF ids to local DOF ids using rowMap
129  // note: The vector decomposition is based on rowMap
130  // LO lDofId = rowMap->getLocalElement(DOFs[j]); // -> i doubt that we need this!
131 
132  // put the same domain id to all DOFs of the same node
133  decompEntries[i * stridedblocksize + j] = amalgPartitionData[i];
134  // decompEntries[lDofId] = amalgPartitionData[i];
135  }
136  }
137 
138  Set(level, "Partition", decomposition);
139 
140 } // Build()
141 
142 } // namespace MueLu
143 
144 #endif /* MUELU_REPARTITIONINTERFACE_DEF_HPP_ */
Exception indicating invalid cast attempted.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
LocalOrdinal LO
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Print statistics that do not involve significant additional computation.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
void Build(Level &level) const
Build an object with this factory.
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 DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.