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