From be30d7233f6d4dd285ba72749953efd445658e77 Mon Sep 17 00:00:00 2001 From: Jonathan Robey Date: Wed, 10 May 2017 16:25:28 -0700 Subject: [PATCH] Add function to access timer data from within program --- doc/news/changes/minor/20170522JonathanRobey | 3 + include/deal.II/base/timer.h | 25 +++++ source/base/timer.cc | 26 +++++ tests/base/timer_03.cc | 108 +++++++++++++++++++ tests/base/timer_03.output | 8 ++ 5 files changed, 170 insertions(+) create mode 100644 doc/news/changes/minor/20170522JonathanRobey create mode 100644 tests/base/timer_03.cc create mode 100644 tests/base/timer_03.output diff --git a/doc/news/changes/minor/20170522JonathanRobey b/doc/news/changes/minor/20170522JonathanRobey new file mode 100644 index 0000000000..354659e9b0 --- /dev/null +++ b/doc/news/changes/minor/20170522JonathanRobey @@ -0,0 +1,3 @@ +New: Add method to access timing data from TimerOutput from code. +
+(Jonathan Robey, 2017/05/22) diff --git a/include/deal.II/base/timer.h b/include/deal.II/base/timer.h index a7dc7d8d71..44a9886ec8 100644 --- a/include/deal.II/base/timer.h +++ b/include/deal.II/base/timer.h @@ -449,6 +449,26 @@ public: never }; + /** + * An enumeration data type that describes the type of data to return + * when fetching the data from the timer. + */ + enum OutputData + { + /** + * Output CPU times. + */ + total_cpu_time, + /** + * Output wall clock times. + */ + total_wall_time, + /** + * Output number of calls. + */ + n_calls + }; + /** * An enumeration data type that describes whether to show CPU times, wall * times, or both CPU and wall times whenever we generate output. @@ -589,6 +609,11 @@ public: */ void exit_section (const std::string §ion_name = std::string()); + /** + * Get a map with the collected data of the specified type for each subsection + */ + std::map get_summary_data (const OutputData kind) const; + /** * Print a formatted table that summarizes the time consumed in the various * sections. diff --git a/source/base/timer.cc b/source/base/timer.cc index 6f877448d6..14bae115b5 100644 --- a/source/base/timer.cc +++ b/source/base/timer.cc @@ -474,6 +474,32 @@ TimerOutput::leave_subsection (const std::string §ion_name) +std::map +TimerOutput::get_summary_data (const OutputData kind) const +{ + std::map output; + for (const auto §ion : sections) + { + switch (kind) + { + case TimerOutput::OutputData::total_cpu_time: + output[section.first] = section.second.total_cpu_time; + break; + case TimerOutput::OutputData::total_wall_time: + output[section.first] = section.second.total_wall_time; + break; + case TimerOutput::OutputData::n_calls: + output[section.first] = section.second.n_calls; + break; + default: + Assert(false, ExcNotImplemented()); + } + } + return output; +} + + + void TimerOutput::print_summary () const { diff --git a/tests/base/timer_03.cc b/tests/base/timer_03.cc new file mode 100644 index 0000000000..31139a7f71 --- /dev/null +++ b/tests/base/timer_03.cc @@ -0,0 +1,108 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 1998 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +#include "../tests.h" +#include +#include +#include +#include +#include + +// compute the ratio of two measurements and compare to +// the expected value. + +void compare (double t1, double t2, double ratio) +{ + double r = t2/t1; + double d = std::fabs(r-ratio) / ratio; + + // relative error < 25%? + if (d <= .25) + { + deallog << "OK" << std::endl; + } + else + { + deallog << "Ratio " << r << " should be " << ratio << std::endl; + } +} + +void match(double v1, double v2) +{ + double eps = 1.0e-6; + if (std::fabs(v1-v2) cpu_times = tO.get_summary_data(TimerOutput::OutputData::total_cpu_time); + std::map calls = tO.get_summary_data(TimerOutput::OutputData::n_calls); + + match(calls["Section1"], 1.0); + match(calls["Section2"], 1.0); + match(calls["Section3"], 1.0); + match(calls["Section4"], 2.0); + + compare (cpu_times["Section1"],cpu_times["Section2"],1.); + compare (cpu_times["Section1"],cpu_times["Section3"],2.); + compare (cpu_times["Section3"],cpu_times["Section4"],1.); +} + diff --git a/tests/base/timer_03.output b/tests/base/timer_03.output new file mode 100644 index 0000000000..5cf798a265 --- /dev/null +++ b/tests/base/timer_03.output @@ -0,0 +1,8 @@ + +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK -- 2.39.5