MueLu Version of the Day
Loading...
Searching...
No Matches
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
15namespace MueLu {
16
17namespace {
19std::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
38TimeMonitor::TimeMonitor(const BaseClass& object, const std::string& msg, MsgType timerLevel) {
39 // Inherit props from 'object'
40 SetVerbLevel(object.GetVerbLevel());
42
43#ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
44 useStackedTimer_ = !Teuchos::TimeMonitor::stackedTimerNameIsDefault();
45#else
46 useStackedTimer_ = false;
47#endif
48
49 if (IsPrint(timerLevel) &&
50 /* disable timer if never printed: */ (IsPrint(RuntimeTimings) || (!IsPrint(NoTimeReport)))) {
51 label_ = cleanupLabel("MueLu: " + msg);
52
53 if (!IsPrint(NoTimeReport)) {
54#ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
55 if (useStackedTimer_) {
56 const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
57 stackedTimer->start(label_);
58 } else
59#endif
60 {
61 // TODO: there is no function to register a timer in Teuchos::TimeMonitor after the creation of the timer. But would be useful...
62 static std::map<std::string, RCP<Teuchos::Time>> timers;
63 auto it = timers.find(label_);
64 if (it == timers.end()) {
65 timer_ = Teuchos::TimeMonitor::getNewTimer(label_);
66 timers[label_] = timer_;
67 } else {
68 timer_ = it->second;
69 // Start the timer (this is what is done by Teuchos::TimeMonitor)
70 timer_->incrementNumCalls();
71 timer_->start();
72 }
73 }
74 } else {
75 timer_ = rcp(new Teuchos::Time(label_));
76 // Start the timer (this is what is done by Teuchos::TimeMonitor)
77 timer_->incrementNumCalls();
78 timer_->start();
79 }
80 }
81} // TimeMonitor::TimeMonitor()
82
84
86 // Stop the timer if present
87
88#ifdef HAVE_TEUCHOS_ADD_TIME_MONITOR_TO_STACKED_TIMER
89 if (useStackedTimer_ && (!label_.empty())) {
90 try {
91 const auto stackedTimer = Teuchos::TimeMonitor::getStackedTimer();
92 stackedTimer->stop(label_);
93 } catch (std::runtime_error&) {
94 std::ostringstream warning;
95 warning << "\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;
103 Teuchos::TimeMonitor::setStackedTimer(Teuchos::null);
104 }
105 } else
106#endif
107 {
108 if (timer_ != Teuchos::null) {
109 timer_->stop();
110 }
111 }
112} // TimeMonitor::~TimeMonitor()
113
114} // namespace MueLu
Base class for MueLu classes.
RCP< Teuchos::Time > timer_
VerbLevel GetVerbLevel() const
Get the verbosity level.
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
void SetVerbLevel(const VerbLevel verbLevel)
Set the verbosity level of this object.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
Namespace for MueLu classes and methods.
@ RuntimeTimings
Timers that are enabled (using Timings0/Timings1) will be printed during the execution.
@ NoTimeReport
By default, enabled timers appears in the teuchos time monitor summary. Use this option if you do not...