Reference documentation for deal.II version Git a0b41b6d0f 2020-02-26 20:08:13 -0600
\(\newcommand{\vcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\vcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_timer_h
17 #define dealii_timer_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/conditional_ostream.h>
22 #include <deal.II/base/mpi.h>
23 #include <deal.II/base/thread_management.h>
24 #include <deal.II/base/utilities.h>
25 
26 #include <chrono>
27 #include <list>
28 #include <map>
29 #include <string>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
38 struct CPUClock
39 {
45  using duration = std::chrono::microseconds;
46 
50  using rep = duration::rep;
51 
55  using period = duration::period;
56 
60  using time_point = std::chrono::time_point<CPUClock, duration>;
61 
65  static const bool is_steady = true;
66 
72  static time_point
73  now() noexcept;
74 };
75 
119 class Timer
120 {
121 public:
125  Timer();
126 
141  Timer(MPI_Comm mpi_communicator, const bool sync_lap_times = false);
142 
151  DEAL_II_DEPRECATED
153  get_data() const;
154 
162  get_last_lap_wall_time_data() const;
163 
173  DEAL_II_DEPRECATED
175  get_total_data() const;
176 
184  get_accumulated_wall_time_data() const;
185 
193  template <class StreamType>
194  DEAL_II_DEPRECATED void
195  print_data(StreamType &stream) const;
196 
201  template <class StreamType>
202  void
203  print_last_lap_wall_time_data(StreamType &stream) const;
204 
212  template <class StreamType>
213  DEAL_II_DEPRECATED void
214  print_total_data(StreamType &stream) const;
215 
220  template <class StreamType>
221  void
222  print_accumulated_wall_time_data(StreamType &stream) const;
223 
229  void
230  start();
231 
240  double
241  stop();
242 
247  void
248  reset();
249 
253  void
254  restart();
255 
262  DEAL_II_DEPRECATED
263  double
264  operator()() const;
265 
270  double
271  wall_time() const;
272 
277  double
278  last_wall_time() const;
279 
288  double
289  cpu_time() const;
290 
295  double
296  last_cpu_time() const;
297 
303  DEAL_II_DEPRECATED
304  double
305  get_lap_time() const;
306 
307 private:
319  template <class clock_type_>
321  {
325  using clock_type = clock_type_;
326 
331 
336 
342 
347 
352 
358 
363  void
364  reset();
365  };
366 
370  using wall_clock_type = std::chrono::steady_clock;
371 
376 
381 
386 
390  bool running;
391 
398 
404 
411 
419 };
420 
421 
422 
423 // TODO: The following class is not thread-safe
613 {
614 public:
620  class Scope
621  {
622  public:
627  Scope(::TimerOutput &timer_, const std::string &section_name);
628 
632  ~Scope();
633 
638  void
639  stop();
640 
641  private:
646 
650  const std::string section_name;
651 
655  bool in;
656  };
657 
663  {
679  never
680  };
681 
687  {
699  n_calls
700  };
701 
707  {
723  cpu_and_wall_times_grouped
724  };
725 
736  TimerOutput(std::ostream & stream,
737  const OutputFrequency output_frequency,
738  const OutputType output_type);
739 
751  const OutputFrequency output_frequency,
752  const OutputType output_type);
753 
777  TimerOutput(MPI_Comm mpi_comm,
778  std::ostream & stream,
779  const OutputFrequency output_frequency,
780  const OutputType output_type);
781 
805  TimerOutput(MPI_Comm mpi_comm,
806  ConditionalOStream & stream,
807  const OutputFrequency output_frequency,
808  const OutputType output_type);
809 
814  ~TimerOutput();
815 
820  void
821  enter_subsection(const std::string &section_name);
822 
828  DEAL_II_DEPRECATED void
829  enter_section(const std::string &section_name);
830 
835  void
836  leave_subsection(const std::string &section_name = "");
837 
843  DEAL_II_DEPRECATED void
844  exit_section(const std::string &section_name = "");
845 
849  std::map<std::string, double>
850  get_summary_data(const OutputData kind) const;
851 
856  void
857  print_summary() const;
858 
876  void
877  print_wall_time_statistics(const MPI_Comm mpi_comm,
878  const double print_quantile = 0.) const;
879 
886  void
887  disable_output();
888 
895  void
896  enable_output();
897 
901  void
902  reset();
903 
904 private:
909 
914 
915 
921 
926  struct Section
927  {
928  Timer timer;
929  double total_cpu_time;
930  double total_wall_time;
931  unsigned int n_calls;
932  };
933 
937  std::map<std::string, Section> sections;
938 
943 
949 
956  std::list<std::string> active_sections;
957 
962 
968 };
969 
970 
971 
972 /* ---------------- inline functions ----------------- */
973 
974 
975 inline void
977 {
978  reset();
979  start();
980 }
981 
982 
983 
984 inline const Utilities::MPI::MinMaxAvg &
986 {
987  return last_lap_wall_time_data;
988 }
989 
990 
991 
992 inline const Utilities::MPI::MinMaxAvg &
994 {
995  return last_lap_wall_time_data;
996 }
997 
998 
999 
1000 inline const Utilities::MPI::MinMaxAvg &
1002 {
1003  return accumulated_wall_time_data;
1004 }
1005 
1006 
1007 
1008 inline const Utilities::MPI::MinMaxAvg &
1010 {
1011  return accumulated_wall_time_data;
1012 }
1013 
1014 
1015 
1016 template <class StreamType>
1017 inline void
1018 Timer::print_data(StreamType &stream) const
1019 {
1020  print_last_lap_wall_time_data(stream);
1021 }
1022 
1023 
1024 
1025 template <class StreamType>
1026 inline void
1027 Timer::print_last_lap_wall_time_data(StreamType &stream) const
1028 {
1029  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
1030  stream << statistic.max << " wall,"
1031  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
1032  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
1033 }
1034 
1035 
1036 
1037 template <class StreamType>
1038 inline void
1039 Timer::print_total_data(StreamType &stream) const
1040 {
1041  print_accumulated_wall_time_data(stream);
1042 }
1043 
1044 
1045 
1046 template <class StreamType>
1047 inline void
1049 {
1050  const Utilities::MPI::MinMaxAvg statistic = get_accumulated_wall_time_data();
1051  stream << statistic.max << " wall,"
1052  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
1053  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
1054 }
1055 
1056 
1057 
1058 inline void
1059 TimerOutput::enter_section(const std::string &section_name)
1060 {
1061  enter_subsection(section_name);
1062 }
1063 
1064 
1065 
1066 inline void
1067 TimerOutput::exit_section(const std::string &section_name)
1068 {
1069  leave_subsection(section_name);
1070 }
1071 
1073  const std::string & section_name_)
1074  : timer(timer_)
1075  , section_name(section_name_)
1076  , in(true)
1077 {
1079 }
1080 
1081 
1082 
1083 inline void
1085 {
1086  if (!in)
1087  return;
1088  in = false;
1089 
1091 }
1092 
1093 
1094 DEAL_II_NAMESPACE_CLOSE
1095 
1096 #endif
typename clock_type::time_point time_point_type
Definition: timer.h:330
duration::period period
Definition: timer.h:55
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:985
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:1048
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:961
::TimerOutput & timer
Definition: timer.h:645
MPI_Comm mpi_communicator
Definition: timer.h:397
static time_point now() noexcept
Definition: timer.cc:111
std::chrono::steady_clock wall_clock_type
Definition: timer.h:370
Timer timer_all
Definition: timer.h:920
Threads::Mutex mutex
Definition: timer.h:967
void enter_section(const std::string &section_name)
Definition: timer.h:1059
OutputType output_type
Definition: timer.h:913
bool output_is_enabled
Definition: timer.h:948
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:410
bool sync_lap_times
Definition: timer.h:403
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:1027
std::chrono::microseconds duration
Definition: timer.h:45
const std::string section_name
Definition: timer.h:650
duration_type accumulated_time
Definition: timer.h:346
typename clock_type::duration duration_type
Definition: timer.h:335
OutputFrequency
Definition: timer.h:662
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:380
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:993
bool running
Definition: timer.h:390
duration_type last_lap_time
Definition: timer.h:351
std::list< std::string > active_sections
Definition: timer.h:956
static const bool is_steady
Definition: timer.h:65
void restart()
Definition: timer.h:976
void exit_section(const std::string &section_name="")
Definition: timer.h:1067
void print_total_data(StreamType &stream) const
Definition: timer.h:1039
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:1009
std::map< std::string, Section > sections
Definition: timer.h:937
ConditionalOStream out_stream
Definition: timer.h:942
void print_data(StreamType &stream) const
Definition: timer.h:1018
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:60
Definition: timer.h:119
duration::rep rep
Definition: timer.h:50
OutputFrequency output_frequency
Definition: timer.h:908
time_point_type current_lap_start_time
Definition: timer.h:341
unsigned int min_index
Definition: mpi.h:686
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:418
unsigned int max_index
Definition: mpi.h:696
const Utilities::MPI::MinMaxAvg & get_total_data() const
Definition: timer.h:1001
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:1072
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:385