[MOAB-dev] commit/MOAB: tautges: Adding a CpuTimer class, originally implemented in WriteHDF5.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Sep 11 17:19:42 CDT 2013


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/fee72829b71b/
Changeset:   fee72829b71b
Branch:      master
User:        tautges
Date:        2013-09-12 00:19:36
Summary:     Adding a CpuTimer class, originally implemented in WriteHDF5.

Fixing the Makefile.am, which I'd forgotten to change when I moved FileOptions.hpp.

Passes serial & parallel tests.

Affected #:  3 files

diff --git a/src/Makefile.am b/src/Makefile.am
index 7cebb7c..6c4b890 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -57,7 +57,6 @@ libMOAB_la_SOURCES = \
   Factory.cpp \
   FBEngine.cpp \
   FileOptions.cpp \
-  FileOptions.hpp \
   GeomUtil.cpp \
   GeomTopoTool.cpp \
   HigherOrderFactory.cpp \
@@ -152,6 +151,7 @@ nobase_libMOAB_la_include_HEADERS = \
   moab/CartVect.hpp \
   moab/Compiler.hpp \
   moab/Core.hpp \
+  moab/CpuTimer.hpp \
   moab/DualTool.hpp \
   moab/Error.hpp \
   moab/GeomTopoTool.hpp \
@@ -160,6 +160,7 @@ nobase_libMOAB_la_include_HEADERS = \
   moab/EntityType.hpp \
   moab/EntityHandle.hpp \
   moab/FBEngine.hpp \
+  moab/FileOptions.hpp \
   moab/Forward.hpp \
   moab/GeomUtil.hpp \
   moab/Interface.hpp \

diff --git a/src/io/WriteHDF5.cpp b/src/io/WriteHDF5.cpp
index f48399a..337cb96 100644
--- a/src/io/WriteHDF5.cpp
+++ b/src/io/WriteHDF5.cpp
@@ -50,15 +50,10 @@
 #include "moab/CN.hpp"
 #include "moab/FileOptions.hpp"
 #include "moab/Version.h"
+#include "moab/CpuTimer.hpp"
 #include "IODebugTrack.hpp"
 #include "mhdf.h"
 
-#ifdef USE_MPI
-#define RUNTIME MPI_Wtime()
-#else
-#define RUNTIME (clock()/(double)CLOCKS_PER_SEC)
-#endif
-
 /* Access HDF5 file handle for debugging
 #include <H5Fpublic.h>
 struct file { uint32_t magic; hid_t handle; };
@@ -100,15 +95,6 @@ struct file { uint32_t magic; hid_t handle; };
 
 namespace moab {
 
-class CpuTimer {
-private:
-  double atBirth, atLast;
-public:
-  CpuTimer() : atBirth(RUNTIME), atLast(atBirth) {}
-  double since_birth() { return (atLast = RUNTIME) - atBirth; };
-  double elapsed() { double tmp = atLast; return (atLast = RUNTIME) - tmp; }
-};
-
 template <typename T> inline 
 void VALGRIND_MAKE_VEC_UNDEFINED( std::vector<T>& v ) {
     VALGRIND_MAKE_MEM_UNDEFINED( &v[0], v.size() * sizeof(T) );
@@ -601,7 +587,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
   topState.end(result);
   CHK_MB_ERR_0(result);
   
-  times[GATHER_TIME] = timer.elapsed();
+  times[GATHER_TIME] = timer.time_elapsed();
   
   //if (nodeSet.range.size() == 0)
   //  return error(MB_ENTITY_NOT_FOUND);
@@ -650,7 +636,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
   if (MB_SUCCESS != result)
     return error(result);
 
-  times[CREATE_TIME] = timer.elapsed();
+  times[CREATE_TIME] = timer.time_elapsed();
 
   dbgOut.tprint(1,"Writing Nodes.\n");
     // Write node coordinates
@@ -662,7 +648,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
       return error(result);
   }
 
-  times[COORD_TIME] = timer.elapsed();
+  times[COORD_TIME] = timer.time_elapsed();
 
   dbgOut.tprint(1,"Writing connectivity.\n");
   
@@ -674,7 +660,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
     if (MB_SUCCESS != result)
       return error(result);
   }
-  times[CONN_TIME] = timer.elapsed();
+  times[CONN_TIME] = timer.time_elapsed();
 
   dbgOut.tprint(1,"Writing sets.\n");
   
@@ -684,7 +670,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
     return error(result);
   debug_barrier();
   
-  times[SET_TIME] = timer.elapsed();
+  times[SET_TIME] = timer.time_elapsed();
   dbgOut.tprint(1,"Writing adjacencies.\n");
   
     // Write adjacencies
@@ -701,7 +687,7 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
     if (MB_SUCCESS != result)
       return error(result);
   }
-  times[ADJ_TIME] = timer.elapsed();
+  times[ADJ_TIME] = timer.time_elapsed();
 
   dbgOut.tprint(1,"Writing tags.\n");
   
@@ -716,9 +702,9 @@ ErrorCode WriteHDF5::write_file_impl( const char* filename,
     if (MB_SUCCESS != result)
       return error(result);
   }
-  times[TAG_TIME] = timer.elapsed();
+  times[TAG_TIME] = timer.time_elapsed();
   
-  times[TOTAL_TIME] = timer.since_birth();
+  times[TOTAL_TIME] = timer.time_since_birth();
 
   if (cputime) {
     print_times( times );
@@ -1374,7 +1360,7 @@ ErrorCode WriteHDF5::write_sets( double* times )
     mhdf_closeData( filePtr, table, &status );
     CHK_MHDF_ERR_0(status);
    
-    times[SET_PARENT] = timer.elapsed();
+    times[SET_PARENT] = timer.time_elapsed();
     track.all_reduce();
   }
   
@@ -1393,7 +1379,7 @@ ErrorCode WriteHDF5::write_sets( double* times )
     mhdf_closeData( filePtr, table, &status );
     CHK_MHDF_ERR_0(status);
    
-    times[SET_CHILD] = timer.elapsed();
+    times[SET_CHILD] = timer.time_elapsed();
     track.all_reduce();
   }
   
@@ -1415,7 +1401,7 @@ ErrorCode WriteHDF5::write_sets( double* times )
     mhdf_closeData( filePtr, table, &status );
     CHK_MHDF_ERR_0(status);
    
-    times[SET_CONTENT] = timer.elapsed();
+    times[SET_CONTENT] = timer.time_elapsed();
     track.all_reduce();
   }
   assert( ranged_sets.size() + null_stripped_sets.size() == set_sizes.size() );
@@ -1544,7 +1530,7 @@ ErrorCode WriteHDF5::write_sets( double* times )
   mhdf_closeData( filePtr, table, &status );
   CHK_MHDF_ERR_0(status);
 
-  times[SET_META] = timer.elapsed();
+  times[SET_META] = timer.time_elapsed();
   track_meta.all_reduce();
 
   return MB_SUCCESS;
@@ -1881,7 +1867,7 @@ ErrorCode WriteHDF5::write_tag( const TagDesc& tag_data,
   if (array_len == MB_VARIABLE_LENGTH && tag_data.write_sparse) {
     dbgOut.printf( 2, "Writing sparse data for var-len tag: \"%s\"\n", name.c_str() );
     rval = write_var_len_tag( tag_data, name, moab_type, hdf5_type, elem_size );
-    times[VARLEN_TAG_TIME] += timer.elapsed();
+    times[VARLEN_TAG_TIME] += timer.time_elapsed();
   }
   else {
     int data_len = elem_size;
@@ -1890,7 +1876,7 @@ ErrorCode WriteHDF5::write_tag( const TagDesc& tag_data,
     if (tag_data.write_sparse) {
       dbgOut.printf( 2, "Writing sparse data for tag: \"%s\"\n", name.c_str() );
       rval = write_sparse_tag( tag_data, name, moab_type, hdf5_type, data_len );
-      times[SPARSE_TAG_TIME] += timer.elapsed();
+      times[SPARSE_TAG_TIME] += timer.time_elapsed();
     }
     for (size_t i = 0; MB_SUCCESS == rval && i < tag_data.dense_list.size(); ++i) {
       const ExportSet* set = find( tag_data.dense_list[i] );
@@ -1901,7 +1887,7 @@ ErrorCode WriteHDF5::write_tag( const TagDesc& tag_data,
       rval = write_dense_tag( tag_data, *set, name, moab_type, hdf5_type, data_len );
       subState.end(rval);
     }
-    times[DENSE_TAG_TIME] += timer.elapsed();
+    times[DENSE_TAG_TIME] += timer.time_elapsed();
   }
  
   H5Tclose( hdf5_type );

diff --git a/src/moab/CpuTimer.hpp b/src/moab/CpuTimer.hpp
new file mode 100644
index 0000000..cdda903
--- /dev/null
+++ b/src/moab/CpuTimer.hpp
@@ -0,0 +1,63 @@
+#ifndef CPUTIMER_HPP
+#define CPUTIMER_HPP
+
+#ifdef USE_MPI
+#  include "moab_mpi.h"
+#else
+#  include <sys/resource.h>
+#endif
+
+namespace moab 
+{
+    
+class CpuTimer {
+private:
+  double tAtBirth, tAtLast;
+  double mAtBirth, mAtLast;
+  long rssAtBirth, rssAtLast;
+
+  double runtime();
+  long runmem();
+  
+public:
+  CpuTimer() : tAtBirth(runtime()), tAtLast(tAtBirth) {}
+  double time_since_birth() { return (tAtLast = runtime()) - tAtBirth; };
+  double time_elapsed() { double tmp = tAtLast; return (tAtLast = runtime()) - tmp; }
+  long mem_since_birth() {return (mAtLast=runmem()) - mAtBirth;}
+  long mem_elapsed() {long tmp = mAtLast; return (mAtLast=runmem()) - tmp;}
+};
+
+    inline double CpuTimer::runtime() 
+    {
+#if defined(_MSC_VER) || defined(__MINGW32__)
+      return (double)clock() / CLOCKS_PER_SEC;
+#elif defined(USE_MPI)
+      return MPI_Wtime();
+#else      
+      struct rusage r_usage;
+      getrusage(RUSAGE_SELF, &r_usage);
+      double utime = (double)r_usage.ru_utime.tv_sec +
+          ((double)r_usage.ru_utime.tv_usec/1.e6);
+      double stime = (double)r_usage.ru_stime.tv_sec +
+          ((double)r_usage.ru_stime.tv_usec/1.e6);
+      return utime + stime;
+#endif
+    }
+
+    inline long CpuTimer::runmem() 
+    {
+#if defined(_MSC_VER) || defined(__MINGW32__)
+      return 0;
+#elif defined(USE_MPI)
+      return 0;
+#else
+      struct rusage r_usage;
+      getrusage(RUSAGE_SELF, &r_usage);
+      mAtLast = r_usage.ru_maxrss;
+      return mAtLast;
+#endif
+    }
+
+}
+
+#endif

Repository URL: https://bitbucket.org/fathomteam/moab/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the moab-dev mailing list