r111: initial cvs import
authorKevin M. Rosenberg <kevin@rosenberg.net>
Mon, 19 Jun 2000 19:12:05 +0000 (19:12 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Mon, 19 Jun 2000 19:12:05 +0000 (19:12 +0000)
include/timer.h [new file with mode: 0644]

diff --git a/include/timer.h b/include/timer.h
new file mode 100644 (file)
index 0000000..781ee94
--- /dev/null
@@ -0,0 +1,147 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/time.h>
+
+class Timer
+{
+ public:
+    Timer (void)
+       { m_timeStart = ttime(); }
+
+    virtual ~Timer (void)
+       {}
+
+    virtual double timerEnd (void)
+      {
+       m_timeEnd = ttime();
+       m_timeElapsed = m_timeEnd - m_timeStart;
+  
+       return (m_timeElapsed);
+      }
+
+    virtual void timerReport (const char* const msg) const
+      {
+         cout << msg << ": " << m_timeElapsed << " seconds" << endl;
+      }
+
+    virtual double timerEndAndReport (const char* const msg)
+      {
+       timerEnd ();
+       timerReport (msg);
+      }
+
+    double getTimeElapsed (void) const
+       { return m_timeElapsed; }
+
+ protected:
+    double m_timeStart;
+    double m_timeEnd;
+    double m_timeElapsed;
+
+    double ttime(void) const
+       {
+           struct timeval now;
+           if (gettimeofday (&now, NULL))
+               return 0;
+           
+           return (now.tv_sec + static_cast<double>(now.tv_usec) / 1000000.);
+       }
+};
+
+
+#ifdef HAVE_MPI
+
+#include "mpi++.h"
+
+class TimerMPI : public Timer
+{
+ public:
+    TimerMPI (MPI::Intracomm& comm)
+       : m_comm(comm)
+      {
+         m_timeStart = MPI::Wtime();
+      }
+
+    virtual ~TimerMPI (void)
+      {}
+
+    virtual double timerEnd (void)
+      {
+       m_timeEnd = MPI::Wtime();
+       m_timeElapsed = m_timeEnd - m_timeStart;
+  
+       return (m_timeElapsed);
+      }
+
+    virtual void timerReport (const char* const msg)
+      {
+         if (m_comm.Get_rank() == 0)
+             cout << msg << ": " << m_timeElapsed << " seconds" << endl;
+      }
+
+    virtual double timerEndAndReport (const char* const msg)
+      {
+       timerEnd ();
+       timerReport (msg);
+      }
+
+    virtual void timerReportAllProcesses (const char* const msg)
+      {
+         timerReport (msg);
+      }
+
+ protected:
+    MPI::Intracomm& m_comm;
+};
+
+class TimerCollectiveMPI : public TimerMPI
+{
+ public:
+    TimerCollectiveMPI (MPI::Intracomm& comm)
+       : TimerMPI::TimerMPI (comm)
+      {
+       m_comm.Barrier();
+       m_timeStart = MPI::Wtime();
+      }
+
+    virtual ~TimerCollectiveMPI (void)
+      {}
+
+    virtual double timerEnd (void)
+      {
+       m_timeEnd = MPI::Wtime();
+       m_timeElapsed = m_timeEnd - m_timeStart;
+       m_comm.Reduce (&m_timeElapsed, &m_timeMin, 1, MPI::DOUBLE, MPI::MIN, 0);
+       m_comm.Reduce (&m_timeElapsed, &m_timeMax, 1, MPI::DOUBLE, MPI::MAX, 0);
+  
+       return (m_timeElapsed);
+      }
+
+    virtual double timerEndAndReport (const char* const msg)
+      {
+       timerEnd ();
+       timerReport (msg);
+      }
+
+    virtual void timerReport (const char* const msg)
+      {
+       if (m_comm.Get_rank() == 0)
+         cout << msg << " " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds" << endl;
+      }
+
+    virtual void timerReportAllProcesses (const char* const msg)
+      {
+         cout << msg << ": " << "Minimum=" << m_timeMin << ", Maximum=" << m_timeMax << " seconds (Rank " << m_comm.Get_rank() << ")" << endl;
+      }
+
+ private:
+    double m_timeMin;
+    double m_timeMax;
+};
+#endif
+
+
+
+