MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_IsorropiaInterface_def.hpp
Go to the documentation of this file.
1 /*
2  * MueLu_IsorropiaInterface_def.hpp
3  *
4  * Created on: Jun 10, 2013
5  * Author: tobias
6  */
7 
8 #ifndef MUELU_ISORROPIAINTERFACE_DEF_HPP_
9 #define MUELU_ISORROPIAINTERFACE_DEF_HPP_
10 
12 
13 #include <Teuchos_Utils.hpp>
14 //#include <Teuchos_DefaultMpiComm.hpp> //TODO: fwd decl.
15 //#include <Teuchos_OpaqueWrapper.hpp> //TODO: fwd decl.
16 
17 #include <Xpetra_MapFactory.hpp>
19 #include <Xpetra_BlockedMap.hpp>
21 
22 #ifdef HAVE_MUELU_ISORROPIA
23 #include <Isorropia_Exception.hpp>
24 
25 #ifdef HAVE_MUELU_EPETRA
27 #include <Epetra_CrsGraph.h>
28 #include <Isorropia_EpetraPartitioner.hpp>
29 #endif
30 
31 #include <Xpetra_TpetraCrsGraph.hpp>
32 #endif // ENDIF HAVE_MUELU_ISORROPIA
33 
34 #include "MueLu_Level.hpp"
35 #include "MueLu_Exceptions.hpp"
36 #include "MueLu_Monitor.hpp"
37 
38 #include "MueLu_AmalgamationInfo.hpp"
39 
40 namespace MueLu {
41 
42 template <class LocalOrdinal, class GlobalOrdinal, class Node>
44  RCP<ParameterList> validParamList = rcp(new ParameterList());
45 
46  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
47  validParamList->set<RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
48  validParamList->set<RCP<const FactoryBase> >("UnAmalgamationInfo", Teuchos::null, "Generating factory of UnAmalgamationInfo");
49 
50  return validParamList;
51 }
52 
53 template <class LocalOrdinal, class GlobalOrdinal, class Node>
55  Input(currentLevel, "A");
56  Input(currentLevel, "number of partitions");
57  Input(currentLevel, "UnAmalgamationInfo");
58 }
59 
60 template <class LocalOrdinal, class GlobalOrdinal, class Node>
62  FactoryMonitor m(*this, "Build", level);
64 
65  RCP<Matrix> A = Get<RCP<Matrix> >(level, "A");
66  RCP<AmalgamationInfo> amalInfo = Get<RCP<AmalgamationInfo> >(level, "UnAmalgamationInfo");
67  GO numParts = Get<int>(level, "number of partitions");
68 
69  RCP<const Map> rowMap = A->getRowMap();
70  RCP<const Map> colMap = A->getColMap();
71 
72  if (numParts == 1 || numParts == -1) {
73  // Running on one processor, so decomposition is the trivial one, all zeros.
75  Set(level, "AmalgamatedPartition", decomposition);
76  return;
77  }
78 
79  // ok, reconstruct graph information of matrix A
80  // Note, this is the non-rebalanced matrix A and we need the graph
81  // of the non-rebalanced matrix A. We cannot make use of the "Graph"
82  // that is/will be built for the aggregates later for several reasons
83  // 1) the "Graph" for the aggregates is meant to be based on the rebalanced matrix A
84  // 2) we cannot call a CoalesceDropFactory::Build here since this is very complicated and
85  // completely messes up the whole factory chain
86  // 3) CoalesceDropFactory is meant to provide some minimal Graph information for the aggregation
87  // (LWGraph), but here we need the full CrsGraph for Isorropia as input
88 
89  // That is, why this code is somewhat redundant to CoalesceDropFactory
90 
91  LO blockdim = 1; // block dim for fixed size blocks
92  GO indexBase = rowMap->getIndexBase(); // index base of maps
93  GO offset = 0;
94  LO blockid = -1; // block id in strided map
95  LO nStridedOffset = 0; // DOF offset for strided block id "blockid" (default = 0)
96  // LO stridedblocksize = blockdim; // size of strided block id "blockid" (default = fullblocksize, only if blockid!=-1 stridedblocksize <= fullblocksize)
97 
98  // 1) check for blocking/striding information
99  // fill above variables
100  if (A->IsView("stridedMaps") &&
101  Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
102  Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
103  RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
104  TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null, Exceptions::BadCast, "MueLu::IsorropiaInterface::Build: cast to strided row map failed.");
105  blockdim = strMap->getFixedBlockSize();
106  offset = strMap->getOffset();
107  blockid = strMap->getStridedBlockId();
108  if (blockid > -1) {
109  std::vector<size_t> stridingInfo = strMap->getStridingData();
110  for (size_t j = 0; j < Teuchos::as<size_t>(blockid); j++)
111  nStridedOffset += stridingInfo[j];
112  // stridedblocksize = Teuchos::as<LocalOrdinal>(stridingInfo[blockid]);
113 
114  } // else {
115  // stridedblocksize = blockdim;
116  //}
117  oldView = A->SwitchToView(oldView);
118  // GetOStream(Statistics0) << "IsorropiaInterface::Build():" << " found blockdim=" << blockdim << " from strided maps (blockid=" << blockid << ", strided block size=" << stridedblocksize << "). offset=" << offset << std::endl;
119  } else
120  GetOStream(Statistics0) << "IsorropiaInterface::Build(): no striding information available. Use blockdim=1 with offset=0" << std::endl;
121 
122  // 2) get row map for amalgamated matrix (graph of A)
123  // with same distribution over all procs as row map of A
124  RCP<const Map> nodeMap = amalInfo->getNodeRowMap();
125  RCP<const BlockedMap> bnodeMap = Teuchos::rcp_dynamic_cast<const BlockedMap>(nodeMap);
126  if (!bnodeMap.is_null()) nodeMap = bnodeMap->getMap();
127 
128  GetOStream(Statistics0) << "IsorropiaInterface:Build(): nodeMap " << nodeMap->getLocalNumElements() << "/" << nodeMap->getGlobalNumElements() << " elements" << std::endl;
129 
130  // 3) create graph of amalgamated matrix
131  RCP<CrsGraph> crsGraph = CrsGraphFactory::Build(nodeMap, A->getLocalMaxNumRowEntries() * blockdim);
132 
133  // 4) do amalgamation. generate graph of amalgamated matrix
134  for (LO row = 0; row < Teuchos::as<LO>(A->getRowMap()->getLocalNumElements()); row++) {
135  // get global DOF id
136  GO grid = rowMap->getGlobalElement(row);
137 
138  // translate grid to nodeid
139  // JHU 2019-20-May this is identical to AmalgamationFactory::DOFGid2NodeId(), and is done
140  // to break a circular dependence when libraries are built statically
141  GO nodeId = (grid - offset - indexBase) / blockdim + indexBase;
142 
143  size_t nnz = A->getNumEntriesInLocalRow(row);
146  A->getLocalRowView(row, indices, vals);
147 
148  RCP<std::vector<GO> > cnodeIds = Teuchos::rcp(new std::vector<GO>); // global column block ids
149  LO realnnz = 0;
150  for (LO col = 0; col < Teuchos::as<LO>(nnz); col++) {
151  GO gcid = colMap->getGlobalElement(indices[col]); // global column id
152 
153  if (vals[col] != 0.0) {
154  // JHU 2019-20-May this is identical to AmalgamationFactory::DOFGid2NodeId(), and is done
155  // to break a circular dependence when libraries are built statically
156  GO cnodeId = (gcid - offset - indexBase) / blockdim + indexBase;
157  cnodeIds->push_back(cnodeId);
158  realnnz++; // increment number of nnz in matrix row
159  }
160  }
161 
162  Teuchos::ArrayRCP<GO> arr_cnodeIds = Teuchos::arcp(cnodeIds);
163 
164  if (arr_cnodeIds.size() > 0)
165  crsGraph->insertGlobalIndices(nodeId, arr_cnodeIds());
166  }
167  // fill matrix graph
168  crsGraph->fillComplete(nodeMap, nodeMap);
169 
170 #ifdef HAVE_MPI
171 #ifdef HAVE_MUELU_ISORROPIA
172 
173  // prepare parameter list for Isorropia
174  Teuchos::ParameterList paramlist;
175  paramlist.set("NUM PARTS", toString(numParts));
176 
177  /*STRUCTURALLY SYMMETRIC [NO/yes] (is symmetrization required?)
178  PARTITIONING METHOD [block/cyclic/random/rcb/rib/hsfc/graph/HYPERGRAPH]
179  NUM PARTS [int k] (global number of parts)
180  IMBALANCE TOL [float tol] (1.0 is perfect balance)
181  BALANCE OBJECTIVE [ROWS/nonzeros]
182  */
183  Teuchos::ParameterList& sublist = paramlist.sublist("Zoltan");
184  sublist.set("LB_APPROACH", "PARTITION");
185 
186 #ifdef HAVE_MUELU_EPETRA
187  RCP<Xpetra::EpetraCrsGraphT<GO, Node> > epCrsGraph = Teuchos::rcp_dynamic_cast<Xpetra::EpetraCrsGraphT<GO, Node> >(crsGraph);
188  if (epCrsGraph != Teuchos::null) {
189  RCP<const Epetra_CrsGraph> epetraCrsGraph = epCrsGraph->getEpetra_CrsGraph();
190 
191  RCP<Isorropia::Epetra::Partitioner> isoPart = Teuchos::rcp(new Isorropia::Epetra::Partitioner(epetraCrsGraph, paramlist));
192 
193  int size = 0;
194  const int* array = NULL;
195  isoPart->extractPartsView(size, array);
196 
197  TEUCHOS_TEST_FOR_EXCEPTION(size != Teuchos::as<int>(nodeMap->getLocalNumElements()), Exceptions::RuntimeError, "length of array returned from extractPartsView does not match local length of rowMap");
198 
200  ArrayRCP<GO> decompEntries = decomposition->getDataNonConst(0);
201 
202  // fill vector with amalgamated information about partitioning
203  for (int i = 0; i < size; i++) {
204  decompEntries[i] = Teuchos::as<GO>(array[i]);
205  }
206 
207  Set(level, "AmalgamatedPartition", decomposition);
208  }
209 #endif // ENDIF HAVE_MUELU_EPETRA
210 
211 #ifdef HAVE_MUELU_INST_DOUBLE_INT_INT
212  RCP<Xpetra::TpetraCrsGraph<LO, GO, Node> > tpCrsGraph = Teuchos::rcp_dynamic_cast<Xpetra::TpetraCrsGraph<LO, GO, Node> >(crsGraph);
213  TEUCHOS_TEST_FOR_EXCEPTION(tpCrsGraph != Teuchos::null, Exceptions::RuntimeError, "Tpetra is not supported with Isorropia.");
214 #else
215  TEUCHOS_TEST_FOR_EXCEPTION(false, Exceptions::RuntimeError, "Isorropia is an interface to Zoltan which only has support for LO=GO=int and SC=double.");
216 #endif // ENDIF HAVE_MUELU_INST_DOUBLE_INT_INT
217 #endif // HAVE_MUELU_ISORROPIA
218 #else // if we don't have MPI
219 
220  // Running on one processor, so decomposition is the trivial one, all zeros.
222  Set(level, "AmalgamatedPartition", decomposition);
223 
224 #endif // HAVE_MPI
225  // throw a more helpful error message if something failed
226  // TEUCHOS_TEST_FOR_EXCEPTION(!level.IsAvailable("AmalgamatedPartition"), Exceptions::RuntimeError, "IsorropiaInterface::Build : no \'Partition\' vector available on level. Isorropia failed to build a partition of the non-repartitioned graph of A. Please make sure, that Isorropia is correctly compiled (Epetra/Tpetra).");
227 
228 } // Build()
229 
230 } // namespace MueLu
231 
232 //#endif //if defined(HAVE_MUELU_ISORROPIA) && defined(HAVE_MPI)
233 
234 #endif /* MUELU_ISORROPIAINTERFACE_DEF_HPP_ */
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Exception indicating invalid cast attempted.
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
GlobalOrdinal GO
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Build(Level &level) const
Build an object with this factory.
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
size_type size() 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
std::string viewLabel_t
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Exception throws to report errors in the internal logical of the program.
bool is_null() const