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 
137  //
138  // \param updateInputData If true this indicates that either
139  // this is the first attempt at solution, or that we
140  // are computing a new solution and the input data has
141  // changed since the previous solution was computed.
142  // If false, this indicates that we are computing a
143  // new solution using the same input data was used for
144  // the previous solution, even though the parameters
145  // may have been changed.
146  //
147  // For the sake of performance, we ask the caller to set \c updateInputData
148  // to false if he/she is computing a new solution using the same input data,
149  // but different problem parameters, than that which was used to compute
150  // the most recent solution.
151 
152  void solve(bool updateInputData=true);
153 
155  //
156  // \return a reference to the solution to the most recent solve().
157 
159  if(localOrderingSolution_ == Teuchos::null) {
160  throw std::logic_error( "OrderingProblem was not created with local"
161  " ordering. Set parameter order_method_type to local or both."
162  " Or use getGlobalOrderingSolution()." );
163  }
164  return setupSolution(localOrderingSolution_);
165  }
166 
168  //
169  // \return a reference to the solution to the most recent solve().
170 
172  if(globalOrderingSolution_ == Teuchos::null) {
173  throw std::logic_error( "OrderingProblem was not created with global"
174  " ordering. Set parameter order_method_type to global or both."
175  " Or use getLocalOrderingSolution()." );
176  }
177  return setupSolution(globalOrderingSolution_);
178  }
179 
180 private:
181  template<typename ordering_solution_t>
182  ordering_solution_t *setupSolution(RCP<ordering_solution_t> solution) {
183  // std::cout << "havePerm= " << solution->havePerm() << " haveInverse= "
184  // << solution->haveInverse() << std::endl;
185  // Compute Perm or InvPerm, if one is missing.
186  if (!(solution->havePerm()))
187  solution->computePerm();
188  if (!(solution->haveInverse()))
189  solution->computeInverse();
190  return solution.getRawPtr();
191  }
192 
193  void createOrderingProblem();
194 
195  // local or global ordering is determined by which RCP is NULL
196  RCP<LocalOrderingSolution<lno_t> > localOrderingSolution_;
197  RCP<GlobalOrderingSolution<gno_t> > globalOrderingSolution_;
198 
199  size_t localNumObjects_;
200 };
201 
203 template <typename Adapter>
204 void OrderingProblem<Adapter>::solve(bool /* updateInputData */)
205 {
206  HELLO;
207 
208  // TODO: Assuming one MPI process now. nVtx = ngids = nlids
209  try
210  {
211  std::string method_type = this->params_->template
212  get<std::string>("order_method_type", "local");
213 
214  if(method_type == "local" || method_type == "both") {
215  localOrderingSolution_ = rcp(new LocalOrderingSolution<lno_t>(localNumObjects_));
216  }
217  if(method_type == "global" || method_type == "both") {
218  globalOrderingSolution_ = rcp(new GlobalOrderingSolution<gno_t>(localNumObjects_));
219  }
220  }
222 
223  // Determine which algorithm to use based on defaults and parameters.
224  // TODO: Use rcm if graph model is defined, otherwise use natural.
225  // Need some exception handling here, too.
226 
227  std::string method = this->params_->template
228  get<std::string>("order_method", "rcm");
229 
230  // TODO: Ignore case
231  try
232  {
233 
234  // could be a template... seems maybe more awkward
235  // added this to avoid duplicating local/global below
236  // so many times.
237  #define ZOLTAN2_COMPUTE_ORDERING \
238  if(localOrderingSolution_ != Teuchos::null) { \
239  alg.localOrder(localOrderingSolution_); \
240  } \
241  if(globalOrderingSolution_ != Teuchos::null) { \
242  alg.globalOrder(globalOrderingSolution_); \
243  }
244 
245  modelFlag_t graphFlags;
246  graphFlags.set(REMOVE_SELF_EDGES);
247  graphFlags.set(BUILD_LOCAL_GRAPH);
248 
249  if (method.compare("rcm") == 0) {
250  AlgRCM<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
251  this->comm_, this->envConst_, graphFlags);
253  }
254  else if (method.compare("natural") == 0) {
255  AlgNatural<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
256  this->comm_, this->envConst_);
258  }
259  else if (method.compare("random") == 0) {
260  AlgRandom<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
261  this->comm_, this->envConst_);
263  }
264  else if (method.compare("sorted_degree") == 0) {
265  AlgSortedDegree<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
266  this->comm_, this->envConst_,
267  graphFlags);
269  }
270  else if (method.compare("metis") == 0) {
271  AlgMetis<base_adapter_t> alg(this->baseInputAdapter_, this->params_,
272  this->comm_, this->envConst_, graphFlags);
274  }
275  else if (method.compare("minimum_degree") == 0) {
276  std::string pkg = this->params_->template get<std::string>(
277  "order_package", "amd");
278  if (pkg.compare("amd") == 0)
279  {
280  AlgAMD<base_adapter_t> alg(this->baseInputAdapter_,
281  this->params_, this->comm_, this->envConst_, graphFlags);
283  }
284  }
285  else if (method.compare("scotch") == 0) { // BDD Adding scotch ordering
286  AlgPTScotch<Adapter> alg(this->envConst_, this->comm_,
287  this->baseInputAdapter_);
289  }
290 
291 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
292  else if (method.compare("nd") == 0) {
293  AlgND<base_adapter_t> alg(this->envConst_, this->comm_, this->baseInputAdapter_, graphFlags);
295  }
296 #endif
297 
298  }
300 }
301 
303 //template <typename Adapter>
304 //void OrderingProblem<Adapter>::redistribute()
305 //{
306 // HELLO;
307 //}
308 
311 // Method with common functionality for creating a OrderingProblem.
312 // Individual constructors do appropriate conversions of input, etc.
313 // This method does everything that all constructors must do.
314 
315 template <typename Adapter>
317 {
318  HELLO;
319  using Teuchos::ParameterList;
320 
321  // Determine which parameters are relevant here.
322  // For now, assume parameters similar to Zoltan:
323  // MODEL = graph, hypergraph, geometric, ids
324  // ALGORITHM = rcm, random, amd
325 
326  ModelType modelType = IdentifierModelType; //default, change later
327  std::string method = this->params_->template
328  get<std::string>("order_method", "rcm");
329 
330  if ((method == std::string("rcm")) ||
331  (method == std::string("sorted_degree")) ||
332  (method == std::string("metis")) ||
333  (method == std::string("minimum_degree"))) {
334  modelType = GraphModelType;
335  }
336 
337 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
338  if ((method == std::string("nd")))
339  {
340  modelType = GraphModelType;
341  }
342 
343 #endif
344 
345  // Select Model based on parameters and InputAdapter type
346 
347  // std::bitset<NUM_MODEL_FLAGS> graphFlags;
348  // std::bitset<NUM_MODEL_FLAGS> idFlags;
349 
350 
351  //MMW: need to change this to allow multiple models
352  // as I did with partitioning, use modelAvail_
353 
354  const auto adapterType = this->baseInputAdapter_->adapterType();
355  switch (modelType)
356  {
357 
358  case GraphModelType:
359  {
360  switch (adapterType)
361  {
362  case MatrixAdapterType:
363  {
364  localNumObjects_ = this->baseInputAdapter_->getLocalNumIDs();
365  }
366  break;
367 
368  case GraphAdapterType:
369  {
370  const auto ia = dynamic_cast<const GraphAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
371  localNumObjects_ = ia->getLocalNumVertices();
372  }
373  break;
374 
375  case MeshAdapterType:
376  {
377  const auto ia = dynamic_cast<const MeshAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
378  localNumObjects_ = ia->getLocalNumOf(ia->getPrimaryEntityType());
379  }
380  break;
381 
382  default:{
383  // Avoid warning
384  }
385  }
386  }
387  break;
388 
389  case IdentifierModelType:
390  {
391  localNumObjects_ = this->baseInputAdapter_->getLocalNumIDs();
392  }
393  break;
394 
395  case HypergraphModelType:
396  case CoordinateModelType:
397  {
398  std::cout << __func__zoltan2__
399  << " Model type " << modelType << " not yet supported."
400  << std::endl;
401  }
402  break;
403 
404  default:
405  {
406  std::cout << __func__zoltan2__ << " Invalid model" << modelType
407  << std::endl;
408  }
409  break;
410  }
411 }
412 } //namespace Zoltan2
413 #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