Zoltan2
Zoltan2_MatchingProblem.hpp
Go to the documentation of this file.
1 #if 0
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Zoltan2: A package of combinatorial algorithms for scientific computing
7 // Copyright 2012 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Karen Devine (kddevin@sandia.gov)
40 // Erik Boman (egboman@sandia.gov)
41 // Siva Rajamanickam (srajama@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 
51 #ifndef _ZOLTAN2_MATCHINGPROBLEM_HPP_
52 #define _ZOLTAN2_MATCHINGPROBLEM_HPP_
53 
54 #include <Zoltan2_Standards.hpp>
55 
56 #include <Zoltan2_Problem.hpp>
57 #include <Zoltan2_MatchingAlgorithms.hpp>
59 
60 #include <Zoltan2_GraphModel.hpp>
61 #include <string>
62 
63 #include <bitset>
64 
65 using Teuchos::rcp_dynamic_cast;
66 
67 namespace Zoltan2{
68 
70 
90 template<typename Adapter>
91 class MatchingProblem : 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;
99  typedef typename Adapter::base_adapter_t base_adapter_t;
100 
101 #ifdef HAVE_ZOLTAN2_MPI
102  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
103 #endif
104 
107  virtual ~MatchingProblem() {};
108 
109 
110 #ifdef HAVE_ZOLTAN2_MPI
111 
113  MatchingProblem(Adapter *A, ParameterList *p, MPI_Comm comm)
114  : Problem<Adapter>(A, p, comm)
115  {
116  HELLO;
117  createMatchingProblem();
118  };
119 #endif
120 
123  MatchingProblem(Adapter *A, ParameterList *p) : Problem<Adapter>(A, p)
124  {
125  HELLO;
126  createMatchingProblem();
127  };
128 
130  //
131  // \param updateInputData If true this indicates that either
132  // this is the first attempt at solution, or that we
133  // are computing a new solution and the input data has
134  // changed since the previous solution was computed.
135  // If false, this indicates that we are computing a
136  // new solution using the same input data was used for
137  // the previous solution, even though the parameters
138  // may have been changed.
139  //
140  // For the sake of performance, we ask the caller to set \c updateInputData
141  // to false if he/she is computing a new solution using the same input data,
142  // but different problem parameters, than that which was used to compute
143  // the most recent solution.
144 
145  void solve(bool updateInputData=true);
146 
148  //
149  // \return a reference to the solution to the most recent solve().
150 
151  MatchingSolution<Adapter> *getSolution() {
152  // Get the raw ptr from the rcp
153  return solution_.getRawPtr();
154  };
155 
156 private:
157  void createMatchingProblem();
158 
159  RCP<MatchingSolution<Adapter> > solution_;
160 
161  RCP<Comm<int> > problemComm_;
162  RCP<const Comm<int> > problemCommConst_;
163 
164 #ifdef HAVE_ZOLTAN2_MPI
165  MPI_Comm mpiComm_;
166 #endif
167 };
168 
169 
171 template <typename Adapter>
172 void MatchingProblem<Adapter>::solve(bool newData)
173 {
174  HELLO;
175 
176  size_t nVtx = this->baseModel_->getLocalNumObjects();
177 
178  try
179  {
180  this->solution_ = rcp(new MatchingSolution<Adapter>(nVtx));
181  }
183 
184  // Determine which algorithm to use based on defaults and parameters.
185  // Need some exception handling here, too.
186 
187  std::string method = this->params_->template get<std::string>("color_method", "SerialGreedy");
188 
189  try
190  {
191  // TODO: Ignore case
192  if (method.compare("SerialGreedy") == 0)
193  {
194  AlgSerialGreedy<Adapter> alg(this->graphModel_, this->params_,
195  this->env_, problemComm_);
196  alg.color(this->solution_);
197  }
198 #if 0 // TODO later
199  else if (method.compare("speculative") == 0) // Gebremedhin-Manne
200  {
201  AlgGM<base_adapter_t> alg(this->graphModel_, problemComm_);
202  alg.color(this->solution_, this->params_);
203  }
204 #endif
205  }
207 
208 }
209 
211 //template <typename Adapter>
212 //void MatchingProblem<Adapter>::redistribute()
213 //{
214 // HELLO;
215 //}
216 
219 // Method with common functionality for creating a MatchingProblem.
220 // Individual constructors do appropriate conversions of input, etc.
221 // This method does everything that all constructors must do.
222 
223 template <typename Adapter>
224 void MatchingProblem<Adapter>::createMatchingProblem()
225 {
226  HELLO;
227  using Teuchos::ParameterList;
228 
229 // cout << __func__zoltan2__ << " input adapter type "
230 // << this->inputAdapter_->inputAdapterType() << " "
231 // << this->inputAdapter_->inputAdapterName() << endl;
232 
233  // Create a copy of the user's communicator.
234 
235  problemComm_ = this->comm_->duplicate();
236  problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
237 
238 
239 #ifdef HAVE_ZOLTAN2_MPI
240 
241  // TPLs may want an MPI communicator
242 
243  Comm<int> *c = problemComm_.getRawPtr();
244  Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
245  if (mc){
246  RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
247  mpiComm_ = (*wrappedComm.getRawPtr())();
248  }
249  else{
250  mpiComm_ = MPI_COMM_SELF; // or would this be an error?
251  }
252 
253 #endif
254 
255  // Only graph model supported.
256  // TODO: Allow hypergraph later?
257 
258  ModelType modelType = GraphModelType;
259 
260  // Select Model based on parameters and InputAdapter type
261 
262  std::bitset<NUM_MODEL_FLAGS> graphFlags;
263  std::bitset<NUM_MODEL_FLAGS> idFlags;
264 
265  switch (modelType) {
266 
267  case GraphModelType:
268  graphFlags.set(REMOVE_SELF_EDGES);
269  graphFlags.set(BUILD_LOCAL_GRAPH);
270  this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
271  this->baseInputAdapter_, this->envConst_, problemCommConst_, graphFlags));
272 
273  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
274  this->graphModel_);
275 
276  break;
277 
278 
279  case IdentifierModelType:
280  case HypergraphModelType:
281  case CoordinateModelType:
282  cout << __func__zoltan2__ << " Model type " << modelType << " not yet supported."
283  << endl;
284  break;
285 
286  default:
287  cout << __func__zoltan2__ << " Invalid model" << modelType << endl;
288  break;
289  }
290 }
291 } //namespace Zoltan2
292 
293 #endif
294 #endif
#define HELLO
ModelType
An identifier for the general type of model.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
algorithm requires no self edges
Defines the Problem base class.
Gathering definitions used in software development.
Defines the GraphModel interface.
model represents graph within only one rank
#define __func__zoltan2__