Zoltan2
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 #include <bitset>
65 
66 using Teuchos::rcp_dynamic_cast;
67 
68 namespace Zoltan2{
69 
71 
90 template<typename Adapter>
91 class OrderingProblem : public Problem<Adapter>
92 {
93 public:
94 
95  typedef typename Adapter::scalar_t scalar_t;
96  typedef typename Adapter::gno_t gno_t;
97  typedef typename Adapter::lno_t lno_t;
98  typedef typename Adapter::user_t user_t;
100 
101 #ifdef HAVE_ZOLTAN2_MPI
102  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
103 #endif
104 
107  virtual ~OrderingProblem() {}
108 
109  OrderingProblem(Adapter *A, ParameterList *p,
110  const RCP<const Teuchos::Comm<int> > &comm) :
111  Problem<Adapter>(A, p, comm)
112  {
113  HELLO;
114  createOrderingProblem();
115  }
116 
117 #ifdef HAVE_ZOLTAN2_MPI
118 
120  OrderingProblem(Adapter *A, ParameterList *p, MPI_Comm mpicomm) :
121  OrderingProblem(A, p,
122  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
123  Teuchos::opaqueWrapper(mpicomm))))
124  {}
125 #endif
126 
129  OrderingProblem(Adapter *A, ParameterList *p) :
130  OrderingProblem(A, p, Teuchos::DefaultComm<int>::getComm())
131  {}
132 
135  static void getValidParameters(ParameterList & pl)
136  {
137  RCP<Teuchos::StringValidator> order_method_Validator =
138  Teuchos::rcp( new Teuchos::StringValidator(
139  Teuchos::tuple<std::string>( "rcm", "minimum_degree", "natural",
140  "random", "sorted_degree", "scotch", "nd" )));
141  pl.set("order_method", "rcm", "order algorithm",
142  order_method_Validator);
143 
144  RCP<Teuchos::StringValidator> order_method_type_Validator =
145  Teuchos::rcp( new Teuchos::StringValidator(
146  Teuchos::tuple<std::string>( "local", "global", "both" )));
147  pl.set("order_method_type", "local", "local or global or both",
148  order_method_type_Validator);
149 
150  RCP<Teuchos::StringValidator> order_package_Validator = Teuchos::rcp(
151  new Teuchos::StringValidator(
152  Teuchos::tuple<std::string>( "amd", "package2", "package3" )));
153  pl.set("order_package", "amd", "package to use in ordering",
154  order_package_Validator);
155  }
156 
158  //
159  // \param updateInputData If true this indicates that either
160  // this is the first attempt at solution, or that we
161  // are computing a new solution and the input data has
162  // changed since the previous solution was computed.
163  // If false, this indicates that we are computing a
164  // new solution using the same input data was used for
165  // the previous solution, even though the parameters
166  // may have been changed.
167  //
168  // For the sake of performance, we ask the caller to set \c updateInputData
169  // to false if he/she is computing a new solution using the same input data,
170  // but different problem parameters, than that which was used to compute
171  // the most recent solution.
172 
173  void solve(bool updateInputData=true);
174 
176  //
177  // \return a reference to the solution to the most recent solve().
178 
180  if(localOrderingSolution_ == Teuchos::null) {
181  throw std::logic_error( "OrderingProblem was not created with local"
182  " ordering. Set parameter order_method_type to local or both."
183  " Or use getGlobalOrderingSolution()." );
184  }
185  return setupSolution(localOrderingSolution_);
186  }
187 
189  //
190  // \return a reference to the solution to the most recent solve().
191 
193  if(globalOrderingSolution_ == Teuchos::null) {
194  throw std::logic_error( "OrderingProblem was not created with global"
195  " ordering. Set parameter order_method_type to global or both."
196  " Or use getLocalOrderingSolution()." );
197  }
198  return setupSolution(globalOrderingSolution_);
199  }
200 
201 private:
202  template<typename ordering_solution_t>
203  ordering_solution_t *setupSolution(RCP<ordering_solution_t> solution) {
204  // std::cout << "havePerm= " << solution->havePerm() << " haveInverse= "
205  // << solution->haveInverse() << std::endl;
206  // Compute Perm or InvPerm, if one is missing.
207  if (!(solution->havePerm()))
208  solution->computePerm();
209  if (!(solution->haveInverse()))
210  solution->computeInverse();
211  return solution.getRawPtr();
212  }
213 
214  void createOrderingProblem();
215 
216  // local or global ordering is determined by which RCP is NULL
217  RCP<LocalOrderingSolution<lno_t> > localOrderingSolution_;
218  RCP<GlobalOrderingSolution<gno_t> > globalOrderingSolution_;
219 
220 };
221 
223 template <typename Adapter>
224 void OrderingProblem<Adapter>::solve(bool updateInputData)
225 {
226  HELLO;
227 
228  size_t nVtx = this->baseModel_->getLocalNumObjects();
229 
230  // TODO: Assuming one MPI process now. nVtx = ngids = nlids
231  try
232  {
233  std::string method_type = this->params_->template
234  get<std::string>("order_method_type", "local");
235 
236  if(method_type == "local" || method_type == "both") {
237  localOrderingSolution_ = rcp(new LocalOrderingSolution<lno_t>(nVtx));
238  }
239  if(method_type == "global" || method_type == "both") {
240  globalOrderingSolution_ = rcp(new GlobalOrderingSolution<gno_t>(nVtx));
241  }
242  }
244 
245  // Determine which algorithm to use based on defaults and parameters.
246  // TODO: Use rcm if graph model is defined, otherwise use natural.
247  // Need some exception handling here, too.
248 
249  std::string method = this->params_->template
250  get<std::string>("order_method", "rcm");
251 
252  // TODO: Ignore case
253  try
254  {
255 
256  // could be a template... seems maybe more awkward
257  // added this to avoid duplicating local/global below
258  // so many times.
259  #define ZOLTAN2_COMPUTE_ORDERING \
260  if(localOrderingSolution_ != Teuchos::null) { \
261  alg.localOrder(localOrderingSolution_); \
262  } \
263  if(globalOrderingSolution_ != Teuchos::null) { \
264  alg.globalOrder(globalOrderingSolution_); \
265  }
266 
267  if (method.compare("rcm") == 0) {
268  AlgRCM<base_adapter_t> alg(this->graphModel_, this->params_, this->comm_);
270  }
271  else if (method.compare("natural") == 0) {
273  this->comm_);
275  }
276  else if (method.compare("random") == 0) {
278  this->comm_);
280  }
281  else if (method.compare("sorted_degree") == 0) {
283  this->comm_);
285  }
286  else if (method.compare("minimum_degree") == 0) {
287  std::string pkg = this->params_->template get<std::string>(
288  "order_package", "amd");
289  if (pkg.compare("amd") == 0)
290  {
292  this->params_, this->comm_);
294  }
295  }
296  else if (method.compare("scotch") == 0) { // BDD Adding scotch ordering
297  AlgPTScotch<Adapter> alg(this->envConst_, this->comm_,
298  this->baseInputAdapter_);
300  }
301 
302 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL_WOLF
303  else if (method.compare("nd") == 0) {
304  AlgND<Adapter> alg(this->envConst_, this->comm_, this->graphModel_,
305  this->coordinateModel_,this->baseInputAdapter_);
307  }
308 #endif
309 
310  }
312 }
313 
315 //template <typename Adapter>
316 //void OrderingProblem<Adapter>::redistribute()
317 //{
318 // HELLO;
319 //}
320 
323 // Method with common functionality for creating a OrderingProblem.
324 // Individual constructors do appropriate conversions of input, etc.
325 // This method does everything that all constructors must do.
326 
327 template <typename Adapter>
329 {
330  HELLO;
331  using Teuchos::ParameterList;
332 
333  // Determine which parameters are relevant here.
334  // For now, assume parameters similar to Zoltan:
335  // MODEL = graph, hypergraph, geometric, ids
336  // ALGORITHM = rcm, random, amd
337 
338  ModelType modelType = IdentifierModelType; //default, change later
339  std::string method = this->params_->template
340  get<std::string>("order_method", "rcm");
341 
342  if ((method == std::string("rcm")) ||
343  (method == std::string("sorted_degree")) ||
344  (method == std::string("minimum_degree"))) {
345  modelType = GraphModelType;
346  }
347 
348 #ifdef INCLUDE_ZOLTAN2_EXPERIMENTAL_WOLF
349  if ((method == std::string("nd")))
350  {
351  modelType = GraphModelType;
352  }
353 
354 #endif
355 
356  // Select Model based on parameters and InputAdapter type
357 
358  std::bitset<NUM_MODEL_FLAGS> graphFlags;
359  std::bitset<NUM_MODEL_FLAGS> idFlags;
360 
361 
362  //MMW: need to change this to allow multiple models
363  // as I did with partitioning, use modelAvail_
364 
365  switch (modelType) {
366 
367  case GraphModelType:
368  graphFlags.set(REMOVE_SELF_EDGES);
369  graphFlags.set(BUILD_LOCAL_GRAPH);
370  this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
371  this->baseInputAdapter_, this->envConst_, this->comm_, graphFlags));
372 
373  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
374  this->graphModel_);
375 
376  break;
377 
378 
379  case IdentifierModelType:
381  this->baseInputAdapter_, this->envConst_, this->comm_, idFlags));
382 
383  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
384  this->identifierModel_);
385 
386  break;
387 
388  case HypergraphModelType:
389  case CoordinateModelType:
390  std::cout << __func__zoltan2__
391  << " Model type " << modelType << " not yet supported."
392  << std::endl;
393  break;
394 
395  default:
396  std::cout << __func__zoltan2__ << " Invalid model" << modelType
397  << std::endl;
398  break;
399  }
400 }
401 } //namespace Zoltan2
402 #endif
RCP< GraphModel< base_adapter_t > > graphModel_
#define HELLO
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
RCP< const base_adapter_t > baseInputAdapter_
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.
RCP< const Comm< int > > comm_
RCP< IdentifierModel< base_adapter_t > > identifierModel_
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.
RCP< const Comm< int > > getComm()
Return the communicator used by the problem.
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
RCP< CoordinateModel< base_adapter_t > > coordinateModel_
Defines the Zoltan2_EvaluateOrdering.hpp class.
GlobalOrderingSolution< gno_t > * getGlobalOrderingSolution()
Get the global ordering solution to the problem.
GraphModel defines the interface required for graph models.
The base class for all model classes.
RCP< ParameterList > params_
IdentifierModel defines the interface for all identifier models.
Defines the GraphModel interface.
RCP< const Model< base_adapter_t > > baseModel_
static void getValidParameters(ParameterList &pl)
Set up validators specific to this Problem.
model represents graph within only one rank
RCP< const Environment > envConst_
#define __func__zoltan2__
LocalOrderingSolution< lno_t > * getLocalOrderingSolution()
Get the local ordering solution to the problem.