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 //
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 
760 protected:
764 };
765 
766 
770  public TimeMonitor {
771 public:
772 
775 
784  SyncTimeMonitor(Time& timer, Ptr<const Comm<int> > comm, bool reset=false);
785 
787  SyncTimeMonitor () = delete;
788 
790  ~SyncTimeMonitor() override;
792 
793 private:
794  // \brief Communicator on which barrier will be called.
795  Ptr<const Comm<int> > comm_;
796 };
797 
798 
799 } // namespace Teuchos
800 
801 
802 namespace Teuchos {
803 
813 {
814  virtual void summarize (std::ostream& out) {
816  }
817 };
818 
861 public:
864  if (is_null (CommandLineProcessor::getTimeMonitorSurrogate ())) {
865  CommandLineProcessor::setTimeMonitorSurrogate (Teuchos::rcp (new TimeMonitorSurrogateImpl));
866  }
867  }
868 };
869 
870 } // end namespace Teuchos
871 
872 
873 namespace {
874 
875 // Inject the implementation in every translation unit.
876 Teuchos::TimeMonitorSurrogateImplInserter timeMonitorSurrogateImplInserter;
877 
878 } // namespace (anonymous)
879 
880 #endif // TEUCHOS_TIMEMONITOR_H
SyncTimeMonitor()=delete
Default constructor is deleted, since it would be unsafe.
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[])
~SyncTimeMonitor() override
Destructor: stops the timer.
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.