Zoltan2
Zoltan2_Problem.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_PROBLEM_HPP_
51 #define _ZOLTAN2_PROBLEM_HPP_
52 
53 #include <Zoltan2_Standards.hpp>
54 #include <Zoltan2_GraphModel.hpp>
57 #include <Zoltan2_Algorithm.hpp>
58 #include <Zoltan2_TimerManager.hpp>
59 #include <Teuchos_StandardParameterEntryValidators.hpp>
60 #include <Teuchos_Tuple.hpp>
62 
63 namespace Zoltan2{
64 
68 
69 template<typename Adapter>
70 class Problem {
71 public:
72 
75  Problem(const Adapter *input, ParameterList *params):
76  inputAdapter_(rcp(input,false)),
77  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
78  false)),
80  params_(), comm_(), env_(), envConst_(), timer_()
81  {
82  RCP<const Comm<int> > tmp = DefaultComm<int>::getComm();
83  comm_ = tmp->duplicate();
84  setupProblemEnvironment(params);
85  }
86 
87 
90  Problem(const Adapter *input, ParameterList *params,
91  const RCP<const Comm<int> > &comm):
92  inputAdapter_(rcp(input,false)),
93  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
94  false)),
96  params_(), comm_(), env_(), envConst_(), timer_()
97  {
98  comm_ = comm->duplicate();
99  setupProblemEnvironment(params);
100  }
101 
102 #ifdef HAVE_ZOLTAN2_MPI
103 
105  Problem(const Adapter *input, ParameterList *params, MPI_Comm comm):
106  inputAdapter_(rcp(input,false)),
107  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
108  false)),
110  params_(), comm_(), env_(), envConst_(), timer_()
111  {
112  RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
113  Teuchos::opaqueWrapper(comm);
114  RCP<const Comm<int> > tmp =
115  rcp<const Comm<int> >(new Teuchos::MpiComm<int>(wrapper));
116  comm_ = tmp->duplicate();
117  setupProblemEnvironment(params);
118  }
119 #endif
120 
123  virtual ~Problem() {};
124 
127  RCP<const Comm<int> > getComm() { return comm_; }
128 
131  void resetParameters(ParameterList *params);
132 
135  virtual void solve(bool updateInputData) = 0;
136 
153 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
154  void printTimers() const {return;}
155 #else
156  void printTimers() const
157  {
158  if (!timer_.is_null())
159  timer_->printAndResetToZero();
160  }
161 #endif
162 
163  // Set up validators which are general to all probloems
164  static void getValidParameters(ParameterList & pl)
165  {
166  // bool parameter
167  pl.set("compute_metrics", false, "Compute metrics after computing solution",
169 
170  RCP<Teuchos::StringValidator> hypergraph_model_type_Validator =
171  Teuchos::rcp( new Teuchos::StringValidator(
172  Teuchos::tuple<std::string>( "traditional", "ghosting" )));
173  pl.set("hypergraph_model_type", "traditional", "construction type when "
174  "creating a hypergraph model", hypergraph_model_type_Validator);
175 
176  // bool parameter
177  pl.set("subset_graph", false, "If \"true\", the graph input is to be "
178  "subsetted. If a vertex neighbor is not a valid vertex, it will be "
179  "omitted from the pList. Otherwise, an invalid neighbor identifier "
180  "is considered an error.", Environment::getBoolValidator());
181 
182  RCP<Teuchos::StringValidator> symmetrize_input_Validator = Teuchos::rcp(
183  new Teuchos::StringValidator(
184  Teuchos::tuple<std::string>( "no", "transpose", "bipartite" )));
185  pl.set("symmetrize_input", "no", "Symmetrize input prior to pList. "
186  "If \"transpose\", symmetrize A by computing A plus ATranspose. "
187  "If \"bipartite\", A becomes [[0 A][ATranspose 0]].",
188  symmetrize_input_Validator);
189 
190  // these sublists are used for parameters which do not get validated
191  pl.sublist("zoltan_parameters");
192  pl.sublist("parma_parameters");
193  }
194 
198  const RCP<const Environment> & getEnvironment() const
199  {
200  return this->envConst_;
201  }
202 
203 protected:
204 
205  // The Problem is templated on the input adapter. We interact
206  // with the input adapter through the base class interface.
207  // The Model objects are also templated on the input adapter and
208  // are explicitly instantiated for each base input type (vector,
209  // graph, matrix, mesh, identifier list, and coordinate list).
210 
212 
213  RCP<const Adapter> inputAdapter_;
214  RCP<const base_adapter_t> baseInputAdapter_;
215 
216  RCP<GraphModel<base_adapter_t> > graphModel_;
217  RCP<IdentifierModel<base_adapter_t> > identifierModel_;
218  RCP<CoordinateModel<base_adapter_t> > coordinateModel_;
219 
220  // Algorithms are passed a base model class, and query
221  // the model through the base class interface (graph, hypergraph,
222  // identifiers, or coordinates).
223 
224  RCP<const Model<base_adapter_t> > baseModel_;
225 
226  // Every problem needs an algorithm, right?
227  RCP<Algorithm<Adapter> > algorithm_;
228 
229  RCP<ParameterList> params_;
230  RCP<const Comm<int> > comm_;
231 
232  // The Problem has a non const Environment object. This is because
233  // the Problem creates the Environment and may update it before
234  // finally calling the algorithm.
235 
236  RCP<Environment> env_;
237 
238  // The Problem needs a const version of the Environment. No other
239  // methods are permitted to change the Environment.
240 
241  RCP<const Environment> envConst_;
242 
243  // If the user requested timing, this is the TimerManager.
244 
245  RCP<TimerManager> timer_;
246 
247 private:
248  void setupProblemEnvironment(ParameterList *pl);
249 
250 };
251 
252 template <typename Adapter>
253  void Problem<Adapter>::setupProblemEnvironment(ParameterList *params)
254 {
255  try{
256  env_ = rcp(new Environment(*params, Teuchos::DefaultComm<int>::getComm()));
257  }
259 
260  envConst_ = rcp_const_cast<const Environment>(env_);
261 
262  ParameterList &processedParameters = env_->getParametersNonConst();
263  params_ = rcp<ParameterList>(&processedParameters, false);
264 
265 #ifndef Z2_OMIT_ALL_PROFILING
266  ParameterList pl = *params_;
267 
268  // Give a timer to the Environment if requested.
269  bool haveType=false, haveStream=false, haveFile=false;
270  int choice = MACRO_TIMERS; // default timer type
271 
272  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
273 
274  if (pe){
275  choice = pe->getValue<int>(&choice);
276  haveType = true;
277  }
278 
279  TimerType tt = static_cast<TimerType>(choice);
280 
281  std::string fname;
282  pe = pl.getEntryPtr("timer_output_file");
283  if (pe){
284  haveFile = true;
285  fname = pe->getValue<std::string>(&fname);
286  std::ofstream *dbgFile = new std::ofstream;
287  if (comm_->getRank()==0){
288  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
289  try{
290  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
291  }
292  catch(std::exception &e){
293  throw std::runtime_error(e.what());
294  }
295  }
296  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
297  }
298  else{
299  choice = COUT_STREAM; // default output stream
300  pe = pl.getEntryPtr("timer_output_stream");
301  if (pe){
302  choice = pe->getValue<int>(&choice);
303  haveStream = true;
304  }
305 
306  OSType outputStream = static_cast<OSType>(choice);
307 
308  if (haveStream || haveType){
309  if (outputStream == COUT_STREAM)
310  timer_ = rcp(new TimerManager(comm_, &std::cout, tt));
311  else if (outputStream == CERR_STREAM)
312  timer_ = rcp(new TimerManager(comm_, &std::cerr, tt));
313  else if (outputStream == NULL_STREAM){
314  std::ofstream *of = NULL;
315  timer_ = rcp(new TimerManager(comm_, of, tt));
316  }
317  }
318  }
319 
320  if (haveType || haveStream || haveFile)
321  env_->setTimer(timer_);
322 
323 #endif
324 
325 }
326 
327 template <typename Adapter>
328  void Problem<Adapter>::resetParameters(ParameterList *params)
329 {
330  setupProblemEnvironment(params);
331 
332  // We assume the timing output parameters have not changed,
333  // and carry on with the same timer.
334 
335  if (!timer_.is_null())
336  env_->setTimer(timer_);
337 }
338 
339 } // namespace Zoltan2
340 
341 #endif
RCP< GraphModel< base_adapter_t > > graphModel_
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Time an algorithm (or other entity) as a whole.
RCP< const base_adapter_t > baseInputAdapter_
virtual void solve(bool updateInputData)=0
Method that creates a solution.
Problem(const Adapter *input, ParameterList *params)
Constructor where communicator is Teuchos default.
Adapter::base_adapter_t base_adapter_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
static RCP< Teuchos::BoolParameterEntryValidator > getBoolValidator()
Exists to make setting up validators less cluttered.
virtual ~Problem()
Destructor.
TimerType
The type of timers which should be active.
RCP< Algorithm< Adapter > > algorithm_
/dev/null: do actions but don&#39;t output results
Problem(const Adapter *input, ParameterList *params, const RCP< const Comm< int > > &comm)
Constructor where Teuchos communicator is specified.
RCP< const Comm< int > > comm_
void resetParameters(ParameterList *params)
Reset the list of parameters.
const RCP< const Environment > & getEnvironment() const
Get the current Environment. Useful for testing.
RCP< IdentifierModel< base_adapter_t > > identifierModel_
Defines the IdentifierModel interface.
OSType
Output stream types.
static void getValidParameters(ParameterList &pl)
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.
RCP< const Adapter > inputAdapter_
RCP< CoordinateModel< base_adapter_t > > coordinateModel_
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void printTimers() const
Return the communicator passed to the problem.
RCP< TimerManager > timer_
Define IntegerRangeList validator.
Defines the CoordinateModel classes.
RCP< Environment > env_
Gathering definitions used in software development.
RCP< ParameterList > params_
Defines the GraphModel interface.
RCP< const Model< base_adapter_t > > baseModel_
RCP< const Environment > envConst_
Declarations for TimerManager.