MueLu  Version of the Day
MueLu_TimeMonitor.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_TIMEMONITOR_HPP
47 #define MUELU_TIMEMONITOR_HPP
48 
49 #include <string>
50 #include <Teuchos_DefaultComm.hpp>
51 #include <Teuchos_Time.hpp>
52 #include <Teuchos_TimeMonitor.hpp>
53 #include "MueLu_ConfigDefs.hpp"
54 #include "MueLu_BaseClass.hpp"
55 #include "MueLu_VerboseObject.hpp"
57 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
58 #include "Teuchos_StackedTimer.hpp"
59 #include <sstream>
60 #endif
61 
62 namespace MueLu {
63 
68  class TimeMonitor : public BaseClass {
69 
70  public:
71 
72  TimeMonitor(const BaseClass& object, const std::string& msg, MsgType timerLevel = Timings0) {
73  // Inherit props from 'object'
74  SetVerbLevel (object.GetVerbLevel());
76 
77  if (IsPrint(timerLevel) &&
78  /* disable timer if never printed: */ (IsPrint(RuntimeTimings) || (!IsPrint(NoTimeReport)))) {
79 
80  if (!IsPrint(NoTimeReport)) {
81  // TODO: there is no function to register a timer in Teuchos::TimeMonitor after the creation of the timer. But would be useful...
82  timer_ = Teuchos::TimeMonitor::getNewTimer("MueLu: " + msg);
83  } else {
84  timer_ = rcp(new Teuchos::Time("MueLu: " + msg));
85  }
86 
87  // Start the timer (this is what is done by Teuchos::TimeMonitor)
88  timer_->start();
89  timer_->incrementNumCalls();
90 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
91  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
92  if (nonnull(stackedTimer))
93  stackedTimer->start(timer_->name());
94 #endif
95  }
96  }
97 
99  // Stop the timer if present
100  if (timer_ != Teuchos::null) {
101  timer_->stop();
102 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
103  try {
104  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
105  if (nonnull(stackedTimer))
106  stackedTimer->stop(timer_->name());
107  }
108  catch (std::runtime_error) {
109  std::ostringstream warning;
110  warning <<
111  "\n*********************************************************************\n"
112  "WARNING: Overlapping timers detected!\n"
113  "A TimeMonitor timer was stopped before a nested subtimer was\n"
114  "stopped. This is not allowed by the StackedTimer. This corner case\n"
115  "typically occurs if the TimeMonitor is stored in an RCP and the RCP is\n"
116  "assigned to a new timer. To disable this warning, either fix the\n"
117  "ordering of timer creation and destuction or disable the StackedTimer\n"
118  "support in the TimeMonitor by setting the StackedTimer to null\n"
119  "with:\n"
120  "Teuchos::TimeMonitor::setStackedTimer(Teuchos::null)\n"
121  "*********************************************************************\n";
122  std::cout << warning.str() << std::endl;
123  Teuchos::TimeMonitor::setStackedTimer(Teuchos::null);
124  }
125 #endif
126  }
127  }
128 
129  protected:
131 
132  private:
133  RCP<Teuchos::Time> timer_;
134  };
135 
136  //TODO: code duplication MutuallyExclusiveTimeMonitor / TimeMonitor
137 
143  template <class TagName>
145 
146  public:
147 
154  MutuallyExclusiveTimeMonitor(const BaseClass& object, const std::string& msg, MsgType timerLevel = Timings0) {
155  // Inherit props from 'object'
156  SetVerbLevel (object.GetVerbLevel());
158 
159  if (IsPrint(timerLevel) &&
160  /* disable timer if never printed: */ (IsPrint(RuntimeTimings) || (!IsPrint(NoTimeReport)))) {
161 
162  if (!IsPrint(NoTimeReport)) {
163  timer_ = MutuallyExclusiveTime<TagName>::getNewTimer("MueLu: " + msg /*+ " (MutuallyExclusive)" */);
164  } else {
165  timer_ = rcp(new MutuallyExclusiveTime<TagName> ("MueLu: " + msg /*+ " (MutuallyExclusive)" */));
166  }
167 
168  timer_->start();
169  timer_->incrementNumCalls();
170  }
171  }
172 
174  // Stop the timer if present
175  if (timer_ != Teuchos::null)
176  timer_->stop();
177  }
178 
179  protected:
181 
182  private:
183  RCP<MutuallyExclusiveTime<TagName> > timer_; // keep a reference on the timer to print stats if RuntimeTimings=ON //TODO:use base class instead
184  };
185 
186 } // namespace MueLu
187 
188 #endif // MUELU_TIMEMONITOR_HPP
189 
190 
High level timing information (use Teuchos::TimeMonitor::summarize() to print)
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
RCP< Teuchos::Time > timer_
TimeMonitor(const BaseClass &object, const std::string &msg, MsgType timerLevel=Timings0)
static RCP< MutuallyExclusiveTime< TagName > > getNewTimer(const std::string &name)
Return a new MutuallyExclusiveTime that is registered with the Teuchos::TimeMonitor (for timer summar...
Namespace for MueLu classes and methods.
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.
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
MutuallyExclusiveTimeMonitor(const BaseClass &object, const std::string &msg, MsgType timerLevel=Timings0)
Constructor.
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
Similar to TimeMonitor, but uses MutuallyExclusiveTime objects.
By default, enabled timers appears in the teuchos time monitor summary. Use this option if you do not...
RCP< MutuallyExclusiveTime< TagName > > timer_
Timers that are enabled (using Timings0/Timings1) will be printed during the execution.
Base class for MueLu classes.
This class wraps a Teuchos::Time and maintains a mutually exclusive property between wrapped timers...
VerbLevel GetVerbLevel() const
Get the verbosity level.