MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_TimeMonitor.cpp
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 
47 #include "MueLu_ConfigDefs.hpp"
48 #include "MueLu_TimeMonitor.hpp"
49 #include "MueLu_FactoryBase.hpp"
50 #include "MueLu_Level.hpp"
51 
52 namespace MueLu {
53 
54  TimeMonitor::TimeMonitor(const BaseClass& object, const std::string& msg, MsgType timerLevel) {
55  // Inherit props from 'object'
56  SetVerbLevel (object.GetVerbLevel());
58 
59  if (IsPrint(timerLevel) &&
60  /* disable timer if never printed: */ (IsPrint(RuntimeTimings) || (!IsPrint(NoTimeReport)))) {
61 
62  if (!IsPrint(NoTimeReport)) {
63  // TODO: there is no function to register a timer in Teuchos::TimeMonitor after the creation of the timer. But would be useful...
64  timer_ = Teuchos::TimeMonitor::getNewTimer("MueLu: " + msg);
65  } else {
66  timer_ = rcp(new Teuchos::Time("MueLu: " + msg));
67  }
68 
69  // Start the timer (this is what is done by Teuchos::TimeMonitor)
70  timer_->start();
72 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
73  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
74  if (nonnull(stackedTimer))
75  stackedTimer->start(timer_->name());
76 #endif
77  }
78  } //TimeMonitor::TimeMonitor()
79 
81 
83  // Stop the timer if present
84  if (timer_ != Teuchos::null) {
85  timer_->stop();
86 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
87  try {
88  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
89  if (nonnull(stackedTimer))
90  stackedTimer->stop(timer_->name());
91  }
92  catch (std::runtime_error&) {
93  std::ostringstream warning;
94  warning <<
95  "\n*********************************************************************\n"
96  "WARNING: Overlapping timers detected!\n"
97  "A TimeMonitor timer was stopped before a nested subtimer was\n"
98  "stopped. This is not allowed by the StackedTimer. This corner case\n"
99  "typically occurs if the TimeMonitor is stored in an RCP and the RCP is\n"
100  "assigned to a new timer. To disable this warning, either fix the\n"
101  "ordering of timer creation and destuction or disable the StackedTimer\n";
102  std::cout << warning.str() << std::endl;
104  }
105 #endif
106  }
107  } //TimeMonitor::~TimeMonitor()
108 
111 
112 } // namespace MueLu
static Teuchos::RCP< Teuchos::StackedTimer > getStackedTimer()
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
RCP< Teuchos::Time > timer_
static RCP< Time > getNewTimer(const std::string &name)
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
VerbLevel GetVerbLevel() const
Get the verbosity level.
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
double stop()
Similar to TimeMonitor, but uses MutuallyExclusiveTime objects.
static void setStackedTimer(const Teuchos::RCP< Teuchos::StackedTimer > &t)
By default, enabled timers appears in the teuchos time monitor summary. Use this option if you do not...
const std::string & name() const
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
Timers that are enabled (using Timings0/Timings1) will be printed during the execution.
Base class for MueLu classes.
bool nonnull(const boost::shared_ptr< T > &p)
void incrementNumCalls()
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.