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 };
165 
166 
168 template <typename Adapter>
169 void MatchingProblem<Adapter>::solve(bool newData)
170 {
171  HELLO;
172 
173  size_t nVtx = this->baseModel_->getLocalNumObjects();
174 
175  try
176  {
177  this->solution_ = rcp(new MatchingSolution<Adapter>(nVtx));
178  }
180 
181  // Determine which algorithm to use based on defaults and parameters.
182  // Need some exception handling here, too.
183 
184  std::string method = this->params_->template get<std::string>("color_method", "SerialGreedy");
185 
186  try
187  {
188  // TODO: Ignore case
189  if (method.compare("SerialGreedy") == 0)
190  {
191  AlgSerialGreedy<Adapter> alg(this->graphModel_, this->params_,
192  this->env_, problemComm_);
193  alg.color(this->solution_);
194  }
195 #if 0 // TODO later
196  else if (method.compare("speculative") == 0) // Gebremedhin-Manne
197  {
198  AlgGM<base_adapter_t> alg(this->graphModel_, problemComm_);
199  alg.color(this->solution_, this->params_);
200  }
201 #endif
202  }
204 
205 }
206 
208 //template <typename Adapter>
209 //void MatchingProblem<Adapter>::redistribute()
210 //{
211 // HELLO;
212 //}
213 
216 // Method with common functionality for creating a MatchingProblem.
217 // Individual constructors do appropriate conversions of input, etc.
218 // This method does everything that all constructors must do.
219 
220 template <typename Adapter>
221 void MatchingProblem<Adapter>::createMatchingProblem()
222 {
223  HELLO;
224  using Teuchos::ParameterList;
225 
226 // cout << __func__zoltan2__ << " input adapter type "
227 // << this->inputAdapter_->inputAdapterType() << " "
228 // << this->inputAdapter_->inputAdapterName() << endl;
229 
230  // Create a copy of the user's communicator.
231 
232  problemComm_ = this->comm_->duplicate();
233  problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
234 
235  // Only graph model supported.
236  // TODO: Allow hypergraph later?
237 
238  ModelType modelType = GraphModelType;
239 
240  // Select Model based on parameters and InputAdapter type
241 
242  std::bitset<NUM_MODEL_FLAGS> graphFlags;
243  std::bitset<NUM_MODEL_FLAGS> idFlags;
244 
245  switch (modelType) {
246 
247  case GraphModelType:
248  graphFlags.set(REMOVE_SELF_EDGES);
249  graphFlags.set(BUILD_LOCAL_GRAPH);
250  this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
251  this->baseInputAdapter_, this->envConst_, problemCommConst_, graphFlags));
252 
253  this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
254  this->graphModel_);
255 
256  break;
257 
258 
259  case IdentifierModelType:
260  case HypergraphModelType:
261  case CoordinateModelType:
262  cout << __func__zoltan2__ << " Model type " << modelType << " not yet supported."
263  << endl;
264  break;
265 
266  default:
267  cout << __func__zoltan2__ << " Invalid model" << modelType << endl;
268  break;
269  }
270 }
271 } //namespace Zoltan2
272 
273 #endif
274 #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__