Line data Source code
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 :
48 : /**
49 : * PAPI stands for Performance Application Programming Interface.
50 : * This class was supposed to provide an interface to the hardware
51 : * timers that PAPI exposes, but it never really got developed.
52 : *
53 : * \author Benjamin S. Kirk
54 : * \date 2002
55 : * \brief A class for interfacing with hardware timers.
56 : */
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 :
73 : struct timeval the_time_start;
74 : struct timeval the_time_stop;
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
89 0 : PerfMon::reset ()
90 : {
91 0 : gettimeofday (&the_time_start, nullptr);
92 :
93 : #ifdef HAVE_PAPI_H
94 : Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
95 : #endif
96 0 : }
97 :
98 :
99 :
100 : inline
101 : double
102 0 : PerfMon::print (std::string msg, std::ostream & my_out)
103 : {
104 0 : gettimeofday (&the_time_stop, nullptr);
105 :
106 : #ifdef HAVE_PAPI_H
107 : Papi::PAPI_flops (&rtime, & ptime, &flpins, &mflops);
108 : #endif
109 :
110 0 : const double elapsed_time = ((double) (the_time_stop.tv_sec - the_time_start.tv_sec)) +
111 0 : ((double) (the_time_stop.tv_usec - the_time_start.tv_usec))/1000000.;
112 :
113 0 : if (verbose)
114 : {
115 :
116 0 : if (proc_id == 0)
117 : {
118 0 : if (msg == "NULL")
119 0 : my_out << " " << id_string
120 0 : << ": elapsed time: "
121 0 : << elapsed_time << " (sec)"
122 0 : << std::endl;
123 : else
124 : my_out << " " << msg
125 0 : << ": elapsed time: "
126 0 : << elapsed_time << " (sec)"
127 0 : << 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 0 : return elapsed_time;
146 : }
147 :
148 :
149 : inline
150 0 : PerfMon::PerfMon (std::string id,
151 : const unsigned int v,
152 0 : const unsigned int pid) :
153 0 : id_string(id),
154 0 : verbose(v),
155 0 : proc_id(pid)
156 : {
157 0 : reset ();
158 0 : }
159 :
160 :
161 :
162 : inline
163 0 : PerfMon::~PerfMon ()
164 : {
165 0 : print ();
166 0 : }
167 :
168 :
169 :
170 :
171 : } // namespace libMesh
172 :
173 :
174 : #endif // LIBMESH_PERFMON_H
|