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