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 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
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 Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_ORDERINGPROBLEM_HPP_
51 #define _ZOLTAN2_ORDERINGPROBLEM_HPP_
52 
53 #include <Zoltan2_Problem.hpp>
57 
58 #include <Zoltan2_GraphModel.hpp>
59 #include <string>
60 #ifdef HAVE_ZOLTAN2_OVIS
61 #include <ovis.h>
62 #endif
63 
64 
65 
66 
67 
68 #include <bitset>
69 
70 using Teuchos::rcp_dynamic_cast;
71 
72 namespace Zoltan2{
73 
75 
94 template<typename Adapter>
95 class OrderingProblem : public Problem<Adapter>
96 {
97 public:
98 
99  typedef typename Adapter::scalar_t scalar_t;
100  typedef typename Adapter::gno_t gno_t;
101  typedef typename Adapter::lno_t lno_t;
102  typedef typename Adapter::user_t user_t;
104 
105 #ifdef HAVE_ZOLTAN2_MPI
106  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
107 #endif
108 
111  virtual ~OrderingProblem() {}
112 
113  OrderingProblem(Adapter *A, ParameterList *p,
114  const RCP<const Teuchos::Comm<int> > &comm) :
115  Problem<Adapter>(A, p, comm)
116  {
117  HELLO;
118  createOrderingProblem();
119  }
120 
121 #ifdef HAVE_ZOLTAN2_MPI
122 
124  OrderingProblem(Adapter *A, ParameterList *p, MPI_Comm mpicomm) :
125  OrderingProblem(A, p,
126  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
127  Teuchos::opaqueWrapper(mpicomm))))
128  {}
129 #endif
130 
133  OrderingProblem(Adapter *A, ParameterList *p) :
134  OrderingProblem(A, p, Tpetra::getDefaultComm())
135  {}
136 
139  static void getValidParameters(ParameterList & pl)
140  {
141 
142 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
144 #endif
145 
146  RCP<Teuchos::StringValidator> order_method_Validator =
147  Teuchos::rcp( new Teuchos::StringValidator(
148  Teuchos::tuple<std::string>( "rcm", "minimum_degree", "natural",
149  "random", "sorted_degree", "scotch", "nd" )));
150  pl.set("order_method", "rcm", "order algorithm",
151  order_method_Validator);
152 
153  RCP<Teuchos::StringValidator> order_method_type_Validator =
154  Teuchos::rcp( new Teuchos::StringValidator(
155  Teuchos::tuple<std::string>( "local", "global", "both" )));
156  pl.set("order_method_type", "local", "local or global or both",
157  order_method_type_Validator);
158 
159  RCP<Teuchos::StringValidator> order_package_Validator = Teuchos::rcp(
160  new Teuchos::StringValidator(
161  Teuchos::tuple<std::string>( "amd", "package2", "package3" )));
162  pl.set("order_package", "amd", "package to use in ordering",
163  order_package_Validator);
164  }
165 
167  //
168  // \param updateInputData If true this indicates that either
169  // this is the first attempt at solution, or that we
170  // are computing a new solution and the input data has
171  // changed since the previous solution was computed.
172  // If false, this indicates that we are computing a
173  // new solution using the same input data was used for
174  // the previous solution, even though the parameters
175  // may have been changed.
176  //
177  // For the sake of performance, we ask the caller to set \c updateInputData
178  // to false if he/she is computing a new solution using the same input data,
179  // but different problem parameters, than that which was used to compute
180  // the most recent solution.
181 
182  void solve(bool updateInputData=true);
183 
185  //
186  // \return a reference to the solution to the most recent solve().
187 
189  if(localOrderingSolution_ == Teuchos::null) {
190  throw std::logic_error( "OrderingProblem was not created with local"
191  " ordering. Set parameter order_method_type to local or both."
192  " Or use getGlobalOrderingSolution()." );
193  }
194  return setupSolution(localOrderingSolution_);
195  }
196 
198  //
199  // \return a reference to the solution to the most recent solve().
200 
202  if(globalOrderingSolution_ == Teuchos::null) {
203  throw std::logic_error( "OrderingProblem was not created with global"
204  " ordering. Set parameter order_method_type to global or both."
205  " Or use getLocalOrderingSolution()." );
206  }
207  return setupSolution(globalOrderingSolution_);
208  }
209 
210 private:
211  template<typename ordering_solution_t>
212  ordering_solution_t *setupSolution(RCP<ordering_solution_t> solution) {
213  // std::cout << "havePerm= " << solution->havePerm() << " haveInverse= "
214  // << solution->haveInverse() << std::endl;
215  // Compute Perm or InvPerm, if one is missing.
216  if (!(solution->havePerm()))
217  solution->computePerm();
218  if (!(solution->haveInverse()))
219  solution->computeInverse();
220  return solution.getRawPtr();
221  }
222 
223  void createOrderingProblem();
224 
225  // local or global ordering is determined by which RCP is NULL
226  RCP<LocalOrderingSolution<lno_t> > localOrderingSolution_;
227  RCP<GlobalOrderingSolution<gno_t> > globalOrderingSolution_;
228 
229 };
230 
232 template <typename Adapter>
233 void OrderingProblem<Adapter>::solve(bool /* updateInputData */)
234 {
235  HELLO;
236 
237  size_t nVtx = this->baseModel_->getLocalNumObjects();
238 
239  // TODO: Assuming one MPI process now. nVtx = ngids = nlids
240  try
241  {
242  std::string method_type = this->params_->template
243  get<std::string>("order_method_type", "local");
244 
245  if(method_type == "local" || method_type == "both") {
246  localOrderingSolution_ = rcp(new LocalOrderingSolution<lno_t>(nVtx));
247  }
248  if(method_type == "global" || method_type == "both") {
249  globalOrderingSolution_ = rcp(new GlobalOrderingSolution<gno_t>(nVtx));
250  }
251  }
253 
254  // Determine which algorithm to use based on defaults and parameters.
255  // TODO: Use rcm if graph model is defined, otherwise use natural.
256  // Need some exception handling here, too.
257 
258  std::string method = this->params_->template
259  get<std::string>("order_method", "rcm");
260 
261  // TODO: Ignore case
262  try
263  {
264 
265  // could be a template... seems maybe more awkward
266  // added this to avoid duplicating local/global below
267  // so many times.
268  #define ZOLTAN2_COMPUTE_ORDERING \
269  if(localOrderingSolution_ != Teuchos::null) { \
270  alg.localOrder(localOrderingSolution_); \
271  } \
272  if(globalOrderingSolution_ != Teuchos::null) { \
273  alg.globalOrder(globalOrderingSolution_); \
274  }
275 
276  if (method.compare("rcm") == 0) {
277  AlgRCM<base_adapter_t> alg(this->graphModel_, this->params_, this->comm_);
279  }
280  else if (method.compare("natural") == 0) {
281  AlgNatural<base_adapter_t> alg(this->identifierModel_, this->params_,
282  this->comm_);
284  }
285  else if (method.compare("random") == 0) {
286  AlgRandom<base_adapter_t> alg(this->identifierModel_, this->params_,
287  this->comm_);
289  }
290  else if (method.compare("sorted_degree") == 0) {
291  AlgSortedDegree<base_adapter_t> alg(this->graphModel_, this->params_,
292  this->comm_);
294  }
295  else if (method.compare("minimum_degree") == 0) {
296  std::string pkg = this->params_->template get<std::string>(
297  "order_package", "amd");
298  if (pkg.compare("amd") == 0)
299  {
300  AlgAMD<base_adapter_t> alg(this->graphModel_,
301  this->params_, this->comm_);
303  }
304  }
305  else if (method.compare("scotch") == 0) { // BDD Adding scotch ordering
306  AlgPTScotch<Adapter> alg(this->envConst_, this->comm_,
307  this->baseInputAdapter_);
309  }
310 
311 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
312  else if (method.compare("nd") == 0) {
313  AlgND<Adapter> alg(this->envConst_, this->comm_, this->graphModel_,
314  this->coordinateModel_,this->baseInputAdapter_);
316  }
317 #endif
318 
319  }
321 }
322 
324 //template <typename Adapter>
325 //void OrderingProblem<Adapter>::redistribute()
326 //{
327 // HELLO;
328 //}
329 
332 // Method with common functionality for creating a OrderingProblem.
333 // Individual constructors do appropriate conversions of input, etc.
334 // This method does everything that all constructors must do.
335 
336 template <typename Adapter>
338 {
339  HELLO;
340  using Teuchos::ParameterList;
341 
342  // Determine which parameters are relevant here.
343  // For now, assume parameters similar to Zoltan:
344  // MODEL = graph, hypergraph, geometric, ids
345  // ALGORITHM = rcm, random, amd
346 
347  ModelType modelType = IdentifierModelType; //default, change later
348  std::string method = this->params_->template
349  get<std::string>("order_method", "rcm");
350 
351  if ((method == std::string("rcm")) ||
352  (method == std::string("sorted_degree")) ||
353  (method == std::string("minimum_degree"))) {
354  modelType = GraphModelType;
355  }
356 
357 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL
358  if ((method == std::string("nd")))
359  {
360  modelType = GraphModelType;
361  }
362 
363 #endif
364 
365  // Select Model based on parameters and InputAdapter type
366 
367  std::bitset<NUM_MODEL_FLAGS> graphFlags;
368  std::bitset<NUM_MODEL_FLAGS> idFlags;
369 
370 
371  //MMW: need to change this to allow multiple models
372  // as I did with partitioning, use modelAvail_
373 
374  switch (modelType) {
375 
376  case GraphModelType:
377  graphFlags.set(REMOVE_SELF_EDGES);
378  graphFlags.set(BUILD_LOCAL_GRAPH);
379  this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
380  this->baseInputAdapter_, this->envConst_, this->comm_, graphFlags));
381 
382  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
383  this->graphModel_);
384 
385  break;
386 
387 
388  case IdentifierModelType:
389  this->identifierModel_ = rcp(new IdentifierModel<base_adapter_t>(
390  this->baseInputAdapter_, this->envConst_, this->comm_, idFlags));
391 
392  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
393  this->identifierModel_);
394 
395  break;
396 
397  case HypergraphModelType:
398  case CoordinateModelType:
399  std::cout << __func__zoltan2__
400  << " Model type " << modelType << " not yet supported."
401  << std::endl;
402  break;
403 
404  default:
405  std::cout << __func__zoltan2__ << " Invalid model" << modelType
406  << std::endl;
407  break;
408  }
409 }
410 } //namespace Zoltan2
411 #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.
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
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:74