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 // 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_PROBLEM_HPP_
15 #define _ZOLTAN2_PROBLEM_HPP_
16 
17 #include <Zoltan2_Standards.hpp>
18 #include <Zoltan2_Algorithm.hpp>
19 #include <Zoltan2_TimerManager.hpp>
20 #include <Teuchos_StandardParameterEntryValidators.hpp>
21 #include <Teuchos_Tuple.hpp>
23 
24 namespace Zoltan2{
25 
28 // problem types.
29 
30 class ProblemRoot {
31  public:
32  virtual ~ProblemRoot() {} // required virtual declaration
33 
34  // could consider storing comm_ here...
35  // this accessor means we can get comm without template upcast first
36  virtual RCP<const Comm<int> > getComm() = 0;
37 
40  virtual void solve(bool updateInputData = true) = 0;
41 };
42 
46 
47 template<typename Adapter>
48 class Problem : public ProblemRoot {
49 public:
50 
53  Problem(const Adapter *input, ParameterList *params,
54  const RCP<const Comm<int> > &comm):
55  inputAdapter_(rcp(input,false)),
56  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input), false)),
57  algorithm_(),
58  params_(),
59  comm_(),
60  env_(rcp(new Environment(*params, comm))),
61  envConst_(rcp_const_cast<const Environment>(env_)),
62  timer_()
63  {
64  comm_ = comm->duplicate();
65  setupProblemEnvironment(params);
66  }
67 
70  virtual ~Problem() {};
71 
74  RCP<const Comm<int> > getComm() { return comm_; }
75 
78  void resetParameters(ParameterList *params);
79 
96 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
97  void printTimers() const {return;}
98 #else
99  void printTimers() const
100  {
101  if (!timer_.is_null())
102  timer_->printAndResetToZero();
103  }
104 #endif
105 
106  // Set up validators which are general to all probloems
107  static void getValidParameters(ParameterList & pl)
108  {
109  // bool parameter
110  pl.set("compute_metrics", false, "Compute metrics after computing solution",
112 
113  RCP<Teuchos::StringValidator> hypergraph_model_type_Validator =
114  Teuchos::rcp( new Teuchos::StringValidator(
115  Teuchos::tuple<std::string>( "traditional", "ghosting" )));
116  pl.set("hypergraph_model_type", "traditional", "construction type when "
117  "creating a hypergraph model", hypergraph_model_type_Validator);
118 
119  // bool parameter
120  pl.set("subset_graph", false, "If \"true\", the graph input is to be "
121  "subsetted. If a vertex neighbor is not a valid vertex, it will be "
122  "omitted from the pList. Otherwise, an invalid neighbor identifier "
123  "is considered an error.", Environment::getBoolValidator());
124 
125  RCP<Teuchos::StringValidator> symmetrize_input_Validator = Teuchos::rcp(
126  new Teuchos::StringValidator(
127  Teuchos::tuple<std::string>( "no", "transpose", "bipartite" )));
128  pl.set("symmetrize_input", "no", "Symmetrize input prior to pList. "
129  "If \"transpose\", symmetrize A by computing A plus ATranspose. "
130  "If \"bipartite\", A becomes [[0 A][ATranspose 0]].",
131  symmetrize_input_Validator);
132 
133  // these sublists are used for parameters which do not get validated
134  pl.sublist("zoltan_parameters");
135  pl.sublist("parma_parameters");
136  pl.sublist("sarma_parameters");
137  }
138 
142  const RCP<const Environment> & getEnvironment() const
143  {
144  return this->envConst_;
145  }
146 
147 protected:
148 
149  // The Problem is templated on the input adapter. We interact
150  // with the input adapter through the base class interface.
151  // The Model objects are also templated on the input adapter and
152  // are explicitly instantiated for each base input type (vector,
153  // graph, matrix, mesh, identifier list, and coordinate list).
154 
156 
157  RCP<const Adapter> inputAdapter_;
158  RCP<const base_adapter_t> baseInputAdapter_;
159 
160  // Every problem needs an algorithm, right?
161  RCP<Algorithm<Adapter> > algorithm_;
162 
163  RCP<ParameterList> params_;
164  RCP<const Comm<int> > comm_;
165 
166  // The Problem has a non const Environment object. This is because
167  // the Problem creates the Environment and may update it before
168  // finally calling the algorithm.
169 
170  RCP<Environment> env_;
171 
172  // The Problem needs a const version of the Environment. No other
173  // methods are permitted to change the Environment.
174 
175  RCP<const Environment> envConst_;
176 
177  // If the user requested timing, this is the TimerManager.
178 
179  RCP<TimerManager> timer_;
180 
181 private:
182  void setupProblemEnvironment(ParameterList *pl);
183 
184 };
185 
186 template <typename Adapter>
187  void Problem<Adapter>::setupProblemEnvironment(ParameterList * /* params */)
188 {
189  ParameterList &processedParameters = env_->getParametersNonConst();
190  params_ = rcp<ParameterList>(&processedParameters, false);
191 
192 #ifndef Z2_OMIT_ALL_PROFILING
193  ParameterList pl = *params_;
194 
195  // Give a timer to the Environment if requested.
196  bool haveType=false, haveStream=false, haveFile=false;
197  int choice = MACRO_TIMERS; // default timer type
198 
199  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
200 
201  if (pe){
202  choice = pe->getValue<int>(&choice);
203  haveType = true;
204  }
205 
206  TimerType tt = static_cast<TimerType>(choice);
207 
208  std::string fname;
209  pe = pl.getEntryPtr("timer_output_file");
210  if (pe){
211  haveFile = true;
212  fname = pe->getValue<std::string>(&fname);
213  std::ofstream *dbgFile = new std::ofstream;
214  if (comm_->getRank()==0){
215  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
216  try{
217  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
218  }
219  catch(std::exception &e){
220  throw std::runtime_error(e.what());
221  }
222  }
223  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
224  }
225  else{
226  choice = COUT_STREAM; // default output stream
227  pe = pl.getEntryPtr("timer_output_stream");
228  if (pe){
229  choice = pe->getValue<int>(&choice);
230  haveStream = true;
231  }
232 
233  OSType outputStream = static_cast<OSType>(choice);
234 
235  if (haveStream || haveType){
236  if (outputStream == COUT_STREAM)
237  timer_ = rcp(new TimerManager(comm_, &std::cout, tt));
238  else if (outputStream == CERR_STREAM)
239  timer_ = rcp(new TimerManager(comm_, &std::cerr, tt));
240  else if (outputStream == NULL_STREAM){
241  std::ofstream *of = NULL;
242  timer_ = rcp(new TimerManager(comm_, of, tt));
243  }
244  }
245  }
246 
247  if (haveType || haveStream || haveFile)
248  env_->setTimer(timer_);
249 
250 #endif
251 
252 }
253 
254 template <typename Adapter>
255  void Problem<Adapter>::resetParameters(ParameterList *params)
256 {
257  env_->resetParameters(*params);
258  setupProblemEnvironment(params);
259 
260  // We assume the timing output parameters have not changed,
261  // and carry on with the same timer.
262 
263  if (!timer_.is_null())
264  env_->setTimer(timer_);
265 }
266 
267 } // namespace Zoltan2
268 
269 #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.