Reference documentation for deal.II version Git f15f581df6 2020-07-10 15:30:09 -0400
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
timer.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2020 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 
22 #include <deal.II/base/mpi.h>
24 #include <deal.II/base/utilities.h>
25 
26 #include <chrono>
27 #include <list>
28 #include <map>
29 #include <string>
30 
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 
118 class Timer
119 {
120 public:
124  Timer();
125 
140  Timer(MPI_Comm mpi_communicator, const bool sync_lap_times = false);
141 
149  get_last_lap_wall_time_data() const;
150 
158  get_accumulated_wall_time_data() const;
159 
164  template <class StreamType>
165  void
166  print_last_lap_wall_time_data(StreamType &stream) const;
167 
172  template <class StreamType>
173  void
174  print_accumulated_wall_time_data(StreamType &stream) const;
175 
181  void
182  start();
183 
192  double
193  stop();
194 
199  void
200  reset();
201 
205  void
206  restart();
207 
212  double
213  wall_time() const;
214 
219  double
220  last_wall_time() const;
221 
230  double
231  cpu_time() const;
232 
237  double
238  last_cpu_time() const;
239 
240 private:
252  template <class clock_type_>
254  {
258  using clock_type = clock_type_;
259 
264 
269 
275 
280 
285 
291 
296  void
297  reset();
298  };
299 
303  using wall_clock_type = std::chrono::steady_clock;
304 
309 
314 
319 
323  bool running;
324 
331 
337 
344 
352 };
353 
354 
355 
356 // TODO: The following class is not thread-safe
545 {
546 public:
552  class Scope
553  {
554  public:
559  Scope(::TimerOutput &timer_, const std::string &section_name);
560 
564  ~Scope();
565 
570  void
571  stop();
572 
573  private:
578 
582  const std::string section_name;
583 
587  bool in;
588  };
589 
595  {
611  never
612  };
613 
619  {
631  n_calls
632  };
633 
639  {
655  cpu_and_wall_times_grouped
656  };
657 
668  TimerOutput(std::ostream & stream,
669  const OutputFrequency output_frequency,
670  const OutputType output_type);
671 
683  const OutputFrequency output_frequency,
684  const OutputType output_type);
685 
709  TimerOutput(MPI_Comm mpi_comm,
710  std::ostream & stream,
711  const OutputFrequency output_frequency,
712  const OutputType output_type);
713 
737  TimerOutput(MPI_Comm mpi_comm,
738  ConditionalOStream & stream,
739  const OutputFrequency output_frequency,
740  const OutputType output_type);
741 
746  ~TimerOutput();
747 
752  void
753  enter_subsection(const std::string &section_name);
754 
760  DEAL_II_DEPRECATED void
761  enter_section(const std::string &section_name);
762 
767  void
768  leave_subsection(const std::string &section_name = "");
769 
775  DEAL_II_DEPRECATED void
776  exit_section(const std::string &section_name = "");
777 
781  std::map<std::string, double>
782  get_summary_data(const OutputData kind) const;
783 
788  void
789  print_summary() const;
790 
808  void
809  print_wall_time_statistics(const MPI_Comm mpi_comm,
810  const double print_quantile = 0.) const;
811 
818  void
819  disable_output();
820 
827  void
828  enable_output();
829 
833  void
834  reset();
835 
836 private:
841 
846 
847 
853 
858  struct Section
859  {
863  unsigned int n_calls;
864  };
865 
869  std::map<std::string, Section> sections;
870 
875 
881 
888  std::list<std::string> active_sections;
889 
894 
900 };
901 
902 
903 
904 /* ---------------- inline functions ----------------- */
905 
906 
907 inline void
909 {
910  reset();
911  start();
912 }
913 
914 
915 
916 inline const Utilities::MPI::MinMaxAvg &
918 {
919  return last_lap_wall_time_data;
920 }
921 
922 
923 
924 inline const Utilities::MPI::MinMaxAvg &
926 {
927  return accumulated_wall_time_data;
928 }
929 
930 
931 
932 template <class StreamType>
933 inline void
934 Timer::print_last_lap_wall_time_data(StreamType &stream) const
935 {
936  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
937  stream << statistic.max << " wall,"
938  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
939  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
940 }
941 
942 
943 
944 template <class StreamType>
945 inline void
947 {
948  const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_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 inline void
957 TimerOutput::enter_section(const std::string &section_name)
958 {
959  enter_subsection(section_name);
960 }
961 
962 
963 
964 inline void
965 TimerOutput::exit_section(const std::string &section_name)
966 {
967  leave_subsection(section_name);
968 }
969 
971  const std::string & section_name_)
972  : timer(timer_)
973  , section_name(section_name_)
974  , in(true)
975 {
977 }
978 
979 
980 
981 inline void
983 {
984  if (!in)
985  return;
986  in = false;
987 
989 }
990 
991 
993 
994 #endif
typename clock_type::time_point time_point_type
Definition: timer.h:263
duration::period period
Definition: timer.h:55
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:946
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:893
::TimerOutput & timer
Definition: timer.h:577
MPI_Comm mpi_communicator
Definition: timer.h:330
static time_point now() noexcept
Definition: timer.cc:111
std::chrono::steady_clock wall_clock_type
Definition: timer.h:303
Timer timer_all
Definition: timer.h:852
double total_cpu_time
Definition: timer.h:861
Threads::Mutex mutex
Definition: timer.h:899
void enter_section(const std::string &section_name)
Definition: timer.h:957
OutputType output_type
Definition: timer.h:845
unsigned int n_calls
Definition: timer.h:863
bool output_is_enabled
Definition: timer.h:880
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:343
bool sync_lap_times
Definition: timer.h:336
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:934
std::chrono::microseconds duration
Definition: timer.h:45
const std::string section_name
Definition: timer.h:582
duration_type accumulated_time
Definition: timer.h:279
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:363
typename clock_type::duration duration_type
Definition: timer.h:268
OutputFrequency
Definition: timer.h:594
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:313
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:917
bool running
Definition: timer.h:323
duration_type last_lap_time
Definition: timer.h:284
std::list< std::string > active_sections
Definition: timer.h:888
static const bool is_steady
Definition: timer.h:65
void leave_subsection(const std::string &section_name="")
Definition: timer.cc:445
void restart()
Definition: timer.h:908
void exit_section(const std::string &section_name="")
Definition: timer.h:965
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:362
double total_wall_time
Definition: timer.h:862
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:925
std::map< std::string, Section > sections
Definition: timer.h:869
ConditionalOStream out_stream
Definition: timer.h:874
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:60
Definition: timer.h:118
void enter_subsection(const std::string &section_name)
Definition: timer.cc:403
duration::rep rep
Definition: timer.h:50
OutputFrequency output_frequency
Definition: timer.h:840
#define DEAL_II_DEPRECATED
Definition: config.h:152
time_point_type current_lap_start_time
Definition: timer.h:274
unsigned int min_index
Definition: mpi.h:715
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:351
unsigned int max_index
Definition: mpi.h:725
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:970
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:318