Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_Algorithm.hpp>
55 #include <Zoltan2_TimerManager.hpp>
56 #include <Teuchos_StandardParameterEntryValidators.hpp>
57 #include <Teuchos_Tuple.hpp>
59 
60 namespace Zoltan2{
61 
64 // problem types.
65 
66 class ProblemRoot {
67  public:
68  virtual ~ProblemRoot() {} // required virtual declaration
69 
70  // could consider storing comm_ here...
71  // this accessor means we can get comm without template upcast first
72  virtual RCP<const Comm<int> > getComm() = 0;
73 
76  virtual void solve(bool updateInputData = true) = 0;
77 };
78 
82 
83 template<typename Adapter>
84 class Problem : public ProblemRoot {
85 public:
86 
89  Problem(const Adapter *input, ParameterList *params,
90  const RCP<const Comm<int> > &comm):
91  inputAdapter_(rcp(input,false)),
92  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input), false)),
93  algorithm_(),
94  params_(),
95  comm_(),
96  env_(rcp(new Environment(*params, comm))),
97  envConst_(rcp_const_cast<const Environment>(env_)),
98  timer_()
99  {
100  comm_ = comm->duplicate();
101  setupProblemEnvironment(params);
102  }
103 
106  virtual ~Problem() {};
107 
110  RCP<const Comm<int> > getComm() { return comm_; }
111 
114  void resetParameters(ParameterList *params);
115 
132 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
133  void printTimers() const {return;}
134 #else
135  void printTimers() const
136  {
137  if (!timer_.is_null())
138  timer_->printAndResetToZero();
139  }
140 #endif
141 
142  // Set up validators which are general to all probloems
143  static void getValidParameters(ParameterList & pl)
144  {
145  // bool parameter
146  pl.set("compute_metrics", false, "Compute metrics after computing solution",
148 
149  RCP<Teuchos::StringValidator> hypergraph_model_type_Validator =
150  Teuchos::rcp( new Teuchos::StringValidator(
151  Teuchos::tuple<std::string>( "traditional", "ghosting" )));
152  pl.set("hypergraph_model_type", "traditional", "construction type when "
153  "creating a hypergraph model", hypergraph_model_type_Validator);
154 
155  // bool parameter
156  pl.set("subset_graph", false, "If \"true\", the graph input is to be "
157  "subsetted. If a vertex neighbor is not a valid vertex, it will be "
158  "omitted from the pList. Otherwise, an invalid neighbor identifier "
159  "is considered an error.", Environment::getBoolValidator());
160 
161  RCP<Teuchos::StringValidator> symmetrize_input_Validator = Teuchos::rcp(
162  new Teuchos::StringValidator(
163  Teuchos::tuple<std::string>( "no", "transpose", "bipartite" )));
164  pl.set("symmetrize_input", "no", "Symmetrize input prior to pList. "
165  "If \"transpose\", symmetrize A by computing A plus ATranspose. "
166  "If \"bipartite\", A becomes [[0 A][ATranspose 0]].",
167  symmetrize_input_Validator);
168 
169  // these sublists are used for parameters which do not get validated
170  pl.sublist("zoltan_parameters");
171  pl.sublist("parma_parameters");
172  pl.sublist("sarma_parameters");
173  }
174 
178  const RCP<const Environment> & getEnvironment() const
179  {
180  return this->envConst_;
181  }
182 
183 protected:
184 
185  // The Problem is templated on the input adapter. We interact
186  // with the input adapter through the base class interface.
187  // The Model objects are also templated on the input adapter and
188  // are explicitly instantiated for each base input type (vector,
189  // graph, matrix, mesh, identifier list, and coordinate list).
190 
192 
193  RCP<const Adapter> inputAdapter_;
194  RCP<const base_adapter_t> baseInputAdapter_;
195 
196  // Every problem needs an algorithm, right?
197  RCP<Algorithm<Adapter> > algorithm_;
198 
199  RCP<ParameterList> params_;
200  RCP<const Comm<int> > comm_;
201 
202  // The Problem has a non const Environment object. This is because
203  // the Problem creates the Environment and may update it before
204  // finally calling the algorithm.
205 
206  RCP<Environment> env_;
207 
208  // The Problem needs a const version of the Environment. No other
209  // methods are permitted to change the Environment.
210 
211  RCP<const Environment> envConst_;
212 
213  // If the user requested timing, this is the TimerManager.
214 
215  RCP<TimerManager> timer_;
216 
217 private:
218  void setupProblemEnvironment(ParameterList *pl);
219 
220 };
221 
222 template <typename Adapter>
223  void Problem<Adapter>::setupProblemEnvironment(ParameterList * /* params */)
224 {
225  ParameterList &processedParameters = env_->getParametersNonConst();
226  params_ = rcp<ParameterList>(&processedParameters, false);
227 
228 #ifndef Z2_OMIT_ALL_PROFILING
229  ParameterList pl = *params_;
230 
231  // Give a timer to the Environment if requested.
232  bool haveType=false, haveStream=false, haveFile=false;
233  int choice = MACRO_TIMERS; // default timer type
234 
235  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
236 
237  if (pe){
238  choice = pe->getValue<int>(&choice);
239  haveType = true;
240  }
241 
242  TimerType tt = static_cast<TimerType>(choice);
243 
244  std::string fname;
245  pe = pl.getEntryPtr("timer_output_file");
246  if (pe){
247  haveFile = true;
248  fname = pe->getValue<std::string>(&fname);
249  std::ofstream *dbgFile = new std::ofstream;
250  if (comm_->getRank()==0){
251  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
252  try{
253  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
254  }
255  catch(std::exception &e){
256  throw std::runtime_error(e.what());
257  }
258  }
259  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
260  }
261  else{
262  choice = COUT_STREAM; // default output stream
263  pe = pl.getEntryPtr("timer_output_stream");
264  if (pe){
265  choice = pe->getValue<int>(&choice);
266  haveStream = true;
267  }
268 
269  OSType outputStream = static_cast<OSType>(choice);
270 
271  if (haveStream || haveType){
272  if (outputStream == COUT_STREAM)
273  timer_ = rcp(new TimerManager(comm_, &std::cout, tt));
274  else if (outputStream == CERR_STREAM)
275  timer_ = rcp(new TimerManager(comm_, &std::cerr, tt));
276  else if (outputStream == NULL_STREAM){
277  std::ofstream *of = NULL;
278  timer_ = rcp(new TimerManager(comm_, of, tt));
279  }
280  }
281  }
282 
283  if (haveType || haveStream || haveFile)
284  env_->setTimer(timer_);
285 
286 #endif
287 
288 }
289 
290 template <typename Adapter>
291  void Problem<Adapter>::resetParameters(ParameterList *params)
292 {
293  env_->resetParameters(*params);
294  setupProblemEnvironment(params);
295 
296  // We assume the timing output parameters have not changed,
297  // and carry on with the same timer.
298 
299  if (!timer_.is_null())
300  env_->setTimer(timer_);
301 }
302 
303 } // namespace Zoltan2
304 
305 #endif
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Time an algorithm (or other entity) as a whole.
RCP< const base_adapter_t > baseInputAdapter_
Adapter::base_adapter_t base_adapter_t
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.
ProblemRoot allows ptr storage and safe dynamic_cast of all.
RCP< const Comm< int > > comm_
void resetParameters(ParameterList *params)
Reset the list of parameters.
virtual RCP< const Comm< int > > getComm()=0
virtual void solve(bool updateInputData=true)=0
Method that creates a solution.
list fname
Begin.
Definition: validXML.py:19
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_
void printTimers() const
Return the communicator passed to the problem.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
RCP< TimerManager > timer_
Define IntegerRangeList validator.
RCP< Environment > env_
Gathering definitions used in software development.
RCP< ParameterList > params_
const RCP< const Environment > & getEnvironment() const
Get the current Environment. Useful for testing.
RCP< const Environment > envConst_
Declarations for TimerManager.