Zoltan2  Version of the Day
Zoltan2_Environment.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_ENVIRONMENT_HPP_
51 #define _ZOLTAN2_ENVIRONMENT_HPP_
52 
53 #include <Zoltan2_config.h>
54 #include <Zoltan2_Util.hpp>
55 #include <Zoltan2_IO.hpp>
56 #include <Zoltan2_Parameters.hpp>
57 #include <Zoltan2_DebugManager.hpp>
58 #include <Zoltan2_TimerManager.hpp>
60 
61 #include <Teuchos_RCP.hpp>
62 #include <Teuchos_ParameterList.hpp>
63 #include <Teuchos_Comm.hpp>
64 #include <Teuchos_DefaultComm.hpp>
65 #include <Teuchos_CommHelpers.hpp>
66 
67 namespace Zoltan2 {
68 
83 
84 public:
85 
86  typedef long memory_t;
88  typedef Teuchos::RCP<const Teuchos::Comm<int> > Comm_t;
89  typedef Teuchos::RCP<DebugManager> DebugManager_t;
90  typedef Teuchos::RCP<MetricOutputManager<memory_t> > MemoryProfilerManager_t;
91  typedef Teuchos::RCP<TimerManager> Timer_t;
92 
93  int myRank_;
95  int numProcs_;
109  Environment(Teuchos::ParameterList &problemParams,
110  const Teuchos::RCP<const Teuchos::Comm<int> > &comm );
111 
118  Environment();
119 
122  ~Environment();
123 
130  void setTimer(RCP<TimerManager> &timer) { timerOut_=timer; timingOn_=true;}
131 
132 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
133 
134  void localInputAssertion(const char *file, int lineNum,
135  const char *msg, bool ok, AssertionLevel level) const {}
136 
137  void globalInputAssertion(const char *file, int lineNum,
138  const char *msg, bool ok, AssertionLevel level,
139  const Comm_t &comm=comm_) const {}
140 
141  void localBugAssertion(const char *file, int lineNum,
142  const char *msg, bool ok, AssertionLevel level) const {}
143 
144  void globalBugAssertion(const char *file, int lineNum,
145  const char *msg, bool ok, AssertionLevel level,
146  const Comm_t &comm=comm_) const {}
147 
148  void localMemoryAssertion(const char *file, int lineNum,
149  size_t nobj, bool ok) const {}
150 
151  void globalMemoryAssertion(const char *file, int lineNum,
152  size_t nobj, bool ok, const Comm_t &comm=comm_) const {}
153 
154 #else
155 
169  void localInputAssertion(const char *file, int lineNum,
170  const char *msg, bool ok, AssertionLevel level) const {
171 
172  if (level <= errorCheckLevel_ && !ok){
173  std::ostringstream emsg;
174  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
175  if (msg)
176  emsg << myRank_ << ": error: " << msg << std::endl;
177  throw std::runtime_error(emsg.str());
178  }
179  }
196  void globalInputAssertion(const char *file, int lineNum,
197  const char *msg, bool ok, AssertionLevel level,
198  const Comm_t &comm) const {
199 
200  if (level <= errorCheckLevel_){
201  int anyFail=0, fail = (!ok ? 1 : 0);
202  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail,
203  &anyFail);
204  if (anyFail > 0){
205  std::ostringstream emsg;
206  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
207  if (msg && !ok)
208  emsg << myRank_ << ": error: " << msg << std::endl;
209  else
210  emsg << myRank_ << ": exiting" << std::endl;
211 
212  throw std::runtime_error(emsg.str());
213  }
214  }
215  }
216 
236  void localBugAssertion(const char *file, int lineNum,
237  const char *msg, bool ok, AssertionLevel level) const {
238 
239  if (level <= errorCheckLevel_ && !ok){
240  std::ostringstream emsg;
241  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
242  if (msg)
243  emsg << myRank_ << ": bug: " << msg << std::endl;
244  throw std::logic_error(emsg.str());
245  }
246  }
247 
269  void globalBugAssertion(const char *file, int lineNum,
270  const char *msg, bool ok, AssertionLevel level,
271  const Comm_t &comm) const {
272 
273  if (level <= errorCheckLevel_){
274  int anyFail=0, fail = (!ok ? 1 : 0);
275  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail,
276  &anyFail);
277  if (anyFail > 0){
278 
279  std::ostringstream emsg;
280  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
281  if (msg && !ok)
282  emsg << myRank_ << ": bug: " << msg << std::endl;
283  else
284  emsg << myRank_ << ": exiting" << std::endl;
285 
286  throw std::logic_error(emsg.str());
287  }
288  }
289  }
290 
304  void localMemoryAssertion(const char *file, int lineNum, size_t nobj,
305  bool ok) const {
306 
307  if (!ok){
308  std::cerr << myRank_ << ": " << file << ", " << lineNum<< std::endl;
309  std::cerr << myRank_ << ": " << nobj << " objects" << std::endl;
310  throw std::bad_alloc();
311  }
312  }
313 
329  void globalMemoryAssertion(const char *file, int lineNum,
330  size_t nobj, bool ok, const Comm_t &comm) const {
331 
332  int anyFail=0, fail = (!ok ? 1 : 0);
333  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail, &anyFail);
334  if (anyFail > 0){
335  std::cerr << myRank_ << ": " << file << ", " << lineNum<< std::endl;
336  if (!ok)
337  std::cerr << myRank_ << ": " << nobj << " objects" << std::endl;
338  else
339  std::cerr << myRank_ << ": exiting" << std::endl;
340 
341  throw std::bad_alloc();
342  }
343  }
344 #endif
345 
346  // For debugging and profiling output, we define "char *" versions
347  // as well as "string" versions to avoid runtime conversion of "char *"
348  // to "string".
349 
361 #ifdef Z2_OMIT_ALL_STATUS_MESSAGES
362  void debug(MessageOutputLevel level, const char *msg) const{ return;}
363  void debug(MessageOutputLevel level, const std::string msg) const{ return;}
364 #else
365  void debug(MessageOutputLevel level, const char *msg) const{
366  debugOut_->print(level, msg);}
367  void debug(MessageOutputLevel level, const std::string &msg) const{
368  debugOut_->print(level, msg);}
369 #endif
370 
371 #ifdef Z2_OMIT_ALL_PROFILING
372 
373  void timerStart(TimerType tt, const char * timerName) const {return;}
374  void timerStart(TimerType tt, const string &timerName) const {return;}
375  void timerStart(TimerType tt, const char * timerName, int,
376  int fieldWidth=0) const {return;}
377  void timerStart(TimerType tt, const string &timerName, int,
378  int fieldWidth=0) const {return;}
379 
380  void timerStop(TimerType tt, const char * timerName) const {return;}
381  void timerStop(TimerType tt, const string &timerName) const {return;}
382  void timerStop(TimerType tt, const char * timerName, int,
383  int fieldWidth=0) const {return;}
384  void timerStop(TimerType tt, const string &timerName, int,
385  int fieldWidth=0) const {return;}
386 
387 #else
388 
391  void timerStart(TimerType tt, const char *timerName) const {
392  if (timingOn_) timerOut_->start(tt, timerName); }
393 
394  void timerStart(TimerType tt, const string &timerName) const {
395  if (timingOn_) timerOut_->start(tt, timerName); }
396 
399  void timerStart(TimerType tt, const char *timerName, int num,
400  int fieldWidth=0) const {
401  if (timingOn_){
402  ostringstream oss;
403  oss << timerName << " ";
404  if (fieldWidth > 0){
405  oss.width(fieldWidth);
406  oss.fill('0');
407  }
408  oss << num;
409  timerOut_->start(tt, oss.str());
410  }
411  }
412 
413  void timerStart(TimerType tt, const string &timerName, int num,
414  int fieldWidth=0) const {
415  if (timingOn_){
416  ostringstream oss;
417  oss << timerName << " ";
418  if (fieldWidth > 0){
419  oss.width(fieldWidth);
420  oss.fill('0');
421  }
422  oss << num;
423  timerOut_->start(tt, oss.str());
424  }
425  }
426 
430  void timerStop(TimerType tt, const char *timerName) const {
431  if (timingOn_) timerOut_->stop(tt, timerName); }
432 
433  void timerStop(TimerType tt, const string &timerName) const {
434  if (timingOn_) timerOut_->stop(tt, timerName); }
435 
439  void timerStop(TimerType tt, const char *timerName, int num,
440  int fieldWidth=0) const {
441  if (timingOn_){
442  ostringstream oss;
443  oss << timerName << " ";
444  if (fieldWidth > 0){
445  oss.width(fieldWidth);
446  oss.fill('0');
447  }
448  oss << num;
449  timerOut_->stop(tt, oss.str());
450  }
451  }
452 
453  void timerStop(TimerType tt, const string &timerName, int num,
454  int fieldWidth=0) const {
455  if (timingOn_){
456  ostringstream oss;
457  oss << timerName << " ";
458  if (fieldWidth > 0){
459  oss.width(fieldWidth);
460  oss.fill('0');
461  }
462  oss << num;
463  timerOut_->stop(tt, oss.str());
464  }
465  }
466 
467 #endif
468 
484 #ifdef Z2_OMIT_ALL_PROFILING
485  void memory(const char *msg) const {return;}
486 
487  void memory(const std::string &msg) const {return; }
488 #else
489  void memory(const char *msg) const
490  {if (memoryOn_)
491  memoryOut_->print(msg, getProcessKilobytes());}
492 
493  void memory(const std::string &msg) const
494  {if (memoryOn_)
495  memoryOut_->print(msg, getProcessKilobytes());}
496 #endif
497 
502  const Teuchos::ParameterList &getParameters() const { return params_; }
503 
508  Teuchos::ParameterList &getParametersNonConst() { return params_; }
509 
513  bool doTiming() const { return timingOn_; }
514 
518 #ifdef Z2_OMIT_ALL_STATUS_MESSAGES
519  bool doStatus() const { return false;}
520  MessageOutputLevel getDebugLevel() const {return NO_STATUS;}
521 #else
522  bool doStatus() const { return debugOut_->getDebugLevel() > NO_STATUS;}
523 
524  MessageOutputLevel getDebugLevel() const {return debugOut_->getDebugLevel(); }
525 #endif
526 
530  bool doMemoryProfiling() const { return memoryOn_;}
531 
543  const Teuchos::ParameterList &getUnvalidatedParameters() const {
544  return unvalidatedParams_; }
545 
559  static void convertStringToInt(Teuchos::ParameterList &params);
560 
561 private:
562 
565  void commitParameters();
566 
571  Teuchos::ParameterList unvalidatedParams_;
572 
581  Teuchos::ParameterList params_;
582 
583  DebugManager_t debugOut_;
585  Timer_t timerOut_;
586  bool timingOn_;
587 
588  MemoryProfilerManager_t memoryOut_;
589  bool memoryOn_;
590  RCP<std::ofstream> memoryOutputFile_;
591 };
592 
596 #define Z2_UNSET_STRING std::string("notSet")
597 
599 // Templated namespace definitions used by the class
600 
613 template<typename metric_t>
614  void makeMetricOutputManager(int rank, bool iPrint,
615  std::string fname, int ost,
616  Teuchos::RCP<MetricOutputManager<metric_t> > &mgr,
617  std::string units, int fieldWidth,
618  RCP<std::ofstream> &fptr)
619 {
620  typedef MetricOutputManager<metric_t> manager_t;
621 
622  OSType os = static_cast<OSType>(ost);
623 
624  bool haveFname = (fname != Z2_UNSET_STRING);
625  bool haveStreamName = (os != NUM_OUTPUT_STREAMS);
626 
627  if (!haveFname && !haveStreamName){
628  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cout, true,
629  units, fieldWidth));
630  return;
631  }
632 
633  if (haveFname){
634  std::ofstream *oFile = NULL;
635  if (iPrint){
636  oFile = new std::ofstream;
637  std::string newFname;
638  addNumberToFileName(rank, fname, newFname);
639  try{
640  oFile->open(newFname.c_str(), std::ios::out|std::ios::trunc);
641  }
642  catch(std::exception &e){
643  throw std::runtime_error(e.what());
644  }
645  fptr = rcp(oFile);
646  }
647  mgr = Teuchos::rcp(new manager_t(rank, iPrint, *oFile, true,
648  units, fieldWidth));
649  return;
650  }
651 
652  if (os == COUT_STREAM)
653  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cout, true,
654  units, fieldWidth));
655  else if (os == CERR_STREAM)
656  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cerr, true,
657  units, fieldWidth));
658  else if (os == NULL_STREAM)
659  mgr = Teuchos::rcp(new manager_t(rank, false, std::cout, true,
660  units, fieldWidth));
661  else
662  throw std::logic_error("invalid metric output stream was not caught");
663 }
664 
665 } // namespace Zoltan2
666 
667 #endif
Environment()
Default Constructor.
const Teuchos::ParameterList & getParameters() const
Returns a reference to the user&#39;s parameter list.
void globalMemoryAssertion(const char *file, int lineNum, size_t nobj, bool ok, const Comm_t &comm) const
Test for successful memory allocation on every process.
bool doStatus() const
Return true if debug output was requested, even if this process is not printing out debug messages...
void timerStop(TimerType tt, const char *timerName, int num, int fieldWidth=0) const
Stop a named timer, with a number as part of the name.
Defines the MetricOutputManager class.
void setTimer(RCP< TimerManager > &timer)
Provide the Timer object to the Environment.
MetricOutputManager handles output of profiling messages.
void timerStart(TimerType tt, const char *timerName, int num, int fieldWidth=0) const
Start a named timer, with a number as part of the name.
void localInputAssertion(const char *file, int lineNum, const char *msg, bool ok, AssertionLevel level) const
Test for valid user input on local process only.
MessageOutputLevel
The amount of debugging or status output to print.
void timerStop(TimerType tt, const string &timerName) const
Defines Parameter related enumerators, declares functions.
static void convertStringToInt(Teuchos::ParameterList &params)
Convert parameters of type Teuchos::StringToIntegralParameterEntryValidator&lt;int&gt; to integer...
void makeMetricOutputManager(int rank, bool iPrint, std::string fname, int ost, Teuchos::RCP< MetricOutputManager< metric_t > > &mgr, std::string units, int fieldWidth, RCP< std::ofstream > &fptr)
Create an output manager for a metric value.
Debug output manager for Zoltan2.
TimerType
The type of timers which should be active.
void localMemoryAssertion(const char *file, int lineNum, size_t nobj, bool ok) const
Test for successful memory allocation on local process only.
/dev/null: do actions but don&#39;t output results
void timerStart(TimerType tt, const string &timerName, int num, int fieldWidth=0) const
void globalBugAssertion(const char *file, int lineNum, const char *msg, bool ok, AssertionLevel level, const Comm_t &comm) const
Test for valid library behavior on every process.
Teuchos::RCP< const Teuchos::Comm< int > > Comm_t
bool doMemoryProfiling() const
Return true if memory usage output was requested, even if this process is not printing out memory use...
long getProcessKilobytes()
void globalInputAssertion(const char *file, int lineNum, const char *msg, bool ok, AssertionLevel level, const Comm_t &comm) const
Test globally for valid user input.
AssertionLevel
Level of error checking or assertions desired.
int numProcs_
number of processes (relative to comm_)
void debug(MessageOutputLevel level, const char *msg) const
Send a message to the debug output manager.
void timerStop(TimerType tt, const char *timerName) const
Stop a named timer.
void timerStop(TimerType tt, const string &timerName, int num, int fieldWidth=0) const
Teuchos::ParameterList & getParametersNonConst()
Returns a reference to a non-const copy of the parameters.
Declaration of methods to assist in file input/output.
int myRank_
mpi rank (relative to comm_)
OSType
Output stream types.
Comm_t comm_
communicator for environment
void debug(MessageOutputLevel level, const std::string &msg) const
Teuchos::RCP< MetricOutputManager< memory_t > > MemoryProfilerManager_t
const Teuchos::ParameterList & getUnvalidatedParameters() const
Returns a const reference to the user&#39;s original list.
don&#39;t display status/debug messages
void localBugAssertion(const char *file, int lineNum, const char *msg, bool ok, AssertionLevel level) const
Test for valid library behavior on local process only.
long memory_t
data type for Kilobytes
void addNumberToFileName(int number, std::string fname, std::string &newf)
Helper method to add number to a file name.
Definition: Zoltan2_IO.cpp:56
#define Z2_UNSET_STRING
A value to indicate a string parameter that was not set by the user.
void memory(const char *msg) const
Print a message and the kilobytes in use by this process.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Teuchos::RCP< TimerManager > Timer_t
MessageOutputLevel getDebugLevel() const
void memory(const std::string &msg) const
bool doTiming() const
Return true if timing was requested, even if this process is not printing out timing messages...
void timerStart(TimerType tt, const string &timerName) const
A gathering of useful namespace methods.
void timerStart(TimerType tt, const char *timerName) const
Start a named timer.
AssertionLevel errorCheckLevel_
level of error checking to do
Declarations for TimerManager.
Teuchos::RCP< DebugManager > DebugManager_t