MOAB: Mesh Oriented datABase  (version 5.3.0)
MsqTimer.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
00024     pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
00025 
00026   ***************************************************************** */
00027 #ifndef MESQUITE_TIMER_HPP
00028 #define MESQUITE_TIMER_HPP
00029 
00030 #ifdef WIN32
00031 #pragma warning( 4 : 4786 )
00032 #endif
00033 
00034 #include "Mesquite.hpp"
00035 #include "MsqDebug.hpp"
00036 
00037 #include <vector>
00038 #include <utility>
00039 #include <string>
00040 #include <iosfwd>
00041 
00042 namespace MBMesquite
00043 {
00044 class MESQUITE_EXPORT Timer
00045 {
00046   public:
00047     Timer();
00048 
00049     void reset();  // resets the timer as if it were just created
00050 
00051     double since_last_check();  //- return time in seconds since last
00052                                 //- call to since_last_check().  Note that
00053                                 //- calling since_birth() doesn't count
00054                                 //- as a check.  The first time this function
00055                                 //- is called, it returns the time since birth.
00056 
00057     double since_birth() const;  //- return time in seconds since
00058                                  //- object was created.
00059 
00060   private:
00061     double atBirth;      //- Time at birth
00062     double atLastCheck;  //- Time at last call to since_last_check()
00063 };
00064 
00065 class MESQUITE_EXPORT StopWatch
00066 {
00067   public:
00068     // Creates the stopwatch.  The stopwatch is stopped
00069     // until start() is called.
00070     StopWatch() : isRunning( false ), totalTime( 0.0 ), numStarts( 0 ) {}
00071 
00072     // Starts the stopwatch.  If it was already running,
00073     // this function does nothing.
00074     void start();
00075 
00076     // Stops the stopwatch.  If it was not already running,
00077     // this function does nothing
00078     void stop();
00079 
00080     // Stops the stopwatch and resets the total_time() to zero.
00081     void reset();
00082 
00083     // Returns the total accumulated time.  If the stopwatch
00084     // is currently running, the time between the last start()
00085     // and the current time IS included in total_time().
00086     double total_time() const;
00087 
00088     /*! \brief Returns the number of times this StopWatch has
00089       been started.*/
00090     int number_of_starts() const
00091     {
00092         return numStarts;
00093     }
00094 
00095   private:
00096     bool isRunning;
00097     double timeAtLastStart;
00098     double totalTime;
00099     int numStarts;
00100 };
00101 
00102 class StopWatchCollection
00103 {
00104   public:
00105     typedef size_t Key;
00106 
00107     // Create a new collection
00108     MESQUITE_EXPORT StopWatchCollection() {}
00109 
00110     // Add a stopwatch to the collection.  Returns a non-zero
00111     // StopWatchCollection::Key if it succeeds, zero if it fails.
00112     // If a StopWatch with the given name already exists in the
00113     // collection, the Key of the existing StopWatch is returned
00114     // if 'fail_if_exists' is false, or zero is returned if
00115     // 'fail_if_exists' is true.
00116     MESQUITE_EXPORT Key add( const std::string& name, bool fail_if_exists = true );
00117 
00118     // Gets the Key for an existing stopwatch.  If a stopwatch
00119     // with the given name does not exist, function returns zero.
00120     MESQUITE_EXPORT Key get_key( const std::string& name ) const;
00121 
00122     //! Gets the string associated with a key
00123     MESQUITE_EXPORT std::string get_string( const Key key )
00124     {
00125         return mEntries[key - 1].first;
00126     }
00127     //! Gets the string associated with a key
00128     MESQUITE_EXPORT void get_string( const Key key, std::string& new_string )
00129     {
00130         new_string = mEntries[key - 1].first;
00131     }
00132 
00133     // Remove a specific stopwatch.
00134     MESQUITE_EXPORT void remove( const Key key );
00135     MESQUITE_EXPORT void remove( const std::string& name )
00136     {
00137         remove( get_key( name ) );
00138     }
00139 
00140     // start a specific stopwatch
00141     MESQUITE_EXPORT void start( const Key key );
00142     MESQUITE_EXPORT void start( const std::string& name )
00143     {
00144         start( get_key( name ) );
00145     }
00146 
00147     // stop a specific stopwatch
00148     MESQUITE_EXPORT void stop( const Key key );
00149     MESQUITE_EXPORT void stop( const std::string& name )
00150     {
00151         stop( get_key( name ) );
00152     }
00153 
00154     // reset a specific stopwatch
00155     MESQUITE_EXPORT void reset( const Key key );
00156     MESQUITE_EXPORT void reset( const std::string& name )
00157     {
00158         reset( get_key( name ) );
00159     }
00160 
00161     // Get the total time for a specific stopwatch, zero if
00162     // the stopwatch doesn't exist.
00163     MESQUITE_EXPORT double total_time( const Key key ) const;
00164     MESQUITE_EXPORT double total_time( const std::string& name ) const
00165     {
00166         return total_time( get_key( name ) );
00167     }
00168     // Get the number of times a StopWatch was started.
00169     MESQUITE_EXPORT int number_of_starts( const Key key ) const;
00170     MESQUITE_EXPORT int number_of_starts( const std::string& name ) const
00171     {
00172         return number_of_starts( get_key( name ) );
00173     }
00174 
00175     // Gets the number of stop watches in the collection
00176     MESQUITE_EXPORT int number_of_stop_watches()
00177     {
00178         return (int)mEntries.size();
00179     }
00180 
00181     MESQUITE_EXPORT void get_keys_sorted_by_time( std::vector< Key >& sorted_keys );
00182 
00183   private:
00184     std::vector< std::pair< std::string, StopWatch > > mEntries;
00185 };
00186 
00187 MESQUITE_EXPORT std::ostream& operator<<( std::ostream&, StopWatchCollection& coll );
00188 
00189 // A stopWatchCollection available anywhere
00190 extern MBMesquite::StopWatchCollection GlobalStopWatches;
00191 
00192 MESQUITE_EXPORT inline void print_timing_diagnostics( int debugflag )
00193 {
00194     MSQ_DBGOUT( debugflag ) << GlobalStopWatches;
00195 }
00196 
00197 MESQUITE_EXPORT inline void print_timing_diagnostics( std::ostream& stream )
00198 {
00199     stream << GlobalStopWatches;
00200 }
00201 
00202 class FunctionTimer
00203 {
00204   public:
00205     inline FunctionTimer( StopWatchCollection::Key key ) : mKey( key ) {}
00206     inline void start()
00207     {
00208         GlobalStopWatches.start( mKey );
00209     }
00210     inline ~FunctionTimer()
00211     {
00212         GlobalStopWatches.stop( mKey );
00213     }
00214 
00215   private:
00216     StopWatchCollection::Key mKey;
00217     // Don't allow any of this stuff (make them private)
00218     void* operator new( size_t size );
00219     FunctionTimer( const FunctionTimer& );
00220     FunctionTimer& operator=( const FunctionTimer& );
00221 };
00222 
00223 #ifdef MSQ_USE_FUNCTION_TIMERS
00224 #define MSQ_FUNCTION_TIMER( NAME )                                    \
00225     static MBMesquite::StopWatchCollection::Key _mesquite_timer_key = \
00226         MBMesquite::GlobalStopWatches.add( NAME, false );             \
00227     FunctionTimer _mesquite_timer( _mesquite_timer_key );             \
00228     _mesquite_timer.start()
00229 #else
00230 #define MSQ_FUNCTION_TIMER( NAME )
00231 #endif
00232 
00233 }  // namespace MBMesquite
00234 
00235 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines