| 1 | /* | 
|---|
| 2 | * $Id: bench.cc,v 1.1.1.1 1999-04-09 17:58:58 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.5  1998/03/14 00:04:47  tveldhui | 
|---|
| 10 | * 0.2-alpha-05 | 
|---|
| 11 | * | 
|---|
| 12 | * Revision 1.4  1997/07/16 14:51:20  tveldhui | 
|---|
| 13 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 14 | * | 
|---|
| 15 | * Revision 1.3  1997/01/24 14:42:00  tveldhui | 
|---|
| 16 | * Periodic RCS update | 
|---|
| 17 | * | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef BZ_BENCH_CC | 
|---|
| 21 | #define BZ_BENCH_CC | 
|---|
| 22 |  | 
|---|
| 23 | #ifndef BZ_BENCH_H | 
|---|
| 24 | #error <blitz/bench.cc> must be included via <blitz/bench.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 | Benchmark<P_parameter>::Benchmark(unsigned numImplementations) | 
|---|
| 37 | { | 
|---|
| 38 | state_ = uninitialized; | 
|---|
| 39 | numImplementations_ = numImplementations; | 
|---|
| 40 | numStoredImplementations_ = 0; | 
|---|
| 41 | implementations_ = new BenchmarkImplementation<P_parameter>* [numImplementations_]; | 
|---|
| 42 | rates_.resize(numImplementations, numParameterSettings()); | 
|---|
| 43 | Mflops_.resize(numImplementations, numParameterSettings()); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | template<class P_parameter> | 
|---|
| 47 | Benchmark<P_parameter>::~Benchmark() | 
|---|
| 48 | { | 
|---|
| 49 | delete [] implementations_; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | template<class P_parameter> | 
|---|
| 53 | void Benchmark<P_parameter>::addImplementation( | 
|---|
| 54 | BenchmarkImplementation<P_parameter> * implementation) | 
|---|
| 55 | { | 
|---|
| 56 | BZPRECONDITION(state_ == uninitialized); | 
|---|
| 57 | BZPRECONDITION(numStoredImplementations_ < numImplementations_); | 
|---|
| 58 |  | 
|---|
| 59 | implementations_[numStoredImplementations_++] = implementation; | 
|---|
| 60 |  | 
|---|
| 61 | if (numStoredImplementations_ == numImplementations_) | 
|---|
| 62 | state_ = initialized; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | template<class P_parameter> | 
|---|
| 66 | void Benchmark<P_parameter>::run(ostream& log) | 
|---|
| 67 | { | 
|---|
| 68 | BZPRECONDITION(state_ == initialized); | 
|---|
| 69 | state_ = running; | 
|---|
| 70 |  | 
|---|
| 71 | Timer t; | 
|---|
| 72 |  | 
|---|
| 73 | for (unsigned j=0; j < numImplementations_; ++j) | 
|---|
| 74 | { | 
|---|
| 75 | for (unsigned i=0; i < numParameterSettings(); ++i) | 
|---|
| 76 | { | 
|---|
| 77 | log  << setw(20) << implementations_[j]->implementationName() | 
|---|
| 78 | << " " << setw(8) << getParameterSetting(i) << "  "; | 
|---|
| 79 | log.flush(); | 
|---|
| 80 |  | 
|---|
| 81 | implementations_[j]->initialize(getParameterSetting(i)); | 
|---|
| 82 | implementations_[j]->tickle(); | 
|---|
| 83 |  | 
|---|
| 84 | unsigned long iterations = getIterationSetting(i); | 
|---|
| 85 |  | 
|---|
| 86 | t.start(); | 
|---|
| 87 | implementations_[j]->run(iterations); | 
|---|
| 88 | t.stop(); | 
|---|
| 89 | double tm = t.elapsedSeconds(); | 
|---|
| 90 |  | 
|---|
| 91 | t.start(); | 
|---|
| 92 | implementations_[j]->runOverhead(iterations); | 
|---|
| 93 | t.stop(); | 
|---|
| 94 | double tmOverhead = t.elapsedSeconds(); | 
|---|
| 95 |  | 
|---|
| 96 | rates_(j,i) = iterations / (tm - tmOverhead); | 
|---|
| 97 | Mflops_(j,i) = rates_(j,i) | 
|---|
| 98 | * implementations_[j]->flopsPerIteration() / 1.0e+6; | 
|---|
| 99 |  | 
|---|
| 100 | log << setw(10) << (rates_(j,i)/1.0e+6) << " Mops/s "; | 
|---|
| 101 |  | 
|---|
| 102 | if (implementations_[j]->flopsPerIteration() != 0) | 
|---|
| 103 | { | 
|---|
| 104 | log << "[" << setw(7) << Mflops_(j,i) << " Mflops]"; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | log << endl; | 
|---|
| 108 | log.flush(); | 
|---|
| 109 |  | 
|---|
| 110 | implementations_[j]->done(); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | state_ = done; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | template<class P_parameter> | 
|---|
| 118 | double Benchmark<P_parameter>::getMflops(unsigned implementation, | 
|---|
| 119 | unsigned setting) const | 
|---|
| 120 | { | 
|---|
| 121 | BZPRECONDITION(state_ == done); | 
|---|
| 122 | BZPRECONDITION(implementation < numImplementations_); | 
|---|
| 123 | BZPRECONDITION(setting < numParameterSettings()); | 
|---|
| 124 |  | 
|---|
| 125 | return Mflops_(implementation, setting); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | template<class P_parameter> | 
|---|
| 129 | double Benchmark<P_parameter>::getRate(unsigned implementation, | 
|---|
| 130 | unsigned setting) const | 
|---|
| 131 | { | 
|---|
| 132 | BZPRECONDITION(state_ == done); | 
|---|
| 133 | BZPRECONDITION(implementation < numImplementations_); | 
|---|
| 134 | BZPRECONDITION(setting < numParameterSettings()); | 
|---|
| 135 |  | 
|---|
| 136 | return rates_(implementation, setting); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | template<class P_parameter> | 
|---|
| 140 | void Benchmark<P_parameter>::saveMatlabGraph(const char* filename) const | 
|---|
| 141 | { | 
|---|
| 142 | BZPRECONDITION(state_ == done); | 
|---|
| 143 |  | 
|---|
| 144 | ofstream ofs(filename); | 
|---|
| 145 |  | 
|---|
| 146 | assert(ofs.good()); | 
|---|
| 147 |  | 
|---|
| 148 | ofs << "% This matlab file generated automatically by class Benchmark" | 
|---|
| 149 | << endl << "% of the Blitz++ class library." << endl << endl; | 
|---|
| 150 |  | 
|---|
| 151 | ofs.setf(ios::scientific); | 
|---|
| 152 |  | 
|---|
| 153 | ofs << "parm = [ "; | 
|---|
| 154 | int i; | 
|---|
| 155 | for (i=0; i < numParameterSettings(); ++i) | 
|---|
| 156 | ofs << setprecision(12) << double(getParameterSetting(i)) << " "; | 
|---|
| 157 | ofs << "]; " << endl << endl; | 
|---|
| 158 |  | 
|---|
| 159 | ofs << "Mf = [ "; | 
|---|
| 160 | for (i=0; i < numParameterSettings(); ++i) | 
|---|
| 161 | { | 
|---|
| 162 | for (int j=0; j < numImplementations_; ++j) | 
|---|
| 163 | { | 
|---|
| 164 | ofs << setprecision(12) << getMflops(j,i) << " "; | 
|---|
| 165 | } | 
|---|
| 166 | if (i != numParameterSettings()-1) | 
|---|
| 167 | ofs << ";" << endl; | 
|---|
| 168 | } | 
|---|
| 169 | ofs << "] ;" << endl << endl; | 
|---|
| 170 |  | 
|---|
| 171 | ofs << "semilogx(parm,Mf), title('" << description() << "'), " << endl | 
|---|
| 172 | << "    xlabel('" << parameterDescription() << "'), " | 
|---|
| 173 | << "ylabel('Mflops')" << endl | 
|---|
| 174 | << "legend("; | 
|---|
| 175 |  | 
|---|
| 176 | for (int j=0; j < numImplementations_; ++j) | 
|---|
| 177 | { | 
|---|
| 178 | ofs << "'" << implementations_[j]->implementationName() | 
|---|
| 179 | << "'"; | 
|---|
| 180 | if (j != numImplementations_ - 1) | 
|---|
| 181 | ofs << ", "; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | ofs << ")" << endl; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | BZ_NAMESPACE_END | 
|---|
| 188 |  | 
|---|
| 189 | #endif // BZ_BENCH_CC | 
|---|