Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_Environment.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_ENVIRONMENT_HPP_
15 #define _ZOLTAN2_ENVIRONMENT_HPP_
16 
17 #include <Zoltan2_config.h>
18 #include <Zoltan2_Util.hpp>
19 #include <Zoltan2_IO.hpp>
20 #include <Zoltan2_Parameters.hpp>
21 #include <Zoltan2_DebugManager.hpp>
22 #include <Zoltan2_TimerManager.hpp>
24 
25 #include <Teuchos_RCP.hpp>
26 #include <Teuchos_ParameterList.hpp>
27 #include <Teuchos_Comm.hpp>
28 #include <Teuchos_DefaultComm.hpp>
29 #include <Teuchos_CommHelpers.hpp>
30 #include <Teuchos_StandardParameterEntryValidators.hpp>
31 
32 namespace Zoltan2 {
33 
48 
49 public:
50 
51  typedef long memory_t;
53  typedef Teuchos::RCP<const Teuchos::Comm<int> > Comm_t;
54  typedef Teuchos::RCP<DebugManager> DebugManager_t;
55  typedef Teuchos::RCP<MetricOutputManager<memory_t> > MemoryProfilerManager_t;
56  typedef Teuchos::RCP<TimerManager> Timer_t;
57 
58  int myRank_;
60  int numProcs_;
72  Environment(Teuchos::ParameterList &problemParams,
73  const Teuchos::RCP<const Teuchos::Comm<int> > &comm );
74 
82  Environment(const Teuchos::RCP<const Teuchos::Comm<int> > &comm);
83 
86  ~Environment();
87 
90  void resetParameters(Teuchos::ParameterList &problemParams);
91 
94  static void getValidParameters(ParameterList & pl);
95 
98  static RCP<Teuchos::BoolParameterEntryValidator> getBoolValidator();
99 
102  static RCP<Teuchos::AnyNumberParameterEntryValidator>
104 
107  static RCP<Teuchos::AnyNumberParameterEntryValidator>
109 
116  void setTimer(RCP<TimerManager> &timer) { timerOut_=timer; timingOn_=true;}
117 
118 #ifdef Z2_OMIT_ALL_ERROR_CHECKING
119 
120  inline void localInputAssertion(const char *file, int lineNum,
121  const char *msg, bool ok, AssertionLevel level) const {}
122 
123  inline void globalInputAssertion(const char *file, int lineNum,
124  const char *msg, bool ok, AssertionLevel level,
125  const Comm_t &comm=comm_) const {}
126 
127  inline void localBugAssertion(const char *file, int lineNum,
128  const char *msg, bool ok, AssertionLevel level) const {}
129 
130  inline void globalBugAssertion(const char *file, int lineNum,
131  const char *msg, bool ok, AssertionLevel level,
132  const Comm_t &comm=comm_) const {}
133 
134  inline void localMemoryAssertion(const char *file, int lineNum,
135  size_t nobj, bool ok) const {}
136 
137  inline void globalMemoryAssertion(const char *file, int lineNum,
138  size_t nobj, bool ok, const Comm_t &comm=comm_) const {}
139 
140 #else
141 
155  inline void localInputAssertion(const char *file, int lineNum,
156  const char *msg, bool ok, AssertionLevel level) const {
157 
158  if (level <= errorCheckLevel_ && !ok){
159  std::ostringstream emsg;
160  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
161  if (msg)
162  emsg << myRank_ << ": error: " << msg << std::endl;
163  throw std::runtime_error(emsg.str());
164  }
165  }
182  inline void globalInputAssertion(const char *file, int lineNum,
183  const char *msg, bool ok, AssertionLevel level,
184  const Comm_t &comm) const {
185 
186  if (level <= errorCheckLevel_){
187  int anyFail=0, fail = (!ok ? 1 : 0);
188  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail,
189  &anyFail);
190  if (anyFail > 0){
191  std::ostringstream emsg;
192  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
193  if (msg && !ok)
194  emsg << myRank_ << ": error: " << msg << std::endl;
195  else
196  emsg << myRank_ << ": exiting" << std::endl;
197 
198  throw std::runtime_error(emsg.str());
199  }
200  }
201  }
202 
222  inline void localBugAssertion(const char *file, int lineNum,
223  const char *msg, bool ok, AssertionLevel level) const {
224 
225  if (level <= errorCheckLevel_ && !ok){
226  std::ostringstream emsg;
227  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
228  if (msg)
229  emsg << myRank_ << ": bug: " << msg << std::endl;
230  throw std::logic_error(emsg.str());
231  }
232  }
233 
255  inline void globalBugAssertion(const char *file, int lineNum,
256  const char *msg, bool ok, AssertionLevel level,
257  const Comm_t &comm) const {
258 
259  if (level <= errorCheckLevel_){
260  int anyFail=0, fail = (!ok ? 1 : 0);
261  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail,
262  &anyFail);
263  if (anyFail > 0){
264 
265  std::ostringstream emsg;
266  emsg << myRank_<< ": " << file << "," << lineNum<< std::endl;
267  if (msg && !ok)
268  emsg << myRank_ << ": bug: " << msg << std::endl;
269  else
270  emsg << myRank_ << ": exiting" << std::endl;
271 
272  throw std::logic_error(emsg.str());
273  }
274  }
275  }
276 
290  inline void localMemoryAssertion(const char *file, int lineNum, size_t nobj,
291  bool ok) const {
292 
293  if (!ok){
294  std::cerr << myRank_ << ": " << file << ", " << lineNum<< std::endl;
295  std::cerr << myRank_ << ": " << nobj << " objects" << std::endl;
296  throw std::bad_alloc();
297  }
298  }
299 
315  inline void globalMemoryAssertion(const char *file, int lineNum,
316  size_t nobj, bool ok, const Comm_t &comm) const {
317 
318  int anyFail=0, fail = (!ok ? 1 : 0);
319  Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_MAX, 1, &fail, &anyFail);
320  if (anyFail > 0){
321  std::cerr << myRank_ << ": " << file << ", " << lineNum<< std::endl;
322  if (!ok)
323  std::cerr << myRank_ << ": " << nobj << " objects" << std::endl;
324  else
325  std::cerr << myRank_ << ": exiting" << std::endl;
326 
327  throw std::bad_alloc();
328  }
329  }
330 #endif
331 
332  // For debugging and profiling output, we define "char *" versions
333  // as well as "string" versions to avoid runtime conversion of "char *"
334  // to "string".
335 
347 #ifdef Z2_OMIT_ALL_STATUS_MESSAGES
348  inline void debug(MessageOutputLevel level, const char *msg) const{ return;}
349  inline void debug(MessageOutputLevel level, const std::string& msg) const{
350  return;
351  }
352  inline void debug(int level, const char *msg) const{ return;}
353  inline void debug(int level, const std::string& msg) const{ return;}
354 #else
355  inline void debug(MessageOutputLevel level, const char *msg) const{
356  debugOut_->print(level, msg);
357  }
358 
359  inline void debug(MessageOutputLevel level, const std::string& msg) const{
360  debugOut_->print(level, msg);
361  }
362 
363  inline void debug(int level, const char *msg) const{
364  MessageOutputLevel msg_enum = static_cast<MessageOutputLevel>(level);
365  debugOut_->print(msg_enum, msg);
366  }
367 
368  inline void debug(int level, const std::string& msg) const{
369  MessageOutputLevel msg_enum = static_cast<MessageOutputLevel>(level);
370  debugOut_->print(msg_enum, msg);
371  }
372 #endif
373 
374 #ifdef Z2_OMIT_ALL_PROFILING
375 
376  inline void timerStart(TimerType tt, const char * timerName) const {return;}
377  inline void timerStart(TimerType tt, const std::string &timerName) const {return;}
378  inline void timerStart(TimerType tt, const char * timerName, int,
379  int fieldWidth=0) const {return;}
380  inline void timerStart(TimerType tt, const std::string &timerName, int,
381  int fieldWidth=0) const {return;}
382 
383  inline void timerStop(TimerType tt, const char * timerName) const {return;}
384  inline void timerStop(TimerType tt, const std::string &timerName) const {return;}
385  inline void timerStop(TimerType tt, const char * timerName, int,
386  int fieldWidth=0) const {return;}
387  inline void timerStop(TimerType tt, const std::string &timerName, int,
388  int fieldWidth=0) const {return;}
389 
390 #else
391 
394  inline void timerStart(TimerType tt, const char *timerName) const {
395  if (timingOn_) timerOut_->start(tt, timerName); }
396 
397  inline void timerStart(TimerType tt, const std::string &timerName) const {
398  if (timingOn_) timerOut_->start(tt, timerName); }
399 
402  inline void timerStart(TimerType tt, const char *timerName, int num,
403  int fieldWidth=0) const {
404  if (timingOn_){
405  std::ostringstream oss;
406  oss << timerName << " ";
407  if (fieldWidth > 0){
408  oss.width(fieldWidth);
409  oss.fill('0');
410  }
411  oss << num;
412  timerOut_->start(tt, oss.str());
413  }
414  }
415 
416  inline void timerStart(TimerType tt, const std::string &timerName, int num,
417  int fieldWidth=0) const {
418  if (timingOn_){
419  std::ostringstream oss;
420  oss << timerName << " ";
421  if (fieldWidth > 0){
422  oss.width(fieldWidth);
423  oss.fill('0');
424  }
425  oss << num;
426  timerOut_->start(tt, oss.str());
427  }
428  }
429 
433  inline void timerStop(TimerType tt, const char *timerName) const {
434  if (timingOn_) timerOut_->stop(tt, timerName); }
435 
436  inline void timerStop(TimerType tt, const std::string &timerName) const {
437  if (timingOn_) timerOut_->stop(tt, timerName); }
438 
442  inline void timerStop(TimerType tt, const char *timerName, int num,
443  int fieldWidth=0) const {
444  if (timingOn_){
445  std::ostringstream oss;
446  oss << timerName << " ";
447  if (fieldWidth > 0){
448  oss.width(fieldWidth);
449  oss.fill('0');
450  }
451  oss << num;
452  timerOut_->stop(tt, oss.str());
453  }
454  }
455 
456  inline void timerStop(TimerType tt, const std::string &timerName, int num,
457  int fieldWidth=0) const {
458  if (timingOn_){
459  std::ostringstream oss;
460  oss << timerName << " ";
461  if (fieldWidth > 0){
462  oss.width(fieldWidth);
463  oss.fill('0');
464  }
465  oss << num;
466  timerOut_->stop(tt, oss.str());
467  }
468  }
469 
470 #endif
471 
487 #ifdef Z2_OMIT_ALL_PROFILING
488  inline void memory(const char *msg) const {return;}
489 
490  inline void memory(const std::string &msg) const {return; }
491 #else
492  inline void memory(const char *msg) const
493  {if (memoryOn_)
494  memoryOut_->print(msg, getProcessKilobytes());}
495 
496  inline void memory(const std::string &msg) const
497  {if (memoryOn_)
498  memoryOut_->print(msg, getProcessKilobytes());}
499 #endif
500 
505  const Teuchos::ParameterList &getParameters() const { return params_; }
506 
511  Teuchos::ParameterList &getParametersNonConst() { return params_; }
512 
516  bool doTiming() const { return timingOn_; }
517 
521 #ifdef Z2_OMIT_ALL_STATUS_MESSAGES
522  inline bool doStatus() const { return false;}
523  inline MessageOutputLevel getDebugLevel() const {return NO_STATUS;}
524  inline std::ostream *getDebugOStream() const {return std::cout;}
525 #else
526  inline bool doStatus() const {
527  return (debugOut_->getDebugLevel() > NO_STATUS);
528  }
530  return debugOut_->getDebugLevel();
531  }
532  inline std::ostream *getDebugOStream() const {
533  return debugOut_->getOStream();
534  }
535 #endif
536 
540  bool doMemoryProfiling() const { return memoryOn_;}
541 
553  const Teuchos::ParameterList &getUnvalidatedParameters() const {
554  return unvalidatedParams_; }
555 
567 private:
568 
571  void commitParameters();
572 
577  Teuchos::ParameterList unvalidatedParams_;
578 
587  Teuchos::ParameterList params_;
588 
589  DebugManager_t debugOut_;
591  Timer_t timerOut_;
592  bool timingOn_;
593 
594  MemoryProfilerManager_t memoryOut_;
595  bool memoryOn_;
596  RCP<std::ofstream> memoryOutputFile_;
597 };
598 
602 #define Z2_UNSET_STRING std::string("notSet")
603 
605 // Templated namespace definitions used by the class
606 
619 template<typename metric_t>
620  void makeMetricOutputManager(int rank, bool iPrint,
621  std::string fname, int ost,
622  Teuchos::RCP<MetricOutputManager<metric_t> > &mgr,
623  std::string units, int fieldWidth,
624  RCP<std::ofstream> &fptr)
625 {
626  typedef MetricOutputManager<metric_t> manager_t;
627 
628  OSType os = static_cast<OSType>(ost);
629 
630  bool haveFname = (fname != Z2_UNSET_STRING);
631  bool haveStreamName = (os != NUM_OUTPUT_STREAMS);
632 
633  if (!haveFname && !haveStreamName){
634  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cout, true,
635  units, fieldWidth));
636  return;
637  }
638 
639  if (haveFname){
640  std::ofstream *oFile = NULL;
641  if (iPrint){
642  oFile = new std::ofstream;
643  std::string newFname;
644  addNumberToFileName(rank, fname, newFname);
645  try{
646  oFile->open(newFname.c_str(), std::ios::out|std::ios::trunc);
647  }
648  catch(std::exception &e){
649  throw std::runtime_error(e.what());
650  }
651  fptr = rcp(oFile);
652  }
653  mgr = Teuchos::rcp(new manager_t(rank, iPrint, *oFile, true,
654  units, fieldWidth));
655  return;
656  }
657 
658  if (os == COUT_STREAM)
659  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cout, true,
660  units, fieldWidth));
661  else if (os == CERR_STREAM)
662  mgr = Teuchos::rcp(new manager_t(rank, iPrint, std::cerr, true,
663  units, fieldWidth));
664  else if (os == NULL_STREAM)
665  mgr = Teuchos::rcp(new manager_t(rank, false, std::cout, true,
666  units, fieldWidth));
667  else
668  throw std::logic_error("invalid metric output stream was not caught");
669 }
670 
671 } // namespace Zoltan2
672 
673 #endif
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.
std::ostream * getDebugOStream() const
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.
Defines Parameter related enumerators, declares functions.
static RCP< Teuchos::BoolParameterEntryValidator > getBoolValidator()
Exists to make setting up validators less cluttered.
Environment(Teuchos::ParameterList &problemParams, const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Constructor.
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
static RCP< Teuchos::AnyNumberParameterEntryValidator > getAnyDoubleValidator()
Exists to make setting up validators less cluttered.
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_)
list fname
Begin.
Definition: validXML.py:19
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.
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.
void debug(int level, const char *msg) const
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.
static void getValidParameters(ParameterList &pl)
Collect the paramaters specific to Environment.
void debug(int level, const std::string &msg) const
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:20
#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
void resetParameters(Teuchos::ParameterList &problemParams)
resetParameters and validate them - preserve the comm
static RCP< Teuchos::AnyNumberParameterEntryValidator > getAnyIntValidator()
Exists to make setting up validators less cluttered.
static const std::string fail
void timerStop(TimerType tt, const std::string &timerName) const
MessageOutputLevel getDebugLevel() const
void timerStart(TimerType tt, const std::string &timerName, int num, int fieldWidth=0) const
void memory(const std::string &msg) const
void timerStop(TimerType tt, const std::string &timerName, int num, int fieldWidth=0) const
bool doTiming() const
Return true if timing was requested, even if this process is not printing out timing messages...
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.
void timerStart(TimerType tt, const std::string &timerName) const
Teuchos::RCP< DebugManager > DebugManager_t