Reference documentation for deal.II version Git 3f1f337db3 2021-10-23 13:19:02 -0600
\(\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 
765  void
766  leave_subsection(const std::string &section_name = "");
767 
771  std::map<std::string, double>
772  get_summary_data(const OutputData kind) const;
773 
778  void
779  print_summary() const;
780 
798  void
799  print_wall_time_statistics(const MPI_Comm &mpi_comm,
800  const double print_quantile = 0.) const;
801 
808  void
809  disable_output();
810 
817  void
818  enable_output();
819 
823  void
824  reset();
825 
826 private:
831 
836 
837 
843 
848  struct Section
849  {
853  unsigned int n_calls;
854  };
855 
859  std::map<std::string, Section> sections;
860 
865 
871 
878  std::list<std::string> active_sections;
879 
884 
890 };
891 
892 
893 
894 /* ---------------- inline functions ----------------- */
895 
896 
897 inline void
899 {
900  reset();
901  start();
902 }
903 
904 
905 
906 inline const Utilities::MPI::MinMaxAvg &
908 {
909  return last_lap_wall_time_data;
910 }
911 
912 
913 
914 inline const Utilities::MPI::MinMaxAvg &
916 {
917  return accumulated_wall_time_data;
918 }
919 
920 
921 
922 template <class StreamType>
923 inline void
924 Timer::print_last_lap_wall_time_data(StreamType &stream) const
925 {
926  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
927  stream << statistic.max << " wall,"
928  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
929  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
930 }
931 
932 
933 
934 template <class StreamType>
935 inline void
937 {
938  const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_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 
947  const std::string & section_name_)
948  : timer(timer_)
949  , section_name(section_name_)
950  , in(true)
951 {
953 }
954 
955 
956 
957 inline void
959 {
960  if (!in)
961  return;
962  in = false;
963 
965 }
966 
967 
969 
970 #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:936
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:883
::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:842
double total_cpu_time
Definition: timer.h:851
Threads::Mutex mutex
Definition: timer.h:889
OutputType output_type
Definition: timer.h:835
unsigned int n_calls
Definition: timer.h:853
bool output_is_enabled
Definition: timer.h:870
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:924
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:401
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:907
bool running
Definition: timer.h:323
duration_type last_lap_time
Definition: timer.h:284
std::list< std::string > active_sections
Definition: timer.h:878
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:898
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:400
double total_wall_time
Definition: timer.h:852
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:915
std::map< std::string, Section > sections
Definition: timer.h:859
ConditionalOStream out_stream
Definition: timer.h:864
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:830
time_point_type current_lap_start_time
Definition: timer.h:274
unsigned int min_index
Definition: mpi.h:790
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:351
unsigned int max_index
Definition: mpi.h:800
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:946
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:318