Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_CTimeMonitor.cpp
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 #include "Teuchos_CTimeMonitor.h"
11 #include "Teuchos_TimeMonitor.hpp"
12 #include "Teuchos_implicit_cast.hpp"
13 #include "Teuchos_StandardCatchMacros.hpp"
14 #include "Teuchos_VerboseObject.hpp"
15 #include "Teuchos_CompilerCodeTweakMacros.hpp"
16 
17 
18 namespace {
19 
20 
21 typedef Teuchos::Array< Teuchos::RCP<Teuchos::Time> > TimerArray_t;
22 TimerArray_t timerArray;
23 
24 
25 } // namespace
26 
27 
28 int Teuchos_startTimer( char timerName[], int timerID )
29 {
31  bool success = true;
32  try {
33  if( timerID < 0 ) {
34  // The timer does not exist so create it!
35  timerArray.push_back(Teuchos::TimeMonitor::getNewCounter(timerName));
36  timerArray.back()->start();
37  return timerArray.size()-1;
38  }
39  // Else, the timer already exists so return it
41  timerID >= implicit_cast<int>(timerArray.size()), std::logic_error,
42  "Teuchos_startTimer(...): Error, timerID="<<timerID
43  <<" is >= timerArray.size()="<<timerArray.size()
44  <<" for timerName=\""<<timerName<<"\"!"
45  );
46  Teuchos::RCP<Teuchos::Time> timer = timerArray[timerID];
48  timer->isRunning(), std::logic_error,
49  "Teuchos_startTimer(...): Error, timerID="<<timerID
50  <<", timerName=\""<<timerName<<"\" is already running!"
51  );
52  timer->start();
53  }
56  if (!success) {
57  return -1;
58  }
59  return timerID;
60 }
61 
62 
63 void Teuchos_stopTimer( int timerID )
64 {
66  bool success = true;
67  try {
69  timerID < 0 || timerID >= implicit_cast<int>(timerArray.size()),
70  std::logic_error,
71  "Teuchos_stopTimer(...): Error, timerID="<<timerID<<" is invalid!"
72  );
73  Teuchos::RCP<Teuchos::Time> timer = timerArray[timerID];
74  timer->stop();
75  // Increment the number of times the timer has been used (start to stop).
76  timer->incrementNumCalls();
77  }
80  if (!success) {} // Avoid warnings
81 }
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...
#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()).
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.
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
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...