Reference documentation for deal.II version GIT 7b2de2f2f9 2023-09-24 11:00:02+00:00
\(\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 - 2023 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>
23 #include <deal.II/base/mutex.h>
24 
25 #include <chrono>
26 #include <list>
27 #include <map>
28 #include <string>
29 
31 
37 struct CPUClock
38 {
44  using duration = std::chrono::microseconds;
45 
49  using rep = duration::rep;
50 
54  using period = duration::period;
55 
59  using time_point = std::chrono::time_point<CPUClock, duration>;
60 
64  static const bool is_steady = true;
65 
71  static time_point
72  now() noexcept;
73 };
74 
117 class Timer
118 {
119 public:
123  Timer();
124 
139  Timer(const MPI_Comm mpi_communicator, const bool sync_lap_times = false);
140 
148  get_last_lap_wall_time_data() const;
149 
157  get_accumulated_wall_time_data() const;
158 
163  template <typename StreamType>
164  void
165  print_last_lap_wall_time_data(StreamType &stream) const;
166 
171  template <typename StreamType>
172  void
173  print_accumulated_wall_time_data(StreamType &stream) const;
174 
180  void
181  start();
182 
191  double
192  stop();
193 
198  void
199  reset();
200 
204  void
205  restart();
206 
211  double
212  wall_time() const;
213 
218  double
219  last_wall_time() const;
220 
229  double
230  cpu_time() const;
231 
236  double
237  last_cpu_time() const;
238 
239 private:
251  template <class clock_type_>
253  {
257  using clock_type = clock_type_;
258 
262  using time_point_type = typename clock_type::time_point;
263 
267  using duration_type = typename clock_type::duration;
268 
274 
279 
284 
290 
295  void
296  reset();
297  };
298 
302  using wall_clock_type = std::chrono::steady_clock;
303 
308 
313 
318 
322  bool running;
323 
330 
336 
343 
351 };
352 
353 
354 
355 // TODO: The following class is not thread-safe
550 {
551 public:
557  class Scope
558  {
559  public:
564  Scope(::TimerOutput &timer_, const std::string &section_name);
565 
569  ~Scope();
570 
575  void
576  stop();
577 
578  private:
583 
587  const std::string section_name;
588 
592  bool in;
593  };
594 
600  {
616  never
617  };
618 
624  {
636  n_calls
637  };
638 
644  {
660  cpu_and_wall_times_grouped
661  };
662 
673  TimerOutput(std::ostream &stream,
674  const OutputFrequency output_frequency,
675  const OutputType output_type);
676 
688  const OutputFrequency output_frequency,
689  const OutputType output_type);
690 
714  TimerOutput(const MPI_Comm mpi_comm,
715  std::ostream &stream,
716  const OutputFrequency output_frequency,
717  const OutputType output_type);
718 
742  TimerOutput(const MPI_Comm mpi_comm,
743  ConditionalOStream &stream,
744  const OutputFrequency output_frequency,
745  const OutputType output_type);
746 
751  ~TimerOutput();
752 
757  void
758  enter_subsection(const std::string &section_name);
759 
764  void
765  leave_subsection(const std::string &section_name = "");
766 
770  std::map<std::string, double>
771  get_summary_data(const OutputData kind) const;
772 
777  void
778  print_summary() const;
779 
797  void
798  print_wall_time_statistics(const MPI_Comm mpi_comm,
799  const double print_quantile = 0.) const;
800 
807  void
808  disable_output();
809 
816  void
817  enable_output();
818 
822  void
823  reset();
824 
825 private:
830 
835 
836 
842 
847  struct Section
848  {
852  unsigned int n_calls;
853  };
854 
858  std::map<std::string, Section> sections;
859 
864 
870 
877  std::list<std::string> active_sections;
878 
883 
889 };
890 
891 
892 
893 /* ---------------- inline functions ----------------- */
894 
895 
896 inline void
898 {
899  reset();
900  start();
901 }
902 
903 
904 
905 inline const Utilities::MPI::MinMaxAvg &
907 {
908  return last_lap_wall_time_data;
909 }
910 
911 
912 
913 inline const Utilities::MPI::MinMaxAvg &
915 {
916  return accumulated_wall_time_data;
917 }
918 
919 
920 
921 template <typename StreamType>
922 inline void
923 Timer::print_last_lap_wall_time_data(StreamType &stream) const
924 {
925  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
926  stream << statistic.max << " wall,"
927  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
928  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
929 }
930 
931 
932 
933 template <typename StreamType>
934 inline void
936 {
937  const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
938  stream << statistic.max << " wall,"
939  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
940  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
941 }
942 
943 
944 
946  const std::string &section_name_)
947  : timer(timer_)
948  , section_name(section_name_)
949  , in(true)
950 {
952 }
953 
954 
955 
956 inline void
958 {
959  if (!in)
960  return;
961  in = false;
962 
963  timer.leave_subsection(section_name);
964 }
965 
966 
968 
969 #endif
::TimerOutput & timer
Definition: timer.h:582
const std::string section_name
Definition: timer.h:587
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:945
MPI_Comm mpi_communicator
Definition: timer.h:882
@ cpu_times
Definition: timer.h:648
@ wall_times
Definition: timer.h:652
@ cpu_and_wall_times
Definition: timer.h:656
OutputFrequency output_frequency
Definition: timer.h:829
std::map< std::string, Section > sections
Definition: timer.h:858
OutputType output_type
Definition: timer.h:834
OutputFrequency
Definition: timer.h:600
@ every_call
Definition: timer.h:604
@ summary
Definition: timer.h:608
@ every_call_and_summary
Definition: timer.h:612
std::list< std::string > active_sections
Definition: timer.h:877
Threads::Mutex mutex
Definition: timer.h:888
ConditionalOStream out_stream
Definition: timer.h:863
@ total_wall_time
Definition: timer.h:632
@ total_cpu_time
Definition: timer.h:628
bool output_is_enabled
Definition: timer.h:869
Timer timer_all
Definition: timer.h:841
void enter_subsection(const std::string &section_name)
Definition: timer.cc:402
Definition: timer.h:118
bool sync_lap_times
Definition: timer.h:335
bool running
Definition: timer.h:322
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:350
MPI_Comm mpi_communicator
Definition: timer.h:329
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:312
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:935
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:317
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:914
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:342
void restart()
Definition: timer.h:897
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:906
std::chrono::steady_clock wall_clock_type
Definition: timer.h:302
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:923
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
Definition: timer.h:38
duration::rep rep
Definition: timer.h:49
std::chrono::microseconds duration
Definition: timer.h:44
static time_point now() noexcept
Definition: timer.cc:111
static const bool is_steady
Definition: timer.h:64
duration::period period
Definition: timer.h:54
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:59
double total_wall_time
Definition: timer.h:851
double total_cpu_time
Definition: timer.h:850
unsigned int n_calls
Definition: timer.h:852
clock_type_ clock_type
Definition: timer.h:257
typename clock_type::time_point time_point_type
Definition: timer.h:262
time_point_type current_lap_start_time
Definition: timer.h:273
typename clock_type::duration duration_type
Definition: timer.h:267
duration_type accumulated_time
Definition: timer.h:278
duration_type last_lap_time
Definition: timer.h:283
unsigned int max_index
Definition: mpi.h:980
unsigned int min_index
Definition: mpi.h:970