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 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "MueLu_ConfigDefs.hpp"
11 #include "MueLu_TimeMonitor.hpp"
12 #include "MueLu_FactoryBase.hpp"
13 #include "MueLu_Level.hpp"
14 
15 namespace MueLu {
16 
17 namespace {
19 std::string cleanupLabel(const std::string& label) {
20  // Our main interest is to remove double quotes from the label
21  // since tracing formats can be written in JSON/YAML
22  auto clean_label = label;
23  std::vector<std::string> bad_values = {"\""};
24  std::vector<std::string> good_values = {"'"};
25  for (size_t i = 0; i < bad_values.size(); ++i) {
26  const auto& bad_value = bad_values[i];
27  const auto& good_value = good_values[i];
28  size_t pos = 0;
29  while ((pos = clean_label.find(bad_value, pos)) != std::string::npos) {
30  clean_label.replace(pos, bad_value.size(), good_value);
31  pos += good_value.size();
32  }
33  }
34  return clean_label;
35 }
36 } // namespace
37 
38 TimeMonitor::TimeMonitor(const BaseClass& object, const std::string& msg, MsgType timerLevel) {
39  // Inherit props from 'object'
40  SetVerbLevel(object.GetVerbLevel());
42 
43  if (IsPrint(timerLevel) &&
44  /* disable timer if never printed: */ (IsPrint(RuntimeTimings) || (!IsPrint(NoTimeReport)))) {
45  if (!IsPrint(NoTimeReport)) {
46  // TODO: there is no function to register a timer in Teuchos::TimeMonitor after the creation of the timer. But would be useful...
47  timer_ = Teuchos::TimeMonitor::getNewTimer(cleanupLabel("MueLu: " + msg));
48  } else {
49  timer_ = rcp(new Teuchos::Time(cleanupLabel("MueLu: " + msg)));
50  }
51 
52  // Start the timer (this is what is done by Teuchos::TimeMonitor)
53  timer_->start();
55 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
56  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
57  if (nonnull(stackedTimer))
58  stackedTimer->start(timer_->name());
59 #endif
60  }
61 } // TimeMonitor::TimeMonitor()
62 
64 
66  // Stop the timer if present
67  if (timer_ != Teuchos::null) {
68  timer_->stop();
69 #ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
70  try {
71  const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
72  if (nonnull(stackedTimer))
73  stackedTimer->stop(timer_->name());
74  } catch (std::runtime_error&) {
75  std::ostringstream warning;
76  warning << "\n*********************************************************************\n"
77  "WARNING: Overlapping timers detected!\n"
78  "A TimeMonitor timer was stopped before a nested subtimer was\n"
79  "stopped. This is not allowed by the StackedTimer. This corner case\n"
80  "typically occurs if the TimeMonitor is stored in an RCP and the RCP is\n"
81  "assigned to a new timer. To disable this warning, either fix the\n"
82  "ordering of timer creation and destuction or disable the StackedTimer\n";
83  std::cout << warning.str() << std::endl;
85  }
86 #endif
87  }
88 } // TimeMonitor::~TimeMonitor()
89 
92 
93 } // namespace MueLu
static Teuchos::RCP< Teuchos::StackedTimer > getStackedTimer()
RCP< Teuchos::Time > timer_
static RCP< Time > getNewTimer(const std::string &name)
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
double stop()
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
VerbLevel GetVerbLevel() const
Get the verbosity level.
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
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)
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
void incrementNumCalls()