Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_ScalarFlopCounter.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #include <cassert>
11 #include <iomanip>
12 #include <vector>
13 #include <iostream>
14 
17 
18 // Initialization of static members
19 const char*
21 {
22  "="
23  ,"+"
24  ,"+="
25  ,"unary +"
26  ,"-"
27  ,"-="
28  ,"unary -"
29  ,"*"
30  ,"*="
31  ,"/"
32  ,"/="
33  ,">"
34  ,">="
35  ,"<"
36  ,"<="
37  ,"=="
38  ,"exp"
39  ,"log"
40  ,"log10"
41  ,"sqrt"
42  ,"cbrt"
43  ,"cos"
44  ,"sin"
45  ,"tan"
46  ,"acos"
47  ,"asin"
48  ,"atan"
49  ,"atan2"
50  ,"cosh"
51  ,"sinh"
52  ,"tanh"
53  ,"abs"
54  ,"pow"
55  ,"max"
56  ,"min"
57 };
58 const char*
60 {
61  "="
62  ,"all +-"
63  ,"all *"
64  ,"all /"
65  ,"<,>,=="
66  ,"nonlinear"
67 };
68 unsigned int
70 
72 {
73  reset();
74 }
75 
76 void
78 {
79  ds_array<unsigned int>::zero( &partialFlopCounts[0], int(NUM_OPS) );
80  ds_array<unsigned int>::zero( &partialSummaryFlopCounts[0],
81  int(NUM_SUMMARY_OPS) );
82  ds_array<double>::zero( &flopCounts[0], int(NUM_OPS) );
83  ds_array<double>::zero( &summaryFlopCounts[0], int(NUM_SUMMARY_OPS) );
84  totalFlopCount = 0.0;
85 }
86 
87 void
89 {
90  for (int i=0; i<NUM_OPS; i++) {
91  flopCounts[i] += static_cast<double>(partialFlopCounts[i]);
92  partialFlopCounts[i] = 0;
93  }
94  for (int i=0; i<NUM_SUMMARY_OPS; i++) {
95  summaryFlopCounts[i] += static_cast<double>(partialSummaryFlopCounts[i]);
96  partialSummaryFlopCounts[i] = 0;
97  }
98  totalFlopCount = 0;
99  for (int i=0; i<NUM_OPS; i++)
100  totalFlopCount += flopCounts[i];
101 }
102 
103 void
105 {
106  ESummaryFlopType sft = getSummaryType(ft);
107  if (partialFlopCounts[ft] > flopGranularity) {
108  flopCounts[ft] += static_cast<double>(partialFlopCounts[ft]);
109  partialFlopCounts[ft] =0;
110  }
111  if (partialSummaryFlopCounts[sft] > flopGranularity) {
112  summaryFlopCounts[sft] +=
113  static_cast<double>(partialSummaryFlopCounts[sft]);
114  partialSummaryFlopCounts[sft] = 0;
115  }
116  ++partialFlopCounts[ft];
117  ++partialSummaryFlopCounts[sft];
118 }
119 
122 {
123  switch(ft) {
124  case ASSIGN:
125  return SUMMARY_ASSIGN;
126  case PLUS:
127  case PLUS_ASSIGN:
128  case UNARY_PLUS:
129  case MINUS:
130  case MINUS_ASSIGN:
131  case UNARY_MINUS:
132  return SUMMARY_PLUS_MINUS;
133  case MULTIPLY:
134  case MULTIPLY_ASSIGN:
135  return SUMMARY_MULTIPLY;
136  case DIVIDE:
137  case DIVIDE_ASSIGN:
138  return SUMMARY_DIVIDE;
139  case EXP:
140  case LOG:
141  case LOG10:
142  case SQRT:
143  case CBRT:
144  case COS:
145  case SIN:
146  case TAN:
147  case ACOS:
148  case ASIN:
149  case ATAN:
150  case ATAN2:
151  case COSH:
152  case SINH:
153  case TANH:
154  case ABS:
155  case POW:
156  case MAX:
157  case MIN:
158  return SUMMARY_NONLINEAR;
159  case GREATER_THAN:
160  case GREATER_THAN_EQUAL:
161  case LESS_THAN:
162  case LESS_THAN_EQUAL:
163  case EQUAL:
164  return SUMMARY_COMPARISON;
165  default:
166  assert(0);
167  }
168 
169  // This code is un-reachable, but some compilers will issue a warning
170  // without it
171  return SUMMARY_ASSIGN;
172 }
173 
174 std::ostream&
176  const char* names[],
177  const char* abbr[],
178  const FlopCounts counts[],
179  std::ostream &out)
180 {
181  assert( n >= 1 && names && abbr && counts );
182  const int wo = 10;
183  const int wc = 20;
184  const char spacero[] = "----------";
185  const char spacerc[] = "--------------------";
186  // Print legend
187  if(names) {
188  out << "\nLegend\n------\n";
189  for( int j = 0; j < n; ++j )
190  out << " " << abbr[j] << " = " << names[j] << std::endl;
191  out << std::endl;
192  }
193  // Print table header
194  out << std::left << " " << std::setw(wo) << "op\\count";
195  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << abbr[j];
196  out << std::endl;
197  out << std::right << " " << std::setw(wo) << spacero;
198  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << spacerc;
199  out << std::endl;
200  // Print rows of all operation counts
201  for( int i = 0; i < FlopCounts::NUM_OPS; ++i ) {
202  double theseFlops = 0;
203  for( int j = 0; j < n; ++j ) theseFlops += counts[j].flopCounts[i];
204  if(theseFlops) {
205  out << " " << std::setw(wo) << FlopCounts::flopCountsNames[i];
206  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << counts[j].flopCounts[i];
207  out << std::endl;
208  }
209  }
210  out << std::right << " " << std::setw(wo) << spacero;
211  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << spacerc;
212  out << std::endl;
213  // Print summary rows
214  std::vector<double> totalFlops(n);
215  for( int i = 0; i < FlopCounts::NUM_SUMMARY_OPS; ++i ) {
216  double theseFlops = 0;
217  for( int j = 0; j < n; ++j ) {
218  const double flops = counts[j].summaryFlopCounts[i];
219  theseFlops += flops;
220  totalFlops[j] += flops;
221  }
222  if(theseFlops) {
223  out << " " << std::setw(wo) << FlopCounts::summaryFlopCountsNames[i];
224  for( int j = 0; j < n; ++j )
225  out << " " << std::setw(wc) << counts[j].summaryFlopCounts[i];
226  out << std::endl;
227  }
228  }
229  out << std::right << " " << std::setw(wo) << spacero;
230  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << spacerc;
231  out << std::endl;
232  // Print total flops
233  out << " " << std::setw(wo) << "all flops";
234  for( int j = 0; j < n; ++j ) out << " " << std::setw(wc) << totalFlops[j];
235  out << std::endl;
236  //
237  return out;
238 }
void reset()
Reset flop counters before starting a block of computations. */.
ESummaryFlopType getSummaryType(EFlopType ft)
Get summary op enum from op enum.
void increment(EFlopType ft)
Increment an individual flop counter.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
std::ostream & printCountersTable(const int n, const char *names[], const char *abbr[], const FlopCounts counts[], std::ostream &out)
Print a list of flop counts into a single table.
static unsigned int flopGranularity
The number of flops to accumulate as an integer before converting to a double.
#define MIN(a, b)
ESummaryFlopType
Enum of summary operation categories.
double summaryFlopCounts[NUM_SUMMARY_OPS]
Summary category flop counts.
static const char * flopCountsNames[NUM_OPS]
Names of individual flops.
Class storing flop counts and summary flop counts.
double flopCounts[NUM_OPS]
Individual flop counts.
static const char * summaryFlopCountsNames[NUM_SUMMARY_OPS]
Names for summary operation categories.