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 
990  timer.exit_section(section_name);
991 }
992 
993 
995 
996 #endif
TimerOutput::OutputType
OutputType
Definition: timer.h:640
Timer::print_accumulated_wall_time_data
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:948
TimerOutput::Scope::in
bool in
Definition: timer.h:589
TimerOutput::mpi_communicator
MPI_Comm mpi_communicator
Definition: timer.h:895
TimerOutput::output_frequency
OutputFrequency output_frequency
Definition: timer.h:842
TimerOutput::Scope
Definition: timer.h:554
TimerOutput::cpu_and_wall_times
@ cpu_and_wall_times
Definition: timer.h:653
Timer::wall_clock_type
std::chrono::steady_clock wall_clock_type
Definition: timer.h:304
Threads::Mutex
Definition: thread_management.h:91
TimerOutput::Scope::section_name
const std::string section_name
Definition: timer.h:584
Timer::cpu_times
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:319
Utilities::MPI::MinMaxAvg::min_index
unsigned int min_index
Definition: mpi.h:715
TimerOutput::cpu_times
@ cpu_times
Definition: timer.h:645
CPUClock
Definition: timer.h:38
TimerOutput::Section::total_cpu_time
double total_cpu_time
Definition: timer.h:863
TimerOutput::enter_section
void enter_section(const std::string &section_name)
Definition: timer.h:959
utilities.h
CPUClock::rep
duration::rep rep
Definition: timer.h:50
Timer::ClockMeasurements< CPUClock >::duration_type
typename clock_type::duration duration_type
Definition: timer.h:269
TimerOutput::output_type
OutputType output_type
Definition: timer.h:847
TimerOutput::Section::n_calls
unsigned int n_calls
Definition: timer.h:865
TimerOutput::summary
@ summary
Definition: timer.h:605
Timer::mpi_communicator
MPI_Comm mpi_communicator
Definition: timer.h:331
TimerOutput::output_is_enabled
bool output_is_enabled
Definition: timer.h:882
Timer::last_lap_wall_time_data
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:344
TimerOutput::Section
Definition: timer.h:860
MPI_Comm
TimerOutput::wall_times
@ wall_times
Definition: timer.h:649
CPUClock::period
duration::period period
Definition: timer.h:55
Utilities::MPI::MinMaxAvg::max
double max
Definition: mpi.h:705
TimerOutput::Scope::stop
void stop()
Definition: timer.h:984
CPUClock::now
static time_point now() noexcept
Definition: timer.cc:111
TimerOutput::OutputData
OutputData
Definition: timer.h:620
TimerOutput::Scope::Scope
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:972
Timer
Definition: timer.h:119
Utilities::MPI::MinMaxAvg
Definition: mpi.h:687
thread_management.h
Timer::ClockMeasurements::current_lap_start_time
time_point_type current_lap_start_time
Definition: timer.h:275
TimerOutput::every_call_and_summary
@ every_call_and_summary
Definition: timer.h:609
TimerOutput::mutex
Threads::Mutex mutex
Definition: timer.h:901
TimerOutput
Definition: timer.h:546
conditional_ostream.h
Timer::ClockMeasurements::accumulated_time
duration_type accumulated_time
Definition: timer.h:280
Timer::print_last_lap_wall_time_data
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:936
Timer::ClockMeasurements
Definition: timer.h:254
Timer::wall_times
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:314
TimerOutput::Section::timer
Timer timer
Definition: timer.h:862
Timer::sync_lap_times
bool sync_lap_times
Definition: timer.h:337
TimerOutput::total_cpu_time
@ total_cpu_time
Definition: timer.h:625
TimerOutput::active_sections
std::list< std::string > active_sections
Definition: timer.h:890
TimerOutput::timer_all
Timer timer_all
Definition: timer.h:854
mpi.h
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Timer::get_last_lap_wall_time_data
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:919
DEAL_II_DEPRECATED
#define DEAL_II_DEPRECATED
Definition: config.h:98
Timer::get_accumulated_wall_time_data
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:927
TimerOutput::Scope::timer
::TimerOutput & timer
Definition: timer.h:579
Timer::restart
void restart()
Definition: timer.h:910
TimerOutput::total_wall_time
@ total_wall_time
Definition: timer.h:629
TimerOutput::OutputFrequency
OutputFrequency
Definition: timer.h:596
TimerOutput::Section::total_wall_time
double total_wall_time
Definition: timer.h:864
Utilities::MPI::MinMaxAvg::avg
double avg
Definition: mpi.h:731
Timer::ClockMeasurements< CPUClock >::time_point_type
typename clock_type::time_point time_point_type
Definition: timer.h:264
Utilities::MPI::MinMaxAvg::max_index
unsigned int max_index
Definition: mpi.h:725
CPUClock::time_point
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:60
TimerOutput::every_call
@ every_call
Definition: timer.h:601
Timer::running
bool running
Definition: timer.h:324
ConditionalOStream
Definition: conditional_ostream.h:81
Timer::accumulated_wall_time_data
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:352
config.h
MeshWorker::internal::OutputType
typename FEValuesViews::View< dim, spacedim, Extractor >::template OutputType< NumberType > OutputType
Definition: scratch_data.h:46
CPUClock::is_steady
static const bool is_steady
Definition: timer.h:65
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Utilities::MPI::MinMaxAvg::min
double min
Definition: mpi.h:699
TimerOutput::exit_section
void exit_section(const std::string &section_name="")
Definition: timer.h:967
Timer::ClockMeasurements::last_lap_time
duration_type last_lap_time
Definition: timer.h:285
TimerOutput::sections
std::map< std::string, Section > sections
Definition: timer.h:871
TimerOutput::out_stream
ConditionalOStream out_stream
Definition: timer.h:876
CPUClock::duration
std::chrono::microseconds duration
Definition: timer.h:45