perfmon.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PERFMON_H
21 #define LIBMESH_PERFMON_H
22 
23 
24 // Local includes
25 #include "libmesh/libmesh_common.h"
26 
27 #ifdef HAVE_PAPI_H
28 namespace Papi {
29 # include <papi.h>
30 }
31 #endif
32 
33 // C++ includes
34 #include <cstddef>
35 #include <string>
36 #include <sys/time.h>
37 
38 namespace libMesh
39 {
40 
41 
42 
52 class PerfMon
53 {
54 public:
55  PerfMon (std::string id,
56  const unsigned int v=1,
57  const unsigned int pid=0);
58 
59  ~PerfMon ();
60  void reset ();
61  double print (std::string msg="NULL", std::ostream & out = libMesh::out);
62 
63 private:
64 
65  const std::string id_string;
66 
67  struct timeval the_time_start;
68  struct timeval the_time_stop;
69 
70  const unsigned int verbose;
71  const unsigned int proc_id;
72 
73 #ifdef HAVE_PAPI_H
74  float rtime, ptime, mflops;
75  long long int flpins;
76 #endif
77 };
78 
79 
80 
81 inline
82 void
84 {
85  gettimeofday (&the_time_start, nullptr);
86 
87 #ifdef HAVE_PAPI_H
88  Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
89 #endif
90 }
91 
92 
93 
94 inline
95 double
96 PerfMon::print (std::string msg, std::ostream & my_out)
97 {
98  gettimeofday (&the_time_stop, nullptr);
99 
100 #ifdef HAVE_PAPI_H
101  Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
102 #endif
103 
104  const double elapsed_time = ((double) (the_time_stop.tv_sec - the_time_start.tv_sec)) +
105  ((double) (the_time_stop.tv_usec - the_time_start.tv_usec))/1000000.;
106 
107  if (verbose)
108  {
109 
110  if (proc_id == 0)
111  {
112  if (msg == "NULL")
113  my_out << " " << id_string
114  << ": elapsed time: "
115  << elapsed_time << " (sec)"
116  << std::endl;
117  else
118  my_out << " " << msg
119  << ": elapsed time: "
120  << elapsed_time << " (sec)"
121  << std::endl;
122 
123 #ifdef HAVE_PAPI_H
124  if (msg == "NULL")
125  my_out << " " << id_string
126  << ": mflops: "
127  << mflops
128  << std::endl;
129  else
130  my_out << " " << msg
131  << ": mflops: "
132  << mflops
133  << std::endl;
134 #endif
135 
136  }
137  }
138 
139  return elapsed_time;
140 }
141 
142 
143 inline
144 PerfMon::PerfMon (std::string id,
145  const unsigned int v,
146  const unsigned int pid) :
147  id_string(id),
148  verbose(v),
149  proc_id(pid)
150 {
151  reset ();
152 }
153 
154 
155 
156 inline
158 {
159  print ();
160 }
161 
162 
163 
164 
165 } // namespace libMesh
166 
167 
168 #endif // LIBMESH_PERFMON_H
double print(std::string msg="NULL", std::ostream &out=libMesh::out)
Definition: perfmon.h:96
const std::string id_string
Definition: perfmon.h:65
struct timeval the_time_start
Definition: perfmon.h:67
void reset()
Definition: perfmon.h:83
Definition: perfmon.h:28
A class for interfacing with hardware timers.
Definition: perfmon.h:52
struct timeval the_time_stop
Definition: perfmon.h:68
PerfMon(std::string id, const unsigned int v=1, const unsigned int pid=0)
Definition: perfmon.h:144
float mflops
Definition: perfmon.h:74
long long int flpins
Definition: perfmon.h:75
OStreamProxy out(std::cout)
const unsigned int proc_id
Definition: perfmon.h:71
const unsigned int verbose
Definition: perfmon.h:70