Teuchos_Time.cpp

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   if (reset) totalTime_ = 0;
00047   startTime_ = wallTime();
00048 }
00049 
00050 double Time::stop()
00051 {
00052   if (isRunning_) {
00053     totalTime_ += ( wallTime() - startTime_ );
00054     isRunning_ = false;
00055     startTime_ = 0;
00056   }
00057   return totalTime_;
00058 }
00059 
00060 
00061 
00062 //=============================================================================
00063 double Time::wallTime() 
00064 {
00065   /* KL: warning: this code is probably not portable! */
00066   /* HT: have added some preprocessing to address problem compilers */
00067   /* RAB: I modifed so that timer will work if MPI support is compiled in but not initialized */
00068 #ifdef HAVE_MPI
00069 
00070   int mpiInitialized;
00071   MPI_Initialized(&mpiInitialized);
00072 
00073   if( mpiInitialized ) {
00074 
00075     return(MPI_Wtime());
00076 
00077   }
00078   else {
00079 
00080     clock_t start;
00081 
00082     start = clock();
00083     return( (double)( start ) / CLOCKS_PER_SEC );
00084 
00085   }
00086 
00087 #elif ICL
00088 
00089     clock_t start;
00090 
00091     start = clock();
00092     return( (double)( start ) / CLOCKS_PER_SEC );
00093 
00094 #else
00095 
00096 #  ifndef MINGW
00097   struct timeval tp;
00098   static long start = 0, startu;
00099   if (!start)
00100   {
00101     gettimeofday(&tp, NULL);
00102     start = tp.tv_sec;
00103     startu = tp.tv_usec;
00104     return(0.0);
00105   }
00106   gettimeofday(&tp, NULL);
00107   return( ((double) (tp.tv_sec - start)) + (tp.tv_usec-startu)/1000000.0 );
00108 #  else
00109   return( (double) clock() / CLOCKS_PER_SEC );
00110 #  endif
00111 
00112 #endif
00113 
00114 }

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