Reference documentation for deal.II version 9.2.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 - 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 
119 class Timer
120 {
121 public:
125  Timer();
126 
141  Timer(MPI_Comm mpi_communicator, const bool sync_lap_times = false);
142 
150  get_last_lap_wall_time_data() const;
151 
159  get_accumulated_wall_time_data() const;
160 
165  template <class StreamType>
166  void
167  print_last_lap_wall_time_data(StreamType &stream) const;
168 
173  template <class StreamType>
174  void
175  print_accumulated_wall_time_data(StreamType &stream) const;
176 
182  void
183  start();
184 
193  double
194  stop();
195 
200  void
201  reset();
202 
206  void
207  restart();
208 
213  double
214  wall_time() const;
215 
220  double
221  last_wall_time() const;
222 
231  double
232  cpu_time() const;
233 
238  double
239  last_cpu_time() const;
240 
241 private:
253  template <class clock_type_>
255  {
259  using clock_type = clock_type_;
260 
265 
270 
276 
281 
286 
292 
297  void
298  reset();
299  };
300 
304  using wall_clock_type = std::chrono::steady_clock;
305 
310 
315 
320 
324  bool running;
325 
332 
338 
345 
353 };
354 
355 
356 
357 // TODO: The following class is not thread-safe
547 {
548 public:
554  class Scope
555  {
556  public:
561  Scope(::TimerOutput &timer_, const std::string &section_name);
562 
566  ~Scope();
567 
572  void
573  stop();
574 
575  private:
580 
584  const std::string section_name;
585 
589  bool in;
590  };
591 
597  {
613  never
614  };
615 
621  {
633  n_calls
634  };
635 
641  {
657  cpu_and_wall_times_grouped
658  };
659 
670  TimerOutput(std::ostream & stream,
671  const OutputFrequency output_frequency,
672  const OutputType output_type);
673 
685  const OutputFrequency output_frequency,
686  const OutputType output_type);
687 
711  TimerOutput(MPI_Comm mpi_comm,
712  std::ostream & stream,
713  const OutputFrequency output_frequency,
714  const OutputType output_type);
715 
739  TimerOutput(MPI_Comm mpi_comm,
740  ConditionalOStream & stream,
741  const OutputFrequency output_frequency,
742  const OutputType output_type);
743 
748  ~TimerOutput();
749 
754  void
755  enter_subsection(const std::string &section_name);
756 
762  DEAL_II_DEPRECATED void
763  enter_section(const std::string &section_name);
764 
769  void
770  leave_subsection(const std::string &section_name = "");
771 
777  DEAL_II_DEPRECATED void
778  exit_section(const std::string &section_name = "");
779 
783  std::map<std::string, double>
784  get_summary_data(const OutputData kind) const;
785 
790  void
791  print_summary() const;
792 
810  void
811  print_wall_time_statistics(const MPI_Comm mpi_comm,
812  const double print_quantile = 0.) const;
813 
820  void
821  disable_output();
822 
829  void
830  enable_output();
831 
835  void
836  reset();
837 
838 private:
843 
848 
849 
855 
860  struct Section
861  {
865  unsigned int n_calls;
866  };
867 
871  std::map<std::string, Section> sections;
872 
877 
883 
890  std::list<std::string> active_sections;
891 
896 
902 };
903 
904 
905 
906 /* ---------------- inline functions ----------------- */
907 
908 
909 inline void
911 {
912  reset();
913  start();
914 }
915 
916 
917 
918 inline const Utilities::MPI::MinMaxAvg &
920 {
921  return last_lap_wall_time_data;
922 }
923 
924 
925 
926 inline const Utilities::MPI::MinMaxAvg &
928 {
929  return accumulated_wall_time_data;
930 }
931 
932 
933 
934 template <class StreamType>
935 inline void
936 Timer::print_last_lap_wall_time_data(StreamType &stream) const
937 {
938  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
939  stream << statistic.max << " wall,"
940  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
941  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
942 }
943 
944 
945 
946 template <class StreamType>
947 inline void
949 {
950  const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
951  stream << statistic.max << " wall,"
952  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
953  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
954 }
955 
956 
957 
958 inline void
959 TimerOutput::enter_section(const std::string &section_name)
960 {
961  enter_subsection(section_name);
962 }
963 
964 
965 
966 inline void
967 TimerOutput::exit_section(const std::string &section_name)
968 {
969  leave_subsection(section_name);
970 }
971 
973  const std::string & section_name_)
974  : timer(timer_)
975  , section_name(section_name_)
976  , in(true)
977 {
979 }
980 
981 
982 
983 inline void
985 {
986  if (!in)
987  return;
988  in = false;
989 
991 }
992 
993 
995 
996 #endif
typename clock_type::time_point time_point_type
Definition: timer.h:264
duration::period period
Definition: timer.h:55
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:948
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:895
::TimerOutput & timer
Definition: timer.h:579
MPI_Comm mpi_communicator
Definition: timer.h:331
static time_point now() noexcept
Definition: timer.cc:111
std::chrono::steady_clock wall_clock_type
Definition: timer.h:304
Timer timer_all
Definition: timer.h:854
double total_cpu_time
Definition: timer.h:863
Threads::Mutex mutex
Definition: timer.h:901
void enter_section(const std::string &section_name)
Definition: timer.h:959
OutputType output_type
Definition: timer.h:847
unsigned int n_calls
Definition: timer.h:865
bool output_is_enabled
Definition: timer.h:882
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:344
bool sync_lap_times
Definition: timer.h:337
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:936
std::chrono::microseconds duration
Definition: timer.h:45
const std::string section_name
Definition: timer.h:584
duration_type accumulated_time
Definition: timer.h:280
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
typename clock_type::duration duration_type
Definition: timer.h:269
OutputFrequency
Definition: timer.h:596
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:314
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:919
bool running
Definition: timer.h:324
duration_type last_lap_time
Definition: timer.h:285
std::list< std::string > active_sections
Definition: timer.h:890
static const bool is_steady
Definition: timer.h:65
void restart()
Definition: timer.h:910
void exit_section(const std::string &section_name="")
Definition: timer.h:967
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
double total_wall_time
Definition: timer.h:864
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:927
std::map< std::string, Section > sections
Definition: timer.h:871
ConditionalOStream out_stream
Definition: timer.h:876
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:842
#define DEAL_II_DEPRECATED
Definition: config.h:98
time_point_type current_lap_start_time
Definition: timer.h:275
unsigned int min_index
Definition: mpi.h:715
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:352
unsigned int max_index
Definition: mpi.h:725
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:972
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:319