]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add function to access timer data from within program 4369/head
authorJonathan Robey <class4kayaker@gmail.com>
Wed, 10 May 2017 23:25:28 +0000 (16:25 -0700)
committerJonathan Robey <class4kayaker@gmail.com>
Sat, 27 May 2017 05:17:09 +0000 (22:17 -0700)
doc/news/changes/minor/20170522JonathanRobey [new file with mode: 0644]
include/deal.II/base/timer.h
source/base/timer.cc
tests/base/timer_03.cc [new file with mode: 0644]
tests/base/timer_03.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20170522JonathanRobey b/doc/news/changes/minor/20170522JonathanRobey
new file mode 100644 (file)
index 0000000..354659e
--- /dev/null
@@ -0,0 +1,3 @@
+New: Add method to access timing data from TimerOutput from code.
+<br>
+(Jonathan Robey, 2017/05/22)
index a7dc7d8d712e879788139337fed197c6262a0a8e..44a9886ec8fbfe657dbe7fed4dcca8333b1a9fde 100644 (file)
@@ -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 &section_name = std::string());
 
+  /**
+   * Get a map with the collected data of the specified type for each subsection
+   */
+  std::map<std::string, double> get_summary_data (const OutputData kind) const;
+
   /**
    * Print a formatted table that summarizes the time consumed in the various
    * sections.
index 6f877448d645d7e288ac002f8bc9d8e5d6a85904..14bae115b578e73906083ec1368094b82902c2da 100644 (file)
@@ -474,6 +474,32 @@ TimerOutput::leave_subsection (const std::string &section_name)
 
 
 
+std::map<std::string, double>
+TimerOutput::get_summary_data (const OutputData kind) const
+{
+  std::map<std::string, double> output;
+  for (const auto &section : 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 (file)
index 0000000..31139a7
--- /dev/null
@@ -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 <deal.II/base/timer.h>
+#include <deal.II/base/logstream.h>
+#include <fstream>
+#include <cmath>
+#include <iomanip>
+
+// 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)<eps)
+    {
+      deallog << "OK" << std::endl;
+    }
+  else
+    {
+      deallog << "Value " << v1 << " should be " << v2 << std::endl;
+    }
+}
+
+// burn computer time
+
+double s = 0.;
+void burn (unsigned int n)
+{
+  for (unsigned int i=0 ; i<n ; ++i)
+    {
+      for (unsigned int j=1 ; j<100000 ; ++j)
+        {
+          s += 1./j * i;
+        }
+    }
+}
+
+
+int main ()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  Timer t1,t2;
+  TimerOutput tO(std::cout,
+                 TimerOutput::summary,
+                 TimerOutput::cpu_times);
+  tO.enter_section("Section1");
+  tO.enter_section("Section3");
+  tO.enter_section("Section4");
+  burn (50);
+  tO.exit_section("Section1");
+  tO.exit_section("Section4");
+  tO.enter_section("Section2");
+  burn (50);
+  tO.exit_section("Section2");
+  tO.exit_section("Section3");
+  tO.enter_section("Section4");
+  burn (50);
+  tO.exit_section("Section4");
+
+  std::map<std::string, double> cpu_times = tO.get_summary_data(TimerOutput::OutputData::total_cpu_time);
+  std::map<std::string, double> 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 (file)
index 0000000..5cf798a
--- /dev/null
@@ -0,0 +1,8 @@
+
+DEAL::OK
+DEAL::OK
+DEAL::OK
+DEAL::OK
+DEAL::OK
+DEAL::OK
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.