Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
timer.h
1 // ---------------------------------------------------------------------
2 // @f$Id: timer.h 30040 2013-07-18 17:06:48Z maier @f$
3 //
4 // Copyright (C) 1998 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__timer_h
18 #define __deal2__timer_h
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/conditional_ostream.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 
34 
76 class Timer
77 {
78 public:
82  Timer ();
83 
84 #ifdef DEAL_II_WITH_MPI
85 
109  Timer (MPI_Comm mpi_communicator,
110  bool sync_wall_time = false);
111 
118  const Utilities::MPI::MinMaxAvg &get_data() const;
119 
123  template <class STREAM>
124  void print_data(STREAM &stream) const;
125 
126 
127 #endif
128 
134  void start ();
135 
141  double stop ();
142 
147  void reset ();
148 
155  void restart();
156 
163  double operator() () const;
164 
171  double wall_time () const;
172 
173 private:
174 
181  double start_time;
182 
183 
205 
213 
220 
228 
233  bool running;
234 
240 
241 #ifdef DEAL_II_WITH_MPI
242 
247 
258 #endif
259 };
260 
261 
262 
263 //TODO: The following class is not thread-safe
326 {
327 public:
332  class Scope
333  {
334  public:
339  Scope(::TimerOutput &timer_, const std::string &section_name);
340 
344  ~Scope();
345 
350  void stop();
351 
352  private:
360  bool in;
361  };
362 
368  enum OutputFrequency {every_call, summary, every_call_and_summary, never}
369  output_frequency;
370 
375  enum OutputType {cpu_times, wall_times, cpu_and_wall_times}
376  output_type;
377 
382  TimerOutput (std::ostream &stream,
383  const enum OutputFrequency output_frequency,
384  const enum OutputType output_type);
385 
391  const enum OutputFrequency output_frequency,
392  const enum OutputType output_type);
393 
394 #ifdef DEAL_II_WITH_MPI
395 
406  TimerOutput (MPI_Comm mpi_comm,
407  std::ostream &stream,
408  const enum OutputFrequency output_frequency,
409  const enum OutputType output_type);
410 
422  TimerOutput (MPI_Comm mpi_comm,
423  ConditionalOStream &stream,
424  const enum OutputFrequency output_frequency,
425  const enum OutputType output_type);
426 
427 
428 
429 
430 #endif
431 
437  ~TimerOutput();
438 
445  void enter_subsection (const std::string &section_name);
446 
450  void enter_section (const std::string &section_name);
451 
452  //TODO: make some of these functions DEPRECATED (I would keep enter/exit_section)
453 
459  void leave_subsection (const std::string &section_name = std::string());
460 
464  void exit_section (const std::string &section_name = std::string());
465 
471  void print_summary () const;
472 
482  void disable_output ();
483 
495  void enable_output ();
496 
500  void reset ();
501 
502 private:
510 
516  struct Section
517  {
518  Timer timer;
519  double total_cpu_time;
520  double total_wall_time;
521  unsigned int n_calls;
522  };
523 
528  std::map<std::string, Section> sections;
529 
535 
542 
554  std::list<std::string> active_sections;
555 
560 
567 };
568 
569 
570 
571 /* ---------------- inline functions ----------------- */
572 
573 
574 inline
576 {
577  reset();
578  start();
579 }
580 
581 
582 
583 #ifdef DEAL_II_WITH_MPI
584 
585 inline
588 {
589  return mpi_data;
590 }
591 
592 
593 
594 template <class STREAM>
595 inline
596 void
597 Timer::print_data(STREAM &stream) const
598 {
599  unsigned int my_id = ::Utilities::MPI::this_mpi_process(mpi_communicator);
600  if (my_id==0)
601  stream << mpi_data.max << " wall,"
602  << " max @" << mpi_data.max_index
603  << ", min=" << mpi_data.min << " @" << mpi_data.min_index
604  << ", avg=" << mpi_data.avg
605  << std::endl;
606 }
607 
608 #endif
609 
610 
611 
612 inline
613 void
614 TimerOutput::enter_section (const std::string &section_name)
615 {
616  enter_subsection(section_name);
617 }
618 
619 
620 
621 inline
622 void
623 TimerOutput::exit_section (const std::string &section_name)
624 {
625  leave_subsection(section_name);
626 }
627 
628 inline
629 TimerOutput::Scope::Scope(::TimerOutput &timer_, const std::string &section_name)
630  :
631  timer(timer_), in(true)
632 {
633  timer.enter_section(section_name);
634 }
635 
636 inline
638 {
639  stop();
640 }
641 
642 inline
643 void
645 {
646  if (!in) return;
647  in=false;
648 
649  timer.exit_section();
650 }
651 
652 
653 DEAL_II_NAMESPACE_CLOSE
654 
655 #endif
void reset()
MPI_Comm mpi_communicator
Definition: timer.h:559
::TimerOutput & timer
Definition: timer.h:356
void print_data(STREAM &stream) const
Definition: timer.h:597
void reset()
MPI_Comm mpi_communicator
Definition: timer.h:239
bool sync_wall_time
Definition: timer.h:246
double stop()
double start_wall_time
Definition: timer.h:212
OutputFrequency
Definition: timer.h:368
Timer timer_all
Definition: timer.h:509
Threads::Mutex mutex
Definition: timer.h:566
void enter_section(const std::string &section_name)
Definition: timer.h:614
const Utilities::MPI::MinMaxAvg & get_data() const
Definition: timer.h:587
void exit_section(const std::string &section_name=std::string())
Definition: timer.h:623
bool output_is_enabled
Definition: timer.h:541
double cumulative_time
Definition: timer.h:219
double start_time_children
Definition: timer.h:204
void disable_output()
bool running
Definition: timer.h:233
TimerOutput(std::ostream &stream, const enum OutputFrequency output_frequency, const enum OutputType output_type)
std::list< std::string > active_sections
Definition: timer.h:554
void start()
void restart()
Definition: timer.h:575
double start_time
Definition: timer.h:181
std::map< std::string, Section > sections
Definition: timer.h:528
Utilities::System::MinMaxAvg mpi_data
Definition: timer.h:257
ConditionalOStream out_stream
Definition: timer.h:534
double cumulative_wall_time
Definition: timer.h:227
void enable_output()
double wall_time() const
double operator()() const
Definition: timer.h:76
void enter_subsection(const std::string &section_name)
void print_summary() const
Scope(::TimerOutput &timer_, const std::string &section_name)
Definition: timer.h:629
void leave_subsection(const std::string &section_name=std::string())