Reference documentation for deal.II version 8.5.1
timer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2017 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__timer_h
17 #define dealii__timer_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/conditional_ostream.h>
21 #include <deal.II/base/mpi.h>
22 #include <deal.II/base/thread_management.h>
23 #include <deal.II/base/utilities.h>
24 
25 #ifdef DEAL_II_WITH_MPI
26 # include <mpi.h>
27 #endif
28 
29 #include <string>
30 #include <list>
31 #include <map>
32 
33 DEAL_II_NAMESPACE_OPEN
34 
76 class Timer
77 {
78 public:
82  Timer ();
83 
84 #ifdef DEAL_II_WITH_MPI
85 
101  Timer (MPI_Comm mpi_communicator,
102  const bool sync_wall_time = false);
103 
108  const Utilities::MPI::MinMaxAvg &get_data() const;
109 
113  template <class StreamType>
114  void print_data(StreamType &stream) const;
115 
116 
117 #endif
118 
123  void start ();
124 
129  double stop ();
130 
134  void reset ();
135 
140  void restart();
141 
146  double operator() () const;
147 
152  double wall_time () const;
153 
158  double get_lap_time () const;
159 
160 private:
161 
166  double start_time;
167 
168 
180 
186 
192 
198 
203 
207  bool running;
208 
213 
214 #ifdef DEAL_II_WITH_MPI
215 
219 
227 #endif
228 };
229 
230 
231 
232 //TODO: The following class is not thread-safe
390 {
391 public:
397  class Scope
398  {
399  public:
404  Scope(::TimerOutput &timer_, const std::string &section_name);
405 
409  ~Scope();
410 
415  void stop();
416 
417  private:
425  bool in;
426  };
427 
433  {
450  };
451 
457  {
470  };
471 
482  TimerOutput (std::ostream &stream,
484  const enum OutputType output_type);
485 
498  const enum OutputType output_type);
499 
500 #ifdef DEAL_II_WITH_MPI
501 
524  TimerOutput (MPI_Comm mpi_comm,
525  std::ostream &stream,
527  const enum OutputType output_type);
528 
552  TimerOutput (MPI_Comm mpi_comm,
553  ConditionalOStream &stream,
555  const enum OutputType output_type);
556 
557 
558 
559 
560 #endif
561 
566  ~TimerOutput();
567 
572  void enter_subsection (const std::string &section_name);
573 
577  void enter_section (const std::string &section_name);
578 
579  //TODO: make some of these functions DEPRECATED (I would keep enter/exit_section)
580 
585  void leave_subsection (const std::string &section_name = std::string());
586 
590  void exit_section (const std::string &section_name = std::string());
591 
596  void print_summary () const;
597 
604  void disable_output ();
605 
612  void enable_output ();
613 
617  void reset ();
618 
619 private:
624 
629 
630 
636 
641  struct Section
642  {
643  Timer timer;
644  double total_cpu_time;
645  double total_wall_time;
646  unsigned int n_calls;
647  };
648 
652  std::map<std::string, Section> sections;
653 
658 
664 
671  std::list<std::string> active_sections;
672 
677 
683 };
684 
685 
686 
687 /* ---------------- inline functions ----------------- */
688 
689 
690 inline
692 {
693  reset();
694  start();
695 }
696 
697 
698 
699 #ifdef DEAL_II_WITH_MPI
700 
701 inline
704 {
705  return mpi_data;
706 }
707 
708 
709 
710 template <class StreamType>
711 inline
712 void
713 Timer::print_data(StreamType &stream) const
714 {
715  unsigned int my_id = ::Utilities::MPI::this_mpi_process(mpi_communicator);
716  if (my_id==0)
717  stream << mpi_data.max << " wall,"
718  << " max @" << mpi_data.max_index
719  << ", min=" << mpi_data.min << " @" << mpi_data.min_index
720  << ", avg=" << mpi_data.avg
721  << std::endl;
722 }
723 
724 #endif
725 
726 
727 
728 inline
729 void
730 TimerOutput::enter_section (const std::string &section_name)
731 {
732  enter_subsection(section_name);
733 }
734 
735 
736 
737 inline
738 void
739 TimerOutput::exit_section (const std::string &section_name)
740 {
741  leave_subsection(section_name);
742 }
743 
744 inline
745 TimerOutput::Scope::Scope(::TimerOutput &timer_, const std::string &section_name)
746  :
747  timer(timer_), in(true)
748 {
749  timer.enter_section(section_name);
750 }
751 
752 inline
754 {
755  try
756  {
757  stop();
758  }
759  catch (...)
760  {}
761 }
762 
763 inline
764 void
766 {
767  if (!in) return;
768  in=false;
769 
770  timer.exit_section();
771 }
772 
773 
774 DEAL_II_NAMESPACE_CLOSE
775 
776 #endif
void print_summary() const
Definition: timer.cc:478
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:703
void reset()
Definition: timer.cc:661
double get_lap_time() const
Definition: timer.cc:214
MPI_Comm mpi_communicator
Definition: timer.h:676
::TimerOutput & timer
Definition: timer.h:421
void reset()
Definition: timer.cc:282
MPI_Comm mpi_communicator
Definition: timer.h:212
bool sync_wall_time
Definition: timer.h:218
double stop()
Definition: timer.cc:166
Utilities::MPI::MinMaxAvg mpi_data
Definition: timer.h:226
double start_wall_time
Definition: timer.h:185
Timer timer_all
Definition: timer.h:635
Threads::Mutex mutex
Definition: timer.h:682
void enter_section(const std::string &section_name)
Definition: timer.h:730
void exit_section(const std::string &section_name=std::string())
Definition: timer.h:739
OutputType output_type
Definition: timer.h:628
bool output_is_enabled
Definition: timer.h:663
double wall_time() const
Definition: timer.cc:260
double cumulative_time
Definition: timer.h:191
double last_lap_time
Definition: timer.h:202
double operator()() const
Definition: timer.cc:222
double start_time_children
Definition: timer.h:179
Timer()
Definition: timer.cc:43
OutputFrequency
Definition: timer.h:432
void disable_output()
Definition: timer.cc:647
bool running
Definition: timer.h:207
TimerOutput(std::ostream &stream, const enum OutputFrequency output_frequency, const enum OutputType output_type)
Definition: timer.cc:294
std::list< std::string > active_sections
Definition: timer.h:671
void start()
Definition: timer.cc:127
void restart()
Definition: timer.h:691
double start_time
Definition: timer.h:166
std::map< std::string, Section > sections
Definition: timer.h:652
ConditionalOStream out_stream
Definition: timer.h:657
double cumulative_wall_time
Definition: timer.h:197
void print_data(StreamType &stream) const
Definition: timer.h:713
void enable_output()
Definition: timer.cc:655
Definition: timer.h:76
void enter_subsection(const std::string &section_name)
Definition: timer.cc:372
OutputFrequency output_frequency
Definition: timer.h:623
~TimerOutput()
Definition: timer.cc:354
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:745
void leave_subsection(const std::string &section_name=std::string())
Definition: timer.cc:416