TimeMonitor/cxx_main.cpp

This is an example of how to use the Teuchos::TimeMonitor class.

00001 #include "Teuchos_TimeMonitor.hpp"
00002 #include "Teuchos_Version.hpp"
00003 
00004 #ifdef HAVE_MPI
00005 #include <mpi.h>
00006 #endif
00007 
00008 using namespace Teuchos;
00009 
00010 // Global Timers
00011 RCP<Time> CompTime = TimeMonitor::getNewTimer("Computational Time");
00012 RCP<Time> FactTime = TimeMonitor::getNewTimer("Factorial Time");
00013 
00014 // Quadratic function declaration.
00015 double quadFunc( double x );
00016 
00017 // Factorial function declaration.
00018 double factFunc( int x );
00019 
00020 int main(int argc, char* argv[])
00021 {
00022   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00023 
00024   int i;
00025   double x;
00026 
00027 #ifdef HAVE_MPI
00028   MPI_Init(&argc, &argv);
00029 #endif
00030 
00031   // Apply the quadratic function.
00032   for( i=-100; i<100; i++ ) {
00033     x = quadFunc( (double) i );
00034   }
00035 
00036   // Apply the factorial function.
00037   for( i=0; i<100; i++ ) {
00038     x = factFunc( i );
00039   }
00040 
00041   // Get a summary from the time monitor.
00042   TimeMonitor::summarize();
00043 
00044 #ifdef HAVE_MPI
00045   MPI_Finalize();
00046 #endif
00047   
00048   return 0;
00049 }
00050 
00051 /* Evaluate a quadratic function at point x */
00052 double quadFunc( double x )
00053 {
00054   // Construct a local time monitor, this starts the CompTime timer and will stop when leaving scope.
00055   Teuchos::TimeMonitor LocalTimer(*CompTime);
00056 
00057   // Evaluate the quadratic function.
00058   return ( x*x - 1.0 );
00059 }
00060 
00061 /* Compute the factorial of x */
00062 double factFunc( int x )
00063 {
00064   // Construct a local time monitor, this starts the FactTime timer and will stop when leaving scope.
00065   Teuchos::TimeMonitor LocalTimer(*FactTime);
00066 
00067   // Special returns for specific cases.
00068   if( x == 0 ) return 0.0;
00069   if( x == 1 ) return 1.0;
00070 
00071   // Evaluate the factorial function.
00072   return ( (double) x * factFunc(x-1) );
00073 }

Generated on Tue Jul 13 09:22:58 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7