Skip to content

Commit

Permalink
Teuchos: add hooks to disable/reenable stacked timers for async tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rppawlo committed Jun 9, 2021
1 parent 332e870 commit 440203d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
6 changes: 6 additions & 0 deletions packages/teuchos/comm/src/Teuchos_StackedTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,10 @@ void StackedTimer::enableVerboseTimestamps(const unsigned levels)
void StackedTimer::setVerboseOstream(const Teuchos::RCP<std::ostream>& os)
{verbose_ostream_ = os;}

void StackedTimer::disableTimers()
{enable_timers_ = false;}

void StackedTimer::enableTimers()
{enable_timers_ = true;}

} //namespace Teuchos
52 changes: 34 additions & 18 deletions packages/teuchos/comm/src/Teuchos_StackedTimer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ class StackedTimer
: timer_(0,name,nullptr,false),
enable_verbose_(false),
verbose_timestamp_levels_(0), // 0 disables
verbose_ostream_(Teuchos::rcpFromRef(std::cout))
verbose_ostream_(Teuchos::rcpFromRef(std::cout)),
enable_timers_(true)
{
top_ = &timer_;
if (start_base_timer)
Expand Down Expand Up @@ -516,15 +517,17 @@ class StackedTimer
*/
void start(const std::string name,
const bool push_kokkos_profiling_region = true) {
if (top_ == nullptr)
top_ = timer_.start(name.c_str());
else
top_ = top_->start(name.c_str());
if (enable_timers_) {
if (top_ == nullptr)
top_ = timer_.start(name.c_str());
else
top_ = top_->start(name.c_str());
#if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOSCORE)
if (push_kokkos_profiling_region) {
::Kokkos::Profiling::pushRegion(name);
}
if (push_kokkos_profiling_region) {
::Kokkos::Profiling::pushRegion(name);
}
#endif
}
if (enable_verbose_) {
if (!verbose_timestamp_levels_) {
*verbose_ostream_ << "STARTING: " << name << std::endl;
Expand Down Expand Up @@ -553,15 +556,17 @@ class StackedTimer
*/
void stop(const std::string &name,
const bool pop_kokkos_profiling_region = true) {
if (top_)
top_ = top_->stop(name);
else
timer_.BaseTimer::stop();
if (enable_timers_) {
if (top_)
top_ = top_->stop(name);
else
timer_.BaseTimer::stop();
#if defined(HAVE_TEUCHOS_KOKKOS_PROFILING) && defined(HAVE_TEUCHOSCORE_KOKKOSCORE)
if (pop_kokkos_profiling_region) {
::Kokkos::Profiling::popRegion();
}
if (pop_kokkos_profiling_region) {
::Kokkos::Profiling::popRegion();
}
#endif
}
if (enable_verbose_) {
if (!verbose_timestamp_levels_) {
*verbose_ostream_ << "STOPPING: " << name << std::endl;
Expand Down Expand Up @@ -733,15 +738,23 @@ class StackedTimer
*/
std::string reportWatchrXML(const std::string& name, Teuchos::RCP<const Teuchos::Comm<int> > comm);

// If set to true, print timer start/stop to verbose ostream.
/// If set to true, print timer start/stop to verbose ostream.
void enableVerbose(const bool enable_verbose);

// Enable timestamps in verbose mode for the number of levels specified.
/// Enable timestamps in verbose mode for the number of levels specified.
void enableVerboseTimestamps(const unsigned levels);

// Set the ostream for verbose mode(defaults to std::cout).
/// Set the ostream for verbose mode(defaults to std::cout).
void setVerboseOstream(const Teuchos::RCP<std::ostream>& os);

/// Once called, the start and stop calls are no-ops. Used to stop
/// timers during asynchronous execution.
void disableTimers();

/// Once called, the start and stop calls are reenabled. Used to
/// restart timers after a call to disableTimers().
void enableTimers();

protected:
/// Current level running
LevelTimer *top_;
Expand Down Expand Up @@ -795,6 +808,9 @@ class StackedTimer
/// For debugging, this is the ostream used for printing.
Teuchos::RCP<std::ostream> verbose_ostream_;

/// Used to disable timers for asynchronous work.
bool enable_timers_;

/**
* Flatten the timers into a single array
*/
Expand Down
44 changes: 44 additions & 0 deletions packages/teuchos/comm/test/StackedTimer/stacked_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,47 @@ TEUCHOS_UNIT_TEST(StackedTimer, VerboseTimestamps) {
TEST_ASSERT(os.str().find("L3") == std::string::npos);
}
#endif

// Tests that we can turn off timers for regions of asychronous
// execution.
TEUCHOS_UNIT_TEST(StackedTimer, DisableTimers)
{
Teuchos::StackedTimer timer("My New Timer");
timer.start("Total Time");
{
for (int i=0; i < 10; ++i) {

timer.start("Assembly");
timer.stop("Assembly");

// Async execution means the timers will stop out of order. Out
// of order timers causes exception to be thrown.

timer.disableTimers(); // Stop recording timers
timer.start("Solve");
{
timer.start("Prec");

// This stop() is out of order and would trigger an exception
// if we did not disable the timers above.
timer.stop("Solve");

timer.stop("Prec");
}
timer.enableTimers(); // Start recording timers

// Make sure the timers are reenabled
timer.start("Restarted");
timer.stop("Restarted");
}
}
timer.stop("Total Time");
timer.stopBaseTimer();

TEST_EQUALITY((timer.findTimer("My New Timer@Total Time")).count, 1);
TEST_EQUALITY((timer.findTimer("My New Timer@Total Time@Assembly")).count, 10);
TEST_EQUALITY((timer.findTimer("My New Timer@Total Time@Restarted")).count, 10);
// Should not exist since we disabled the timers
TEST_THROW(timer.findTimer("My New Timer@Total Time@Solve"),std::runtime_error);
TEST_THROW(timer.findTimer("My New Timer@Total Time@Solve@Prec"),std::runtime_error);
}

0 comments on commit 440203d

Please sign in to comment.