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 // ***********************************************************************
38 // @HEADER
39 
40 #ifndef TEUCHOS_TIMEMONITOR_HPP
41 #define TEUCHOS_TIMEMONITOR_HPP
42 
43 
68 #include "Teuchos_Comm.hpp"
69 #include "Teuchos_Time.hpp"
70 
72 
77 #define TEUCHOS_TIMER(funcName, strName) \
78  static Teuchos::Time& funcName() \
79  {static Teuchos::RCP<Time> rtn = \
80  Teuchos::TimeMonitor::getNewCounter(strName); return *rtn;}
81 
82 
91 #define TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, DIFF ) \
92  static Teuchos::RCP<Teuchos::Time> DIFF ## blabla_localTimer; \
93  if(!DIFF ## blabla_localTimer.get()) { \
94  std::ostringstream oss; \
95  oss << FUNCNAME; \
96  DIFF ## blabla_localTimer = Teuchos::TimeMonitor::getNewCounter(oss.str()); \
97  } \
98  Teuchos::TimeMonitor DIFF ## blabla_localTimeMonitor(*DIFF ## blabla_localTimer)
99 
100 
123 #define TEUCHOS_FUNC_TIME_MONITOR( FUNCNAME ) \
124  TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, main )
125 
126 
127 namespace Teuchos {
128 
129 // Forward declaration
130 class StackedTimer;
131 
143 typedef std::map<std::string, std::vector<std::pair<double, double> > > stat_map_type;
144 
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 () = delete;
197 
199  ~TimeMonitor() override;
201 
210  static RCP<Time> getNewTimer (const std::string& name) {
211  return getNewCounter (name);
212  }
213 
229  static void disableTimer (const std::string& name);
230 
239  static void enableTimer (const std::string& name);
240 
247  static void zeroOutTimers();
248 
364  static void
365  computeGlobalTimerStatistics (stat_map_type& statData,
366  std::vector<std::string>& statNames,
367  Ptr<const Comm<int> > comm,
368  const ECounterSetOp setOp=Intersection,
369  const std::string& filter="");
370 
406  static void
407  computeGlobalTimerStatistics (stat_map_type& statData,
408  std::vector<std::string>& statNames,
409  const ECounterSetOp setOp=Intersection,
410  const std::string& filter="");
411 
479  static void
480  summarize (Ptr<const Comm<int> > comm,
481  std::ostream &out=std::cout,
482  const bool alwaysWriteLocal=false,
483  const bool writeGlobalStats=true,
484  const bool writeZeroTimers=true,
485  const ECounterSetOp setOp=Intersection,
486  const std::string& filter="",
487  const bool ignoreZeroTimers=false);
488 
506  static void
507  summarize (std::ostream& out=std::cout,
508  const bool alwaysWriteLocal=false,
509  const bool writeGlobalStats=true,
510  const bool writeZeroTimers=true,
511  const ECounterSetOp setOp=Intersection,
512  const std::string& filter="",
513  const bool ignoreZeroTimers=false);
514 
596  static void
597  report (Ptr<const Comm<int> > comm,
598  std::ostream& out,
599  const std::string& filter,
600  const RCP<ParameterList>& params=null);
601 
606  static void
607  report (Ptr<const Comm<int> > comm,
608  std::ostream& out,
609  const RCP<ParameterList>& params=null);
610 
615  static void
616  report (std::ostream& out,
617  const std::string& filter,
618  const RCP<ParameterList>& params=null);
619 
624  static void
625  report (std::ostream& out,
626  const RCP<ParameterList>& params=null);
627 
629  static RCP<const ParameterList> getValidReportParameters ();
630 
635  static void setStackedTimer(const Teuchos::RCP<Teuchos::StackedTimer>& t);
636 
638  static Teuchos::RCP<Teuchos::StackedTimer> getStackedTimer();
639 
640  private:
645  enum ETimeMonitorReportFormat {
646  REPORT_FORMAT_YAML,
647  REPORT_FORMAT_TABLE
648  };
649 
654  enum ETimeMonitorYamlFormat {
655  YAML_FORMAT_COMPACT,
656  YAML_FORMAT_SPACIOUS
657  };
658 
673  static void
674  summarizeToYaml (Ptr<const Comm<int> > comm,
675  std::ostream& out,
676  const ETimeMonitorYamlFormat yamlStyle,
677  const std::string& filter="");
678 
683  static void
684  summarizeToYaml (std::ostream& out,
685  const ETimeMonitorYamlFormat yamlStyle,
686  const std::string& filter="");
687 
692  static void setReportFormatParameter (ParameterList& plist);
693 
698  static void setYamlFormatParameter (ParameterList& plist);
699 
704  static void setSetOpParameter (ParameterList& plist);
705 
720  static void setReportParameters (const RCP<ParameterList>& params);
721 
723 
724 
728  static ETimeMonitorReportFormat reportFormat_;
729 
733  static ETimeMonitorYamlFormat yamlStyle_;
734 
737  static ECounterSetOp setOp_;
738 
741  static bool alwaysWriteLocal_;
742 
748  static bool writeGlobalStats_;
749 
751  static bool writeZeroTimers_;
753 
758  static bool setParams_;
759 
762  static Teuchos::RCP<Teuchos::StackedTimer> stackedTimer_;
763 };
764 
765 
766 } // namespace Teuchos
767 
768 
769 namespace Teuchos {
770 
780 {
781  virtual void summarize (std::ostream& out) {
783  }
784 };
785 
828 public:
831  if (is_null (CommandLineProcessor::getTimeMonitorSurrogate ())) {
832  CommandLineProcessor::setTimeMonitorSurrogate (Teuchos::rcp (new TimeMonitorSurrogateImpl));
833  }
834  }
835 };
836 
837 } // end namespace Teuchos
838 
839 
840 namespace {
841 
842 // Inject the implementation in every translation unit.
843 Teuchos::TimeMonitorSurrogateImplInserter timeMonitorSurrogateImplInserter;
844 
845 } // namespace (anonymous)
846 
847 #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:925
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:210
Teuchos_ParameterList.hpp
Templated Parameter List class.
Teuchos::ECounterSetOp
ECounterSetOp
Set operation type for mergeCounterNames() to perform.
Definition: Teuchos_PerformanceMonitorBase.hpp:63
Teuchos::TimeMonitorSurrogateImplInserter
Injects run-time dependency of a class on TimeMonitor.
Definition: Teuchos_TimeMonitor.hpp:827
Teuchos_PerformanceMonitorBase.hpp
Common capabilities for collecting and reporting performance data collectively across MPI processes.
Teuchos::rcp
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Definition: Teuchos_RCPDecl.hpp:1242
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
Scope guard for Time, that can compute MPI collective timer statistics.
Definition: Teuchos_TimeMonitor.hpp:179
Teuchos_CommandLineProcessor.hpp
Basic command line parser for input from (argc,argv[])
Teuchos_Time.hpp
Basic wall-clock timer class.
Teuchos::TimeMonitor::TimeMonitor
TimeMonitor()=delete
Default constructor is deleted, since it would be unsafe.
Teuchos::PerformanceMonitorBase
Common capabilities for collecting and reporting performance data across processors.
Definition: Teuchos_PerformanceMonitorBase.hpp:155
Teuchos::Comm
Abstract interface for distributed-memory communication.
Definition: Teuchos_Comm.hpp:310
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:780
Teuchos::CommandLineProcessor::TimeMonitorSurrogate
Interface by which CommandLineProcessor may use TimeMonitor.
Definition: Teuchos_CommandLineProcessor.hpp:667
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:143
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:830