Reference documentation for deal.II version 9.4.1
\(\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\}}\)
Loading...
Searching...
No Matches
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
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
826private:
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
897inline void
899{
900 reset();
901 start();
902}
903
904
905
906inline const Utilities::MPI::MinMaxAvg &
908{
909 return last_lap_wall_time_data;
910}
911
912
913
914inline const Utilities::MPI::MinMaxAvg &
916{
917 return accumulated_wall_time_data;
918}
919
920
921
922template <class StreamType>
923inline void
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
934template <class StreamType>
935inline 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
957inline void
959{
960 if (!in)
961 return;
962 in = false;
963
964 timer.leave_subsection(section_name);
965}
966
967
969
970#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:946
MPI_Comm mpi_communicator
Definition: timer.h:883
@ 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:830
std::map< std::string, Section > sections
Definition: timer.h:859
OutputType output_type
Definition: timer.h:835
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:878
Threads::Mutex mutex
Definition: timer.h:889
ConditionalOStream out_stream
Definition: timer.h:864
@ total_wall_time
Definition: timer.h:633
@ total_cpu_time
Definition: timer.h:629
bool output_is_enabled
Definition: timer.h:870
Timer timer_all
Definition: timer.h:842
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:936
ClockMeasurements< cpu_clock_type > cpu_times
Definition: timer.h:318
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition: timer.h:915
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition: timer.h:343
void restart()
Definition: timer.h:898
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition: timer.h:907
std::chrono::steady_clock wall_clock_type
Definition: timer.h:303
void print_last_lap_wall_time_data(StreamType &stream) const
Definition: timer.h:924
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
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:852
double total_cpu_time
Definition: timer.h:851
unsigned int n_calls
Definition: timer.h:853
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:876
unsigned int min_index
Definition: mpi.h:866