Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_TimeMonitor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_TIMEMONITOR_HPP
43 #define TEUCHOS_TIMEMONITOR_HPP
44 
45 
69 #include "Teuchos_Comm.hpp"
70 #include "Teuchos_Time.hpp"
71 
73 
78 #define TEUCHOS_TIMER(funcName, strName) \
79  static Teuchos::Time& funcName() \
80  {static Teuchos::RCP<Time> rtn = \
81  Teuchos::TimeMonitor::getNewCounter(strName); return *rtn;}
82 
83 
92 #define TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, DIFF ) \
93  static Teuchos::RCP<Teuchos::Time> DIFF ## blabla_localTimer; \
94  if(!DIFF ## blabla_localTimer.get()) { \
95  std::ostringstream oss; \
96  oss << FUNCNAME; \
97  DIFF ## blabla_localTimer = Teuchos::TimeMonitor::getNewCounter(oss.str()); \
98  } \
99  Teuchos::TimeMonitor DIFF ## blabla_localTimeMonitor(*DIFF ## blabla_localTimer)
100 
101 
124 #define TEUCHOS_FUNC_TIME_MONITOR( FUNCNAME ) \
125  TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, main )
126 
127 
128 namespace Teuchos {
129 
130 // Forward declaration
131 class StackedTimer;
132 
144 typedef std::map<std::string, std::vector<std::pair<double, double> > > stat_map_type;
145 
178 class TEUCHOSCOMM_LIB_DLL_EXPORT TimeMonitor :
179  public PerformanceMonitorBase<Time> {
180 public:
181 
184 
193  TimeMonitor (Time& timer, bool reset=false);
194 
196  ~TimeMonitor();
198 
207  static RCP<Time> getNewTimer (const std::string& name) {
208  return getNewCounter (name);
209  }
210 
226  static void disableTimer (const std::string& name);
227 
236  static void enableTimer (const std::string& name);
237 
244  static void zeroOutTimers();
245 
359  static void
360  computeGlobalTimerStatistics (stat_map_type& statData,
361  std::vector<std::string>& statNames,
362  Ptr<const Comm<int> > comm,
363  const ECounterSetOp setOp=Intersection,
364  const std::string& filter="");
365 
394  static void
395  computeGlobalTimerStatistics (stat_map_type& statData,
396  std::vector<std::string>& statNames,
397  const ECounterSetOp setOp=Intersection,
398  const std::string& filter="");
399 
466  static void
467  summarize (Ptr<const Comm<int> > comm,
468  std::ostream &out=std::cout,
469  const bool alwaysWriteLocal=false,
470  const bool writeGlobalStats=true,
471  const bool writeZeroTimers=true,
472  const ECounterSetOp setOp=Intersection,
473  const std::string& filter="",
474  const bool ignoreZeroTimers=false);
475 
492  static void
493  summarize (std::ostream& out=std::cout,
494  const bool alwaysWriteLocal=false,
495  const bool writeGlobalStats=true,
496  const bool writeZeroTimers=true,
497  const ECounterSetOp setOp=Intersection,
498  const std::string& filter="",
499  const bool ignoreZeroTimers=false);
500 
579  static void
580  report (Ptr<const Comm<int> > comm,
581  std::ostream& out,
582  const std::string& filter,
583  const RCP<ParameterList>& params=null);
584 
589  static void
590  report (Ptr<const Comm<int> > comm,
591  std::ostream& out,
592  const RCP<ParameterList>& params=null);
593 
598  static void
599  report (std::ostream& out,
600  const std::string& filter,
601  const RCP<ParameterList>& params=null);
602 
607  static void
608  report (std::ostream& out,
609  const RCP<ParameterList>& params=null);
610 
612  static RCP<const ParameterList> getValidReportParameters ();
613 
617  static void setStackedTimer(const Teuchos::RCP<Teuchos::StackedTimer>& t);
618 
620  static const Teuchos::RCP<Teuchos::StackedTimer>& getStackedTimer();
621 
622  private:
627  enum ETimeMonitorReportFormat {
628  REPORT_FORMAT_YAML,
629  REPORT_FORMAT_TABLE
630  };
631 
636  enum ETimeMonitorYamlFormat {
637  YAML_FORMAT_COMPACT,
638  YAML_FORMAT_SPACIOUS
639  };
640 
655  static void
656  summarizeToYaml (Ptr<const Comm<int> > comm,
657  std::ostream& out,
658  const ETimeMonitorYamlFormat yamlStyle,
659  const std::string& filter="");
660 
665  static void
666  summarizeToYaml (std::ostream& out,
667  const ETimeMonitorYamlFormat yamlStyle,
668  const std::string& filter="");
669 
674  static void setReportFormatParameter (ParameterList& plist);
675 
680  static void setYamlFormatParameter (ParameterList& plist);
681 
686  static void setSetOpParameter (ParameterList& plist);
687 
702  static void setReportParameters (const RCP<ParameterList>& params);
703 
705 
706 
708  static ETimeMonitorReportFormat reportFormat_;
709 
712  static ETimeMonitorYamlFormat yamlStyle_;
713 
715  static ECounterSetOp setOp_;
716 
718  static bool alwaysWriteLocal_;
719 
722  static bool writeGlobalStats_;
723 
725  static bool writeZeroTimers_;
727 
732  static bool setParams_;
733 
735  static Teuchos::RCP<Teuchos::StackedTimer> stackedTimer_;
736 };
737 
738 
739 } // namespace Teuchos
740 
741 
742 namespace Teuchos {
743 
753 {
754  virtual void summarize (std::ostream& out) {
756  }
757 };
758 
801 public:
804  if (is_null (CommandLineProcessor::getTimeMonitorSurrogate ())) {
805  CommandLineProcessor::setTimeMonitorSurrogate (Teuchos::rcp (new TimeMonitorSurrogateImpl));
806  }
807  }
808 };
809 
810 } // end namespace Teuchos
811 
812 
813 namespace {
814 
815 // Inject the implementation in every translation unit.
816 Teuchos::TimeMonitorSurrogateImplInserter timeMonitorSurrogateImplInserter;
817 
818 } // namespace (anonymous)
819 
820 #endif // TEUCHOS_TIMEMONITOR_H
Teuchos::TimeMonitor::summarize
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
Print summary statistics for all timers on the given communicator.
Definition: Teuchos_TimeMonitor.cpp:922
Teuchos::TimeMonitor::getNewTimer
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
Definition: Teuchos_TimeMonitor.hpp:207
Teuchos_ParameterList.hpp
Templated Parameter List class.
Teuchos::ECounterSetOp
ECounterSetOp
Set operation type for mergeCounterNames() to perform.
Definition: Teuchos_PerformanceMonitorBase.hpp:66
Teuchos::TimeMonitorSurrogateImplInserter
Injects run-time dependency of a class on TimeMonitor.
Definition: Teuchos_TimeMonitor.hpp:800
Teuchos_PerformanceMonitorBase.hpp
Provides common capabilities for collecting and reporting performance data across processors.
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Definition: Teuchos_RCPDecl.hpp:1224
Teuchos::Time
Wall-clock timer.
Definition: Teuchos_Time.hpp:85
Teuchos::RCP
Smart reference counting pointer class for automatic garbage collection.
Definition: Teuchos_RCPDecl.hpp:429
Teuchos::Ptr
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Definition: Teuchos_PtrDecl.hpp:104
Teuchos::TimeMonitor
A scope-safe timer wrapper class, that can compute global timer statistics.
Definition: Teuchos_TimeMonitor.hpp:178
Teuchos_CommandLineProcessor.hpp
Basic command line parser for input from (argc,argv[])
Teuchos_Time.hpp
Basic wall-clock timer class.
Teuchos::PerformanceMonitorBase
Common capabilities for collecting and reporting performance data across processors.
Definition: Teuchos_PerformanceMonitorBase.hpp:157
Teuchos::Comm
Abstract interface for distributed-memory communication.
Definition: Teuchos_Comm.hpp:85
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos::TimeMonitorSurrogateImpl
Implementation of TimeMonitorSurrogate that invokes TimeMonitor.
Definition: Teuchos_TimeMonitor.hpp:752
Teuchos::CommandLineProcessor::TimeMonitorSurrogate
Interface by which CommandLineProcessor may use TimeMonitor.
Definition: Teuchos_CommandLineProcessor.hpp:650
Teuchos::stat_map_type
std::map< std::string, std::vector< std::pair< double, double > > > stat_map_type
Global statistics collected from timer data.
Definition: Teuchos_TimeMonitor.hpp:144
Teuchos::is_null
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Definition: Teuchos_RCPStdSharedPtrConversionsDecl.hpp:148
Teuchos::TimeMonitorSurrogateImplInserter::TimeMonitorSurrogateImplInserter
TimeMonitorSurrogateImplInserter()
Constructor: inject dependency on TimeMonitor into CommandLineProcessor.
Definition: Teuchos_TimeMonitor.hpp:803