Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_PerformanceMonitorBase.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_PERFORMANCEMONITORBASE_H
11 #define TEUCHOS_PERFORMANCEMONITORBASE_H
12 
16 
17 #include "Teuchos_ConfigDefs.hpp"
18 #include "Teuchos_Array.hpp"
19 #include "Teuchos_Comm.hpp"
20 #include "Teuchos_RCP.hpp"
21 #include "Teuchos_TableFormat.hpp"
22 #include <cstdlib> // atexit
23 
24 namespace Teuchos
25 {
33  enum ECounterSetOp { Intersection, Union };
34 
59  void
60  mergeCounterNames (const Comm<int>& comm,
61  const Array<std::string>& localNames,
62  Array<std::string>& globalNames,
63  const ECounterSetOp setOp);
64 
75  void
76  unsortedMergePair(const Array<std::string>& localNames,
77  Array<std::string>& globalNames,
78  const ECounterSetOp setOp);
79 
123  template <class T>
125  {
126  public:
128  PerformanceMonitorBase(T& counter_in, bool reset=false)
129  : counter_(counter_in), isRecursiveCall_(counter_.isRunning())
130  {
131  (void) reset; // get rid of "unused parameter" warning
132  counter_.incrementNumCalls ();
133  }
134 
136  PerformanceMonitorBase () = delete;
137 
142  virtual ~PerformanceMonitorBase() = default;
143 
158  static RCP<T> getNewCounter (const std::string& name);
159 
160  private:
170  static void freeTableFormat () {
171  if (format_ != nullptr) {
172  delete format_;
173  format_ = nullptr;
174  }
175  }
176 
186  static void freeCounters () {
187  if (counters_ != nullptr) {
188  delete counters_;
189  counters_ = nullptr;
190  }
191  }
192 
193  public:
202  static TableFormat& format ()
203  {
204  if (format_ == nullptr) {
205  format_ = new TableFormat ();
206  // It _is_ possible for atexit() to fail (e.g., because it ran
207  // out of memory for storing callbacks). We could throw an
208  // exception here in that case, but I think it's better just
209  // to let the minor memory leak happen.
210  static_cast<void>( atexit(freeTableFormat) );
211  }
213  format_ == nullptr, std::logic_error, "Teuchos::PerformanceMonitorBase::"
214  "format: Should never get here! format_ is nullptr.");
215 
216  return *format_;
217  }
218 
226  static RCP<T>
227  lookupCounter (const std::string& name);
228 
233  static void clearCounters ();
234 
240  static void clearCounter (const std::string& name);
241 
242  protected:
243 
245  const T& counter() const { return counter_; }
246 
248  T& counter() { return counter_; }
249 
256  bool isRecursiveCall() const { return isRecursiveCall_; }
257 
262  static std::map<std::string, RCP<T> >& counters ()
263  {
264  if (counters_ == nullptr) {
265  counters_ = new std::map<std::string, RCP<T> > ();
266  // It _is_ possible for atexit() to fail (e.g., because it ran
267  // out of memory for storing callbacks). We could throw an
268  // exception here in that case, but I think it's better just
269  // to let the minor memory leak happen.
270  static_cast<void>( atexit(freeCounters) );
271  }
273  counters_ == nullptr, std::logic_error, "Teuchos::PerformanceMonitorBase::"
274  "counters: Should never get here! counters_ is nullptr.");
275 
276  return *counters_;
277  }
278 
279  private:
281  static TableFormat* format_;
282 
284  static std::map<std::string, RCP<T> >* counters_;
285 
287  T& counter_;
288 
290  bool isRecursiveCall_;
291  };
292 
293  template<class T>
294  TableFormat*
295  PerformanceMonitorBase<T>::format_ = nullptr;
296 
297  template<class T>
298  std::map<std::string, RCP<T> >*
299  PerformanceMonitorBase<T>::counters_ = nullptr;
300 
301  template<class T>
302  RCP<T>
304  {
305  typedef std::map<std::string, RCP<T> > map_type;
306  typedef typename map_type::iterator iter_type;
307 
308  map_type& ctrs = counters ();
309  iter_type it = ctrs.find (name);
310  RCP<T> newCounter = null;
311  if (it == ctrs.end ()) {
312  newCounter = rcp (new T (name));
313 #ifdef HAVE_TEUCHOS_DEBUG
314  const bool wasNotThere = ctrs.insert (std::make_pair (name, newCounter)).second;
316  ! wasNotThere, std::logic_error,
317  "getNewCounter: insert() claims that timer \"" << name << "\" was "
318  "already there in the map, even though find() claims that it was not. "
319  "Please report this bug to the Teuchos developers.");
320 #else
321  // Use the returned iterator to optimize insertion.
322  ctrs.insert (it, std::make_pair (name, newCounter));
323 #endif // HAVE_TEUCHOS_DEBUG
324  } else {
325  newCounter = it->second;
326 #ifdef HAVE_TEUCHOS_DEBUG
328  it->second.is_null (), std::logic_error,
329  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
330  "but looking it up by name resulted in a null timer. "
331  "Please report this bug to the Teuchos developers.");
333  name != it->second->name (), std::logic_error,
334  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
335  "but looking it up by name resulted in a timer with a different name \""
336  << it->second->name () << "\". Please report this bug to the Teuchos "
337  "developers.");
338 #endif // HAVE_TEUCHOS_DEBUG
339  }
340 
341 #ifdef HAVE_TEUCHOS_DEBUG
343  newCounter.is_null (), std::logic_error,
344  "getNewCounter: At end of method, when creating timer \"" << name
345  << "\", newCounter is null. Please report this bug to the Teuchos "
346  "developers.");
347 #endif // HAVE_TEUCHOS_DEBUG
348  return newCounter;
349  }
350 
351  template<class T>
352  RCP<T>
354  {
355  typedef std::map<std::string, RCP<T> > map_type;
356  typedef typename map_type::iterator iter_type;
357 
358  map_type& ctrs = counters ();
359  iter_type it = ctrs.find (name);
360  if (it == ctrs.end ()) {
361  return null;
362  } else {
363  return it->second;
364  }
365  }
366 
367  template<class T>
368  void
369  PerformanceMonitorBase<T>::clearCounter (const std::string& name)
370  {
371  counters ().erase (name);
372  }
373 
374  template<class T>
375  void
377  {
378  counters ().clear ();
379  }
380 
381 } // namespace Teuchos
382 
383 #endif // TEUCHOS_PERFORMANCEMONITORBASE_H
static void clearCounters()
&quot;Forget&quot; about all counters created with getNewCounter().
static void clearCounter(const std::string &name)
&quot;Forget&quot; about any counters with the given name.
const T & counter() const
Constant access to the instance&#39;s counter reference.
static RCP< T > getNewCounter(const std::string &name)
Create a new counter with the specified name and add it to a global set of counters of this type...
static RCP< T > lookupCounter(const std::string &name)
Return the first counter with the given name, or null if none.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
T & counter()
Nonconstant access to the instance&#39;s counter reference.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
PerformanceMonitorBase(T &counter_in, bool reset=false)
Construct with a counter.
virtual ~PerformanceMonitorBase()=default
Destructor.
Provides utilities for formatting tabular output.
void mergeCounterNames(const Comm< int > &comm, const Array< std::string > &localNames, Array< std::string > &globalNames, const ECounterSetOp setOp)
Merge counter names over all processors.
void unsortedMergePair(const Array< std::string > &localNames, Array< std::string > &globalNames, const ECounterSetOp setOp)
static std::map< std::string, RCP< T > > & counters()
Array of all counters that were created with getNewCounter() on the calling (MPI) process...
Templated array class derived from the STL std::vector.
bool isRecursiveCall() const
Whether we are currently in a recursive call of the counter.
Smart reference counting pointer class for automatic garbage collection.
ECounterSetOp
Set operation type for mergeCounterNames() to perform.
Common capabilities for collecting and reporting performance data across processors.
Reference-counted pointer class and non-member templated function implementations.
PerformanceMonitorBase()=delete
Default constructor is deleted, since it would be unsafe.
static TableFormat & format()
Table format that will be used to print a summary of timer results.
Encapsulation of formatting specifications for writing data in a clean tabular form.
bool is_null() const
Returns true if the underlying pointer is null.