/*************************************************************************** * blitz/benchext.cc Methods for Benchmarking class with external control. * * $Id$ * * Copyright (C) 1997-2011 Todd Veldhuizen * * This file is a part of Blitz. * * Blitz is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Blitz is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Blitz. If not, see . * * Suggestions: blitz-devel@lists.sourceforge.net * Bugs: blitz-support@lists.sourceforge.net * * For more information, please see the Blitz++ Home Page: * https://sourceforge.net/projects/blitz/ * ****************************************************************************/ #ifndef BZ_BENCHEXT_CC #define BZ_BENCHEXT_CC #ifndef BZ_BENCHEXT_H #error must be included via #endif //#include #ifdef BZ_HAVE_STD #include #include #else #include #include #endif BZ_NAMESPACE(blitz) template BenchmarkExt::BenchmarkExt(const char* name, int numImplementations) { BZPRECONDITION(numImplementations > 0); description_ = name; numImplementations_ = numImplementations; implementationDescriptions_.resize(numImplementations); parameterDescription_ = "Vector length"; // we don't really know this is the default, do we... setDependentVariable("flops"); /* // 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) = static_cast(BZ_MATHFN_SCOPE(pow)(10.0, (i+1)/4.0)); iterations_ = 5.0e+5 / parameters_; flopsPerIteration_ = parameters_; */ // Set up initial state state_ = initializing; implementationNumber_ = 0; } template BenchmarkExt::~BenchmarkExt() { } template void BenchmarkExt::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_); instr_.resize(numImplementations_, numParameters_); flops_.resize(numImplementations_, numParameters_); } template void BenchmarkExt::setParameterVector(Array parms) { BZPRECONDITION(state_ == initializing); BZPRECONDITION(parms.size() == parameters_.size()); parameters_ = parms; } template void BenchmarkExt::setParameterDescription(const char* string) { parameterDescription_ = string; } template void BenchmarkExt::setIterations(Array iters) { BZPRECONDITION(state_ == initializing); iterations_ = iters; } template void BenchmarkExt::setOpsPerIteration(Array flopsPerIteration) { BZPRECONDITION(flopsPerIteration_.size() == flopsPerIteration.size()); flopsPerIteration_ = flopsPerIteration; } /** Set the dependent variable of the measurements. If the independent variable is seconds, we output "G/s", if it is cycles, we output " void BenchmarkExt::setDependentVariable(const char* dvar) { BZPRECONDITION(Timer::indep_var()!=""); if(timer_.indep_var()=="s") { depvar_ = string("G")+dvar+"/s"; timerconversion_ = 1./1e9; } else if(timer_.indep_var()=="c") { depvar_ = string(dvar)+"/c"; timerconversion_ = 1.; } else { depvar_ = std::string(dvar)+"/"+timer_.indep_var(); timerconversion_ = 1; } } template void BenchmarkExt::beginBenchmarking() { BZPRECONDITION(state_ == initializing); state_ = benchmarking; } template void BenchmarkExt::beginImplementation(const char* description) { // it would really be better if it worked for as many as we give it BZPRECONDITION(implementationNumber_ < numImplementations_); BZPRECONDITION(state_ == benchmarking); implementationDescriptions_[implementationNumber_] = description; state_ = benchmarkingImplementation; parameterNumber_ = 0; } template bool BenchmarkExt::doneImplementationBenchmark() const { BZPRECONDITION(state_ == benchmarkingImplementation); return parameterNumber_ == numParameters_; } template const string& BenchmarkExt::currentImplementation() const { BZPRECONDITION(implementationNumber_ < numImplementations_); return implementationDescriptions_[implementationNumber_]; } template P_parameter BenchmarkExt::getParameter() const { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ < numParameters_); return parameters_(parameterNumber_); } template long BenchmarkExt::getIterations() const { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ < numParameters_); return iterations_(parameterNumber_); } template inline void BenchmarkExt::start() { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ < numParameters_); state_ = running; timer_.start(); } template inline void BenchmarkExt::stop() { timer_.stop(); BZPRECONDITION(state_ == running); state_ = benchmarkingImplementation; times_(int(implementationNumber_), int(parameterNumber_)) = timer_.elapsed(); instr_(int(implementationNumber_), int(parameterNumber_)) = timer_.instr(); flops_(int(implementationNumber_), int(parameterNumber_)) = timer_.flops(); ++parameterNumber_; } template inline void BenchmarkExt::startOverhead() { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ > 0); BZPRECONDITION(parameterNumber_ <= numParameters_); state_ = runningOverhead; timer_.start(); } template inline void BenchmarkExt::stopOverhead() { BZPRECONDITION(state_ == runningOverhead); timer_.stop(); cout << "\ttimer overhead: " << 1.0*timer_.elapsed()/times_(int(implementationNumber_), int(parameterNumber_-1)) << endl; times_(int(implementationNumber_), int(parameterNumber_-1)) -= timer_.elapsed(); instr_(int(implementationNumber_), int(parameterNumber_-1)) -= timer_.instr(); flops_(int(implementationNumber_), int(parameterNumber_-1)) -= timer_.flops(); if(times_(int(implementationNumber_), int(parameterNumber_-1))<0) { cerr << "\tError: Timer underflow in benchmark " << implementationDescriptions_[implementationNumber_] << " " << parameters_(parameterNumber_-1) << endl; times_(int(implementationNumber_), int(parameterNumber_-1)) = blitz::huge(times_(0,0)); } state_ = benchmarkingImplementation; } template inline void BenchmarkExt::skip() { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ < numParameters_); times_(int(implementationNumber_), int(parameterNumber_)) = blitz::quiet_NaN(double()); ++parameterNumber_; state_ = benchmarkingImplementation; } template void BenchmarkExt::endImplementation() { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ == numParameters_); ++implementationNumber_; state_ = benchmarking; } template void BenchmarkExt::endBenchmarking() { BZPRECONDITION(state_ == benchmarking); BZPRECONDITION(implementationNumber_ == numImplementations_); state_ = done; } template double BenchmarkExt::getMflops(unsigned implementation, unsigned parameterNum) const { BZPRECONDITION(state_ == done); BZPRECONDITION(implementation < numImplementations_); BZPRECONDITION(parameterNum < numParameters_); return 1.0*iterations_(parameterNum) * flopsPerIteration_(parameterNum) / times_(int(implementation), int(parameterNum)) * timerconversion_; } template double BenchmarkExt::getinstrperc(int implementation, int parameterNum) const { BZPRECONDITION(state_ == done); BZPRECONDITION(implementation < numImplementations_); BZPRECONDITION(parameterNum < numParameters_); return 1.0*instr_(implementation,parameterNum)/ times_(int(implementation), int(parameterNum)); } template double BenchmarkExt::getflopsperc(int implementation, int parameterNum) const { BZPRECONDITION(state_ == done); BZPRECONDITION(implementation < numImplementations_); BZPRECONDITION(parameterNum < numParameters_); return 1.0*flops_(implementation,parameterNum)/ times_(int(implementation), int(parameterNum)); } template void BenchmarkExt::saveMatlabGraph(const char* filename, const char* graphType) const { BZPRECONDITION(state_ == done); { //ugly but saveMatlabGraph is coded into all benchmarks std::string pyfn(filename); pyfn=pyfn.replace(pyfn.find(".m"),2,std::string(".py"),0,3); savePylabGraph(pyfn.c_str()); } 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('" << depvar_ << "')\n" << "legend("; for (unsigned j=0; j < numImplementations_; ++j) { ofs << "'" << implementationDescriptions_[j] << "'"; if (j != numImplementations_ - 1) ofs << ", "; } ofs << ")" << endl; } template void BenchmarkExt::savePylabGraph(const char* filename, const char* graphType) const { BZPRECONDITION(state_ == done); ofstream ofs(filename); assert(ofs.good()); ofs << "# This python file generated automatically by class Benchmark\n" << "# of the Blitz++ class library.\n" << "from pylab import *\nfrom numpy import *\n" << "clf()\n"; ofs.setf(ios::scientific); ofs << "legnames=["; for (unsigned j=0; j < numImplementations_; ++j) { ofs << "'" << implementationDescriptions_[j] << "'"; if (j != numImplementations_ - 1) ofs << ", "; } ofs << "]\n\nparm = array([ "; unsigned i; for (i=0; i < numParameters_; ++i) ofs << setprecision(12) << double(parameters_(i)) << ", "; ofs << "])\n\n"; ofs << "Mf = array([[ "; for (i=0; i < numParameters_; ++i) { if(i>0) ofs << ", [ "; for (unsigned j=0; j < numImplementations_; ++j) { ofs << setprecision(12) << getMflops(j,i); if(j0) ofs << ", [ "; for (unsigned j=0; j < numImplementations_; ++j) { ofs << setprecision(12) << getinstrperc(j,i); if(j0) ofs << ", [ "; for (unsigned j=0; j < numImplementations_; ++j) { ofs << setprecision(12) << getflopsperc(j,i); if(j