Zoltan2  Version of the Day
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_TimerManager.hpp>
58 
59 using std::cout;
60 using std::endl;
61 
62 namespace Zoltan2{
63 
67 
68 template<typename Adapter>
69 class Problem {
70 public:
71 
72 #ifdef HAVE_ZOLTAN2_MPI
73 
75  Problem(Adapter *, ParameterList *params, MPI_Comm comm);
76 #endif
77 
80  Problem(Adapter *, ParameterList *params);
81 
84  virtual ~Problem() {};
85 
88  void resetParameters(ParameterList *params);
89 
92  virtual void solve(bool updateInputData) = 0;
93 
107 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
108  void printTimers() const {return;}
109 #else
110  void printTimers() const
111  {
112  if (!timer_.is_null())
113  timer_->printAndResetToZero();
114  }
115 #endif
116 
117 
118 protected:
119 
120  // The Problem is templated on the input adapter. We interact
121  // with the input adapter through the base class interface.
122  // The Model objects are also templated on the input adapter and
123  // are explicitly instantiated for each base input type (vector,
124  // graph, matrix, mesh, identifier list, and coordinate list).
125 
126  typedef typename Adapter::base_adapter_t base_adapter_t;
127 
128  Adapter* inputAdapter_;
130 
131  RCP<GraphModel<base_adapter_t> > graphModel_;
132  RCP<IdentifierModel<base_adapter_t> > identifierModel_;
133  RCP<CoordinateModel<base_adapter_t> > coordinateModel_;
134 
135  // Algorithms are passed a base model class, and query
136  // the model through the base class interface (graph, hypergraph,
137  // identifiers, or coordinates).
138 
139  RCP<const Model<base_adapter_t> > baseModel_;
140 
141  RCP<ParameterList> params_;
142  RCP<const Comm<int> > comm_;
143 
144  // The Problem has a non const Environment object. This is because
145  // the Problem creates the Environment and may update it before
146  // finally calling the algorithm.
147 
148  RCP<Environment> env_;
149 
150  // The Problem needs a const version of the Environment. No other
151  // methods are permitted to change the Environment.
152 
153  RCP<const Environment> envConst_;
154 
155  // If the user requested timing, this is the TimerManager.
156 
157  RCP<TimerManager> timer_;
158 
159 private:
160  void setupProblemEnvironment(ParameterList *pl);
161 
162 };
163 
164 #ifdef HAVE_ZOLTAN2_MPI
165 
166 template <typename Adapter>
167  Problem<Adapter>::Problem( Adapter *input, ParameterList *params,
168  MPI_Comm comm) : inputAdapter_(input), baseInputAdapter_(),
169  graphModel_(), identifierModel_(), baseModel_(),
170  params_(), comm_(), env_(), envConst_(), timer_()
171 {
172  HELLO;
173  RCP<Teuchos::OpaqueWrapper<MPI_Comm> > wrapper =
174  Teuchos::opaqueWrapper(comm);
175  comm_ = rcp<const Comm<int> >(new Teuchos::MpiComm<int>(wrapper));
176  setupProblemEnvironment(params);
177 }
178 #endif
179 
180 template <typename Adapter>
181  Problem<Adapter>::Problem( Adapter *input, ParameterList *params):
182  inputAdapter_(input),
183  baseInputAdapter_(dynamic_cast<base_adapter_t *>(input)),
184  graphModel_(), identifierModel_(), baseModel_(),
185  params_(), comm_(), env_(), envConst_(), timer_()
186 {
187  HELLO;
188  comm_ = DefaultComm<int>::getComm();
189  setupProblemEnvironment(params);
190 }
191 
192 template <typename Adapter>
193  void Problem<Adapter>::setupProblemEnvironment(ParameterList *params)
194 {
195  baseInputAdapter_ = dynamic_cast<base_adapter_t *>(inputAdapter_);
196 
197  try{
198  env_ = rcp(new Environment(*params, Teuchos::DefaultComm<int>::getComm()));
199  }
201 
202  envConst_ = rcp_const_cast<const Environment>(env_);
203 
204  ParameterList &processedParameters = env_->getParametersNonConst();
205  params_ = rcp<ParameterList>(&processedParameters, false);
206 
207 #ifndef Z2_OMIT_ALL_PROFILING
208  ParameterList pl = *params_;
209 
210  // Give a timer to the Environment if requested.
211  bool haveType=false, haveStream=false, haveFile=false;
212  int choice = MACRO_TIMERS; // default timer type
213 
214  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("timer_type");
215 
216  if (pe){
217  choice = pe->getValue<int>(&choice);
218  haveType = true;
219  }
220 
221  TimerType tt = static_cast<TimerType>(choice);
222 
223  string fname;
224  pe = pl.getEntryPtr("timer_output_file");
225  if (pe){
226  haveFile = true;
227  fname = pe->getValue<string>(&fname);
228  std::ofstream *dbgFile = new std::ofstream;
229  if (comm_->getRank()==0){
230  // Using Teuchos::TimeMonitor, node 0 prints global timing info.
231  try{
232  dbgFile->open(fname.c_str(), std::ios::out|std::ios::trunc);
233  }
234  catch(std::exception &e){
235  throw std::runtime_error(e.what());
236  }
237  }
238  timer_ = rcp(new TimerManager(comm_, dbgFile, tt));
239  }
240  else{
241  choice = COUT_STREAM; // default output stream
242  pe = pl.getEntryPtr("timer_output_stream");
243  if (pe){
244  choice = pe->getValue<int>(&choice);
245  haveStream = true;
246  }
247 
248  OSType outputStream = static_cast<OSType>(choice);
249 
250  if (haveStream || haveType){
251  if (outputStream == COUT_STREAM)
252  timer_ = rcp(new TimerManager(comm_, &cout, tt));
253  else if (outputStream == CERR_STREAM)
254  timer_ = rcp(new TimerManager(comm_, &cerr, tt));
255  else if (outputStream == NULL_STREAM){
256  std::ofstream *of = NULL;
257  timer_ = rcp(new TimerManager(comm_, of, tt));
258  }
259  }
260  }
261 
262  if (haveType || haveStream || haveFile)
263  env_->setTimer(timer_);
264 
265 #endif
266 
267 }
268 
269 template <typename Adapter>
270  void Problem<Adapter>::resetParameters(ParameterList *params)
271 {
272  env_ = rcp(new Environment(*params, Teuchos::DefaultComm<int>::getComm()));
273  envConst_ = rcp_const_cast<const Environment>(env_);
274 
275  ParameterList &processedParameters = env_->getParametersNonConst();
276  params_ = rcp<ParameterList>(&processedParameters, false);
277 
278  // We assume the timing output parameters have not changed,
279  // and carry on with the same timer.
280 
281  if (!timer_.is_null())
282  env_->setTimer(timer_);
283 }
284 
285 } // namespace Zoltan2
286 
287 #endif
RCP< GraphModel< base_adapter_t > > graphModel_
#define HELLO
Time an algorithm (or other entity) as a whole.
virtual void solve(bool updateInputData)=0
Method that creates a solution.
Adapter::base_adapter_t base_adapter_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
base_adapter_t * baseInputAdapter_
virtual ~Problem()
Destructor.
TimerType
The type of timers which should be active.
/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_
Problem(Adapter *, ParameterList *params)
Constructor where communicator is Teuchos default.
Teuchos::ParameterList & getParametersNonConst()
Returns a reference to a non-const copy of the parameters.
Defines the IdentifierModel interface.
OSType
Output stream types.
Problem base class from which other classes (PartitioningProblem, ColoringProblem, OrderingProblem, MatchingProblem, etc.) derive.
void printTimers() const
If timer data was collected, print out global data.
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.