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 
67 // problem types.
68 
69 class ProblemRoot {
70  public:
71  virtual ~ProblemRoot() {} // required virtual declaration
72 
73  // could consider storing comm_ here...
74  // this accessor means we can get comm without template upcast first
75  virtual RCP<const Comm<int> > getComm() = 0;
76 
79  virtual void solve(bool updateInputData = true) = 0;
80 };
81 
85 
86 template<typename Adapter>
87 class Problem : public ProblemRoot {
88 public:
89 
92  Problem(const Adapter *input, ParameterList *params,
93  const RCP<const Comm<int> > &comm):
94  inputAdapter_(rcp(input,false)),
95  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input), false)),
96  graphModel_(),
98  baseModel_(),
99  algorithm_(),
100  params_(),
101  comm_(),
102  env_(rcp(new Environment(*params, comm))),
103  envConst_(rcp_const_cast<const Environment>(env_)),
104  timer_()
105  {
106  comm_ = comm->duplicate();
107  setupProblemEnvironment(params);
108  }
109 
112  virtual ~Problem() {};
113 
116  RCP<const Comm<int> > getComm() { return comm_; }
117 
120  void resetParameters(ParameterList *params);
121 
138 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
139  void printTimers() const {return;}
140 #else
141  void printTimers() const
142  {
143  if (!timer_.is_null())
144  timer_->printAndResetToZero();
145  }
146 #endif
147 
148  // Set up validators which are general to all probloems
149  static void getValidParameters(ParameterList & pl)
150  {
151  // bool parameter
152  pl.set("compute_metrics", false, "Compute metrics after computing solution",
154 
155  RCP<Teuchos::StringValidator> hypergraph_model_type_Validator =
156  Teuchos::rcp( new Teuchos::StringValidator(
157  Teuchos::tuple<std::string>( "traditional", "ghosting" )));
158  pl.set("hypergraph_model_type", "traditional", "construction type when "
159  "creating a hypergraph model", hypergraph_model_type_Validator);
160 
161  // bool parameter
162  pl.set("subset_graph", false, "If \"true\", the graph input is to be "
163  "subsetted. If a vertex neighbor is not a valid vertex, it will be "
164  "omitted from the pList. Otherwise, an invalid neighbor identifier "
165  "is considered an error.", Environment::getBoolValidator());
166 
167  RCP<Teuchos::StringValidator> symmetrize_input_Validator = Teuchos::rcp(
168  new Teuchos::StringValidator(
169  Teuchos::tuple<std::string>( "no", "transpose", "bipartite" )));
170  pl.set("symmetrize_input", "no", "Symmetrize input prior to pList. "
171  "If \"transpose\", symmetrize A by computing A plus ATranspose. "
172  "If \"bipartite\", A becomes [[0 A][ATranspose 0]].",
173  symmetrize_input_Validator);
174 
175  // these sublists are used for parameters which do not get validated
176  pl.sublist("zoltan_parameters");
177  pl.sublist("parma_parameters");
178  pl.sublist("sarma_parameters");
179  }
180 
184  const RCP<const Environment> & getEnvironment() const
185  {
186  return this->envConst_;
187  }
188 
189 protected:
190 
191  // The Problem is templated on the input adapter. We interact
192  // with the input adapter through the base class interface.
193  // The Model objects are also templated on the input adapter and
194  // are explicitly instantiated for each base input type (vector,
195  // graph, matrix, mesh, identifier list, and coordinate list).
196 
198 
199  RCP<const Adapter> inputAdapter_;
200  RCP<const base_adapter_t> baseInputAdapter_;
201 
202  RCP<GraphModel<base_adapter_t> > graphModel_;
203  RCP<IdentifierModel<base_adapter_t> > identifierModel_;
204  RCP<CoordinateModel<base_adapter_t> > coordinateModel_;
205 
206  // Algorithms are passed a base model class, and query
207  // the model through the base class interface (graph, hypergraph,
208  // identifiers, or coordinates).
209 
210  RCP<const Model<base_adapter_t> > baseModel_;
211 
212  // Every problem needs an algorithm, right?
213  RCP<Algorithm<Adapter> > algorithm_;
214 
215  RCP<ParameterList> params_;
216  RCP<const Comm<int> > comm_;
217 
218  // The Problem has a non const Environment object. This is because
219  // the Problem creates the Environment and may update it before
220  // finally calling the algorithm.
221 
222  RCP<Environment> env_;
223 
224  // The Problem needs a const version of the Environment. No other
225  // methods are permitted to change the Environment.
226 
227  RCP<const Environment> envConst_;
228 
229  // If the user requested timing, this is the TimerManager.
230 
231  RCP<TimerManager> timer_;
232 
233 private:
234  void setupProblemEnvironment(ParameterList *pl);
235 
236 };
237 
238 template <typename Adapter>
239  void Problem<Adapter>::setupProblemEnvironment(ParameterList * /* params */)
240 {
241  ParameterList &processedParameters = env_->getParametersNonConst();
242  params_ = rcp<ParameterList>(&processedParameters, false);
243 
244 #ifndef Z2_OMIT_ALL_PROFILING
245  ParameterList pl = *params_;
246 
247  // Give a timer to the Environment if requested.
248  bool haveType=false, haveStream=false, haveFile=false;
249  int choice = MACRO_TIMERS; // default timer type
250 
251  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
252 
253  if (pe){
254  choice = pe->getValue<int>(&choice);
255  haveType = true;
256  }
257 
258  TimerType tt = static_cast<TimerType>(choice);
259 
260  std::string fname;
261  pe = pl.getEntryPtr("timer_output_file");
262  if (pe){
263  haveFile = true;
264  fname = pe->getValue<std::string>(&fname);
265  std::ofstream *dbgFile = new std::ofstream;
266  if (comm_->getRank()==0){
267  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
268  try{
269  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
270  }
271  catch(std::exception &e){
272  throw std::runtime_error(e.what());
273  }
274  }
275  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
276  }
277  else{
278  choice = COUT_STREAM; // default output stream
279  pe = pl.getEntryPtr("timer_output_stream");
280  if (pe){
281  choice = pe->getValue<int>(&choice);
282  haveStream = true;
283  }
284 
285  OSType outputStream = static_cast<OSType>(choice);
286 
287  if (haveStream || haveType){
288  if (outputStream == COUT_STREAM)
289  timer_ = rcp(new TimerManager(comm_, &std::cout, tt));
290  else if (outputStream == CERR_STREAM)
291  timer_ = rcp(new TimerManager(comm_, &std::cerr, tt));
292  else if (outputStream == NULL_STREAM){
293  std::ofstream *of = NULL;
294  timer_ = rcp(new TimerManager(comm_, of, tt));
295  }
296  }
297  }
298 
299  if (haveType || haveStream || haveFile)
300  env_->setTimer(timer_);
301 
302 #endif
303 
304 }
305 
306 template <typename Adapter>
307  void Problem<Adapter>::resetParameters(ParameterList *params)
308 {
309  env_->resetParameters(*params);
310  setupProblemEnvironment(params);
311 
312  // We assume the timing output parameters have not changed,
313  // and carry on with the same timer.
314 
315  if (!timer_.is_null())
316  env_->setTimer(timer_);
317 }
318 
319 } // namespace Zoltan2
320 
321 #endif
RCP< GraphModel< base_adapter_t > > graphModel_
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Created by mbenlioglu on Aug 31, 2020.
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
const RCP< const Environment > & getEnvironment() const
Get the current Environment. Useful for testing.
virtual void solve(bool updateInputData=true)=0
Method that creates a solution.
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.
fname
Begin.
Definition: validXML.py:19
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.