Reference documentation for deal.II version 9.0.0
timer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_timer_h
17 #define dealii_timer_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/conditional_ostream.h>
21 #include <deal.II/base/mpi.h>
22 #include <deal.II/base/thread_management.h>
23 #include <deal.II/base/utilities.h>
24 
25 #include <chrono>
26 #include <list>
27 #include <map>
28 #include <string>
29 
30 DEAL_II_NAMESPACE_OPEN
31 
37 struct CPUClock
38 {
44  typedef std::chrono::microseconds duration;
45 
49  typedef duration::rep rep;
50 
54  typedef duration::period period;
55 
59  typedef std::chrono::time_point<CPUClock, duration> time_point;
60 
64  static const bool is_steady = true;
65 
71  static time_point now() noexcept;
72 };
73 
117 class Timer
118 {
119 public:
123  Timer ();
124 
139  Timer (MPI_Comm mpi_communicator,
140  const bool sync_lap_times = false);
141 
150  DEAL_II_DEPRECATED
151  const Utilities::MPI::MinMaxAvg &get_data() const;
152 
159  const Utilities::MPI::MinMaxAvg &get_last_lap_wall_time_data() const;
160 
170  DEAL_II_DEPRECATED
171  const Utilities::MPI::MinMaxAvg &get_total_data() const;
172 
179  const Utilities::MPI::MinMaxAvg &get_accumulated_wall_time_data() const;
180 
188  template <class StreamType>
189  DEAL_II_DEPRECATED
190  void print_data(StreamType &stream) const;
191 
196  template <class StreamType>
197  void print_last_lap_wall_time_data(StreamType &stream) const;
198 
206  template <class StreamType>
207  DEAL_II_DEPRECATED
208  void print_total_data(StreamType &stream) const;
209 
214  template <class StreamType>
215  void print_accumulated_wall_time_data(StreamType &stream) const;
216 
222  void start ();
223 
232  double stop ();
233 
238  void reset ();
239 
243  void restart();
244 
251  DEAL_II_DEPRECATED
252  double operator() () const;
253 
258  double wall_time () const;
259 
264  double last_wall_time() const;
265 
274  double cpu_time() const;
275 
280  double last_cpu_time() const;
281 
287  DEAL_II_DEPRECATED
288  double get_lap_time () const;
289 
290 private:
302  template <class clock_type_>
304  {
308  typedef clock_type_ clock_type;
309 
314 
319 
325 
330 
335 
341 
346  void reset();
347  };
348 
352  typedef std::chrono::steady_clock wall_clock_type;
353 
358 
363 
368 
372  bool running;
373 
380 
386 
393 
401 };
402 
403 
404 
405 //TODO: The following class is not thread-safe
563 {
564 public:
570  class Scope
571  {
572  public:
577  Scope(::TimerOutput &timer_, const std::string &section_name);
578 
582  ~Scope();
583 
588  void stop();
589 
590  private:
595 
599  const std::string section_name;
600 
604  bool in;
605  };
606 
612  {
628  never
629  };
630 
636  {
648  n_calls
649  };
650 
656  {
668  cpu_and_wall_times
669  };
670 
681  TimerOutput (std::ostream &stream,
682  const OutputFrequency output_frequency,
683  const OutputType output_type);
684 
696  const OutputFrequency output_frequency,
697  const OutputType output_type);
698 
722  TimerOutput (MPI_Comm mpi_comm,
723  std::ostream &stream,
724  const OutputFrequency output_frequency,
725  const OutputType output_type);
726 
750  TimerOutput (MPI_Comm mpi_comm,
751  ConditionalOStream &stream,
752  const OutputFrequency output_frequency,
753  const OutputType output_type);
754 
759  ~TimerOutput();
760 
765  void enter_subsection (const std::string &section_name);
766 
770  void enter_section (const std::string &section_name);
771 
772  //TODO: make some of these functions DEPRECATED (I would keep enter/exit_section)
773 
778  void leave_subsection (const std::string &section_name = std::string());
779 
783  void exit_section (const std::string &section_name = std::string());
784 
788  std::map<std::string, double> get_summary_data (const OutputData kind) const;
789 
794  void print_summary () const;
795 
802  void disable_output ();
803 
810  void enable_output ();
811 
815  void reset ();
816 
817 private:
822 
827 
828 
834 
839  struct Section
840  {
841  Timer timer;
842  double total_cpu_time;
843  double total_wall_time;
844  unsigned int n_calls;
845  };
846 
850  std::map<std::string, Section> sections;
851 
856 
862 
869  std::list<std::string> active_sections;
870 
875 
881 };
882 
883 
884 
885 /* ---------------- inline functions ----------------- */
886 
887 
888 inline
890 {
891  reset();
892  start();
893 }
894 
895 
896 
897 inline
900 {
901  return last_lap_wall_time_data;
902 }
903 
904 
905 
906 inline
909 {
910  return last_lap_wall_time_data;
911 }
912 
913 
914 
915 inline
918 {
919  return accumulated_wall_time_data;
920 }
921 
922 
923 
924 inline
927 {
928  return accumulated_wall_time_data;
929 }
930 
931 
932 
933 template <class StreamType>
934 inline
935 void
936 Timer::print_data(StreamType &stream) const
937 {
938  print_last_lap_wall_time_data(stream);
939 }
940 
941 
942 
943 template <class StreamType>
944 inline
945 void
946 Timer::print_last_lap_wall_time_data(StreamType &stream) const
947 {
948  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
949  stream << statistic.max << " wall,"
950  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
951  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
952 }
953 
954 
955 
956 template <class StreamType>
957 inline
958 void
959 Timer::print_total_data(StreamType &stream) const
960 {
961  print_accumulated_wall_time_data(stream);
962 }
963 
964 
965 
966 template <class StreamType>
967 inline
968 void
970 {
971  const Utilities::MPI::MinMaxAvg statistic = get_accumulated_wall_time_data();
972  stream << statistic.max << " wall,"
973  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
974  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
975 }
976 
977 
978 
979 inline
980 void
981 TimerOutput::enter_section (const std::string &section_name)
982 {
983  enter_subsection(section_name);
984 }
985 
986 
987 
988 inline
989 void
990 TimerOutput::exit_section (const std::string &section_name)
991 {
992  leave_subsection(section_name);
993 }
994 
995 inline
997  const std::string &section_name_)
998  :
999  timer(timer_),
1000  section_name(section_name_),
1001  in(true)
1002 {
1004 }
1005 
1006 
1007 
1008 inline
1009 void
1011 {
1012  if (!in) return;
1013  in=false;
1014 
1015  timer.exit_section(section_name);
1016 }
1017 
1018 
1019 DEAL_II_NAMESPACE_CLOSE
1020 
1021 #endif
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:899
std::chrono::steady_clock wall_clock_type
Definition: timer.h:352
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:969
Definition: timer.h:37
MPI_Comm mpi_communicator
Definition: timer.h:874
::TimerOutput & timer
Definition: timer.h:594
MPI_Comm mpi_communicator
Definition: timer.h:379
static time_point now() noexcept
Definition: timer.cc:106
clock_type::duration duration_type
Definition: timer.h:318
Timer timer_all
Definition: timer.h:833
Threads::Mutex mutex
Definition: timer.h:880
std::chrono::microseconds duration
Definition: timer.h:44
void enter_section(const std::string &section_name)
Definition: timer.h:981
void exit_section(const std::string &section_name=std::string())
Definition: timer.h:990
clock_type::time_point time_point_type
Definition: timer.h:313
OutputType output_type
Definition: timer.h:826
bool output_is_enabled
Definition: timer.h:861
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:392
bool sync_lap_times
Definition: timer.h:385
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:946
clock_type_ clock_type
Definition: timer.h:308
const std::string section_name
Definition: timer.h:599
duration_type accumulated_time
Definition: timer.h:329
duration::rep rep
Definition: timer.h:49
OutputFrequency
Definition: timer.h:611
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:59
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:362
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:908
bool running
Definition: timer.h:372
duration_type last_lap_time
Definition: timer.h:334
std::list< std::string > active_sections
Definition: timer.h:869
static const bool is_steady
Definition: timer.h:64
void restart()
Definition: timer.h:889
CPUClock cpu_clock_type
Definition: timer.h:357
duration::period period
Definition: timer.h:54
void print_total_data(StreamType &stream) const
Definition: timer.h:959
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:926
std::map< std::string, Section > sections
Definition: timer.h:850
ConditionalOStream out_stream
Definition: timer.h:855
void print_data(StreamType &stream) const
Definition: timer.h:936
Definition: timer.h:117
OutputFrequency output_frequency
Definition: timer.h:821
time_point_type current_lap_start_time
Definition: timer.h:324
unsigned int min_index
Definition: mpi.h:391
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:400
unsigned int max_index
Definition: mpi.h:401
const Utilities::MPI::MinMaxAvg & get_total_data() const
Definition: timer.h:917
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:996
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:367