Reference documentation for deal.II version 8.4.2
timer.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2015 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/thread_management.h>
22 #include <deal.II/base/utilities.h>
23 
24 #ifdef DEAL_II_WITH_MPI
25 # include <mpi.h>
26 #endif
27 
28 #include <string>
29 #include <list>
30 #include <map>
31 
32 DEAL_II_NAMESPACE_OPEN
33 
75 class Timer
76 {
77 public:
81  Timer ();
82 
83 #ifdef DEAL_II_WITH_MPI
84 
100  Timer (MPI_Comm mpi_communicator,
101  const bool sync_wall_time = false);
102 
107  const Utilities::MPI::MinMaxAvg &get_data() const;
108 
112  template <class StreamType>
113  void print_data(StreamType &stream) const;
114 
115 
116 #endif
117 
122  void start ();
123 
128  double stop ();
129 
133  void reset ();
134 
139  void restart();
140 
145  double operator() () const;
146 
151  double wall_time () const;
152 
157  double get_lap_time () const;
158 
159 private:
160 
165  double start_time;
166 
167 
179 
185 
191 
197 
202 
206  bool running;
207 
212 
213 #ifdef DEAL_II_WITH_MPI
214 
218 
226 #endif
227 };
228 
229 
230 
231 //TODO: The following class is not thread-safe
389 {
390 public:
396  class Scope
397  {
398  public:
403  Scope(::TimerOutput &timer_, const std::string &section_name);
404 
408  ~Scope();
409 
414  void stop();
415 
416  private:
424  bool in;
425  };
426 
432  {
433  every_call,
434  summary,
435  every_call_and_summary,
436  never
437  };
438 
444  {
445  cpu_times,
446  wall_times,
447  cpu_and_wall_times
448  };
449 
460  TimerOutput (std::ostream &stream,
461  const enum OutputFrequency output_frequency,
462  const enum OutputType output_type);
463 
475  const enum OutputFrequency output_frequency,
476  const enum OutputType output_type);
477 
478 #ifdef DEAL_II_WITH_MPI
479 
502  TimerOutput (MPI_Comm mpi_comm,
503  std::ostream &stream,
504  const enum OutputFrequency output_frequency,
505  const enum OutputType output_type);
506 
530  TimerOutput (MPI_Comm mpi_comm,
531  ConditionalOStream &stream,
532  const enum OutputFrequency output_frequency,
533  const enum OutputType output_type);
534 
535 
536 
537 
538 #endif
539 
544  ~TimerOutput();
545 
550  void enter_subsection (const std::string &section_name);
551 
555  void enter_section (const std::string &section_name);
556 
557  //TODO: make some of these functions DEPRECATED (I would keep enter/exit_section)
558 
563  void leave_subsection (const std::string &section_name = std::string());
564 
568  void exit_section (const std::string &section_name = std::string());
569 
574  void print_summary () const;
575 
582  void disable_output ();
583 
590  void enable_output ();
591 
595  void reset ();
596 
597 private:
602 
607 
608 
614 
619  struct Section
620  {
621  Timer timer;
622  double total_cpu_time;
623  double total_wall_time;
624  unsigned int n_calls;
625  };
626 
630  std::map<std::string, Section> sections;
631 
636 
642 
649  std::list<std::string> active_sections;
650 
655 
661 };
662 
663 
664 
665 /* ---------------- inline functions ----------------- */
666 
667 
668 inline
670 {
671  reset();
672  start();
673 }
674 
675 
676 
677 #ifdef DEAL_II_WITH_MPI
678 
679 inline
682 {
683  return mpi_data;
684 }
685 
686 
687 
688 template <class StreamType>
689 inline
690 void
691 Timer::print_data(StreamType &stream) const
692 {
693  unsigned int my_id = ::Utilities::MPI::this_mpi_process(mpi_communicator);
694  if (my_id==0)
695  stream << mpi_data.max << " wall,"
696  << " max @" << mpi_data.max_index
697  << ", min=" << mpi_data.min << " @" << mpi_data.min_index
698  << ", avg=" << mpi_data.avg
699  << std::endl;
700 }
701 
702 #endif
703 
704 
705 
706 inline
707 void
708 TimerOutput::enter_section (const std::string &section_name)
709 {
710  enter_subsection(section_name);
711 }
712 
713 
714 
715 inline
716 void
717 TimerOutput::exit_section (const std::string &section_name)
718 {
719  leave_subsection(section_name);
720 }
721 
722 inline
723 TimerOutput::Scope::Scope(::TimerOutput &timer_, const std::string &section_name)
724  :
725  timer(timer_), in(true)
726 {
727  timer.enter_section(section_name);
728 }
729 
730 inline
732 {
733  stop();
734 }
735 
736 inline
737 void
739 {
740  if (!in) return;
741  in=false;
742 
744 }
745 
746 
747 DEAL_II_NAMESPACE_CLOSE
748 
749 #endif
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:681
double get_lap_time() const
Definition: timer.cc:186
MPI_Comm mpi_communicator
Definition: timer.h:654
::TimerOutput & timer
Definition: timer.h:420
void reset()
Definition: timer.cc:254
MPI_Comm mpi_communicator
Definition: timer.h:211
bool sync_wall_time
Definition: timer.h:217
double stop()
Definition: timer.cc:138
Utilities::MPI::MinMaxAvg mpi_data
Definition: timer.h:225
double start_wall_time
Definition: timer.h:184
Timer timer_all
Definition: timer.h:613
Threads::Mutex mutex
Definition: timer.h:660
void enter_section(const std::string &section_name)
Definition: timer.h:708
void exit_section(const std::string &section_name=std::string())
Definition: timer.h:717
OutputType output_type
Definition: timer.h:606
bool output_is_enabled
Definition: timer.h:641
double wall_time() const
Definition: timer.cc:232
double cumulative_time
Definition: timer.h:190
double last_lap_time
Definition: timer.h:201
double operator()() const
Definition: timer.cc:194
double start_time_children
Definition: timer.h:178
Timer()
Definition: timer.cc:41
OutputFrequency
Definition: timer.h:431
bool running
Definition: timer.h:206
std::list< std::string > active_sections
Definition: timer.h:649
void start()
Definition: timer.cc:102
void restart()
Definition: timer.h:669
double start_time
Definition: timer.h:165
std::map< std::string, Section > sections
Definition: timer.h:630
ConditionalOStream out_stream
Definition: timer.h:635
double cumulative_wall_time
Definition: timer.h:196
void print_data(StreamType &stream) const
Definition: timer.h:691
Definition: timer.h:75
OutputFrequency output_frequency
Definition: timer.h:601
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:723