Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TimeMonitor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_TIMEMONITOR_HPP
11 #define TEUCHOS_TIMEMONITOR_HPP
12 
13 
38 #include "Teuchos_Comm.hpp"
39 #include "Teuchos_Time.hpp"
40 
42 
47 #define TEUCHOS_TIMER(funcName, strName) \
48  static Teuchos::Time& funcName() \
49  {static Teuchos::RCP<Time> rtn = \
50  Teuchos::TimeMonitor::getNewCounter(strName); return *rtn;}
51 
52 
61 #define TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, DIFF ) \
62  static Teuchos::RCP<Teuchos::Time> DIFF ## blabla_localTimer; \
63  if(!DIFF ## blabla_localTimer.get()) { \
64  std::ostringstream oss; \
65  oss << FUNCNAME; \
66  DIFF ## blabla_localTimer = Teuchos::TimeMonitor::getNewCounter(oss.str()); \
67  } \
68  Teuchos::TimeMonitor DIFF ## blabla_localTimeMonitor(*DIFF ## blabla_localTimer)
69 
70 
93 #define TEUCHOS_FUNC_TIME_MONITOR( FUNCNAME ) \
94  TEUCHOS_FUNC_TIME_MONITOR_DIFF( FUNCNAME, main )
95 
96 
97 namespace Teuchos {
98 
99 // Forward declaration
100 class StackedTimer;
101 
113 typedef std::map<std::string, std::vector<std::pair<double, double> > > stat_map_type;
114 
148 class TEUCHOSCOMM_LIB_DLL_EXPORT TimeMonitor :
149  public PerformanceMonitorBase<Time> {
150 public:
151 
154 
163  TimeMonitor (Time& timer, bool reset=false);
164 
166  TimeMonitor () = delete;
167 
169  ~TimeMonitor() override;
171 
180  static RCP<Time> getNewTimer (const std::string& name) {
181  return getNewCounter (name);
182  }
183 
199  static void disableTimer (const std::string& name);
200 
209  static void enableTimer (const std::string& name);
210 
217  static void zeroOutTimers();
218 
334  static void
335  computeGlobalTimerStatistics (stat_map_type& statData,
336  std::vector<std::string>& statNames,
337  Ptr<const Comm<int> > comm,
338  const ECounterSetOp setOp=Intersection,
339  const std::string& filter="");
340 
376  static void
377  computeGlobalTimerStatistics (stat_map_type& statData,
378  std::vector<std::string>& statNames,
379  const ECounterSetOp setOp=Intersection,
380  const std::string& filter="");
381 
449  static void
450  summarize (Ptr<const Comm<int> > comm,
451  std::ostream &out=std::cout,
452  const bool alwaysWriteLocal=false,
453  const bool writeGlobalStats=true,
454  const bool writeZeroTimers=true,
455  const ECounterSetOp setOp=Intersection,
456  const std::string& filter="",
457  const bool ignoreZeroTimers=false);
458 
476  static void
477  summarize (std::ostream& out=std::cout,
478  const bool alwaysWriteLocal=false,
479  const bool writeGlobalStats=true,
480  const bool writeZeroTimers=true,
481  const ECounterSetOp setOp=Intersection,
482  const std::string& filter="",
483  const bool ignoreZeroTimers=false);
484 
566  static void
567  report (Ptr<const Comm<int> > comm,
568  std::ostream& out,
569  const std::string& filter,
570  const RCP<ParameterList>& params=null);
571 
576  static void
577  report (Ptr<const Comm<int> > comm,
578  std::ostream& out,
579  const RCP<ParameterList>& params=null);
580 
585  static void
586  report (std::ostream& out,
587  const std::string& filter,
588  const RCP<ParameterList>& params=null);
589 
594  static void
595  report (std::ostream& out,
596  const RCP<ParameterList>& params=null);
597 
599  static RCP<const ParameterList> getValidReportParameters ();
600 
605  static void setStackedTimer(const Teuchos::RCP<Teuchos::StackedTimer>& t);
606 
608  static Teuchos::RCP<Teuchos::StackedTimer> getStackedTimer();
609 
610  static bool stackedTimerNameIsDefault();
611 
612  private:
617  enum ETimeMonitorReportFormat {
618  REPORT_FORMAT_YAML,
619  REPORT_FORMAT_TABLE
620  };
621 
626  enum ETimeMonitorYamlFormat {
627  YAML_FORMAT_COMPACT,
628  YAML_FORMAT_SPACIOUS
629  };
630 
645  static void
646  summarizeToYaml (Ptr<const Comm<int> > comm,
647  std::ostream& out,
648  const ETimeMonitorYamlFormat yamlStyle,
649  const std::string& filter="");
650 
655  static void
656  summarizeToYaml (std::ostream& out,
657  const ETimeMonitorYamlFormat yamlStyle,
658  const std::string& filter="");
659 
664  static void setReportFormatParameter (ParameterList& plist);
665 
670  static void setYamlFormatParameter (ParameterList& plist);
671 
676  static void setSetOpParameter (ParameterList& plist);
677 
692  static void setReportParameters (const RCP<ParameterList>& params);
693 
695 
696 
700  static ETimeMonitorReportFormat reportFormat_;
701 
705  static ETimeMonitorYamlFormat yamlStyle_;
706 
709  static ECounterSetOp setOp_;
710 
713  static bool alwaysWriteLocal_;
714 
720  static bool writeGlobalStats_;
721 
723  static bool writeZeroTimers_;
725 
730  static bool setParams_;
731 
732 protected:
736 };
737 
738 
741 class TEUCHOSCOMM_LIB_DLL_EXPORT SyncTimeMonitor :
742  public TimeMonitor {
743 public:
744 
747 
756  SyncTimeMonitor(Time& timer, Ptr<const Comm<int> > comm, bool reset=false);
757 
759  SyncTimeMonitor () = delete;
760 
762  ~SyncTimeMonitor() override;
764 
765 private:
766  // \brief Communicator on which barrier will be called.
767  Ptr<const Comm<int> > comm_;
768 };
769 
770 
771 } // namespace Teuchos
772 
773 
774 namespace Teuchos {
775 
785 {
786  virtual void summarize (std::ostream& out) {
788  }
789 };
790 
833 public:
836  if (is_null (CommandLineProcessor::getTimeMonitorSurrogate ())) {
837  CommandLineProcessor::setTimeMonitorSurrogate (Teuchos::rcp (new TimeMonitorSurrogateImpl));
838  }
839  }
840 };
841 
842 } // end namespace Teuchos
843 
844 
845 namespace {
846 
847 // Inject the implementation in every translation unit.
848 Teuchos::TimeMonitorSurrogateImplInserter timeMonitorSurrogateImplInserter;
849 
850 } // namespace (anonymous)
851 
852 #endif // TEUCHOS_TIMEMONITOR_H
std::map< std::string, std::vector< std::pair< double, double > > > stat_map_type
Global statistics collected from timer data.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Basic wall-clock timer class.
A TimeMonitor that waits at a MPI barrier before destruction.
Implementation of TimeMonitorSurrogate that invokes TimeMonitor.
static RCP< Time > getNewTimer(const std::string &name)
Return a new timer with the given name (class method).
static Teuchos::RCP< Teuchos::StackedTimer > stackedTimer_
Stacked timer for optional injection of timing from TimeMonitor-enabled objects.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
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.
Wall-clock timer.
Interface by which CommandLineProcessor may use TimeMonitor.
TimeMonitorSurrogateImplInserter()
Constructor: inject dependency on TimeMonitor into CommandLineProcessor.
Basic command line parser for input from (argc,argv[])
Smart reference counting pointer class for automatic garbage collection.
Common capabilities for collecting and reporting performance data collectively across MPI processes...
ECounterSetOp
Set operation type for mergeCounterNames() to perform.
Common capabilities for collecting and reporting performance data across processors.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
Scope guard for Time, that can compute MPI collective timer statistics.
Injects run-time dependency of a class on TimeMonitor.