[mlpack-svn] r10106 - in mlpack/trunk/src/mlpack: . core/io core/utilities

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Nov 1 12:19:45 EDT 2011


Author: mamidon
Date: 2011-11-01 12:19:45 -0400 (Tue, 01 Nov 2011)
New Revision: 10106

Added:
   mlpack/trunk/src/mlpack/core/utilities/timers.cpp
   mlpack/trunk/src/mlpack/core/utilities/timers.hpp
Modified:
   mlpack/trunk/src/mlpack/core.h
   mlpack/trunk/src/mlpack/core/io/cli.cpp
   mlpack/trunk/src/mlpack/core/io/cli.hpp
   mlpack/trunk/src/mlpack/core/utilities/CMakeLists.txt
Log:
Split timer functionality from CLI to Timers.


Modified: mlpack/trunk/src/mlpack/core/io/cli.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/io/cli.cpp	2011-11-01 15:55:02 UTC (rev 10105)
+++ mlpack/trunk/src/mlpack/core/io/cli.cpp	2011-11-01 16:19:45 UTC (rev 10106)
@@ -1,5 +1,6 @@
 #include "cli.hpp"
 #include "log.hpp"
+#include "../utilities/timers.hpp"
 
 #include <list>
 #include <boost/program_options.hpp>
@@ -61,7 +62,7 @@
 
 CLI::~CLI() {
   // Terminate the program timer.
-  StopTimer("total_time");
+  Timers::StopTimer("total_time");
 
   // Did the user ask for verbose output?  If so we need to print everything.
   // But only if the user did not ask for help or info.
@@ -70,7 +71,10 @@
     hierarchy.PrintLeaves();
 
     Log::Info << "Program timers:" << std::endl;
-    hierarchy.PrintTimers();
+    std::map<std::string, timeval> times = Timers::GetAllTimers();
+    std::map<std::string, timeval>::iterator iter;
+    for(iter = times.begin(); iter != times.end(); iter++)
+      Timers::PrintTimer(iter->first.c_str());
   }
 
   // Notify the user if we are debugging, but only if we actually parsed the
@@ -291,7 +295,7 @@
   DefaultMessages();
   RequiredOptions();
 
-  StartTimer("total_time");
+  Timers::StartTimer("total_time");
 }
 
 /*
@@ -316,7 +320,7 @@
   DefaultMessages();
   RequiredOptions();
 
-  StartTimer("total_time");
+  Timers::StartTimer("total_time");
 }
 
 /*
@@ -444,79 +448,8 @@
   return std::string("");
 }
 
-/*
- * Initializes a timer, available like a normal value specified on
- * the command line.  Timers are of type timval
- *
- * @param timerName The name of the timer in question.
- */
-void CLI::StartTimer(const char* timerName) {
-  // Don't want to actually document the timer, the user can do that if he wants
-  timeval tmp;
 
-  tmp.tv_sec = 0;
-  tmp.tv_usec = 0;
 
-  // Also add it to the hierarchy for printing at the end of execution.
-  // We don't have any documentation, so omit it.  Since program execution
-  // already started, a user couldn't get to the documentation anyway.
-  std::string name(timerName);
-  std::string tname = TYPENAME(timeval);
-  CLI::GetSingleton().AddToHierarchy(name, tname);
-
-#ifndef _WIN32
-  gettimeofday(&tmp, NULL);
-#else
-  FileTimeToTimeVal(&tmp);
-#endif
-
-
-  GetParam<timeval>(timerName) = tmp;
-}
-
-/*
- * Halts the timer, and replaces it's value with
- * the delta time from it's start
- *
- * @param timerName The name of the timer in question.
- */
-void CLI::StopTimer(const char* timerName) {
-  timeval delta, b, &a = GetParam<timeval>(timerName);
-
-#ifndef _WIN32
-  gettimeofday(&b, NULL);
-#else
-  FileTimeToTimeVal(&b);
-#endif
-  //Calculate the delta time
-  timersub(&b, &a, &delta);
-  a = delta;
-}
-
-#ifdef _WIN32
-void CLI::FileTimeToTimeVal(timeval* tv) {
-  FILETIME ftime;
-  uint64_t ptime = 0;
-
-  //Acquire the filetime
-  GetSystemTimeAsFileTime(&ftime);
-
-
-  //Now convert FILETIME to timeval
-  //FILETIME records time in 100 nsec intervals gettimeofday in microsecs
-  //FILETIME starts it's epoch January 1 1601; not January 1 1970
-  ptime |= ftime.dwHighDateTime;
-  ptime = ptime << 32;
-  ptime |= ftime.dwLowDateTime;
-  ptime /= 10;  //Convert from 100 nsec INTERVALS to microseconds
-  ptime -= DELTA_EPOC_IN_MICROSECONDS; //Subtract difference from 1970 & 1601
-
-  //Convert from microseconds to seconds
-  tv.tv_sec = (long)(ptime/1000000UL);
-  tv.tv_usec = (long)(ptime%1000000UL);
-}
-#endif //Win32
-
 // Add help parameter.
 PARAM_FLAG("help", "Default help info.", "");
 PARAM_STRING("info", "Get help on a specific module or option.", "", "");

Modified: mlpack/trunk/src/mlpack/core/io/cli.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/io/cli.hpp	2011-11-01 15:55:02 UTC (rev 10105)
+++ mlpack/trunk/src/mlpack/core/io/cli.hpp	2011-11-01 16:19:45 UTC (rev 10106)
@@ -670,22 +670,6 @@
   static std::string SanitizeString(const char* str);
 
   /**
-   * Initializes a timer, available like a normal value specified on
-   * the command line.  Timers are of type timeval.
-   *
-   * @param timerName The name of the timer in question.
-   */
-  static void StartTimer(const char* timerName);
-
-  /**
-   * Halts the timer, and replaces its value with the amount of time elapsed
-   * since the timer was started.
-   *
-   * @param timerName The name of the timer in question.
-   */
-  static void StopTimer(const char* timerName);
-
-  /**
    * Parses the values given on the command line, overriding any default values.
    */
   static void UpdateGmap();

Modified: mlpack/trunk/src/mlpack/core/utilities/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/utilities/CMakeLists.txt	2011-11-01 15:55:02 UTC (rev 10105)
+++ mlpack/trunk/src/mlpack/core/utilities/CMakeLists.txt	2011-11-01 16:19:45 UTC (rev 10106)
@@ -7,6 +7,8 @@
     save_restore_utility.hpp
     save_restore_utility.hpp
     save_restore_utility.cpp
+    timers.hpp
+    timers.cpp
 )
 
 # add directory name to sources

Added: mlpack/trunk/src/mlpack/core/utilities/timers.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/utilities/timers.cpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/utilities/timers.cpp	2011-11-01 16:19:45 UTC (rev 10106)
@@ -0,0 +1,112 @@
+#include "timers.hpp"
+#include "../io/cli.hpp"
+#include "../io/log.hpp"
+
+#include <map>
+#include <string>
+
+using namespace mlpack;
+
+std::map<std::string, timeval> Timers::timers;
+
+std::map<std::string, timeval> Timers::GetAllTimers() {
+  return timers;
+}
+
+timeval Timers::GetTimer(const char* timerName) {
+  std::string name(timerName);
+  return timers[name];
+}
+
+void Timers::PrintTimer(const char* timerName) {
+  std::string name=timerName;
+  timeval&t=timers[name];
+  Log::Info<<t.tv_sec<<"."<<std::setw(6)<<std::setfill('0')
+    <<t.tv_usec<<"s";
+
+  //Alsooutputconvenientday/hr/min/sec.
+
+  int days=t.tv_sec/86400;//Integerdivisionroundsdown.
+  int hours=(t.tv_sec%86400)/3600;
+  int minutes=(t.tv_sec%3600)/60;
+  int seconds=(t.tv_sec%60);
+  //Nooutputifitdidn'teventakeaminute.
+  if(!(days==0&&hours==0&&minutes==0)){
+    bool output=false;//Denotesifwehaveoutputanythingyet.
+    Log::Info<<"(";
+    //Onlyoutputunitsiftheyhavenonzerovalues(yes,abittedious).
+    if(days>0){
+      Log::Info<<days<<"days";
+      output=true;
+    }
+    if(hours>0){
+      if(output)
+        Log::Info<<",";
+      Log::Info<<hours<<"hrs";
+      output=true;
+    }
+    if(minutes>0){
+      if(output)
+        Log::Info<<",";
+      Log::Info<<minutes<<"mins";
+      output=true;
+    }
+    if(seconds>0){
+      if(output)
+        Log::Info<<",";
+      Log::Info<<seconds<<"."<<std::setw(1)<<(t.tv_usec/100000)<<
+        "secs";
+      output=true;
+    }
+    Log::Info<<")";
+  }  
+  Log::Info << std::endl;
+}
+
+void Timers::StartTimer(const char* timerName) {
+  //Don't want to actually document the timer
+  std::string name(timerName);
+  timeval tmp;
+
+  tmp.tv_sec = 0;
+  tmp.tv_usec = 0;
+
+#ifndef _WIN32
+  gettimeofday(&tmp, NULL);
+#else
+  FileTimeToTimeVal(&tmp);
+#endif
+  timers[name] = tmp;
+}
+
+void Timers::StopTimer(const char* timerName) {
+  std::string name(timerName);
+  timeval delta, b, a = timers[name];
+
+#ifndef _WIN32
+  gettimeofday(&b, NULL);
+#else
+  FileTimeToTimeVal(&b);
+#endif
+  //Calculate the delta time
+  timersub(&b,&a,&delta);
+  timers[name] = delta;
+}
+
+#ifdef _WIN32
+void Timers::FileTimeToTimeVal(timeval* tv) {
+  FILETIME ftime;
+  uint64_t ptime = 0;
+  //Acquire the file time
+  GetSystemTimeAsFileTime(&ftime);
+  //Now convert FILETIME to timeval
+  ptime |= ftime.dwHighDateTime;
+  ptime = ptime << 32;
+  ptime |= ftime.dwLowDateTime;
+  ptime /= 10; 
+  ptime -= DELTA_EPOC_IN_MICROSECONDS;
+ 
+  tv.tv_sec=(long)(ptime/1000000UL);
+  tv.tv_usec = (long)(ptime%1000000UL)
+}
+#endif //WIN32

Added: mlpack/trunk/src/mlpack/core/utilities/timers.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/utilities/timers.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/utilities/timers.hpp	2011-11-01 16:19:45 UTC (rev 10106)
@@ -0,0 +1,71 @@
+#ifndef MLPACK_TIMERS_H
+#define MLPACK_TIMERS_H
+ 
+#include <map>
+#include <string>
+
+#ifndef _WIN32
+  #include <sys/time.h> //linux
+#else
+  #include <winsock.h> //timeval on windows
+  #include <windows.h> //GetSystemTimeAsFileTime on windows
+//gettimeofday has no equivalent will need to write extra code for that.
+  #if defined(_MSC_VER) || defined(_MSC_EXTENSCLINS)
+    #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
+  #else
+    #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
+  #endif
+#endif //_WIN32
+
+namespace mlpack {
+
+class Timers {
+ public:
+ /*
+  * Returns a copy of all the timers used via this interface.
+  */
+  static std::map<std::string, timeval> GetAllTimers();
+
+ /* 
+  * Returns a copy of the timer specified.
+  *
+  * @param timerName The name of the timer in question.
+  */
+  static timeval GetTimer(const char* timerName);
+
+ /*
+  * Prints the specified timer.  If it took longer than a minute to complete
+  * the timer will be displayed in days, hours, and minutes as well.
+  *
+  * @param timerName The name of the timer in question.
+  */
+  static void PrintTimer(const char* timerName);
+
+ /*
+  * Initializes a timer, available like a normal value specified on
+  * the command line.  Timers are of type timval
+  *
+  * @param timerName The name of the timer in question.
+  */
+  static void StartTimer(const char* timerName);
+
+ /*
+  * Halts the timer, and replaces it's value with
+  * the delta time from it's start
+  *
+  * @param timerName The name of the timer in question.
+  */
+  static void StopTimer(const char* timerName);
+ private:
+  static std::map<std::string, timeval> timers;
+
+  void FileTimeToTimeVal(timeval* tv);
+
+  //Don't want any instances floating around.
+  Timers();
+  ~Timers();
+};
+
+}; //namespace mlpack
+
+#endif //MLPACK_TIMERS_H

Modified: mlpack/trunk/src/mlpack/core.h
===================================================================
--- mlpack/trunk/src/mlpack/core.h	2011-11-01 15:55:02 UTC (rev 10105)
+++ mlpack/trunk/src/mlpack/core.h	2011-11-01 16:19:45 UTC (rev 10106)
@@ -93,5 +93,6 @@
 #include <mlpack/core/math/math_misc.hpp>
 #include <mlpack/core/math/range.hpp>
 #include <mlpack/core/utilities/save_restore_utility.hpp>
+#include <mlpack/core/utilities/timers.hpp>
 
 #endif




More information about the mlpack-svn mailing list