perfmon.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 
43 class PerfMon
44 {
45  public:
46  PerfMon (std::string id,
47  const unsigned int v=1,
48  const unsigned int pid=0);
49 
50  ~PerfMon ();
51  void reset ();
52  double print (std::string msg="NULL", std::ostream &out = libMesh::out);
53 
54  private:
55 
56  const std::string id_string;
57 
58  struct timeval the_time_start;
59  struct timeval the_time_stop;
60 
61  const unsigned int verbose;
62  const unsigned int proc_id;
63 
64 #ifdef HAVE_PAPI_H
65  float rtime, ptime, mflops;
66  long long int flpins;
67 #endif
68 };
69 
70 
71 
72 inline
73 void
75 {
76  gettimeofday (&the_time_start, NULL);
77 
78 #ifdef HAVE_PAPI_H
79  Papi::PAPI_flops (&rtime, &ptime, &flpins, &mflops);
80 #endif
81 }
82 
83 
84 
85 inline
86 double
87 PerfMon::print (std::string msg, std::ostream &my_out)
88 {
89  gettimeofday (&the_time_stop, NULL);
90 
91 #ifdef HAVE_PAPI_H
92  Papi::PAPI_flops (&rtime, &ptime, &flpins, &mflops);
93 #endif
94 
95  const double elapsed_time = ((double) (the_time_stop.tv_sec - the_time_start.tv_sec)) +
96  ((double) (the_time_stop.tv_usec - the_time_start.tv_usec))/1000000.;
97 
98  if (verbose)
99  {
100 
101  if (proc_id == 0)
102  {
103  if (msg == "NULL")
104  my_out << " " << id_string
105  << ": elapsed time: "
106  << elapsed_time << " (sec)"
107  << std::endl;
108  else
109  my_out << " " << msg
110  << ": elapsed time: "
111  << elapsed_time << " (sec)"
112  << std::endl;
113 
114 #ifdef HAVE_PAPI_H
115  if (msg == "NULL")
116  my_out << " " << id_string
117  << ": mflops: "
118  << mflops
119  << std::endl;
120  else
121  my_out << " " << msg
122  << ": mflops: "
123  << mflops
124  << std::endl;
125 #endif
126 
127  }
128  }
129 
130  return elapsed_time;
131 }
132 
133 
134 inline
135 PerfMon::PerfMon (std::string id,
136  const unsigned int v,
137  const unsigned int pid) :
138  id_string(id),
139  verbose(v),
140  proc_id(pid)
141 {
142  reset ();
143 }
144 
145 
146 
147 inline
149 {
150  print ();
151 }
152 
153 
154 
155 
156 } // namespace libMesh
157 
158 
159 #endif // LIBMESH_PERFMON_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo