$extrastylesheet
perfmon.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_PERFMON_H
00021 #define LIBMESH_PERFMON_H
00022 
00023 
00024 // Local includes
00025 #include "libmesh/libmesh_common.h"
00026 
00027 #ifdef HAVE_PAPI_H
00028 namespace Papi {
00029 #  include <papi.h>
00030 }
00031 #endif
00032 
00033 // C++ includes
00034 #include <cstddef>
00035 #include <string>
00036 #include <sys/time.h>
00037 
00038 namespace libMesh
00039 {
00040 
00041 
00042 
00043 class PerfMon
00044 {
00045 public:
00046   PerfMon  (std::string id,
00047             const unsigned int v=1,
00048             const unsigned int pid=0);
00049 
00050   ~PerfMon ();
00051   void reset ();
00052   double print (std::string msg="NULL", std::ostream &out = libMesh::out);
00053 
00054 private:
00055 
00056   const std::string id_string;
00057 
00058   struct timeval the_time_start;
00059   struct timeval the_time_stop;
00060 
00061   const unsigned int verbose;
00062   const unsigned int proc_id;
00063 
00064 #ifdef HAVE_PAPI_H
00065   float rtime, ptime, mflops;
00066   long long int flpins;
00067 #endif
00068 };
00069 
00070 
00071 
00072 inline
00073 void
00074 PerfMon::reset ()
00075 {
00076   gettimeofday (&the_time_start, NULL);
00077 
00078 #ifdef HAVE_PAPI_H
00079   Papi::PAPI_flops (&rtime, &ptime, &flpins, &mflops);
00080 #endif
00081 }
00082 
00083 
00084 
00085 inline
00086 double
00087 PerfMon::print (std::string msg, std::ostream &my_out)
00088 {
00089   gettimeofday (&the_time_stop, NULL);
00090 
00091 #ifdef HAVE_PAPI_H
00092   Papi::PAPI_flops (&rtime, &ptime, &flpins, &mflops);
00093 #endif
00094 
00095   const double elapsed_time = ((double) (the_time_stop.tv_sec - the_time_start.tv_sec)) +
00096     ((double) (the_time_stop.tv_usec - the_time_start.tv_usec))/1000000.;
00097 
00098   if (verbose)
00099     {
00100 
00101       if (proc_id == 0)
00102         {
00103           if (msg == "NULL")
00104             my_out << " " << id_string
00105                    << ": elapsed time: "
00106                    << elapsed_time << " (sec)"
00107                    << std::endl;
00108           else
00109             my_out << " " << msg
00110                    << ": elapsed time: "
00111                    << elapsed_time << " (sec)"
00112                    << std::endl;
00113 
00114 #ifdef HAVE_PAPI_H
00115           if (msg == "NULL")
00116             my_out << " " << id_string
00117                    << ": mflops: "
00118                    << mflops
00119                    << std::endl;
00120           else
00121             my_out << " " << msg
00122                    << ": mflops: "
00123                    << mflops
00124                    << std::endl;
00125 #endif
00126 
00127         }
00128     }
00129 
00130   return elapsed_time;
00131 }
00132 
00133 
00134 inline
00135 PerfMon::PerfMon (std::string id,
00136                   const unsigned int v,
00137                   const unsigned int pid) :
00138   id_string(id),
00139   verbose(v),
00140   proc_id(pid)
00141 {
00142   reset ();
00143 }
00144 
00145 
00146 
00147 inline
00148 PerfMon::~PerfMon ()
00149 {
00150   print ();
00151 }
00152 
00153 
00154 
00155 
00156 } // namespace libMesh
00157 
00158 
00159 #endif // LIBMESH_PERFMON_H