Reference documentation for deal.II version GIT relicensing-14-gd371fb467a 2024-02-26 20:50: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: LGPL-2.1-or-later
4// Copyright (C) 1998 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_timer_h
16#define dealii_timer_h
17
18#include <deal.II/base/config.h>
19
21#include <deal.II/base/mpi.h>
22#include <deal.II/base/mutex.h>
23
24#include <chrono>
25#include <list>
26#include <map>
27#include <string>
28
30
37{
43 using duration = std::chrono::microseconds;
44
48 using rep = duration::rep;
49
53 using period = duration::period;
54
58 using time_point = std::chrono::time_point<CPUClock, duration>;
59
63 static const bool is_steady = true;
64
70 static time_point
71 now() noexcept;
72};
73
116class Timer
117{
118public:
122 Timer();
123
138 Timer(const MPI_Comm mpi_communicator, const bool sync_lap_times = false);
139
147 get_last_lap_wall_time_data() const;
148
156 get_accumulated_wall_time_data() const;
157
162 template <typename StreamType>
163 void
164 print_last_lap_wall_time_data(StreamType &stream) const;
165
170 template <typename StreamType>
171 void
172 print_accumulated_wall_time_data(StreamType &stream) const;
173
179 void
180 start();
181
190 double
191 stop();
192
197 void
198 reset();
199
203 void
204 restart();
205
210 double
211 wall_time() const;
212
217 double
218 last_wall_time() const;
219
228 double
229 cpu_time() const;
230
235 double
236 last_cpu_time() const;
237
238private:
250 template <class clock_type_>
252 {
256 using clock_type = clock_type_;
257
261 using time_point_type = typename clock_type::time_point;
262
266 using duration_type = typename clock_type::duration;
267
273
278
283
289
294 void
295 reset();
296 };
297
301 using wall_clock_type = std::chrono::steady_clock;
302
307
312
317
322
329
335
342
350};
351
352
353
354// TODO: The following class is not thread-safe
549{
550public:
556 class Scope
557 {
558 public:
563 Scope(::TimerOutput &timer_, const std::string &section_name);
564
568 ~Scope();
569
574 void
575 stop();
576
577 private:
582
586 const std::string section_name;
587
591 bool in;
592 };
593
617
637
643 {
659 cpu_and_wall_times_grouped
660 };
661
672 TimerOutput(std::ostream &stream,
673 const OutputFrequency output_frequency,
674 const OutputType output_type);
675
687 const OutputFrequency output_frequency,
688 const OutputType output_type);
689
713 TimerOutput(const MPI_Comm mpi_comm,
714 std::ostream &stream,
715 const OutputFrequency output_frequency,
716 const OutputType output_type);
717
741 TimerOutput(const MPI_Comm mpi_comm,
742 ConditionalOStream &stream,
743 const OutputFrequency output_frequency,
744 const OutputType output_type);
745
750 ~TimerOutput();
751
756 void
757 enter_subsection(const std::string &section_name);
758
763 void
764 leave_subsection(const std::string &section_name = "");
765
769 std::map<std::string, double>
770 get_summary_data(const OutputData kind) const;
771
776 void
777 print_summary() const;
778
796 void
797 print_wall_time_statistics(const MPI_Comm mpi_comm,
798 const double print_quantile = 0.) const;
799
806 void
807 disable_output();
808
815 void
816 enable_output();
817
821 void
822 reset();
823
824private:
829
834
835
841
846 struct Section
847 {
851 unsigned int n_calls;
852 };
853
857 std::map<std::string, Section> sections;
858
863
869
876 std::list<std::string> active_sections;
877
882
888};
889
890
891
892/* ---------------- inline functions ----------------- */
893
894
895inline void
897{
898 reset();
899 start();
900}
901
902
903
904inline const Utilities::MPI::MinMaxAvg &
906{
907 return last_lap_wall_time_data;
908}
909
910
911
912inline const Utilities::MPI::MinMaxAvg &
914{
915 return accumulated_wall_time_data;
916}
917
918
919
920template <typename StreamType>
921inline void
923{
924 const Utilities::MPI::MinMaxAvg &statistic = get_last_lap_wall_time_data();
925 stream << statistic.max << " wall,"
926 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
927 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
928}
929
930
931
932template <typename StreamType>
933inline void
935{
936 const Utilities::MPI::MinMaxAvg &statistic = get_accumulated_wall_time_data();
937 stream << statistic.max << " wall,"
938 << " max @" << statistic.max_index << ", min=" << statistic.min << " @"
939 << statistic.min_index << ", avg=" << statistic.avg << std::endl;
940}
941
942
943
945 const std::string &section_name_)
946 : timer(timer_)
947 , section_name(section_name_)
948 , in(true)
949{
951}
952
953
954
955inline void
957{
958 if (!in)
959 return;
960 in = false;
961
962 timer.leave_subsection(section_name);
963}
964
965
967
968#endif
::TimerOutput & timer
Definition timer.h:581
const std::string section_name
Definition timer.h:586
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition timer.h:944
MPI_Comm mpi_communicator
Definition timer.h:881
@ cpu_times
Definition timer.h:647
@ wall_times
Definition timer.h:651
@ cpu_and_wall_times
Definition timer.h:655
OutputFrequency output_frequency
Definition timer.h:828
std::map< std::string, Section > sections
Definition timer.h:857
OutputType output_type
Definition timer.h:833
OutputFrequency
Definition timer.h:599
@ every_call
Definition timer.h:603
@ every_call_and_summary
Definition timer.h:611
std::list< std::string > active_sections
Definition timer.h:876
Threads::Mutex mutex
Definition timer.h:887
ConditionalOStream out_stream
Definition timer.h:862
@ total_wall_time
Definition timer.h:631
@ total_cpu_time
Definition timer.h:627
bool output_is_enabled
Definition timer.h:868
Timer timer_all
Definition timer.h:840
void enter_subsection(const std::string &section_name)
Definition timer.cc:401
Definition timer.h:117
bool sync_lap_times
Definition timer.h:334
bool running
Definition timer.h:321
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
Definition timer.h:349
MPI_Comm mpi_communicator
Definition timer.h:328
ClockMeasurements< wall_clock_type > wall_times
Definition timer.h:311
void print_accumulated_wall_time_data(StreamType &stream) const
Definition timer.h:934
ClockMeasurements< cpu_clock_type > cpu_times
Definition timer.h:316
const Utilities::MPI::MinMaxAvg & get_accumulated_wall_time_data() const
Definition timer.h:913
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
Definition timer.h:341
void restart()
Definition timer.h:896
const Utilities::MPI::MinMaxAvg & get_last_lap_wall_time_data() const
Definition timer.h:905
std::chrono::steady_clock wall_clock_type
Definition timer.h:301
void print_last_lap_wall_time_data(StreamType &stream) const
Definition timer.h:922
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
duration::rep rep
Definition timer.h:48
std::chrono::microseconds duration
Definition timer.h:43
static time_point now() noexcept
Definition timer.cc:110
static const bool is_steady
Definition timer.h:63
duration::period period
Definition timer.h:53
std::chrono::time_point< CPUClock, duration > time_point
Definition timer.h:58
double total_wall_time
Definition timer.h:850
double total_cpu_time
Definition timer.h:849
unsigned int n_calls
Definition timer.h:851
clock_type_ clock_type
Definition timer.h:256
typename clock_type::time_point time_point_type
Definition timer.h:261
time_point_type current_lap_start_time
Definition timer.h:272
typename clock_type::duration duration_type
Definition timer.h:266
duration_type accumulated_time
Definition timer.h:277
duration_type last_lap_time
Definition timer.h:282
unsigned int max_index
Definition mpi.h:979
unsigned int min_index
Definition mpi.h:969