Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_PerformanceMonitorBase.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_PERFORMANCEMONITORBASE_H
43 #define TEUCHOS_PERFORMANCEMONITORBASE_H
44 
50 #include "Teuchos_ConfigDefs.hpp"
51 #include "Teuchos_Array.hpp"
52 #include "Teuchos_Comm.hpp"
53 #include "Teuchos_RCP.hpp"
54 #include "Teuchos_TableFormat.hpp"
55 #include <cstdlib> // atexit
56 
57 namespace Teuchos
58 {
67 
92  void
93  mergeCounterNames (const Comm<int>& comm,
94  const Array<std::string>& localNames,
95  Array<std::string>& globalNames,
96  const ECounterSetOp setOp);
97 
141  template <class T>
143  {
144  public:
146  PerformanceMonitorBase(T& counter_in, bool reset=false)
147  : counter_(counter_in), isRecursiveCall_(counter_.isRunning())
148  {
149  (void) reset; // get rid of "unused parameter" warning
150  counter_.incrementNumCalls ();
151  }
152 
158 
173  static RCP<T> getNewCounter (const std::string& name);
174 
175  private:
185  static void freeTableFormat () {
186  if (format_ != NULL) {
187  delete format_;
188  format_ = NULL;
189  }
190  }
191 
201  static void freeCounters () {
202  if (counters_ != NULL) {
203  delete counters_;
204  counters_ = NULL;
205  }
206  }
207 
208  public:
217  static TableFormat& format ()
218  {
219  if (format_ == NULL) {
220  format_ = new TableFormat ();
221  // It _is_ possible for atexit() to fail (e.g., because it ran
222  // out of memory for storing callbacks). We could throw an
223  // exception here in that case, but I think it's better just
224  // to let the minor memory leak happen.
225  (void) atexit (freeTableFormat);
226  }
228  format_ == NULL, std::logic_error, "Teuchos::PerformanceMonitorBase::"
229  "format: Should never get here! format_ is NULL.");
230 
231  return *format_;
232  }
233 
241  static RCP<T>
242  lookupCounter (const std::string& name);
243 
248  static void clearCounters ();
249 
259  static TEUCHOS_DEPRECATED void clearTimers ();
260 
266  static void clearCounter (const std::string& name);
267 
278  static TEUCHOS_DEPRECATED void clearTimer (const std::string& name);
279 
280  protected:
281 
283  const T& counter() const { return counter_; }
284 
286  T& counter() { return counter_; }
287 
294  bool isRecursiveCall() const { return isRecursiveCall_; }
295 
300  static std::map<std::string, RCP<T> >& counters ()
301  {
302  if (counters_ == NULL) {
303  counters_ = new std::map<std::string, RCP<T> > ();
304  // It _is_ possible for atexit() to fail (e.g., because it ran
305  // out of memory for storing callbacks). We could throw an
306  // exception here in that case, but I think it's better just
307  // to let the minor memory leak happen.
308  (void) atexit (freeCounters);
309  }
311  counters_ == NULL, std::logic_error, "Teuchos::PerformanceMonitorBase::"
312  "counters: Should never get here! counters_ is NULL.");
313 
314  return *counters_;
315  }
316 
317  private:
320 
322  static std::map<std::string, RCP<T> >* counters_;
323 
326 
329  };
330 
331  template<class T>
332  TableFormat*
334 
335  template<class T>
336  std::map<std::string, RCP<T> >*
338 
339  template<class T>
340  RCP<T>
342  {
343  typedef std::map<std::string, RCP<T> > map_type;
344  typedef typename map_type::iterator iter_type;
345 
346  map_type& ctrs = counters ();
347  iter_type it = ctrs.find (name);
348  RCP<T> newCounter = null;
349  if (it == ctrs.end ()) {
350  newCounter = rcp (new T (name));
351 #ifdef HAVE_TEUCHOS_DEBUG
352  const bool wasNotThere = ctrs.insert (std::make_pair (name, newCounter)).second;
354  ! wasNotThere, std::logic_error,
355  "getNewCounter: insert() claims that timer \"" << name << "\" was "
356  "already there in the map, even though find() claims that it was not. "
357  "Please report this bug to the Teuchos developers.");
358 #else
359  // Use the returned iterator to optimize insertion.
360  ctrs.insert (it, std::make_pair (name, newCounter));
361 #endif // HAVE_TEUCHOS_DEBUG
362  } else {
363  newCounter = it->second;
364 #ifdef HAVE_TEUCHOS_DEBUG
366  it->second.is_null (), std::logic_error,
367  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
368  "but looking it up by name resulted in a null timer. "
369  "Please report this bug to the Teuchos developers.");
371  name != it->second->name (), std::logic_error,
372  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
373  "but looking it up by name resulted in a timer with a different name \""
374  << it->second->name () << "\". Please report this bug to the Teuchos "
375  "developers.");
376 #endif // HAVE_TEUCHOS_DEBUG
377  }
378 
379 #ifdef HAVE_TEUCHOS_DEBUG
381  newCounter.is_null (), std::logic_error,
382  "getNewCounter: At end of method, when creating timer \"" << name
383  << "\", newCounter is null. Please report this bug to the Teuchos "
384  "developers.");
385 #endif // HAVE_TEUCHOS_DEBUG
386  return newCounter;
387  }
388 
389  template<class T>
390  RCP<T>
392  {
393  typedef std::map<std::string, RCP<T> > map_type;
394  typedef typename map_type::iterator iter_type;
395 
396  map_type& ctrs = counters ();
397  iter_type it = ctrs.find (name);
398  if (it == ctrs.end ()) {
399  return null;
400  } else {
401  return it->second;
402  }
403  }
404 
405  template<class T>
406  void
407  PerformanceMonitorBase<T>::clearCounter (const std::string& name)
408  {
409  counters ().erase (name);
410  }
411 
412  template<class T>
413  void
414  PerformanceMonitorBase<T>::clearTimer (const std::string& name)
415  {
416  clearCounter (name);
417  }
418 
419  template<class T>
420  void
422  {
423  clearCounters ();
424  }
425 
426  template<class T>
427  void
429  {
430  counters ().clear ();
431  }
432 
433 } // namespace Teuchos
434 
435 #endif // TEUCHOS_PERFORMANCEMONITORBASE_H
static void clearCounters()
"Forget" about all counters created with getNewCounter().
static void clearCounter(const std::string &name)
"Forget" about any counters with the given name.
static TEUCHOS_DEPRECATED void clearTimers()
"Forget" about all counters created with getNewCounter().
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.
bool isRecursiveCall() const
Whether we are currently in a recursive call of the counter.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
T & counter()
Nonconstant access to the instance&#39;s counter reference.
bool is_null() const
Returns true if the underlying pointer is null.
T & counter_
Reference to the counter being wrapped.
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.
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.
bool isRecursiveCall_
Whether we are currently in a recursive call of the counter.
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.
static TEUCHOS_DEPRECATED void clearTimer(const std::string &name)
"Forget" about any counters with the given name.
static std::map< std::string, RCP< T > > * counters_
Singleton object returned by counters().
static void freeTableFormat()
Free the singleton returned by format().
Smart reference counting pointer class for automatic garbage collection.
const T & counter() const
Constant access to the instance&#39;s counter reference.
ECounterSetOp
Set operation type for mergeCounterNames() to perform.
static TableFormat * format_
Singleton object returned by format().
Common capabilities for collecting and reporting performance data across processors.
Reference-counted pointer class and non-member templated function implementations.
static void freeCounters()
Free the singleton returned by counters().
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.