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 
67  class PrintMonitor : public BaseClass {
68 
69  public:
70 
72  PrintMonitor(const BaseClass& object, const std::string& msg, MsgType msgLevel = Runtime0) : object_(object) {
73  tabbed = false;
74  if (object_.IsPrint(msgLevel)) {
75  // Print description and new indent
76  object_.GetOStream(msgLevel, 0) << msg << std::endl;
77  object_.getOStream()->pushTab();
78  tabbed = true;
79  }
80  }
81 
82  ~PrintMonitor() { if (tabbed) object_.getOStream()->popTab(); }
83 
84  private:
85  PrintMonitor();
86 
87  bool tabbed;
89  };
90 
105  class Monitor: public BaseClass {
106  public:
114  Monitor(const BaseClass& object, const std::string & msg, MsgType msgLevel = Runtime0, MsgType timerLevel = Timings0)
115  : printMonitor_(object, msg + " (" + object.description() + ")", msgLevel),
116  timerMonitor_(object, object.ShortClassName() + ": " + msg + " (total)", timerLevel)
117  { }
118 
119  private:
124  };
125 
126  //---------------------------------------------------------------------------------------------------
127 
144  class SubMonitor: public BaseClass {
145  public:
153  SubMonitor(const BaseClass& object, const std::string & msg, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
154  : printMonitor_(object, msg, msgLevel),
155  timerMonitor_(object, object.ShortClassName() + ": " + msg + " (sub, total)", timerLevel)
156  { }
157 
158  private:
161  };
162 
163 //convert integer timer number to string
164 #ifdef HAVE_MUELU_PROFILING
165 #define MUELU_TIMER_AS_STRING static_cast<std::ostringstream*>( &(std::ostringstream() << " " << timerIdentifier_++) )->str()
166 #else
167 #define MUELU_TIMER_AS_STRING
168 #endif
169 
170  //---------------------------------------------------------------------------------------------------
171 
191  class FactoryMonitor: public Monitor {
192  public:
193 
194  static int timerIdentifier_;
195 
204  FactoryMonitor(const BaseClass& object, const std::string & msg, int levelID, MsgType msgLevel = static_cast<MsgType>(Test | Runtime0), MsgType timerLevel = Timings0)
205  : Monitor(object, msg, msgLevel, timerLevel),
206  timerMonitorExclusive_(object, object.ShortClassName() + ": " + msg, timerLevel)
207  {
208  if (object.IsPrint(TimingsByLevel)) {
209  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
210  " (total, level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
212  MUELU_TIMER_AS_STRING + ": " + msg + " (level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
213  }
214  }
215 
226  FactoryMonitor(const BaseClass& object, const std::string & msg, const Level & level, MsgType msgLevel = static_cast<MsgType>(Test | Runtime0), MsgType timerLevel = Timings0)
227  : Monitor(object, msg, msgLevel, timerLevel),
228  timerMonitorExclusive_(object, object.ShortClassName() + ": " + msg, timerLevel)
229  {
230  if (object.IsPrint(TimingsByLevel)) {
231  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
232  " (total, level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
234  MUELU_TIMER_AS_STRING + ": " + msg + " (level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
235  }
236  }
237 
238  private:
245  };
246 
247  //---------------------------------------------------------------------------------------------------
248 
264  public:
265 
274  SubFactoryMonitor(const BaseClass& object, const std::string & msg, int levelID, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
275  : SubMonitor(object, msg, msgLevel, timerLevel)
276  {
277  if (object.IsPrint(TimingsByLevel))
278  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
279  " (sub, total, level=" + Teuchos::Utils::toString(levelID) + ")", timerLevel));
280  }
281 
290  SubFactoryMonitor(const BaseClass& object, const std::string & msg, const Level & level, MsgType msgLevel = Runtime1, MsgType timerLevel = Timings1)
291  : SubMonitor(object, msg, msgLevel, timerLevel)
292  {
293  if (object.IsPrint(TimingsByLevel))
294  levelTimeMonitor_ = rcp(new TimeMonitor(object, object.ShortClassName() + ": " + msg +
295  " (sub, total, level=" + Teuchos::Utils::toString(level.GetLevelID()) + ")", timerLevel));
296  }
297  private:
300  };
301 
302 } // namespace MueLu
303 
304 #endif // MUELU_MONITOR_HPP
RCP< TimeMonitor > levelTimeMonitor_
Total time spent on this level in this object and all children.
High level timing information (use Teuchos::TimeMonitor::summarize() to print)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
virtual std::string ShortClassName() const
Return the class name of the object, without template parameters and without namespace.
Timer to be used in factories. Similar to Monitor but with additional timers.
Timer to be used in non-factories. Similar to Monitor, but doesn&#39;t print object description.
One-liner description of what is happening.
virtual RCP< FancyOStream > getOStream() const
Namespace for MueLu classes and methods.
MutuallyExclusiveTimeMonitor< FactoryBase > timerMonitorExclusive_
Total time spent on all levels in this object only, excluding all children.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
Integrates Teuchos::TimeMonitor with MueLu verbosity system.
Print skeleton for the run, i.e. factory calls and used parameters.
static std::string toString(const double &x)
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:76
SubFactoryMonitor(const BaseClass &object, const std::string &msg, const Level &level, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Monitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime0, MsgType timerLevel=Timings0)
Constructor.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
RCP< TimeMonitor > levelTimeMonitor_
Total time spent on this level in this object and all its children.
#define MUELU_TIMER_AS_STRING
FactoryMonitor(const BaseClass &object, const std::string &msg, int levelID, MsgType msgLevel=static_cast< MsgType >(Test|Runtime0), MsgType timerLevel=Timings0)
Constructor.
Detailed timing information (use Teuchos::TimeMonitor::summarize() to print)
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
Similar to TimeMonitor, but uses MutuallyExclusiveTime objects.
PrintMonitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime0)
Constructor.
FactoryMonitor(const BaseClass &object, const std::string &msg, const Level &level, MsgType msgLevel=static_cast< MsgType >(Test|Runtime0), MsgType timerLevel=Timings0)
Constructor.
TimeMonitor timerMonitor_
Base class for MueLu classes.
Record timing information level by level. Must be used in combinaison with Timings0/Timings1.
SubFactoryMonitor(const BaseClass &object, const std::string &msg, int levelID, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.
Timer to be used in non-factories.
RCP< MutuallyExclusiveTimeMonitor< Level > > levelTimeMonitorExclusive_
Total time spent on this level in this object only, excluding all children.
PrintMonitor printMonitor_
Manages printing.
Description of what is happening (more verbose)
const BaseClass & object_
TimeMonitor timerMonitor_
Records total time spent in this object and all its children, over all levels.
virtual std::string description() const
Return a simple one-line description of this object.
PrintMonitor printMonitor_
SubMonitor(const BaseClass &object, const std::string &msg, MsgType msgLevel=Runtime1, MsgType timerLevel=Timings1)
Constructor.