Reference documentation for deal.II version 9.3.0
\(\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 - 2021 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(const 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
551 {
552 public:
558  class Scope
559  {
560  public:
565  Scope(::TimerOutput &timer_, const std::string &section_name);
566 
570  ~Scope();
571 
576  void
577  stop();
578 
579  private:
584 
588  const std::string section_name;
589 
593  bool in;
594  };
595 
601  {
617  never
618  };
619 
625  {
637  n_calls
638  };
639 
645  {
661  cpu_and_wall_times_grouped
662  };
663 
674  TimerOutput(std::ostream & stream,
675  const OutputFrequency output_frequency,
676  const OutputType output_type);
677 
689  const OutputFrequency output_frequency,
690  const OutputType output_type);
691 
715  TimerOutput(const MPI_Comm & mpi_comm,
716  std::ostream & stream,
717  const OutputFrequency output_frequency,
718  const OutputType output_type);
719 
743  TimerOutput(const MPI_Comm & mpi_comm,
744  ConditionalOStream & stream,
745  const OutputFrequency output_frequency,
746  const OutputType output_type);
747 
752  ~TimerOutput();
753 
758  void
759  enter_subsection(const std::string &section_name);
760 
766  DEAL_II_DEPRECATED void
767  enter_section(const std::string &section_name);
768 
773  void
774  leave_subsection(const std::string &section_name = "");
775 
781  DEAL_II_DEPRECATED void
782  exit_section(const std::string &section_name = "");
783 
787  std::map<std::string, double>
788  get_summary_data(const OutputData kind) const;
789 
794  void
795  print_summary() const;
796 
814  void
815  print_wall_time_statistics(const MPI_Comm &mpi_comm,
816  const double print_quantile = 0.) const;
817 
824  void
825  disable_output();
826 
833  void
834  enable_output();
835 
839  void
840  reset();
841 
842 private:
847 
852 
853 
859 
864  struct Section
865  {
869  unsigned int n_calls;
870  };
871 
875  std::map<std::string, Section> sections;
876 
881 
887 
894  std::list<std::string> active_sections;
895 
900 
906 };
907 
908 
909 
910 /* ---------------- inline functions ----------------- */
911 
912 
913 inline void
915 {
916  reset();
917  start();
918 }
919 
920 
921 
922 inline const Utilities::MPI::MinMaxAvg &
924 {
925  return last_lap_wall_time_data;
926 }
927 
928 
929 
930 inline const Utilities::MPI::MinMaxAvg &
932 {
933  return accumulated_wall_time_data;
934 }
935 
936 
937 
938 template <class StreamType>
939 inline void
940 Timer::print_last_lap_wall_time_data(StreamType &stream) const
941 {
942  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
943  stream << statistic.max << " wall,"
944  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
945  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
946 }
947 
948 
949 
950 template <class StreamType>
951 inline void
953 {
954  const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
955  stream << statistic.max << " wall,"
956  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
957  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
958 }
959 
960 
961 
962 inline void
963 TimerOutput::enter_section(const std::string &section_name)
964 {
965  enter_subsection(section_name);
966 }
967 
968 
969 
970 inline void
971 TimerOutput::exit_section(const std::string &section_name)
972 {
973  leave_subsection(section_name);
974 }
975 
977  const std::string & section_name_)
978  : timer(timer_)
979  , section_name(section_name_)
980  , in(true)
981 {
983 }
984 
985 
986 
987 inline void
989 {
990  if (!in)
991  return;
992  in = false;
993 
995 }
996 
997 
999 
1000 #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:952
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:899
::TimerOutput & timer
Definition: timer.h:583
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:858
double total_cpu_time
Definition: timer.h:867
Threads::Mutex mutex
Definition: timer.h:905
void enter_section(const std::string &section_name)
Definition: timer.h:963
OutputType output_type
Definition: timer.h:851
unsigned int n_calls
Definition: timer.h:869
bool output_is_enabled
Definition: timer.h:886
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:940
std::chrono::microseconds duration
Definition: timer.h:45
const std::string section_name
Definition: timer.h:588
duration_type accumulated_time
Definition: timer.h:279
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:395
typename clock_type::duration duration_type
Definition: timer.h:268
OutputFrequency
Definition: timer.h:600
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:313
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:923
bool running
Definition: timer.h:323
duration_type last_lap_time
Definition: timer.h:284
std::list< std::string > active_sections
Definition: timer.h:894
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:914
void exit_section(const std::string &section_name="")
Definition: timer.h:971
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:394
double total_wall_time
Definition: timer.h:868
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:931
std::map< std::string, Section > sections
Definition: timer.h:875
ConditionalOStream out_stream
Definition: timer.h:880
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:846
#define DEAL_II_DEPRECATED
Definition: config.h:157
time_point_type current_lap_start_time
Definition: timer.h:274
unsigned int min_index
Definition: mpi.h:789
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:351
unsigned int max_index
Definition: mpi.h:799
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:976
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:318