HPCToolkit
Metric-IData.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef prof_Prof_Metric_IData_hpp
61 #define prof_Prof_Metric_IData_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <iostream>
66 
67 #include <string>
68 #include <vector>
69 
70 #include <typeinfo>
71 #include <algorithm>
72 
73 #include <climits>
74 
75 //*************************** User Include Files ****************************
76 
77 #include <include/uint.h>
78 
80 
82 
83 // do not include Metric-Mgr.hpp. In g++, it will create chaos in compilation
84 //#include "Metric-Mgr.hpp"
85 
86 //*************************** Forward Declarations **************************
87 
88 
89 //***************************************************************************
90 
91 namespace Prof {
92 namespace Metric {
93  class Mgr ;
94 
95 //***************************************************************************
96 // IData
97 //
98 // Interface/Mixin for metric data
99 //
100 // Optimized for the two expected common cases:
101 // 1. no metrics (hpcstruct's using Prof::Struct::Tree)
102 // 2. a known number of metrics (which may then be expanded)
103 //***************************************************************************
104 
105 class IData {
106 public:
107  typedef std::vector<hpcrun_metricVal_t> MetricVec;
108 
109 public:
110  // --------------------------------------------------------
111  // Create/Destroy
112  // --------------------------------------------------------
113  IData(size_t size = 0)
114  {
115  ensureMetricsSize(size);
116  }
117 
118  virtual ~IData()
119  {
120  }
121 
122  IData(const IData& x)
123  : m_metrics(x.m_metrics)
124  {
125  }
126 
127  IData&
128  operator=(const IData& x)
129  {
130  m_metrics = x.m_metrics;
131  return *this;
132  }
133 
134  // --------------------------------------------------------
135  // Metrics
136  // --------------------------------------------------------
137 
138  static const uint npos = UINT_MAX;
139 
140  bool
142  uint mEndId = Metric::IData::npos) const
143  {
144  if (mBegId == IData::npos) {
145  mBegId = 0;
146  }
147  mEndId = std::min(numMetrics(), mEndId);
148 
149  for (uint i = mBegId; i < mEndId; ++i) {
150  if (hasMetric(i)) {
151  return true;
152  }
153  }
154  return false;
155  }
156 
157  bool
158  hasMetric(size_t mId) const
159  { return (m_metrics[mId].r != 0.0); }
160 
161  bool
162  hasMetricSlow(size_t mId) const
163  { return (mId < m_metrics.size() && hasMetric(mId)); }
164 
165 
166  double
167  metric(size_t mId) const
168  { return m_metrics[mId].r; }
169 
170  double&
171  metric(size_t mId)
172  { return m_metrics[mId].r; }
173 
174 
176  metricObject(size_t mId)
177  {
178  return m_metrics[mId];
179  }
180 
181  double
182  demandMetric(size_t mId, size_t size = 0) const
183  {
184  size_t sz = std::max(size, mId+1);
185  ensureMetricsSize(sz);
186  return metric(mId);
187  }
188 
189  double&
190  demandMetric(size_t mId, size_t size = 0)
191  {
192  size_t sz = std::max(size, mId+1);
193  ensureMetricsSize(sz);
194  return metric(mId);
195  }
196 
197 
198  // zeroMetrics: takes bounds of the form [mBegId, mEndId)
199  // N.B.: does not have demandZeroMetrics() semantics
200  void
201  zeroMetrics(uint mBegId, uint mEndId)
202  {
203  for (uint i = mBegId; i < mEndId; ++i) {
204  metric(i) = 0.0;
205  }
206  }
207 
208 
209  void
211  {
212  m_metrics.clear();;
213  }
214 
215  // ensureMetricsSize: ensures a vector of the requested size exists
216  void
217  ensureMetricsSize(size_t size) const
218  {
219  if (size > m_metrics.size()) {
220  hpcrun_metricVal_t val = {.r = 0.0};
221  m_metrics.resize(size, val /*value*/); // inserts at end
222  }
223  }
224 
225  void
227  {
228  hpcrun_metricVal_t val = {.r = 0.0};
229  m_metrics.insert(m_metrics.begin(), numMetrics, val);
230  }
231 
232  uint
233  numMetrics() const
234  { return m_metrics.size(); }
235 
236 
237  // --------------------------------------------------------
238  //
239  // --------------------------------------------------------
240 
241  std::string
242  toStringMetrics(int oFlags = 0, const char* pfx = "") const;
243 
244 
245  // [mBegId, mEndId)
246  std::ostream&
247  writeMetricsXML(std::ostream& os,
248  const Mgr *metricMgr,
249  uint mBegId = Metric::IData::npos,
250  uint mEndId = Metric::IData::npos,
251  int oFlags = 0, const char* pfx = "") const;
252 
253 
254  std::ostream&
255  dumpMetrics(std::ostream& os = std::cerr, int oFlags = 0,
256  const char* pfx = "") const;
257 
258  void
259  ddumpMetrics() const;
260 
261 
262 private:
263  mutable MetricVec m_metrics;
264 
265 };
266 
267 //***************************************************************************
268 
269 } // namespace Metric
270 } // namespace Prof
271 
272 
273 #endif /* prof_Prof_Metric_IData_hpp */
IData(size_t size=0)
IData(const IData &x)
hpcrun_metricVal_t & metricObject(size_t mId)
bool hasMetricSlow(size_t mId) const
double metric(size_t mId) const
bool hasMetrics(uint mBegId=Metric::IData::npos, uint mEndId=Metric::IData::npos) const
void zeroMetrics(uint mBegId, uint mEndId)
void insertMetricsBefore(size_t numMetrics)
uint numMetrics() const
std::vector< hpcrun_metricVal_t > MetricVec
unsigned int uint
Definition: uint.h:124
double & demandMetric(size_t mId, size_t size=0)
bool hasMetric(size_t mId) const
double demandMetric(size_t mId, size_t size=0) const
double & metric(size_t mId)
void ensureMetricsSize(size_t size) const
IData & operator=(const IData &x)
std::string toStringMetrics(int oFlags=0, const char *pfx="") const
std::ostream & writeMetricsXML(std::ostream &os, const Mgr *metricMgr, uint mBegId=Metric::IData::npos, uint mEndId=Metric::IData::npos, int oFlags=0, const char *pfx="") const
std::ostream & dumpMetrics(std::ostream &os=std::cerr, int oFlags=0, const char *pfx="") const
void ddumpMetrics() const
static const uint npos