Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_CTimeMonitor.cpp
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 #include "Teuchos_CTimeMonitor.h"
43 #include "Teuchos_TimeMonitor.hpp"
48 
49 
50 namespace {
51 
52 
53 typedef Teuchos::Array< Teuchos::RCP<Teuchos::Time> > TimerArray_t;
54 TimerArray_t timerArray;
55 
56 
57 } // namespace
58 
59 
60 int Teuchos_startTimer( char timerName[], int timerID )
61 {
63  bool success = true;
64  try {
65  if( timerID < 0 ) {
66  // The timer does not exist so create it!
67  timerArray.push_back(Teuchos::TimeMonitor::getNewCounter(timerName));
68  timerArray.back()->start();
69  return timerArray.size()-1;
70  }
71  // Else, the timer already exists so return it
73  timerID >= implicit_cast<int>(timerArray.size()), std::logic_error,
74  "Teuchos_startTimer(...): Error, timerID="<<timerID
75  <<" is >= timerArray.size()="<<timerArray.size()
76  <<" for timerName=\""<<timerName<<"\"!"
77  );
78  Teuchos::RCP<Teuchos::Time> timer = timerArray[timerID];
80  timer->isRunning(), std::logic_error,
81  "Teuchos_startTimer(...): Error, timerID="<<timerID
82  <<", timerName=\""<<timerName<<"\" is already running!"
83  );
84  timer->start();
85  }
88  if (!success) {
89  return -1;
90  }
91  return timerID;
92 }
93 
94 
95 void Teuchos_stopTimer( int timerID )
96 {
98  bool success = true;
99  try {
101  timerID < 0 || timerID >= implicit_cast<int>(timerArray.size()),
102  std::logic_error,
103  "Teuchos_stopTimer(...): Error, timerID="<<timerID<<" is invalid!"
104  );
105  Teuchos::RCP<Teuchos::Time> timer = timerArray[timerID];
106  timer->stop();
107  // Increment the number of times the timer has been used (start to stop).
108  timer->incrementNumCalls();
109  }
112  if (!success) {} // Avoid warnings
113 }
static RCP< Time > 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...
void Teuchos_stopTimer(int timerID)
Stop a timer that was started with Teuchos_startTimer().
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
double stop()
Stop the timer, if the timer is enabled (see disable()).
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
TypeTo implicit_cast(const TypeFrom &t)
Perform an implicit cast of concrete types with the casted object returned by value.
int Teuchos_startTimer(char timerName[], int timerID)
Start a timer with a given name and ID.
Scope protection wrapper for Teuchos::Time, with timer reporting functionality.
Smart reference counting pointer class for automatic garbage collection.
void incrementNumCalls()
Increment the number of times this timer has been called, if the timer is enabled (see disable())...
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...