Teuchos - Trilinos Tools Package  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 
56 namespace Teuchos
57 {
65  enum ECounterSetOp { Intersection, Union };
66 
91  void
92  mergeCounterNames (const Comm<int>& comm,
93  const Array<std::string>& localNames,
94  Array<std::string>& globalNames,
95  const ECounterSetOp setOp);
96 
140  template <class T>
142  {
143  public:
145  PerformanceMonitorBase(T& counter_in, bool reset=false)
146  : counter_(counter_in), isRecursiveCall_(counter_.isRunning())
147  {
148  (void) reset; // get rid of "unused parameter" warning
149  counter_.incrementNumCalls ();
150  }
151 
157 
172  static RCP<T> getNewCounter (const std::string& name);
173 
175  static TableFormat& format()
176  {
177  // WARNING This is not thread safe! In particular, if multiple
178  // threads call this method for the "first time" at the same
179  // time, the RCP may be initialized incorrectly.
180  static RCP<TableFormat> rtn=rcp(new TableFormat());
181  return *rtn;
182  }
183 
191  static RCP<T>
192  lookupCounter (const std::string& name);
193 
198  static void clearCounters ();
199 
209  static TEUCHOS_DEPRECATED void clearTimers ();
210 
216  static void clearCounter (const std::string& name);
217 
228  static TEUCHOS_DEPRECATED void clearTimer (const std::string& name);
229 
230  protected:
231 
233  const T& counter() const { return counter_; }
234 
236  T& counter() { return counter_; }
237 
244  bool isRecursiveCall() const { return isRecursiveCall_; }
245 
247  static std::map<std::string, RCP<T> >& counters()
248  {
249  // Use the "Meyers Trick" to create static data safely.
250  //
251  // WARNING This is not thread safe! In particular, if multiple
252  // threads call counters() for the "first time" at the same
253  // time, the array may be initialized incorrectly.
254  static std::map<std::string, RCP<T> > theCounters;
255  static int initialized;
256  if (! initialized) {
257  theCounters.clear ();
258  initialized = 1;
259  }
260  return theCounters;
261  }
262 
263  private:
264 
266  T& counter_;
267 
269  bool isRecursiveCall_;
270  };
271 
272  template<class T>
273  RCP<T>
275  {
276  typedef std::map<std::string, RCP<T> > map_type;
277  typedef typename map_type::iterator iter_type;
278 
279  map_type& ctrs = counters ();
280  iter_type it = ctrs.find (name);
281  RCP<T> newCounter = null;
282  if (it == ctrs.end ()) {
283  newCounter = rcp (new T (name));
284 #ifdef HAVE_TEUCHOS_DEBUG
285  const bool wasNotThere = ctrs.insert (std::make_pair (name, newCounter)).second;
287  ! wasNotThere, std::logic_error,
288  "getNewCounter: insert() claims that timer \"" << name << "\" was "
289  "already there in the map, even though find() claims that it was not. "
290  "Please report this bug to the Teuchos developers.");
291 #else
292  // Use the returned iterator to optimize insertion.
293  ctrs.insert (it, std::make_pair (name, newCounter));
294 #endif // HAVE_TEUCHOS_DEBUG
295  } else {
296  newCounter = it->second;
297 #ifdef HAVE_TEUCHOS_DEBUG
299  it->second.is_null (), std::logic_error,
300  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
301  "but looking it up by name resulted in a null timer. "
302  "Please report this bug to the Teuchos developers.");
304  name != it->second->name (), std::logic_error,
305  "getNewCounter: Timer \"" << name << "\" was already there in the map, "
306  "but looking it up by name resulted in a timer with a different name \""
307  << it->second->name () << "\". Please report this bug to the Teuchos "
308  "developers.");
309 #endif // HAVE_TEUCHOS_DEBUG
310  }
311 
312 #ifdef HAVE_TEUCHOS_DEBUG
314  newCounter.is_null (), std::logic_error,
315  "getNewCounter: At end of method, when creating timer \"" << name
316  << "\", newCounter is null. Please report this bug to the Teuchos "
317  "developers.");
318 #endif // HAVE_TEUCHOS_DEBUG
319  return newCounter;
320  }
321 
322  template<class T>
323  RCP<T>
325  {
326  typedef std::map<std::string, RCP<T> > map_type;
327  typedef typename map_type::iterator iter_type;
328 
329  map_type& ctrs = counters ();
330  iter_type it = ctrs.find (name);
331  if (it == ctrs.end ()) {
332  return null;
333  } else {
334  return it->second;
335  }
336  }
337 
338  template<class T>
339  void
340  PerformanceMonitorBase<T>::clearCounter (const std::string& name)
341  {
342  counters ().erase (name);
343  }
344 
345  template<class T>
346  void
347  PerformanceMonitorBase<T>::clearTimer (const std::string& name)
348  {
349  clearCounter (name);
350  }
351 
352  template<class T>
353  void
355  {
356  clearCounters ();
357  }
358 
359  template<class T>
360  void
362  {
363  counters ().clear ();
364  }
365 
366 } // namespace Teuchos
367 
368 #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.
const T & counter() const
Constant access to the instance&#39;s counter reference.
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.
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.
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.
static std::map< std::string, RCP< T > > & counters()
The class&#39; array of all counters that were created with getNewCounter().
Templated array class derived from the STL std::vector.
Abstract interface for distributed-memory communication.
static TEUCHOS_DEPRECATED void clearTimer(const std::string &name)
"Forget" about any counters with the given name.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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.
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.