Teuchos_Time.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // Kris
00030 // 07.08.03 -- Move into Teuchos package/namespace
00031 
00032 #include "Teuchos_Time.hpp"
00033 
00034 using namespace Teuchos;
00035 
00036 //=============================================================================
00037 Time::Time(const string& name, bool start) 
00038   : startTime_(0), totalTime_(0), isRunning_(false), name_(name)
00039 {
00040   if(start) this->start();
00041 }
00042 
00043 void Time::start(bool reset)
00044 {
00045   isRunning_ = true;
00046   startTime_ = wallTime();
00047   if(reset) totalTime_ = 0;
00048 }
00049 
00050 double Time::stop()
00051 {
00052   totalTime_ += ( wallTime() - startTime_ );
00053   isRunning_ = false;
00054   startTime_ = 0;
00055   return totalTime_;
00056 }
00057 
00058 
00059 
00060 //=============================================================================
00061 double Time::wallTime() 
00062 {
00063   /* KL: warning: this code is probably not portable! */
00064   /* HT: have added some preprocessing to address problem compilers */
00065   /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
00066 #ifdef HAVE_MPI
00067 
00068   int mpiInitialized;
00069   MPI_Initialized(&mpiInitialized);
00070 
00071   if( mpiInitialized ) {
00072 
00073     return(MPI_Wtime());
00074 
00075   }
00076   else {
00077 
00078     clock_t start;
00079 
00080     start = clock();
00081     return( (double)( start ) / CLOCKS_PER_SEC );
00082 
00083   }
00084 
00085 #elif ICL
00086 
00087     clock_t start;
00088 
00089     start = clock();
00090     return( (double)( start ) / CLOCKS_PER_SEC );
00091 
00092 #else
00093 
00094 #  ifndef MINGW
00095   struct timeval tp;
00096   static long start = 0, startu;
00097   if (!start)
00098   {
00099     gettimeofday(&tp, NULL);
00100     start = tp.tv_sec;
00101     startu = tp.tv_usec;
00102     return(0.0);
00103   }
00104   gettimeofday(&tp, NULL);
00105   return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
00106 #  else
00107   return( (double) clock() / CLOCKS_PER_SEC );
00108 #  endif
00109 
00110 #endif
00111 
00112 }

Generated on Thu Sep 18 12:41:18 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1