MueLu  Version of the Day
MueLu_Monitor.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_MONITOR_HPP
47 #define MUELU_MONITOR_HPP
48 
49 #include <string>
50 #include <algorithm> // for swap
51 #include <ostream> // for basic_ostream, operator<<, etc
52 #include "Teuchos_FancyOStream.hpp" // for OSTab, FancyOStream
53 #include "Teuchos_RCPDecl.hpp" // for RCP
54 #include "Teuchos_RCP.hpp" // for RCP::RCP<T>, RCP::operator=, etc
55 #include "Teuchos_Utils.hpp" // for Utils
56 #include "MueLu_VerbosityLevel.hpp" // for MsgType, MsgType::Runtime0, etc
57 #include "MueLu_BaseClass.hpp"
59 #include "MueLu_Level.hpp"
60 #include "MueLu_TimeMonitor.hpp"
61 
62 namespace MueLu {
63 
66  static std::string getColonLabel(const std::string& label) {
67  if (label != "")
68  return label + ": ";
69  else
70  return "";
71  }
72  };
73 
74 
78  class PrintMonitor : public BaseClass {
79 
80  public:
81 
83  PrintMonitor(const BaseClass& object, const std::string& msg, MsgType msgLevel = Runtime0) : object_(object) {
84  tabbed = false;
85  if (object_.IsPrint(msgLevel)) {
86  // Print description and new indent
87  object_.GetOStream(msgLevel, 0) << msg << std::endl;
88  object_.getOStream()->pushTab();
89  tabbed = true;
90  }
91  }
92 
93  ~PrintMonitor() { if (tabbed) object_.getOStream()->popTab(); }
94 
95  private:
96  PrintMonitor();
97 
98  bool tabbed;
100  };
101 
116  class Monitor: public BaseClass {
117  public:
125  Monitor(const BaseClass& object, const std::string & msg, MsgType msgLevel = Runtime0, MsgType timerLevel = Timings0)
126  : printMonitor_(object, msg + " (" + object.description() + ")", msgLevel),
127  timerMonitor_(object, object.ShortClassName() + ": " + msg + " (total)", timerLevel)
128  { }
129 
138  Monitor(const BaseClass& object, const std::string & msg, const std::string & label, MsgType msgLevel = Runtime0, MsgType timerLevel = Timings0)
139  : printMonitor_(object, label + msg + " (" + object.description() + ")", msgLevel),
140  timerMonitor_(object, label + object.ShortClassName() + ": " + msg + " (total)", timerLevel)
141  { }
142 
143  private:
148  };
149 
150  //---------------------------------------------------------------------------------------------------
151 
168  class SubMonitor: public BaseClass {
169  public:
177  SubMonitor(const BaseClass& object, const std::string & msg, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
178  : printMonitor_(object, msg, msgLevel),
179  timerMonitor_(object, object.ShortClassName() + ": " + msg + " (sub, total)", timerLevel)
180  { }
181 
190  SubMonitor(const BaseClass& object, const std::string & msg, const std::string & label, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
191  : printMonitor_(object, label + msg, msgLevel),
192  timerMonitor_(object, label + object.ShortClassName() + ": " + msg + " (sub, total)", timerLevel)
193  { }
194 
195  private:
198  };
199 
200 //convert integer timer number to string
201 #ifdef HAVE_MUELU_PROFILING
202 #define MUELU_TIMER_AS_STRING static_cast<std::ostringstream*>( &(std::ostringstream() << " " << timerIdentifier_++) )->str()
203 #else
204 #define MUELU_TIMER_AS_STRING
205 #endif
206 
207  //---------------------------------------------------------------------------------------------------
208 
228  class FactoryMonitor: public Monitor {
229  public:
230 
231  static int timerIdentifier_;
232 
241  FactoryMonitor(const BaseClass& object, const std::string & msg, int levelID, MsgType msgLevel = static_cast<MsgType>(Test | Runtime0), MsgType timerLevel = Timings0)
242  : Monitor(object, msg, msgLevel, timerLevel),
243  timerMonitorExclusive_(object, object.ShortClassName() + ": " + msg, timerLevel)
244  {
245  if (object.IsPrint(TimingsByLevel)) {
246  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
247  " (total, level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
249  MUELU_TIMER_AS_STRING + ": " + msg + " (level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
250  }
251  }
252 
263  FactoryMonitor(const BaseClass& object, const std::string & msg, const Level & level, MsgType msgLevel = static_cast<MsgType>(Test | Runtime0), MsgType timerLevel = Timings0)
264  : Monitor(object, msg, FormattingHelper::getColonLabel(level.getObjectLabel()), msgLevel, timerLevel),
265  timerMonitorExclusive_(object, FormattingHelper::getColonLabel(level.getObjectLabel()) + object.ShortClassName() + ": " + msg, timerLevel)
266  {
267  if (object.IsPrint(TimingsByLevel)) {
268  std::string label = FormattingHelper::getColonLabel(level.getObjectLabel());
269  levelTimeMonitor_ = rcp(new TimeMonitor(object, label+object.ShortClassName() + ": " + msg +
270  " (total, level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
272  MUELU_TIMER_AS_STRING + ": " + msg + " (level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
273  }
274  }
275 
276  private:
278  RCP<TimeMonitor> levelTimeMonitor_;
282  RCP<MutuallyExclusiveTimeMonitor<Level> > levelTimeMonitorExclusive_;
283  };
284 
285  //---------------------------------------------------------------------------------------------------
286 
302  public:
303 
312  SubFactoryMonitor(const BaseClass& object, const std::string & msg, int levelID, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
313  : SubMonitor(object, msg, msgLevel, timerLevel)
314  {
315  if (object.IsPrint(TimingsByLevel))
316  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
317  " (sub, total, level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
318  }
319 
328  SubFactoryMonitor(const BaseClass& object, const std::string & msg, const Level & level, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
329  : SubMonitor(object, msg, FormattingHelper::getColonLabel(level.getObjectLabel()), msgLevel, timerLevel)
330  {
331  if (object.IsPrint(TimingsByLevel)) {
332  std::string label = FormattingHelper::getColonLabel(level.getObjectLabel());
333  levelTimeMonitor_ = rcp(new TimeMonitor(object, label+object.ShortClassName() + ": " + msg +
334  " (sub, total, level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
335  }
336  }
337  private:
339  RCP<TimeMonitor> levelTimeMonitor_;
340  };
341 
342 } // namespace MueLu
343 
344 #endif // MUELU_MONITOR_HPP
MueLu::FactoryMonitor::levelTimeMonitor_
RCP< TimeMonitor > levelTimeMonitor_
Total time spent on this level in this object and all its children.
Definition: MueLu_Monitor.hpp:278
MueLu::Level::GetLevelID
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
MueLu::Test
Print skeleton for the run, i.e. factory calls and used parameters.
Definition: MueLu_VerbosityLevel.hpp:81
MueLu::SubMonitor::SubMonitor
SubMonitor(const BaseClass &object, const std::string &msg, const std::string &label, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Definition: MueLu_Monitor.hpp:190
MueLu::Runtime1
Description of what is happening (more verbose)
Definition: MueLu_VerbosityLevel.hpp:63
MueLu::toString
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
Definition: MueLu_Utilities_decl.hpp:952
MueLu::Describable::description
virtual std::string description() const
Return a simple one-line description of this object.
Definition: MueLu_Describable.cpp:61
MueLu::FactoryMonitor
Timer to be used in factories. Similar to Monitor but with additional timers.
Definition: MueLu_Monitor.hpp:228
MueLu_VerbosityLevel.hpp
MueLu::PrintMonitor::object_
const BaseClass & object_
Definition: MueLu_Monitor.hpp:99
MueLu::Runtime0
One-liner description of what is happening.
Definition: MueLu_VerbosityLevel.hpp:62
MueLu::SubMonitor::SubMonitor
SubMonitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Definition: MueLu_Monitor.hpp:177
MueLu::FactoryMonitor::levelTimeMonitorExclusive_
RCP< MutuallyExclusiveTimeMonitor< Level > > levelTimeMonitorExclusive_
Total time spent on this level in this object only, excluding all children.
Definition: MueLu_Monitor.hpp:282
MueLu::Timings0
High level timing information (use Teuchos::TimeMonitor::summarize() to print)
Definition: MueLu_VerbosityLevel.hpp:74
MueLu::FactoryMonitor::timerIdentifier_
static int timerIdentifier_
Definition: MueLu_Monitor.hpp:231
MueLu
Namespace for MueLu classes and methods.
Definition: MueLu_BrickAggregationFactory_decl.hpp:76
MueLu::TimingsByLevel
Record timing information level by level. Must be used in combinaison with Timings0/Timings1.
Definition: MueLu_VerbosityLevel.hpp:76
MueLu::Monitor::Monitor
Monitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime0, MsgType timerLevel=Timings0)
Constructor.
Definition: MueLu_Monitor.hpp:125
MueLu::BaseClass
Base class for MueLu classes.
Definition: MueLu_BaseClass.hpp:61
MueLu::SubMonitor::printMonitor_
PrintMonitor printMonitor_
Definition: MueLu_Monitor.hpp:196
MueLu_Level.hpp
MUELU_TIMER_AS_STRING
#define MUELU_TIMER_AS_STRING
Definition: MueLu_Monitor.hpp:204
MueLu::SubFactoryMonitor
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
Definition: MueLu_Monitor.hpp:301
MueLu::FormattingHelper
Definition: MueLu_Monitor.hpp:64
MueLu::MsgType
MsgType
Definition: MueLu_VerbosityLevel.hpp:53
MueLu::SubMonitor
Timer to be used in non-factories. Similar to Monitor, but doesn't print object description.
Definition: MueLu_Monitor.hpp:168
MueLu::PrintMonitor::PrintMonitor
PrintMonitor()
MueLu::VerboseObject::GetOStream
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Definition: MueLu_VerboseObject.cpp:118
MueLu::SubMonitor::timerMonitor_
TimeMonitor timerMonitor_
Definition: MueLu_Monitor.hpp:197
MueLu::PrintMonitor::tabbed
bool tabbed
Definition: MueLu_Monitor.hpp:98
MueLu::Monitor::Monitor
Monitor(const BaseClass &object, const std::string &msg, const std::string &label, MsgType msgLevel=Runtime0, MsgType timerLevel=Timings0)
Constructor.
Definition: MueLu_Monitor.hpp:138
MueLu::PrintMonitor::PrintMonitor
PrintMonitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime0)
Constructor.
Definition: MueLu_Monitor.hpp:83
MueLu::TimeMonitor
Integrates Teuchos::TimeMonitor with MueLu verbosity system.
Definition: MueLu_TimeMonitor.hpp:68
MueLu::SubFactoryMonitor::SubFactoryMonitor
SubFactoryMonitor(const BaseClass &object, const std::string &msg, int levelID, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Definition: MueLu_Monitor.hpp:312
MueLu::PrintMonitor::~PrintMonitor
~PrintMonitor()
Definition: MueLu_Monitor.hpp:93
MueLu::FactoryMonitor::timerMonitorExclusive_
MutuallyExclusiveTimeMonitor< FactoryBase > timerMonitorExclusive_
Total time spent on all levels in this object only, excluding all children.
Definition: MueLu_Monitor.hpp:280
MueLu::VerboseObject::IsPrint
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
Definition: MueLu_VerboseObject.cpp:114
MueLu::Timings1
Detailed timing information (use Teuchos::TimeMonitor::summarize() to print)
Definition: MueLu_VerbosityLevel.hpp:75
MueLu::Monitor::printMonitor_
PrintMonitor printMonitor_
Manages printing.
Definition: MueLu_Monitor.hpp:145
MueLu_FactoryBase_fwd.hpp
MueLu::FactoryMonitor::FactoryMonitor
FactoryMonitor(const BaseClass &object, const std::string &msg, const Level &level, MsgType msgLevel=static_cast< MsgType >(Test|Runtime0), MsgType timerLevel=Timings0)
Constructor.
Definition: MueLu_Monitor.hpp:263
MueLu_TimeMonitor.hpp
MueLu::Monitor
Timer to be used in non-factories.
Definition: MueLu_Monitor.hpp:116
MueLu::MutuallyExclusiveTimeMonitor
Similar to TimeMonitor, but uses MutuallyExclusiveTime objects.
Definition: MueLu_TimeMonitor.hpp:144
MueLu::FactoryMonitor::FactoryMonitor
FactoryMonitor(const BaseClass &object, const std::string &msg, int levelID, MsgType msgLevel=static_cast< MsgType >(Test|Runtime0), MsgType timerLevel=Timings0)
Constructor.
Definition: MueLu_Monitor.hpp:241
MueLu::SubFactoryMonitor::levelTimeMonitor_
RCP< TimeMonitor > levelTimeMonitor_
Total time spent on this level in this object and all children.
Definition: MueLu_Monitor.hpp:339
MueLu::Monitor::timerMonitor_
TimeMonitor timerMonitor_
Records total time spent in this object and all its children, over all levels.
Definition: MueLu_Monitor.hpp:147
MueLu::FormattingHelper::getColonLabel
static std::string getColonLabel(const std::string &label)
Helper function for object label.
Definition: MueLu_Monitor.hpp:66
MueLu::SubFactoryMonitor::SubFactoryMonitor
SubFactoryMonitor(const BaseClass &object, const std::string &msg, const Level &level, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Definition: MueLu_Monitor.hpp:328
MueLu::Level
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
MueLu_BaseClass.hpp
MueLu::PrintMonitor
Definition: MueLu_Monitor.hpp:78
MueLu::Describable::ShortClassName
virtual std::string ShortClassName() const
Return the class name of the object, without template parameters and without namespace.
Definition: MueLu_Describable.cpp:74