Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_ColoringProblem.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #ifndef _ZOLTAN2_COLORINGPROBLEM_HPP_
15 #define _ZOLTAN2_COLORINGPROBLEM_HPP_
16 
17 #include <Zoltan2_Standards.hpp>
18 
19 #include <Zoltan2_Problem.hpp>
22 
23 #include <Zoltan2_GraphModel.hpp>
24 #include <string>
25 
26 #include <bitset>
27 
28 namespace Zoltan2{
29 
31 
51 template<typename Adapter>
52 class ColoringProblem : public Problem<Adapter>
53 {
54 public:
55 
56  typedef typename Adapter::scalar_t scalar_t;
57  typedef typename Adapter::gno_t gno_t;
58  typedef typename Adapter::lno_t lno_t;
59  typedef typename Adapter::user_t user_t;
61 
62 #ifdef HAVE_ZOLTAN2_MPI
63  typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
64 #endif
65 
68  virtual ~ColoringProblem() {};
69 
72  ColoringProblem(Adapter *A, ParameterList *p,
73  const Teuchos::RCP<const Teuchos::Comm<int> > &comm) :
74  Problem<Adapter>(A, p, comm)
75  {
76  HELLO;
77  createColoringProblem();
78  };
79 
80 #ifdef HAVE_ZOLTAN2_MPI
81 
83  ColoringProblem(Adapter *A, ParameterList *p, MPI_Comm mpicomm) :
84  ColoringProblem(A, p,
85  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(
86  Teuchos::opaqueWrapper(mpicomm))))
87  {}
88 #endif
89 
92  ColoringProblem(Adapter *A, ParameterList *p) :
93  ColoringProblem(A, p, Tpetra::getDefaultComm())
94  {}
95 
98  static void getValidParameters(ParameterList & pl)
99  {
100  RCP<Teuchos::StringValidator> color_method_Validator = Teuchos::rcp(
101  new Teuchos::StringValidator(
102  Teuchos::tuple<std::string>( "SerialGreedy","D1","D1-2GL","D2","PD2" )));
103  pl.set("color_method", "SerialGreedy", "coloring algorithm",
104  color_method_Validator);
105  pl.set("verbose", false, "print all output", Environment::getBoolValidator());
106  pl.set("timing", false, "print timing data", Environment::getBoolValidator());
107  pl.set("serial_threshold",0,"vertices to recolor in serial",Environment::getAnyIntValidator());
108  pl.set("recolor_degrees",true,"recolor based on vertex degrees",Environment::getBoolValidator());
109  }
110 
112  //
113  // \param updateInputData If true this indicates that either
114  // this is the first attempt at solution, or that we
115  // are computing a new solution and the input data has
116  // changed since the previous solution was computed.
117  // If false, this indicates that we are computing a
118  // new solution using the same input data was used for
119  // the previous solution, even though the parameters
120  // may have been changed.
121  //
122  // For the sake of performance, we ask the caller to set \c updateInputData
123  // to false if he/she is computing a new solution using the same input data,
124  // but different problem parameters, than that which was used to compute
125  // the most recent solution.
126 
127  void solve(bool updateInputData=true);
128 
130  //
131  // \return a reference to the solution to the most recent solve().
132 
134  // Get the raw ptr from the rcp
135  return solution_.getRawPtr();
136  };
137 
138 private:
139  void createColoringProblem();
140 
141  RCP<ColoringSolution<Adapter> > solution_;
142  size_t localNumObjects_;
143 };
144 
145 
147 template <typename Adapter>
149 {
150  HELLO;
151 
152  try
153  {
154  this->solution_ = rcp(new ColoringSolution<Adapter>(localNumObjects_));
155  }
157 
158  // Determine which algorithm to use based on defaults and parameters.
159  // Need some exception handling here, too.
160 
161  std::string method = this->params_->template get<std::string>("color_method", "SerialGreedy");
162 
163  try
164  {
165  // TODO: Ignore case
166  if (method.compare("SerialGreedy") == 0)
167  {
168  modelFlag_t graphFlags;
169  graphFlags.set(REMOVE_SELF_EDGES);
170  graphFlags.set(BUILD_LOCAL_GRAPH);
171 
172  AlgSerialGreedy<Adapter> alg(this->inputAdapter_, this->params_,
173  this->env_, this->comm_, graphFlags);
174  alg.color(this->solution_);
175  }
176  else if (method.compare("D1") == 0)
177  {
178  AlgDistance1<Adapter> alg(this->inputAdapter_, this->params_,
179  this->env_, this->comm_);
180  alg.color(this->solution_);
181  }
182  else if (method.compare("D1-2GL") == 0)
183  {
184  AlgDistance1TwoGhostLayer<Adapter> alg(this->inputAdapter_,this->params_,
185  this->env_, this->comm_);
186  alg.color(this->solution_);
187  } else if(method.compare("D2") == 0)
188  {
189  AlgDistance2<Adapter> alg(this->inputAdapter_, this->params_,
190  this->env_, this->comm_);
191  alg.color(this->solution_);
192  } else if (method.compare("PD2") == 0)
193  {
194  AlgPartialDistance2<Adapter> alg(this->inputAdapter_, this->params_,
195  this->env_, this->comm_);
196  alg.color(this->solution_);
197  }
198  }
200 }
201 
203 //template <typename Adapter>
204 //void ColoringProblem<Adapter>::redistribute()
205 //{
206 // HELLO;
207 //}
208 
211 // Method with common functionality for creating a ColoringProblem.
212 // Individual constructors do appropriate conversions of input, etc.
213 // This method does everything that all constructors must do.
214 
215 template <typename Adapter>
217 {
218  HELLO;
219  using Teuchos::ParameterList;
220 
221 // std::cout << __func__zoltan2__ << " input adapter type "
222 // << this->inputAdapter_->inputAdapterType() << " "
223 // << this->inputAdapter_->inputAdapterName() << std::endl;
224 
225  // Create a copy of the user's communicator.
226 
227  // Only graph model supported.
228  // TODO: Allow hypergraph later?
229 
230  ModelType modelType = GraphModelType;
231  const auto adapterType = this->baseInputAdapter_->adapterType();
232 
233  // Select Model based on parameters and InputAdapter type
234 
235  switch (modelType)
236  {
237 
238  case GraphModelType:
239  {
240  switch (adapterType)
241  {
242  case MatrixAdapterType:
243  {
244  localNumObjects_ = this->baseInputAdapter_->getLocalNumIDs();
245  }
246  break;
247 
248  case GraphAdapterType:
249  {
250  const auto ia = dynamic_cast<const GraphAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
251  localNumObjects_ = ia->getLocalNumVertices();
252  }
253  break;
254 
255  case MeshAdapterType:
256  {
257  const auto ia = dynamic_cast<const MeshAdapter<user_t> *>(&(*(this->baseInputAdapter_)));
258  localNumObjects_ = ia->getLocalNumOf(ia->getPrimaryEntityType());
259  }
260  break;
261 
262  default:
263  {
264  // Avoid warning
265  }
266  }
267  }
268  break;
269 
270  case IdentifierModelType:
271  case HypergraphModelType:
272  case CoordinateModelType:
273  std::cout << __func__zoltan2__ << " Model type " << modelType
274  << " not yet supported." << std::endl;
275  break;
276 
277  default:
278  std::cout << __func__zoltan2__ << " Invalid model" << modelType
279  << std::endl;
280  break;
281  }
282 }
283 } //namespace Zoltan2
284 
285 #endif
#define HELLO
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
ColoringProblem(Adapter *A, ParameterList *p, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Constructor that uses a Teuchos::Comm.
ColoringProblem sets up coloring problems for the user.
ModelType
An identifier for the general type of model.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
static RCP< Teuchos::BoolParameterEntryValidator > getBoolValidator()
Exists to make setting up validators less cluttered.
void solve(bool updateInputData=true)
Direct the problem to create a solution.
ColoringSolution< Adapter > * getSolution()
Get the solution to the problem.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
Adapter::base_adapter_t base_adapter_t
void color(const RCP< ColoringSolution< Adapter > > &solution)
Coloring method.
algorithm requires no self edges
void color(const RCP< ColoringSolution< Adapter > > &solution)
Coloring method.
void color(const RCP< ColoringSolution< Adapter > > &solution)
Coloring method.
Problem base class from which other classes (PartitioningProblem, ColoringProblem, OrderingProblem, MatchingProblem, etc.) derive.
Defines the Problem base class.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
static RCP< Teuchos::AnyNumberParameterEntryValidator > getAnyIntValidator()
Exists to make setting up validators less cluttered.
static void getValidParameters(ParameterList &pl)
Set up validators specific to this Problem.
Gathering definitions used in software development.
Defines the ColoringSolution class.
ColoringProblem(Adapter *A, ParameterList *p)
Constructor that uses a default communicator.
Defines the GraphModel interface.
model represents graph within only one rank
#define __func__zoltan2__
virtual ~ColoringProblem()
Destructor.
The class containing coloring solution.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39