Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
comm/example/TimeMonitor/cxx_main.cpp
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 #include "Teuchos_TimeMonitor.hpp"
11 #include "Teuchos_Version.hpp"
12 
13 #ifdef HAVE_MPI
14 #include <mpi.h>
15 #endif
16 
17 using namespace Teuchos;
18 
19 // Global Timers
22 
23 // Quadratic function declaration.
24 double quadFunc( double x );
25 
26 // Factorial function declaration.
27 double factFunc( int x );
28 
29 int main(int argc, char* argv[])
30 {
31  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
32 
33  int i;
34  double x;
35 
36 #ifdef HAVE_MPI
37  MPI_Init(&argc, &argv);
38 #endif
39 
40  // Apply the quadratic function.
41  for( i=-100; i<100; i++ ) {
42  x = quadFunc( (double) i );
43  (void)x; // Not used!
44  }
45 
46  // Apply the factorial function.
47  for( i=0; i<100; i++ ) {
48  x = factFunc( i );
49  (void)x; // Not used!
50  }
51 
52  // Get a summary from the time monitor.
54 
55 #ifdef HAVE_MPI
56  MPI_Finalize();
57 #endif
58 
59  return 0;
60 }
61 
62 /* Evaluate a quadratic function at point x */
63 double quadFunc( double x )
64 {
65  // Construct a local time monitor, this starts the CompTime timer and will stop when leaving scope.
66  Teuchos::TimeMonitor LocalTimer(*CompTime);
67 
68  // Evaluate the quadratic function.
69  return ( x*x - 1.0 );
70 }
71 
72 /* Compute the factorial of x */
73 double factFunc( int x )
74 {
75  // Construct a local time monitor, this starts the FactTime timer and will stop when leaving scope.
76  Teuchos::TimeMonitor LocalTimer(*FactTime);
77 
78  // Special returns for specific cases.
79  if( x == 0 ) return 0.0;
80  if( x == 1 ) return 1.0;
81 
82  // Evaluate the factorial function.
83  return ( (double) x * factFunc(x-1) );
84 }
double quadFunc(double x)
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...
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
Print summary statistics for all timers on the given communicator.
RCP< Time > FactTime
std::string Teuchos_Version()
int main(int argc, char *argv[])
double factFunc(int x)
Scope guard for Teuchos::Time, with MPI collective timer reporting.
Smart reference counting pointer class for automatic garbage collection.
RCP< Time > CompTime
Scope guard for Time, that can compute MPI collective timer statistics.