Reference documentation for deal.II version 9.3.3
\(\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>
25
26#include <chrono>
27#include <list>
28#include <map>
29#include <string>
30
32
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
118class Timer
119{
120public:
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
240private:
252 template <class clock_type_>
254 {
258 using clock_type = clock_type_;
259
263 using time_point_type = typename clock_type::time_point;
264
268 using duration_type = typename clock_type::duration;
269
275
280
285
291
296 void
297 reset();
298 };
299
303 using wall_clock_type = std::chrono::steady_clock;
304
309
314
319
324
331
337
344
352};
353
354
355
356// TODO: The following class is not thread-safe
551{
552public:
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
767 enter_section(const std::string &section_name);
768
773 void
774 leave_subsection(const std::string &section_name = "");
775
782 exit_section(const std::string &section_name = "");
783
787 std::map<std::string, double>
788 get_summary_data(const OutputData kind) const;
789
794 void
795 print_summary() const;
796
814 void
815 print_wall_time_statistics(const MPI_Comm &mpi_comm,
816 const double print_quantile = 0.) const;
817
824 void
825 disable_output();
826
833 void
834 enable_output();
835
839 void
840 reset();
841
842private:
847
852
853
859
864 struct Section
865 {
869 unsigned int n_calls;
870 };
871
875 std::map<std::string, Section> sections;
876
881
887
894 std::list<std::string> active_sections;
895
900
906};
907
908
909
910/* ---------------- inline functions ----------------- */
911
912
913inline void
915{
916 reset();
917 start();
918}
919
920
921
922inline const Utilities::MPI::MinMaxAvg &
924{
925 return last_lap_wall_time_data;
926}
927
928
929
930inline const Utilities::MPI::MinMaxAvg &
932{
933 return accumulated_wall_time_data;
934}
935
936
937
938template <class StreamType>
939inline void
941{
942 const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
943 stream << statistic.max << " wall,"
944 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
945 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
946}
947
948
949
950template <class StreamType>
951inline void
953{
954 const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
955 stream << statistic.max << " wall,"
956 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
957 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
958}
959
960
961
962inline void
963TimerOutput::enter_section(const std::string &section_name)
964{
965 enter_subsection(section_name);
966}
967
968
969
970inline void
971TimerOutput::exit_section(const std::string &section_name)
972{
973 leave_subsection(section_name);
974}
975
977 const std::string & section_name_)
978 : timer(timer_)
979 , section_name(section_name_)
980 , in(true)
981{
983}
984
985
986
987inline void
989{
990 if (!in)
991 return;
992 in = false;
993
994 timer.leave_subsection(section_name);
995}
996
997
999
1000#endif
::TimerOutput & timer
Definition: timer.h:583
const std::string section_name
Definition: timer.h:588
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:976
MPI_Comm mpi_communicator
Definition: timer.h:899
@ cpu_times
Definition: timer.h:649
@ wall_times
Definition: timer.h:653
@ cpu_and_wall_times
Definition: timer.h:657
OutputFrequency output_frequency
Definition: timer.h:846
std::map< std::string, Section > sections
Definition: timer.h:875
OutputType output_type
Definition: timer.h:851
OutputFrequency
Definition: timer.h:601
@ every_call
Definition: timer.h:605
@ summary
Definition: timer.h:609
@ every_call_and_summary
Definition: timer.h:613
std::list< std::string > active_sections
Definition: timer.h:894
Threads::Mutex mutex
Definition: timer.h:905
void exit_section(const std::string &section_name="")
Definition: timer.h:971
ConditionalOStream out_stream
Definition: timer.h:880
void enter_section(const std::string &section_name)
Definition: timer.h:963
@ total_wall_time
Definition: timer.h:633
@ total_cpu_time
Definition: timer.h:629
bool output_is_enabled
Definition: timer.h:886
Timer timer_all
Definition: timer.h:858
void enter_subsection(const std::string &section_name)
Definition: timer.cc:403
Definition: timer.h:119
bool sync_lap_times
Definition: timer.h:336
bool running
Definition: timer.h:323
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition: timer.h:351
MPI_Comm mpi_communicator
Definition: timer.h:330
ClockMeasurements< wall_clock_type > wall_times
Definition: timer.h:313
void print_accumulated_wall_time_data(StreamType &stream) const
Definition: timer.h:952
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:318
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:931
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:343
void restart()
Definition: timer.h:914
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:923
std::chrono::steady_clock wall_clock_type
Definition: timer.h:303
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:940
#define DEAL_II_DEPRECATED
Definition: config.h:162
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:402
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
Definition: timer.h:39
duration::rep rep
Definition: timer.h:50
std::chrono::microseconds duration
Definition: timer.h:45
static time_point now() noexcept
Definition: timer.cc:111
static const bool is_steady
Definition: timer.h:65
duration::period period
Definition: timer.h:55
std::chrono::time_point< CPUClock, duration > time_point
Definition: timer.h:60
double total_wall_time
Definition: timer.h:868
double total_cpu_time
Definition: timer.h:867
unsigned int n_calls
Definition: timer.h:869
clock_type_ clock_type
Definition: timer.h:258
typename clock_type::time_point time_point_type
Definition: timer.h:263
time_point_type current_lap_start_time
Definition: timer.h:274
typename clock_type::duration duration_type
Definition: timer.h:268
duration_type accumulated_time
Definition: timer.h:279
duration_type last_lap_time
Definition: timer.h:284
unsigned int max_index
Definition: mpi.h:799
unsigned int min_index
Definition: mpi.h:789