This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchext.cc
298 lines (234 loc) · 7.9 KB
/
benchext.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 1997 Todd Veldhuizen <[email protected]>
* All rights reserved. Please see <blitz/blitz.h> for terms and
* conditions of use.
*
*/
#ifndef BZ_BENCHEXT_CC
#define BZ_BENCHEXT_CC
#ifndef BZ_BENCHEXT_H
#error <blitz/benchext.cc> must be included via <blitz/benchext.h>
#endif
#include <blitz/vector-et.h>
#ifdef BZ_HAVE_STD
#include <fstream>
#else
#include <fstream.h>
#endif
BZ_NAMESPACE(blitz)
template<typename P_parameter>
BenchmarkExt<P_parameter>::BenchmarkExt(const char* name,
int numImplementations)
{
BZPRECONDITION(numImplementations > 0);
description_ = name;
numImplementations_ = numImplementations;
implementationDescriptions_.resize(numImplementations);
parameterDescription_ = "Vector length";
rateDescription_ = "Mflops/s";
// Set up default parameters and iterations
setNumParameters(19);
// NEEDS_WORK: once pow(X,Y) is supported, can just say
// parameters_ = pow(10.0, Range(1,20)/4.0);
for (unsigned i=0; i < numParameters_; ++i)
parameters_[i] = (P_parameter)::pow(10.0, (i+1)/4.0);
iterations_ = 5.0e+5 / parameters_;
flopsPerIteration_ = parameters_;
// Set up initial state
state_ = initializing;
implementationNumber_ = 0;
}
template<typename P_parameter>
BenchmarkExt<P_parameter>::~BenchmarkExt()
{
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setNumParameters(int numParameters)
{
//BZPRECONDITION(state_ == initializing);
numParameters_ = numParameters;
parameters_.resize(numParameters_);
iterations_.resize(numParameters_);
flopsPerIteration_.resize(numParameters_);
// Set up timer and Mflops array
times_.resize(numImplementations_, numParameters_);
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setParameterVector(Vector<P_parameter> parms)
{
BZPRECONDITION(state_ == initializing);
BZPRECONDITION(parms.length() == parameters_.length());
// NEEDS_WORK: should use operator=(), once that problem
// gets sorted out.
// parameters_ = parms;
for (int i=0; i < parameters_.length(); ++i)
parameters_[i] = parms(i);
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setParameterDescription(const char* string)
{
parameterDescription_ = string;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setIterations(Vector<long> iters)
{
BZPRECONDITION(state_ == initializing);
// NEEDS_WORK: should use operator=(), once that problem
// gets sorted out.
// iterations_ = iters;
for (int i=0; i < iterations_.length(); ++i)
iterations_[i] = iters(i);
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setFlopsPerIteration(Vector<double>
flopsPerIteration)
{
BZPRECONDITION(flopsPerIteration_.length() == flopsPerIteration.length());
// NEEDS_WORK: should use operator=(), once that problem
// gets sorted out.
// flopsPerIteration_ = flopsPerIteration;
for (int i=0; i < flopsPerIteration_.length(); ++i)
flopsPerIteration_[i] = flopsPerIteration[i];
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::setRateDescription(const char* string)
{
rateDescription_ = string;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::beginBenchmarking()
{
BZPRECONDITION(state_ == initializing);
state_ = benchmarking;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::beginImplementation(const char* description)
{
BZPRECONDITION(implementationNumber_ < numImplementations_);
BZPRECONDITION(state_ == benchmarking);
implementationDescriptions_[implementationNumber_] = description;
state_ = benchmarkingImplementation;
parameterNumber_ = 0;
}
template<typename P_parameter>
bool BenchmarkExt<P_parameter>::doneImplementationBenchmark() const
{
BZPRECONDITION(state_ == benchmarkingImplementation);
return parameterNumber_ == numParameters_;
}
template<typename P_parameter>
P_parameter BenchmarkExt<P_parameter>::getParameter() const
{
BZPRECONDITION(state_ == benchmarkingImplementation);
BZPRECONDITION(parameterNumber_ < numParameters_);
return parameters_[parameterNumber_];
}
template<typename P_parameter>
long BenchmarkExt<P_parameter>::getIterations() const
{
BZPRECONDITION(state_ == benchmarkingImplementation);
BZPRECONDITION(parameterNumber_ < numParameters_);
return iterations_[parameterNumber_];
}
template<typename P_parameter>
inline void BenchmarkExt<P_parameter>::start()
{
BZPRECONDITION(state_ == benchmarkingImplementation);
BZPRECONDITION(parameterNumber_ < numParameters_);
state_ = running;
timer_.start();
}
template<typename P_parameter>
inline void BenchmarkExt<P_parameter>::stop()
{
timer_.stop();
BZPRECONDITION(state_ == running);
state_ = benchmarkingImplementation;
times_(implementationNumber_, parameterNumber_) = timer_.elapsedSeconds();
++parameterNumber_;
}
template<typename P_parameter>
inline void BenchmarkExt<P_parameter>::startOverhead()
{
BZPRECONDITION(state_ == benchmarkingImplementation);
BZPRECONDITION(parameterNumber_ > 0);
BZPRECONDITION(parameterNumber_ <= numParameters_);
state_ = runningOverhead;
overheadTimer_.start();
}
template<typename P_parameter>
inline void BenchmarkExt<P_parameter>::stopOverhead()
{
BZPRECONDITION(state_ == runningOverhead);
overheadTimer_.stop();
times_(implementationNumber_, parameterNumber_-1) -=
overheadTimer_.elapsedSeconds();
state_ = benchmarkingImplementation;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::endImplementation()
{
BZPRECONDITION(state_ == benchmarkingImplementation);
BZPRECONDITION(parameterNumber_ == numParameters_);
++implementationNumber_;
state_ = benchmarking;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::endBenchmarking()
{
BZPRECONDITION(state_ == benchmarking);
BZPRECONDITION(implementationNumber_ == numImplementations_);
state_ = done;
}
template<typename P_parameter>
double BenchmarkExt<P_parameter>::getMflops(unsigned implementation,
unsigned parameterNum) const
{
BZPRECONDITION(state_ == done);
BZPRECONDITION(implementation < numImplementations_);
BZPRECONDITION(parameterNum < numParameters_);
return iterations_(parameterNum) * flopsPerIteration_(parameterNum)
/ times_(implementation, parameterNum) / 1.0e+6;
}
template<typename P_parameter>
void BenchmarkExt<P_parameter>::saveMatlabGraph(const char* filename, const char* graphType) const
{
BZPRECONDITION(state_ == done);
ofstream ofs(filename);
assert(ofs.good());
ofs << "% This matlab file generated automatically by class Benchmark"
<< endl << "% of the Blitz++ class library." << endl << endl;
ofs.setf(ios::scientific);
// This will be a lot simpler once Matlab-style output formatting
// of vectors & matrices is finished.
// ofs << "parm = " << parameters_ << ";" << endl << endl;
ofs << "parm = [ ";
unsigned i;
for (i=0; i < numParameters_; ++i)
ofs << setprecision(12) << double(parameters_[i]) << " ";
ofs << "]; " << endl << endl;
ofs << "Mf = [ ";
for (i=0; i < numParameters_; ++i)
{
for (unsigned j=0; j < numImplementations_; ++j)
{
ofs << setprecision(12) << getMflops(j,i) << " ";
}
if (i != numParameters_ - 1)
ofs << ";" << endl;
}
ofs << "] ;" << endl << endl;
ofs << graphType << "(parm,Mf), title('" << description_ << "'), " << endl
<< " xlabel('" << parameterDescription_ << "'), "
<< "ylabel('" << rateDescription_ << "')" << endl
<< "legend(";
for (unsigned j=0; j < numImplementations_; ++j)
{
ofs << "'" << implementationDescriptions_(j) << "'";
if (j != numImplementations_ - 1)
ofs << ", ";
}
ofs << ")" << endl;
}
BZ_NAMESPACE_END
#endif // BZ_BENCHEXT_CC