Zoltan2  Version of the Day
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>
56 
57 #include <Zoltan2_GraphModel.hpp>
58 #include <string>
59 #ifdef HAVE_ZOLTAN2_OVIS
60 #include <ovis.h>
61 #endif
62 
63 #include <bitset>
64 
65 using Teuchos::rcp_dynamic_cast;
66 using namespace std;
67 
68 namespace Zoltan2{
69 
71 
87 template<typename Adapter>
88 class OrderingProblem : public Problem<Adapter>
89 {
90 public:
91 
92  typedef typename Adapter::scalar_t scalar_t;
93  typedef typename Adapter::gid_t gid_t;
94  typedef typename Adapter::gno_t gno_t;
95  typedef typename Adapter::lno_t lno_t;
96  typedef typename Adapter::user_t user_t;
97  typedef typename Adapter::base_adapter_t base_adapter_t;
98 
99 #ifdef HAVE_ZOLTAN2_MPI
100  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
101 #endif
102 
105  virtual ~OrderingProblem() {};
106 
107 
108 #ifdef HAVE_ZOLTAN2_MPI
109 
111  OrderingProblem(Adapter *A, ParameterList *p, MPI_Comm comm)
112  : Problem<Adapter>(A, p, comm)
113  {
114  HELLO;
115  createOrderingProblem();
116  };
117 #endif
118 
121  OrderingProblem(Adapter *A, ParameterList *p) : Problem<Adapter>(A, p)
122  {
123  HELLO;
124  createOrderingProblem();
125  };
126 
128  //
129  // \param updateInputData If true this indicates that either
130  // this is the first attempt at solution, or that we
131  // are computing a new solution and the input data has
132  // changed since the previous solution was computed.
133  // If false, this indicates that we are computing a
134  // new solution using the same input data was used for
135  // the previous solution, even though the parameters
136  // may have been changed.
137  //
138  // For the sake of performance, we ask the caller to set \c updateInputData
139  // to false if he/she is computing a new solution using the same input data,
140  // but different problem parameters, than that which was used to compute
141  // the most recent solution.
142 
143  void solve(bool updateInputData=true);
144 
146  //
147  // \return a reference to the solution to the most recent solve().
148 
150  return solution_.getRawPtr();
151  };
152 
153 private:
154  void createOrderingProblem();
155 
156  RCP<OrderingSolution<gid_t, lno_t> > solution_;
157 
158  RCP<Comm<int> > problemComm_;
159  RCP<const Comm<int> > problemCommConst_;
160 
161 #ifdef HAVE_ZOLTAN2_MPI
162  MPI_Comm mpiComm_;
163 #endif
164 };
165 
167 template <typename Adapter>
169 {
170  HELLO;
171 
172  size_t nVtx = this->baseModel_->getLocalNumObjects();
173 
174  // TODO: Assuming one MPI process now. nVtx = ngids = nlids
175  try
176  {
177  this->solution_ = rcp(new OrderingSolution<gid_t, lno_t>(nVtx, nVtx));
178  }
180 
181  // Determine which algorithm to use based on defaults and parameters.
182  // TODO: Use rcm if graph model is defined, otherwise use natural.
183  // Need some exception handling here, too.
184 
185  string method = this->params_->template get<string>("order_method", "rcm");
186 
187  // TODO: Ignore case
188  try
189  {
190  if (method.compare("rcm") == 0)
191  {
192  AlgRCM<base_adapter_t>(this->graphModel_, this->solution_, this->params_,
193  problemComm_);
194  }
195  else if (method.compare("natural") == 0)
196  {
197  AlgNatural<base_adapter_t>(this->identifierModel_, this->solution_, this->params_, problemComm_);
198  }
199  else if (method.compare("random") == 0)
200  {
201  AlgRandom<base_adapter_t>(this->identifierModel_, this->solution_, this->params_, problemComm_);
202  }
203  else if (method.compare("minimum_degree") == 0)
204  {
205  string pkg = this->params_->template get<string>("order_package", "amd");
206  if (pkg.compare("amd") == 0)
207  {
208  AlgAMD<base_adapter_t>(this->graphModel_, this->solution_, this->params_,
209  problemComm_);
210  }
211  }
212  }
214 
215 #ifdef HAVE_ZOLTAN2_MPI
216 
217  // The algorithm may have changed the communicator. Change it back.
218 
219  RCP<const mpiWrapper_t > wrappedComm = rcp(new mpiWrapper_t(mpiComm_));
220  problemComm_ = rcp(new Teuchos::MpiComm<int>(wrappedComm));
221  problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
222 
223 #endif
224 
225 }
226 
228 //template <typename Adapter>
229 //void OrderingProblem<Adapter>::redistribute()
230 //{
231 // HELLO;
232 //}
233 
236 // Method with common functionality for creating a OrderingProblem.
237 // Individual constructors do appropriate conversions of input, etc.
238 // This method does everything that all constructors must do.
239 
240 template <typename Adapter>
242 {
243  HELLO;
244  using Teuchos::ParameterList;
245 
246 // cout << __func__ << " input adapter type "
247 // << this->inputAdapter_->inputAdapterType() << " "
248 // << this->inputAdapter_->inputAdapterName() << endl;
249 
250 #ifdef HAVE_ZOLTAN2_OVIS
251  ovis_enabled(this->comm_->getRank());
252 #endif
253 
254  // Create a copy of the user's communicator.
255 
256  problemComm_ = this->comm_->duplicate();
257  problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
258 
259 
260 #ifdef HAVE_ZOLTAN2_MPI
261 
262  // TPLs may want an MPI communicator
263 
264  Comm<int> *c = problemComm_.getRawPtr();
265  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
266  if (mc){
267  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
268  mpiComm_ = (*wrappedComm.getRawPtr())();
269  }
270  else{
271  mpiComm_ = MPI_COMM_SELF; // or would this be an error?
272  }
273 
274 #endif
275 
276  // Determine which parameters are relevant here.
277  // For now, assume parameters similar to Zoltan:
278  // MODEL = graph, hypergraph, geometric, ids
279  // ALGORITHM = rcm, random, amd
280 
281  ModelType modelType = IdentifierModelType; //default, change later
282  string method = this->params_->template get<string>("order_method", "rcm");
283 
284  if ((method == string("rcm")) ||
285  (method == string("minimum_degree"))) {
286  modelType = GraphModelType;
287  }
288 
289  // Select Model based on parameters and InputAdapter type
290 
291  std::bitset<NUM_MODEL_FLAGS> graphFlags;
292  std::bitset<NUM_MODEL_FLAGS> idFlags;
293 
294  switch (modelType) {
295 
296  case GraphModelType:
297  graphFlags.set(SELF_EDGES_MUST_BE_REMOVED);
298  this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
299  this->baseInputAdapter_, this->envConst_, problemCommConst_, graphFlags));
300 
301  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
302  this->graphModel_);
303 
304  break;
305 
306 
307 
308  case IdentifierModelType:
309  this->identifierModel_ = rcp(new IdentifierModel<base_adapter_t>(
310  this->baseInputAdapter_, this->envConst_, problemCommConst_, idFlags));
311 
312  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
313  this->identifierModel_);
314 
315  break;
316 
317  case HypergraphModelType:
318  case CoordinateModelType:
319  cout << __func__ << " Model type " << modelType << " not yet supported."
320  << endl;
321  break;
322 
323  default:
324  cout << __func__ << " Invalid model" << modelType << endl;
325  break;
326  }
327 }
328 } //namespace Zoltan2
329 #endif
#define HELLO
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.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
OrderingProblem sets up ordering problems for the user.
OrderingSolution< gid_t, lno_t > * getSolution()
Get the solution to the problem.
OrderingProblem(Adapter *A, ParameterList *p)
Constructor that uses a default communicator.
Problem base class from which other classes (PartitioningProblem, ColoringProblem, OrderingProblem, MatchingProblem, etc.) derive.
Defines the Problem base class.
Adapter::base_adapter_t base_adapter_t
double scalar_t
GraphModel defines the interface required for graph models.
The base class for all model classes.
algorithm requires no self edges
IdentifierModel defines the interface for all identifier models.
Defines the GraphModel interface.
The class containing ordering solutions and metrics.