HPCToolkit
TextUtil.cpp
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 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::ostream;
64 
65 #include <iomanip>
66 
67 #include <string>
68 using std::string;
69 
70 #include <vector>
71 using std::vector;
72 
73 #include <algorithm>
74 
75 #include <typeinfo>
76 
77 #include <cmath> // pow
78 
79 //*************************** User Include Files ****************************
80 
81 #include "TextUtil.hpp"
82 
84 
85 //*************************** Forward Declarations ***************************
86 
87 //****************************************************************************
88 
89 namespace Analysis {
90 
91 namespace TextUtil {
92 
94  ostream& os,
95  int numDecPct, int numDecVal)
96  : m_mMgr(metricMgr),
97  m_os(os),
98  m_numDecPct(numDecPct),
99  m_numDecVal(numDecVal),
100  m_annotWidthTot(0)
101 {
102  m_annotWidth.resize(m_mMgr.size(), 0);
103  m_sciFmtLoThrsh_pct.resize(m_mMgr.size(), 0.0);
104  m_sciFmtHiThrsh_pct.resize(m_mMgr.size(), 0.0);
105  m_sciFmtLoThrsh_val.resize(m_mMgr.size(), 0.0);
106  m_sciFmtHiThrsh_val.resize(m_mMgr.size(), 0.0);
107  m_dispPercent.resize(m_mMgr.size(), false);
108  m_isForceable.resize(m_mMgr.size(), false);
109 
110  for (uint mId = 0; mId < m_mMgr.size(); ++mId) {
111  const Prof::Metric::ADesc* m = m_mMgr.metric(mId);
112  if (!m->isVisible()) {
113  continue;
114  }
115 
116  m_isForceable[mId] = (typeid(*m) == typeid(Prof::Metric::SampledDesc)
117  || !m->isPercent());
118 
119  // Compute annotation widths.
120  // NOTE: decimal digits shown as 'd' below
121  if (m->doDispPercent()) {
122  m_dispPercent[mId] = true;
123  if (m_numDecPct >= 1) {
124  // xxx.dd% or x.dE-yy% (latter for small values)
125  m_annotWidth[mId] = std::max(8, 5 + m_numDecPct);
126  }
127  else {
128  // xxx%
129  m_annotWidth[mId] = 4;
130  }
131  }
132  else {
133  // x.dd or x.dE+yy (latter for large numbers)
134  m_annotWidth[mId] = std::max(7, 2 + m_numDecVal);
135  }
136 
137  // compute threshholds
138  int nNonUnitsPct = (m_numDecPct == 0) ? 1 : m_numDecPct + 2; // . + %
139  int nNonUnitsVal = (m_numDecVal == 0) ? 0 : m_numDecVal + 1; // .
140 
141  int nUnitsPct = std::max(0, m_annotWidth[mId] - nNonUnitsPct);
142  int nUnitsVal = std::max(0, m_annotWidth[mId] - nNonUnitsVal);
143 
144  m_sciFmtHiThrsh_pct[mId] = std::pow(10.0, (double)nUnitsPct);
145  m_sciFmtHiThrsh_val[mId] = std::pow(10.0, (double)nUnitsVal);
146 
147  m_sciFmtLoThrsh_pct[mId] = std::pow(10.0, (double)-m_numDecPct);
148  m_sciFmtLoThrsh_val[mId] = std::pow(10.0, (double)-m_numDecVal);
149 
150  m_annotWidthTot += m_annotWidth[mId] + 1/*space*/;
151  }
152 
153  m_os.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
154  m_os << std::right << std::noshowpos;
155  m_os << std::showbase;
156 }
157 
158 
159 void
161 {
162  m_os << "Metric definitions. column: name (nice-name) [units] {details}:\n";
163 
164  uint colId = 1;
165  for (uint mId = 0; mId < m_mMgr.size(); ++mId) {
166  const Prof::Metric::ADesc* m = m_mMgr.metric(mId);
167 
168  m_os << std::fixed << std::setw(4) << std::setfill(' ');
169  if (m->isVisible()) {
170  m_os << colId;
171  colId++;
172  }
173  else {
174  m_os << "-";
175  }
176 
177  m_os << ": " << m->toString() << std::endl;
178  }
179 }
180 
181 
182 void
183 ColumnFormatter::genCol(uint mId, double metricVal, double metricTot,
185 {
186  if (!isDisplayed(mId)) {
187  return;
188  }
189 
190  bool dispPercent = m_dispPercent[mId];
191  if (flg != Flag_NULL && m_isForceable[mId]) {
192  dispPercent = (flg == Flag_ForcePct);
193  }
194 
195  double val = metricVal;
196 
197  if (dispPercent) {
198  // convert 'val' to a percent if necessary
199  const Prof::Metric::ADesc* m = m_mMgr.metric(mId);
200  if (!m->isPercent() && metricTot != 0.0) {
201  val = (metricVal / metricTot) * 100.0; // make the percent
202  }
203 
204  m_os << std::showpoint << std::setw(m_annotWidth[mId] - 1);
205  if (val != 0.0 &&
206  (val < m_sciFmtLoThrsh_pct[mId] || val >= m_sciFmtHiThrsh_pct[mId])) {
207  m_os << std::scientific
208  << std::setprecision(m_annotWidth[mId] - 7); // x.dE-yy%
209  }
210  else {
211  m_os << std::fixed
212  << std::setprecision(m_numDecPct);
213  }
214  m_os << std::setfill(' ') << val << "%";
215  }
216  else {
217  m_os << std::setw(m_annotWidth[mId]);
218  if (val != 0.0 &&
219  (val < m_sciFmtLoThrsh_val[mId] || val >= m_sciFmtHiThrsh_val[mId])) {
220  m_os << std::scientific
221  << std::setprecision(m_annotWidth[mId] - 6); // x.dE+yy
222  }
223  else {
224  m_os << std::fixed;
225  if (m_numDecVal == 0) {
226  m_os << std::noshowpoint << std::setprecision(0);
227  }
228  else {
229  m_os << std::setprecision(m_numDecVal);
230  }
231  }
232  m_os << std::setfill(' ') << val;
233  }
234  m_os << " ";
235 }
236 
237 
238 void
240 {
241  //const PerfMetric* m = m_mMgr.metric(mId);
242  if (!isDisplayed(mId)) {
243  return;
244  }
245  m_os << std::setw(m_annotWidth[mId]) << std::setfill(' ') << " " << " ";
246 }
247 
248 
249 //****************************************************************************
250 
251 
252 } // namespace TextUtil
253 
254 } // namespace Analysis
std::vector< double > m_sciFmtLoThrsh_pct
Definition: TextUtil.hpp:156
std::vector< bool > m_dispPercent
Definition: TextUtil.hpp:162
bool isPercent() const
void genCol(uint mid, double metricVal, double metricTot, Flag flg=Flag_NULL)
Definition: TextUtil.cpp:183
bool isVisible() const
std::vector< double > m_sciFmtLoThrsh_val
Definition: TextUtil.hpp:159
virtual std::string toString() const
bool doDispPercent() const
ColumnFormatter(const Prof::Metric::Mgr &metricMgr, std::ostream &os, int numDecPct, int numDecVal)
Definition: TextUtil.cpp:93
uint size() const
Definition: Metric-Mgr.hpp:153
unsigned int uint
Definition: uint.h:124
const Prof::Metric::Mgr & m_mMgr
Definition: TextUtil.hpp:146
std::vector< bool > m_isForceable
Definition: TextUtil.hpp:163
Metric::ADesc * metric(uint i)
Definition: Metric-Mgr.hpp:131
std::vector< double > m_sciFmtHiThrsh_pct
Definition: TextUtil.hpp:157
std::vector< double > m_sciFmtHiThrsh_val
Definition: TextUtil.hpp:160