deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20:01+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\}}\)
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1998 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_timer_h
14#define dealii_timer_h
15
16#include <deal.II/base/config.h>
17
19#include <deal.II/base/mpi.h>
20#include <deal.II/base/mutex.h>
21
22#include <chrono>
23#include <list>
24#include <map>
25#include <optional>
26#include <string>
27
29
36{
42 using duration = std::chrono::microseconds;
43
47 using rep = duration::rep;
48
52 using period = duration::period;
53
57 using time_point = std::chrono::time_point<CPUClock, duration>;
58
62 static const bool is_steady = true;
63
69 static time_point
70 now() noexcept;
71};
72
127class Timer
128{
129public:
133 Timer();
134
149 Timer(const MPI_Comm mpi_communicator, const bool sync_lap_times = false);
150
158 get_last_lap_wall_time_data() const;
159
167 get_accumulated_wall_time_data() const;
168
173 template <typename StreamType>
174 void
175 print_last_lap_wall_time_data(StreamType &stream) const;
176
181 template <typename StreamType>
182 void
183 print_accumulated_wall_time_data(StreamType &stream) const;
184
192 void
193 start();
194
201 void
202 stop();
203
208 void
209 reset();
210
214 void
215 restart();
216
220 bool
221 is_running() const;
222
234 double
235 wall_time() const;
236
247 double
248 last_wall_time() const;
249
258 double
259 cpu_time() const;
260
273 double
274 last_cpu_time() const;
275
282 unsigned int
283 n_laps() const;
284
285private:
297 template <class clock_type_>
299 {
303 using clock_type = clock_type_;
304
308 using time_point_type = typename clock_type::time_point;
309
313 using duration_type = typename clock_type::duration;
314
320
325
330
336
341 void
342 reset();
343 };
344
348 using wall_clock_type = std::chrono::steady_clock;
349
354
360
366
371
385 void
386 synchronize_and_update() const;
387
396 mutable bool is_synchronized;
397
404
410
418
428
434 unsigned int n_timed_laps;
435
446};
447
448
449
450// TODO: The following class is not thread-safe
650{
651public:
657 class Scope
658 {
659 public:
664 Scope(::TimerOutput &timer_, const std::string &section_name);
665
669 ~Scope();
670
676 void
677 stop();
678
679 private:
684
688 const std::string section_name;
689
693 bool in;
694 };
695
719
739
745 {
761 cpu_and_wall_times_grouped
762 };
763
774 TimerOutput(std::ostream &stream,
775 const OutputFrequency output_frequency,
776 const OutputType output_type);
777
782 const OutputFrequency output_frequency,
783 const OutputType output_type);
784
795 TimerOutput(const MPI_Comm mpi_communicator_timing,
796 std::ostream &stream,
797 const OutputFrequency output_frequency,
798 const OutputType output_type);
799
803 TimerOutput(const MPI_Comm mpi_communicator_timing,
804 ConditionalOStream &stream,
805 const OutputFrequency output_frequency,
806 const OutputType output_type);
807
812 ~TimerOutput();
813
818 void
819 enter_subsection(const std::string &section_name);
820
825 void
826 leave_subsection(const std::string &section_name = "");
827
831 std::map<std::string, double>
832 get_summary_data(const OutputData kind) const;
833
838 void
839 print_summary() const;
840
864 void
865 print_wall_time_statistics(const MPI_Comm mpi_communicator_statistics,
866 const double print_quantile = 0.) const;
867
874 void
875 disable_output();
876
883 void
884 enable_output();
885
889 void
890 reset();
891
892private:
897
902
903
909
913 std::map<std::string, Timer> sections;
914
919
925
932 std::list<std::string> active_sections;
933
938 std::optional<MPI_Comm> mpi_communicator_timing;
939
945};
946
947
948
949/* ---------------- inline functions ----------------- */
950
951
952inline void
954{
955 reset();
956 start();
957}
958
959
960
961inline const Utilities::MPI::MinMaxAvg &
963{
964 if (running == false)
965 synchronize_and_update();
966
967 return last_lap_wall_time_data;
968}
969
970
971
972inline const Utilities::MPI::MinMaxAvg &
974{
975 if (running == false)
976 synchronize_and_update();
977
978 return accumulated_wall_time_data;
979}
980
981
982
983template <typename StreamType>
984inline void
986{
987 const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
988 stream << statistic.max << " wall,"
989 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
990 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
991}
992
993
994
995template <typename StreamType>
996inline void
998{
999 const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
1000 stream << statistic.max << " wall,"
1001 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
1002 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
1003}
1004
1005
1006
1008 const std::string &section_name_)
1009 : timer(timer_)
1010 , section_name(section_name_)
1011 , in(true)
1012{
1014}
1015
1016
1017
1018inline void
1020{
1021 if (!in)
1022 return;
1023 in = false;
1024
1025 timer.leave_subsection(section_name);
1026}
1027
1028
1030
1031#endif
::TimerOutput & timer
Definition timer.h:683
const std::string section_name
Definition timer.h:688
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition timer.h:1007
@ cpu_times
Definition timer.h:749
@ wall_times
Definition timer.h:753
@ cpu_and_wall_times
Definition timer.h:757
std::optional< MPI_Comm > mpi_communicator_timing
Definition timer.h:938
OutputFrequency output_frequency
Definition timer.h:896
std::map< std::string, Timer > sections
Definition timer.h:913
OutputType output_type
Definition timer.h:901
OutputFrequency
Definition timer.h:701
@ every_call
Definition timer.h:705
@ every_call_and_summary
Definition timer.h:713
std::list< std::string > active_sections
Definition timer.h:932
Threads::Mutex mutex
Definition timer.h:944
ConditionalOStream out_stream
Definition timer.h:918
@ total_wall_time
Definition timer.h:733
@ total_cpu_time
Definition timer.h:729
bool output_is_enabled
Definition timer.h:924
Timer timer_all
Definition timer.h:908
void enter_subsection(const std::string &section_name)
Definition timer.cc:489
Definition timer.h:128
bool sync_lap_times
Definition timer.h:409
Threads::Mutex mutex
Definition timer.h:445
bool running
Definition timer.h:370
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition timer.h:427
unsigned int n_timed_laps
Definition timer.h:434
MPI_Comm mpi_communicator
Definition timer.h:403
ClockMeasurements< wall_clock_type > wall_times
Definition timer.h:359
void print_accumulated_wall_time_data(StreamType &stream) const
Definition timer.h:997
ClockMeasurements< cpu_clock_type > cpu_times
Definition timer.h:365
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition timer.h:973
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition timer.h:417
void restart()
Definition timer.h:953
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition timer.h:962
bool is_synchronized
Definition timer.h:396
std::chrono::steady_clock wall_clock_type
Definition timer.h:348
void print_last_lap_wall_time_data(StreamType &stream) const
Definition timer.h:985
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
duration::rep rep
Definition timer.h:47
std::chrono::microseconds duration
Definition timer.h:42
static time_point now() noexcept
Definition timer.cc:111
static const bool is_steady
Definition timer.h:62
duration::period period
Definition timer.h:52
std::chrono::time_point< CPUClock, duration > time_point
Definition timer.h:57
clock_type_ clock_type
Definition timer.h:303
typename clock_type::time_point time_point_type
Definition timer.h:308
time_point_type current_lap_start_time
Definition timer.h:319
typename clock_type::duration duration_type
Definition timer.h:313
duration_type accumulated_time
Definition timer.h:324
duration_type last_lap_time
Definition timer.h:329
unsigned int max_index
Definition mpi.h:994
unsigned int min_index
Definition mpi.h:984