From 122677352011bca504927d2f675662f2ee016d5c Mon Sep 17 00:00:00 2001 From: Harry Moffat Date: Mon, 11 Jun 2007 15:20:03 +0000 Subject: [PATCH] Wall Clock Timer: Added class that implements a simple wall clock timer --- Cantera/src/base/CMakeLists.txt | 2 +- Cantera/src/base/Makefile.in | 4 +- Cantera/src/base/clockWC.cpp | 65 +++++++++++++++++++++++ Cantera/src/base/clockWC.h | 94 +++++++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 Cantera/src/base/clockWC.cpp create mode 100644 Cantera/src/base/clockWC.h diff --git a/Cantera/src/base/CMakeLists.txt b/Cantera/src/base/CMakeLists.txt index f581c9e3b..0dcc5c2ee 100644 --- a/Cantera/src/base/CMakeLists.txt +++ b/Cantera/src/base/CMakeLists.txt @@ -1,3 +1,3 @@ SET (CTBASE_SRCS misc.cpp ct2ctml.cpp ctml.cpp - plots.cpp stringUtils.cpp xml.cpp) + plots.cpp stringUtils.cpp xml.cpp clockWC.cpp) ADD_LIBRARY(ctbase ${CTBASE_SRCS}) diff --git a/Cantera/src/base/Makefile.in b/Cantera/src/base/Makefile.in index f849666bc..2f804a444 100644 --- a/Cantera/src/base/Makefile.in +++ b/Cantera/src/base/Makefile.in @@ -26,11 +26,11 @@ PIC_FLAG=@PIC@ CXX_FLAGS = @CXXFLAGS@ $(LOCAL_DEFS) $(CXX_OPT) $(PIC_FLAG) $(DEBUG_FLAG) -BASE_OBJ = ct2ctml.o ctml.o misc.o plots.o stringUtils.o xml.o +BASE_OBJ = ct2ctml.o ctml.o misc.o plots.o stringUtils.o xml.o clockWC.o BASE_H = ct_defs.h ctexceptions.h logger.h XML_Writer.h \ ctml.h plots.h stringUtils.h xml.h config.h utilities.h \ - Array.h vec_functions.h global.h FactoryBase.h + Array.h vec_functions.h global.h FactoryBase.h clockWC.h CXX_INCLUDES = -I. @CXX_INCLUDES@ LIB = @buildlib@/libctbase.a diff --git a/Cantera/src/base/clockWC.cpp b/Cantera/src/base/clockWC.cpp new file mode 100644 index 000000000..7b7bd7936 --- /dev/null +++ b/Cantera/src/base/clockWC.cpp @@ -0,0 +1,65 @@ +/** + * @file clockWC.cpp + * Definitions for a simple class that implements an Ansi C wall clock timer + * (see \ref Cantera::clockWC). + */ +/* + * $Author$ + * $Revision$ + * $Date$ + */ +/* + * Copywrite 2004 Sandia Corporation. Under the terms of Contract + * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government + * retains certain rights in this software. + * See file License.txt for licensing information. + */ + +#include +#include "clockWC.h" +namespace Cantera { + + + clockWC::clockWC() : + last_num_ticks(clock()), + clock_rollovers(0u), + start_ticks(0), + inv_clocks_per_sec(1./(double)CLOCKS_PER_SEC), + clock_width((double)(1L<<((int)sizeof(clock_t)*8-2))*4./(double)CLOCKS_PER_SEC) + { + start_ticks = last_num_ticks; + } + + /* + * Reinitialize the tick counters within the object + */ + double clockWC::start() + { + start_ticks = last_num_ticks = clock(); + clock_rollovers = 0u; + return 0.0; + } + + /* + * Returns system cpu and wall clock time in seconds. This + * is a strictly Ansi C timer, since clock() is defined as an + * Ansi C function. On some machines clock() returns type + * unsigned long (HP) and on others (SUN) it returns type long. + * An attempt to recover the actual time for clocks which have + * rolled over is made also. However, it only works if this + * function is called fairly regularily during + * the solution procedure. + * + * clock() -> returns the time in microseconds. Division by + * the macro CLOCKS_PER_SEC recovers the time in seconds. + */ + double clockWC::secondsWC() + { + clock_t num_ticks = clock(); + if (num_ticks < last_num_ticks) clock_rollovers++; + double value = (num_ticks - start_ticks) * inv_clocks_per_sec; + if (clock_rollovers) value += clock_rollovers * clock_width; + last_num_ticks = num_ticks; + return(value); + } +} diff --git a/Cantera/src/base/clockWC.h b/Cantera/src/base/clockWC.h new file mode 100644 index 000000000..8d3ad1654 --- /dev/null +++ b/Cantera/src/base/clockWC.h @@ -0,0 +1,94 @@ +/** + * @file clockWC.h + * Declarations for a simple class that implements an Ansi C wall clock timer + * (see \ref Cantera::clockWC). + */ +/* + * $Author$ + * $Revision$ + * $Date$ + */ +/* + * Copywrite 2004 Sandia Corporation. Under the terms of Contract + * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government + * retains certain rights in this software. + * See file License.txt for licensing information. + */ + +#ifndef CT_CLOCKWC_H +#define CT_CLOCKWC_H + +#include +namespace Cantera { + + //! The class provides the wall clock timer in seconds + /*! + * This routine relies on the ANSI C routine, clock(), for + * its basic operation. Therefore, it should be fairly + * portable. + * + * The clock will rollover if the calculation is long enough. + * The wraparound time is roughly 72 minutes for a 32 bit system. + * This object senses that by seeing if the raw tick counter is + * has decreased from the last time. If it senses a wraparound has + * occurred, it increments an internal counter to account for this. + * Therefore, for long calculations, this object must be called + * at regular intervals for the seconds timer to be accurate. + * + * An example of how to use the timer is given below. timeToDoCalcs + * countains the wall clock time calculated for the operation. + * + * + * @code + * clockWC wc; + * do_hefty_calculations_atLeastgreaterThanAMillisecond(); + * double timeToDoCalcs = wc.secondsWC(); + * @endcode + * + * In general, the process to be timed must take more than a millisecond + * for this clock to enough of a significant resolution to be + * accurate. + * + * @ingroup globalUtilFuncs + * + */ + class clockWC { + public: + //! Constructor + /*! + * This also serves to initialize the ticks within the object + */ + clockWC(); + + //! Resets the internal counters and returns the wall clock time + //! in seconds + double start(); + + //! Returns the wall clock time in seconds since the last reset. + double secondsWC(); + + private: + //! Counters the value of the number of ticks from the last call. + clock_t last_num_ticks; + + //! Number of clock rollovers since the last initialization + /*! + * The clock will rollover if the calculation is long enough. + * This object senses that by seeing if the raw tick counter is + * has decreased from the last time. + */ + unsigned int clock_rollovers; + + //! Counter countaining the value of the number of ticks from + //! the first call (or the reset call). + clock_t start_ticks; + + //! internal constant containing clock ticks per second + const double inv_clocks_per_sec; + + //! internal constant containing the total number of ticks + //! per rollover. + const double clock_width; + }; +} +#endif