Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_OrderingProblem.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #ifndef _ZOLTAN2_ORDERINGPROBLEM_HPP_
15 #define _ZOLTAN2_ORDERINGPROBLEM_HPP_
16 
17 #include <Zoltan2_Problem.hpp>
21 
22 #include <Zoltan2_GraphModel.hpp>
23 #include <string>
24 #ifdef HAVE_ZOLTAN2_OVIS
25 #include <ovis.h>
26 #endif
27 
28 
29 
30 
31 
32 #include <bitset>
33 
34 using Teuchos::rcp_dynamic_cast;
35 
36 namespace Zoltan2{
37 
39 
58 template<typename Adapter>
59 class OrderingProblem : public Problem<Adapter>
60 {
61 public:
62 
63  typedef typename Adapter::scalar_t scalar_t;
64  typedef typename Adapter::gno_t gno_t;
65  typedef typename Adapter::lno_t lno_t;
66  typedef typename Adapter::user_t user_t;
68 
69 #ifdef HAVE_ZOLTAN2_MPI
70  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
71 #endif
72 
75  virtual ~OrderingProblem() {}
76 
77  OrderingProblem(Adapter *A, ParameterList *p,
78  const RCP<const Teuchos::Comm<int> > &comm) :
79  Problem<Adapter>(A, p, comm)
80  {
81  HELLO;
82  createOrderingProblem();
83  }
84 
85 #ifdef HAVE_ZOLTAN2_MPI
86 
88  OrderingProblem(Adapter *A, ParameterList *p, MPI_Comm mpicomm) :
89  OrderingProblem(A, p,
90  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
91  Teuchos::opaqueWrapper(mpicomm))))
92  {}
93 #endif
94 
97  OrderingProblem(Adapter *A, ParameterList *p) :
98  OrderingProblem(A, p, Tpetra::getDefaultComm())
99  {}
100 
103  static void getValidParameters(ParameterList & pl)
104  {
105 
106 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
108 #endif
109 
110  RCP<Teuchos::StringValidator> order_method_Validator =
111  Teuchos::rcp( new Teuchos::StringValidator(
112  Teuchos::tuple<std::string>( "rcm", "metis", "minimum_degree", "natural",
113  "random", "sorted_degree", "scotch", "nd" )));
114  pl.set("order_method", "rcm", "order algorithm",
115  order_method_Validator);
116 
117  RCP<Teuchos::StringValidator> order_method_type_Validator =
118  Teuchos::rcp( new Teuchos::StringValidator(
119  Teuchos::tuple<std::string>( "local", "global", "both" )));
120  pl.set("order_method_type", "local", "local or global or both",
121  order_method_type_Validator);
122 
123  RCP<Teuchos::StringValidator> order_package_Validator = Teuchos::rcp(
124  new Teuchos::StringValidator(
125  Teuchos::tuple<std::string>( "amd", "package2", "package3" )));
126  pl.set("order_package", "amd", "package to use in ordering",
127  order_package_Validator);
128 
129  RCP<Teuchos::StringValidator> rcm_root_selection_Validator = Teuchos::rcp(
130  new Teuchos::StringValidator(
131  Teuchos::tuple<std::string>( "pseudoperipheral", "first", "smallest_degree" )));
132  pl.set("root_method", "pseudoperipheral", "method for selecting RCM root",
133  rcm_root_selection_Validator);
134 
135  pl.set("symmetrize", false, "Whether the input graph should be symmetrizes");
136  }
137 
139  //
140  // \param updateInputData If true this indicates that either
141  // this is the first attempt at solution, or that we
142  // are computing a new solution and the input data has
143  // changed since the previous solution was computed.
144  // If false, this indicates that we are computing a
145  // new solution using the same input data was used for
146  // the previous solution, even though the parameters
147  // may have been changed.
148  //
149  // For the sake of performance, we ask the caller to set \c updateInputData
150  // to false if he/she is computing a new solution using the same input data,
151  // but different problem parameters, than that which was used to compute
152  // the most recent solution.
153 
154  void solve(bool updateInputData=true);
155 
157  //
158  // \return a reference to the solution to the most recent solve().
159 
161  if(localOrderingSolution_ == Teuchos::null) {
162  throw std::logic_error( "OrderingProblem was not created with local"
163  " ordering. Set parameter order_method_type to local or both."
164  " Or use getGlobalOrderingSolution()." );
165  }
166  return setupSolution(localOrderingSolution_);
167  }
168 
170  //
171  // \return a reference to the solution to the most recent solve().
172 
174  if(globalOrderingSolution_ == Teuchos::null) {
175  throw std::logic_error( "OrderingProblem was not created with global"
176  " ordering. Set parameter order_method_type to global or both."
177  " Or use getLocalOrderingSolution()." );
178  }
179  return setupSolution(globalOrderingSolution_);
180  }
181 
182 private:
183  template<typename ordering_solution_t>
184  ordering_solution_t *setupSolution(RCP<ordering_solution_t> solution) {
185  // std::cout << "havePerm= " << solution->havePerm() << " haveInverse= "
186  // << solution->haveInverse() << std::endl;
187  // Compute Perm or InvPerm, if one is missing.
188  if (!(solution->havePerm()))
189  solution->computePerm();
190  if (!(solution->haveInverse()))
191  solution->computeInverse();
192  return solution.getRawPtr();
193  }
194 
195  void createOrderingProblem();
196 
197  // local or global ordering is determined by which RCP is NULL
198  RCP<LocalOrderingSolution<lno_t> > localOrderingSolution_;
199  RCP<GlobalOrderingSolution<gno_t> > globalOrderingSolution_;
200 
201  size_t localNumObjects_;
202 };
203 
205 template <typename Adapter>
206 void OrderingProblem<Adapter>::solve(bool /* updateInputData */)
207 {
208  HELLO;
209 
210  // TODO: Assuming one MPI process now. nVtx = ngids = nlids
211  try
212  {
213  std::string method_type = this->params_->template
214  get<std::string>("order_method_type", "local");
215 
216  if(method_type == "local" || method_type == "both") {
217  localOrderingSolution_ = rcp(new LocalOrderingSolution<lno_t>(localNumObjects_));
218  }
219  if(method_type == "global" || method_type == "both") {
220  globalOrderingSolution_ = rcp(new GlobalOrderingSolution<gno_t>(localNumObjects_));
221  }
222  }
224 
225  // Determine which algorithm to use based on defaults and parameters.
226  // TODO: Use rcm if graph model is defined, otherwise use natural.
227  // Need some exception handling here, too.
228 
229  std::string method = this->params_->template
230  get<std::string>("order_method", "rcm");
231 
232  // TODO: Ignore case
233  try
234  {
235 
236  // could be a template... seems maybe more awkward
237  // added this to avoid duplicating local/global below
238  // so many times.
239  #define ZOLTAN2_COMPUTE_ORDERING \
240  if(localOrderingSolution_ != Teuchos::null) { \
241  alg.localOrder(localOrderingSolution_); \
242  } \
243  if(globalOrderingSolution_ != Teuchos::null) { \
244  alg.globalOrder(globalOrderingSolution_); \
245  }
246 
247  modelFlag_t graphFlags;
248  graphFlags.set(REMOVE_SELF_EDGES);
249  graphFlags.set(BUILD_LOCAL_GRAPH);
250 
251  if (method.compare("rcm") == 0) {
252  AlgRCM<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
253  this->comm_, this->envConst_, graphFlags);
255  }
256  else if (method.compare("natural") == 0) {
257  AlgNatural<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
258  this->comm_, this->envConst_);
260  }
261  else if (method.compare("random") == 0) {
262  AlgRandom<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
263  this->comm_, this->envConst_);
265  }
266  else if (method.compare("sorted_degree") == 0) {
267  AlgSortedDegree<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
268  this->comm_, this->envConst_,
269  graphFlags);
271  }
272  else if (method.compare("metis") == 0) {
273  AlgMetis<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
274  this->comm_, this->envConst_, graphFlags);
276  }
277  else if (method.compare("minimum_degree") == 0) {
278  std::string pkg = this->params_->template get<std::string>(
279  "order_package", "amd");
280  if (pkg.compare("amd") == 0)
281  {
282  AlgAMD<base_adapter_t> alg(this->baseInputAdapter_,
283  this->params_, this->comm_, this->envConst_, graphFlags);
285  }
286  }
287  else if (method.compare("scotch") == 0) { // BDD Adding scotch ordering
288  AlgPTScotch<Adapter> alg(this->envConst_, this->comm_,
289  this->baseInputAdapter_);
291  }
292 
293 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
294  else if (method.compare("nd") == 0) {
295  AlgND<base_adapter_t> alg(this->envConst_, this->comm_, this->baseInputAdapter_, graphFlags);
297  }
298 #endif
299 
300  }
302 }
303 
305 //template <typename Adapter>
306 //void OrderingProblem<Adapter>::redistribute()
307 //{
308 // HELLO;
309 //}
310 
313 // Method with common functionality for creating a OrderingProblem.
314 // Individual constructors do appropriate conversions of input, etc.
315 // This method does everything that all constructors must do.
316 
317 template <typename Adapter>
319 {
320  HELLO;
321  using Teuchos::ParameterList;
322 
323  // Determine which parameters are relevant here.
324  // For now, assume parameters similar to Zoltan:
325  // MODEL = graph, hypergraph, geometric, ids
326  // ALGORITHM = rcm, random, amd
327 
328  ModelType modelType = IdentifierModelType; //default, change later
329  std::string method = this->params_->template
330  get<std::string>("order_method", "rcm");
331 
332  if ((method == std::string("rcm")) ||
333  (method == std::string("sorted_degree")) ||
334  (method == std::string("metis")) ||
335  (method == std::string("minimum_degree"))) {
336  modelType = GraphModelType;
337  }
338 
339 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
340  if ((method == std::string("nd")))
341  {
342  modelType = GraphModelType;
343  }
344 
345 #endif
346 
347  // Select Model based on parameters and InputAdapter type
348 
349  // std::bitset<NUM_MODEL_FLAGS> graphFlags;
350  // std::bitset<NUM_MODEL_FLAGS> idFlags;
351 
352 
353  //MMW: need to change this to allow multiple models
354  // as I did with partitioning, use modelAvail_
355 
356  const auto adapterType = this->baseInputAdapter_->adapterType();
357  switch (modelType)
358  {
359 
360  case GraphModelType:
361  {
362  switch (adapterType)
363  {
364  case MatrixAdapterType:
365  {
366  localNumObjects_ = this->baseInputAdapter_->getLocalNumIDs();
367  }
368  break;
369 
370  case GraphAdapterType:
371  {
372  const auto ia = dynamic_cast<const GraphAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
373  localNumObjects_ = ia->getLocalNumVertices();
374  }
375  break;
376 
377  case MeshAdapterType:
378  {
379  const auto ia = dynamic_cast<const MeshAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
380  localNumObjects_ = ia->getLocalNumOf(ia->getPrimaryEntityType());
381  }
382  break;
383 
384  default:{
385  // Avoid warning
386  }
387  }
388  }
389  break;
390 
391  case IdentifierModelType:
392  {
393  localNumObjects_ = this->baseInputAdapter_->getLocalNumIDs();
394  }
395  break;
396 
397  case HypergraphModelType:
398  case CoordinateModelType:
399  {
400  std::cout << __func__zoltan2__
401  << " Model type " << modelType << " not yet supported."
402  << std::endl;
403  }
404  break;
405 
406  default:
407  {
408  std::cout << __func__zoltan2__ << " Invalid model" << modelType
409  << std::endl;
410  }
411  break;
412  }
413 }
414 } //namespace Zoltan2
415 #endif
#define HELLO
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
ModelType
An identifier for the general type of model.
virtual ~OrderingProblem()
Destructor.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
Defines the OrderingSolution class.
static void getValidParameters(ParameterList &pl)
Set up validators specific to this algorithm.
OrderingProblem sets up ordering problems for the user.
algorithm requires no self edges
#define ZOLTAN2_COMPUTE_ORDERING
OrderingProblem(Adapter *A, ParameterList *p, const RCP< const Teuchos::Comm< int > > &comm)
OrderingProblem(Adapter *A, ParameterList *p)
Constructor that uses a default communicator.
Problem base class from which other classes (PartitioningProblem, ColoringProblem, OrderingProblem, MatchingProblem, etc.) derive.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Defines the Problem base class.
Adapter::base_adapter_t base_adapter_t
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
Defines the Zoltan2_EvaluateOrdering.hpp class.
GlobalOrderingSolution< gno_t > * getGlobalOrderingSolution()
Get the global ordering solution to the problem.
Defines the GraphModel interface.
static void getValidParameters(ParameterList &pl)
Set up validators specific to this Problem.
model represents graph within only one rank
#define __func__zoltan2__
LocalOrderingSolution< lno_t > * getLocalOrderingSolution()
Get the local ordering solution to the problem.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39