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