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 
60 namespace Zoltan2{
61 
65 
66 template<typename Adapter>
67 class Problem {
68 public:
69 
70 #ifdef HAVE_ZOLTAN2_MPI
71 
73  Problem(const Adapter *input, ParameterList *params, MPI_Comm comm):
74  inputAdapter_(rcp(input,false)),
75  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
76  false)),
78  params_(), comm_(), env_(), envConst_(), timer_()
79  {
80  RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
81  Teuchos::opaqueWrapper(comm);
82  comm_ = rcp<const Comm<int> >(new Teuchos::MpiComm<int>(wrapper));
83  setupProblemEnvironment(params);
84  }
85 #endif
86 
87 
90  Problem(const Adapter *input, ParameterList *params):
91  inputAdapter_(rcp(input,false)),
92  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
93  false)),
95  params_(), comm_(), env_(), envConst_(), timer_()
96  {
97  comm_ = DefaultComm<int>::getComm();
98  setupProblemEnvironment(params);
99  }
100 
101 
104  Problem(const Adapter *input, ParameterList *params,
105  RCP<const Comm<int> > &comm):
106  inputAdapter_(rcp(input,false)),
107  baseInputAdapter_(rcp(dynamic_cast<const base_adapter_t *>(input),
108  false)),
110  params_(), comm_(comm), env_(), envConst_(), timer_()
111  {
112  setupProblemEnvironment(params);
113  }
114 
117  virtual ~Problem() {};
118 
121  RCP<const Comm<int> > getComm() { return comm_; }
122 
125  void resetParameters(ParameterList *params);
126 
129  virtual void solve(bool updateInputData) = 0;
130 
147 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
148  void printTimers() const {return;}
149 #else
150  void printTimers() const
151  {
152  if (!timer_.is_null())
153  timer_->printAndResetToZero();
154  }
155 #endif
156 
157 
158 protected:
159 
160  // The Problem is templated on the input adapter. We interact
161  // with the input adapter through the base class interface.
162  // The Model objects are also templated on the input adapter and
163  // are explicitly instantiated for each base input type (vector,
164  // graph, matrix, mesh, identifier list, and coordinate list).
165 
166  typedef typename Adapter::base_adapter_t base_adapter_t;
167 
168  RCP<const Adapter> inputAdapter_;
169  RCP<const base_adapter_t> baseInputAdapter_;
170 
171  RCP<GraphModel<base_adapter_t> > graphModel_;
172  RCP<IdentifierModel<base_adapter_t> > identifierModel_;
173  RCP<CoordinateModel<base_adapter_t> > coordinateModel_;
174 
175  // Algorithms are passed a base model class, and query
176  // the model through the base class interface (graph, hypergraph,
177  // identifiers, or coordinates).
178 
179  RCP<const Model<base_adapter_t> > baseModel_;
180 
181  // Every problem needs an algorithm, right?
182  RCP<Algorithm<Adapter> > algorithm_;
183 
184  RCP<ParameterList> params_;
185  RCP<const Comm<int> > comm_;
186 
187  // The Problem has a non const Environment object. This is because
188  // the Problem creates the Environment and may update it before
189  // finally calling the algorithm.
190 
191  RCP<Environment> env_;
192 
193  // The Problem needs a const version of the Environment. No other
194  // methods are permitted to change the Environment.
195 
196  RCP<const Environment> envConst_;
197 
198  // If the user requested timing, this is the TimerManager.
199 
200  RCP<TimerManager> timer_;
201 
202 private:
203  void setupProblemEnvironment(ParameterList *pl);
204 
205 };
206 
207 template <typename Adapter>
208  void Problem<Adapter>::setupProblemEnvironment(ParameterList *params)
209 {
210  try{
211  env_ = rcp(new Environment(*params, Teuchos::DefaultComm<int>::getComm()));
212  }
214 
215  envConst_ = rcp_const_cast<const Environment>(env_);
216 
217  ParameterList &processedParameters = env_->getParametersNonConst();
218  params_ = rcp<ParameterList>(&processedParameters, false);
219 
220 #ifndef Z2_OMIT_ALL_PROFILING
221  ParameterList pl = *params_;
222 
223  // Give a timer to the Environment if requested.
224  bool haveType=false, haveStream=false, haveFile=false;
225  int choice = MACRO_TIMERS; // default timer type
226 
227  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
228 
229  if (pe){
230  choice = pe->getValue<int>(&choice);
231  haveType = true;
232  }
233 
234  TimerType tt = static_cast<TimerType>(choice);
235 
236  std::string fname;
237  pe = pl.getEntryPtr("timer_output_file");
238  if (pe){
239  haveFile = true;
240  fname = pe->getValue<std::string>(&fname);
241  std::ofstream *dbgFile = new std::ofstream;
242  if (comm_->getRank()==0){
243  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
244  try{
245  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
246  }
247  catch(std::exception &e){
248  throw std::runtime_error(e.what());
249  }
250  }
251  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
252  }
253  else{
254  choice = COUT_STREAM; // default output stream
255  pe = pl.getEntryPtr("timer_output_stream");
256  if (pe){
257  choice = pe->getValue<int>(&choice);
258  haveStream = true;
259  }
260 
261  OSType outputStream = static_cast<OSType>(choice);
262 
263  if (haveStream || haveType){
264  if (outputStream == COUT_STREAM)
265  timer_ = rcp(new TimerManager(comm_, &std::cout, tt));
266  else if (outputStream == CERR_STREAM)
267  timer_ = rcp(new TimerManager(comm_, &std::cerr, tt));
268  else if (outputStream == NULL_STREAM){
269  std::ofstream *of = NULL;
270  timer_ = rcp(new TimerManager(comm_, of, tt));
271  }
272  }
273  }
274 
275  if (haveType || haveStream || haveFile)
276  env_->setTimer(timer_);
277 
278 #endif
279 
280 }
281 
282 template <typename Adapter>
283  void Problem<Adapter>::resetParameters(ParameterList *params)
284 {
285  env_ = rcp(new Environment(*params, Teuchos::DefaultComm<int>::getComm()));
286  envConst_ = rcp_const_cast<const Environment>(env_);
287 
288  ParameterList &processedParameters = env_->getParametersNonConst();
289  params_ = rcp<ParameterList>(&processedParameters, false);
290 
291  // We assume the timing output parameters have not changed,
292  // and carry on with the same timer.
293 
294  if (!timer_.is_null())
295  env_->setTimer(timer_);
296 }
297 
298 } // namespace Zoltan2
299 
300 #endif
RCP< GraphModel< base_adapter_t > > graphModel_
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.
Problem(const Adapter *input, ParameterList *params, RCP< const Comm< int > > &comm)
Constructor where Teuchos communicator is specified.
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
RCP< const Comm< int > > comm_
void resetParameters(ParameterList *params)
Reset the list of parameters.
RCP< IdentifierModel< base_adapter_t > > identifierModel_
Defines the IdentifierModel interface.
OSType
Output stream types.
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.
RCP< CoordinateModel< base_adapter_t > > coordinateModel_
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
RCP< TimerManager > timer_
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.