libMesh
perfmon.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 #ifdef LIBMESH_HAVE_SYS_TIME_H
37 #include <sys/time.h> // gettimeofday() on Unix
38 #endif
39 #ifndef LIBMESH_HAVE_GETTIMEOFDAY
40 #include "libmesh/win_gettimeofday.h" // gettimeofday() on Windows
41 #endif
42 
43 namespace libMesh
44 {
45 
46 
47 
57 class PerfMon
58 {
59 public:
60  PerfMon (std::string id,
61  const unsigned int v=1,
62  const unsigned int pid=0);
63 
64  ~PerfMon ();
65  void reset ();
66  double print (std::string msg="NULL",
67  std::ostream & my_out = libMesh::out);
68 
69 private:
70 
71  const std::string id_string;
72 
75 
76  const unsigned int verbose;
77  const unsigned int proc_id;
78 
79 #ifdef HAVE_PAPI_H
80  float rtime, ptime, mflops;
81  long long int flpins;
82 #endif
83 };
84 
85 
86 
87 inline
88 void
90 {
91  gettimeofday (&the_time_start, nullptr);
92 
93 #ifdef HAVE_PAPI_H
94  Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
95 #endif
96 }
97 
98 
99 
100 inline
101 double
102 PerfMon::print (std::string msg, std::ostream & my_out)
103 {
104  gettimeofday (&the_time_stop, nullptr);
105 
106 #ifdef HAVE_PAPI_H
107  Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
108 #endif
109 
110  const double elapsed_time = ((double) (the_time_stop.tv_sec - the_time_start.tv_sec)) +
111  ((double) (the_time_stop.tv_usec - the_time_start.tv_usec))/1000000.;
112 
113  if (verbose)
114  {
115 
116  if (proc_id == 0)
117  {
118  if (msg == "NULL")
119  my_out << " " << id_string
120  << ": elapsed time: "
121  << elapsed_time << " (sec)"
122  << std::endl;
123  else
124  my_out << " " << msg
125  << ": elapsed time: "
126  << elapsed_time << " (sec)"
127  << std::endl;
128 
129 #ifdef HAVE_PAPI_H
130  if (msg == "NULL")
131  my_out << " " << id_string
132  << ": mflops: "
133  << mflops
134  << std::endl;
135  else
136  my_out << " " << msg
137  << ": mflops: "
138  << mflops
139  << std::endl;
140 #endif
141 
142  }
143  }
144 
145  return elapsed_time;
146 }
147 
148 
149 inline
150 PerfMon::PerfMon (std::string id,
151  const unsigned int v,
152  const unsigned int pid) :
153  id_string(id),
154  verbose(v),
155  proc_id(pid)
156 {
157  reset ();
158 }
159 
160 
161 
162 inline
164 {
165  print ();
166 }
167 
168 
169 
170 
171 } // namespace libMesh
172 
173 
174 #endif // LIBMESH_PERFMON_H
const std::string id_string
Definition: perfmon.h:71
struct timeval the_time_start
Definition: perfmon.h:73
int gettimeofday(struct timeval *tp, struct timezone *tzp)
The libMesh namespace provides an interface to certain functionality in the library.
void reset()
Definition: perfmon.h:89
Definition: perfmon.h:28
double print(std::string msg="NULL", std::ostream &my_out=libMesh::out)
Definition: perfmon.h:102
PAPI stands for Performance Application Programming Interface.
Definition: perfmon.h:57
struct timeval the_time_stop
Definition: perfmon.h:74
OStreamProxy out
PerfMon(std::string id, const unsigned int v=1, const unsigned int pid=0)
Definition: perfmon.h:150
float mflops
Definition: perfmon.h:80
long long int flpins
Definition: perfmon.h:81
const unsigned int proc_id
Definition: perfmon.h:77
const unsigned int verbose
Definition: perfmon.h:76