Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
timer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 
21 #include <deal.II/base/conditional_ostream.h>
22 #include <deal.II/base/mpi.h>
23 #include <deal.II/base/thread_management.h>
24 #include <deal.II/base/utilities.h>
25 
26 #include <chrono>
27 #include <list>
28 #include <map>
29 #include <string>
30 
31 DEAL_II_NAMESPACE_OPEN
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 
151  DEAL_II_DEPRECATED
153  get_data() const;
154 
162  get_last_lap_wall_time_data() const;
163 
173  DEAL_II_DEPRECATED
175  get_total_data() const;
176 
184  get_accumulated_wall_time_data() const;
185 
193  template <class StreamType>
194  DEAL_II_DEPRECATED void
195  print_data(StreamType &stream) const;
196 
201  template <class StreamType>
202  void
203  print_last_lap_wall_time_data(StreamType &stream) const;
204 
212  template <class StreamType>
213  DEAL_II_DEPRECATED void
214  print_total_data(StreamType &stream) const;
215 
220  template <class StreamType>
221  void
222  print_accumulated_wall_time_data(StreamType &stream) const;
223 
229  void
230  start();
231 
240  double
241  stop();
242 
247  void
248  reset();
249 
253  void
254  restart();
255 
262  DEAL_II_DEPRECATED
263  double
264  operator()() const;
265 
270  double
271  wall_time() const;
272 
277  double
278  last_wall_time() const;
279 
288  double
289  cpu_time() const;
290 
295  double
296  last_cpu_time() const;
297 
303  DEAL_II_DEPRECATED
304  double
305  get_lap_time() const;
306 
307 private:
319  template <class clock_type_>
321  {
325  using clock_type = clock_type_;
326 
331 
336 
342 
347 
352 
358 
363  void
364  reset();
365  };
366 
370  using wall_clock_type = std::chrono::steady_clock;
371 
376 
381 
386 
390  bool running;
391 
398 
404 
411 
419 };
420 
421 
422 
423 // TODO: The following class is not thread-safe
581 {
582 public:
588  class Scope
589  {
590  public:
595  Scope(::TimerOutput &timer_, const std::string &section_name);
596 
600  ~Scope();
601 
606  void
607  stop();
608 
609  private:
614 
618  const std::string section_name;
619 
623  bool in;
624  };
625 
631  {
647  never
648  };
649 
655  {
667  n_calls
668  };
669 
675  {
691  cpu_and_wall_times_grouped
692  };
693 
704  TimerOutput(std::ostream & stream,
705  const OutputFrequency output_frequency,
706  const OutputType output_type);
707 
719  const OutputFrequency output_frequency,
720  const OutputType output_type);
721 
745  TimerOutput(MPI_Comm mpi_comm,
746  std::ostream & stream,
747  const OutputFrequency output_frequency,
748  const OutputType output_type);
749 
773  TimerOutput(MPI_Comm mpi_comm,
774  ConditionalOStream & stream,
775  const OutputFrequency output_frequency,
776  const OutputType output_type);
777 
782  ~TimerOutput();
783 
788  void
789  enter_subsection(const std::string &section_name);
790 
794  void
795  enter_section(const std::string &section_name);
796 
797  // TODO: make some of these functions DEPRECATED (I would keep
798  // enter/exit_section)
799 
804  void
805  leave_subsection(const std::string &section_name = "");
806 
810  void
811  exit_section(const std::string &section_name = "");
812 
816  std::map<std::string, double>
817  get_summary_data(const OutputData kind) const;
818 
823  void
824  print_summary() const;
825 
832  void
833  disable_output();
834 
841  void
842  enable_output();
843 
847  void
848  reset();
849 
850 private:
855 
860 
861 
867 
872  struct Section
873  {
874  Timer timer;
875  double total_cpu_time;
876  double total_wall_time;
877  unsigned int n_calls;
878  };
879 
883  std::map<std::string, Section> sections;
884 
889 
895 
902  std::list<std::string> active_sections;
903 
908 
914 };
915 
916 
917 
918 /* ---------------- inline functions ----------------- */
919 
920 
921 inline void
923 {
924  reset();
925  start();
926 }
927 
928 
929 
930 inline const Utilities::MPI::MinMaxAvg &
932 {
933  return last_lap_wall_time_data;
934 }
935 
936 
937 
938 inline const Utilities::MPI::MinMaxAvg &
940 {
941  return last_lap_wall_time_data;
942 }
943 
944 
945 
946 inline const Utilities::MPI::MinMaxAvg &
948 {
949  return accumulated_wall_time_data;
950 }
951 
952 
953 
954 inline const Utilities::MPI::MinMaxAvg &
956 {
957  return accumulated_wall_time_data;
958 }
959 
960 
961 
962 template <class StreamType>
963 inline void
964 Timer::print_data(StreamType &stream) const
965 {
966  print_last_lap_wall_time_data(stream);
967 }
968 
969 
970 
971 template <class StreamType>
972 inline void
973 Timer::print_last_lap_wall_time_data(StreamType &stream) const
974 {
975  const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
976  stream << statistic.max << " wall,"
977  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
978  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
979 }
980 
981 
982 
983 template <class StreamType>
984 inline void
985 Timer::print_total_data(StreamType &stream) const
986 {
987  print_accumulated_wall_time_data(stream);
988 }
989 
990 
991 
992 template <class StreamType>
993 inline void
995 {
996  const Utilities::MPI::MinMaxAvg statistic = get_accumulated_wall_time_data();
997  stream << statistic.max << " wall,"
998  << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
999  << statistic.min_index << ", avg=" << statistic.avg << std::endl;
1000 }
1001 
1002 
1003 
1004 inline void
1005 TimerOutput::enter_section(const std::string &section_name)
1006 {
1007  enter_subsection(section_name);
1008 }
1009 
1010 
1011 
1012 inline void
1013 TimerOutput::exit_section(const std::string &section_name)
1014 {
1015  leave_subsection(section_name);
1016 }
1017 
1019  const std::string & section_name_)
1020  : timer(timer_)
1021  , section_name(section_name_)
1022  , in(true)
1023 {
1025 }
1026 
1027 
1028 
1029 inline void
1031 {
1032  if (!in)
1033  return;
1034  in = false;
1035 
1036  timer.exit_section(section_name);
1037 }
1038 
1039 
1040 DEAL_II_NAMESPACE_CLOSE
1041 
1042 #endif
typename clock_type::time_point time_point_type
Definition: timer.h:330
duration::period period
Definition: timer.h:55
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:931
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:994
Definition: timer.h:38
MPI_Comm mpi_communicator
Definition: timer.h:907
::TimerOutput & timer
Definition: timer.h:613
MPI_Comm mpi_communicator
Definition: timer.h:397
static time_point now() noexcept
Definition: timer.cc:110
std::chrono::steady_clock wall_clock_type
Definition: timer.h:370
Timer timer_all
Definition: timer.h:866
Threads::Mutex mutex
Definition: timer.h:913
void enter_section(const std::string &section_name)
Definition: timer.h:1005
OutputType output_type
Definition: timer.h:859
bool output_is_enabled
Definition: timer.h:894
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:410
bool sync_lap_times
Definition: timer.h:403
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:973
std::chrono::microseconds duration
Definition: timer.h:45
const std::string section_name
Definition: timer.h:618
duration_type accumulated_time
Definition: timer.h:346
typename clock_type::duration duration_type
Definition: timer.h:335
OutputFrequency
Definition: timer.h:630
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:380
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:939
bool running
Definition: timer.h:390
duration_type last_lap_time
Definition: timer.h:351
std::list< std::string > active_sections
Definition: timer.h:902
static const bool is_steady
Definition: timer.h:65
void restart()
Definition: timer.h:922
void exit_section(const std::string &section_name="")
Definition: timer.h:1013
void print_total_data(StreamType &stream) const
Definition: timer.h:985
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:955
std::map< std::string, Section > sections
Definition: timer.h:883
ConditionalOStream out_stream
Definition: timer.h:888
void print_data(StreamType &stream) const
Definition: timer.h:964
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:854
time_point_type current_lap_start_time
Definition: timer.h:341
unsigned int min_index
Definition: mpi.h:495
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:418
unsigned int max_index
Definition: mpi.h:505
const Utilities::MPI::MinMaxAvg & get_total_data() const
Definition: timer.h:947
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:1018
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:385