/* * $Id: benchext.cc,v 1.1.1.1 1999-04-09 17:59:01 ansari Exp $ * * Copyright (C) 1997 Todd Veldhuizen * All rights reserved. Please see for terms and * conditions of use. * * $Log: not supported by cvs2svn $ * Revision 1.2 1998/03/14 00:04:47 tveldhui * 0.2-alpha-05 * * Revision 1.1 1997/07/16 14:51:20 tveldhui * Update: Alpha release 0.2 (Arrays) * */ #ifndef BZ_BENCHEXT_CC #define BZ_BENCHEXT_CC #ifndef BZ_BENCHEXT_H #error must be included via #endif #ifdef BZ_HAVE_STD #include #else #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"; rateDescription_ = "Mflops/s"; // 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 (int i=0; i < numParameters_; ++i) parameters_[i] = (P_parameter)::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_); } template void BenchmarkExt::setParameterVector(Vector parms) { BZPRECONDITION(state_ == initializing); BZPRECONDITION(parms.length() == parameters_.length()); // NEEDS_WORK: should use operator=(), once that problem // gets sorted out. // parameters_ = parms; for (int i=0; i < parameters_.length(); ++i) parameters_[i] = parms(i); } template void BenchmarkExt::setParameterDescription(const char* string) { parameterDescription_ = string; } template void BenchmarkExt::setIterations(Vector iters) { BZPRECONDITION(state_ == initializing); // NEEDS_WORK: should use operator=(), once that problem // gets sorted out. // iterations_ = iters; for (int i=0; i < iterations_.length(); ++i) iterations_[i] = iters(i); } template void BenchmarkExt::setFlopsPerIteration(Vector flopsPerIteration) { BZPRECONDITION(flopsPerIteration_.length() == flopsPerIteration.length()); // NEEDS_WORK: should use operator=(), once that problem // gets sorted out. // flopsPerIteration_ = flopsPerIteration; for (int i=0; i < flopsPerIteration_.length(); ++i) flopsPerIteration_[i] = flopsPerIteration[i]; } template void BenchmarkExt::setRateDescription(const char* string) { rateDescription_ = string; } template void BenchmarkExt::beginBenchmarking() { BZPRECONDITION(state_ == initializing); state_ = benchmarking; } template void BenchmarkExt::beginImplementation(const char* description) { BZPRECONDITION(implementationNumber_ < numImplementations_); BZPRECONDITION(state_ == benchmarking); implementationDescriptions_[implementationNumber_] = description; state_ = benchmarkingImplementation; parameterNumber_ = 0; } template _bz_bool BenchmarkExt::doneImplementationBenchmark() const { BZPRECONDITION(state_ == benchmarkingImplementation); return parameterNumber_ == numParameters_; } template P_parameter BenchmarkExt::getParameter() const { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ >= 0); BZPRECONDITION(parameterNumber_ < numParameters_); return parameters_[parameterNumber_]; } template long BenchmarkExt::getIterations() const { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ >= 0); 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_(implementationNumber_, parameterNumber_) = timer_.elapsedSeconds(); ++parameterNumber_; } template inline void BenchmarkExt::startOverhead() { BZPRECONDITION(state_ == benchmarkingImplementation); BZPRECONDITION(parameterNumber_ > 0); BZPRECONDITION(parameterNumber_ <= numParameters_); state_ = runningOverhead; overheadTimer_.start(); } template inline void BenchmarkExt::stopOverhead() { BZPRECONDITION(state_ == runningOverhead); overheadTimer_.stop(); times_(implementationNumber_, parameterNumber_-1) -= overheadTimer_.elapsedSeconds(); 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 iterations_(parameterNum) * flopsPerIteration_(parameterNum) / times_(implementation, parameterNum) / 1.0e+6; } template void BenchmarkExt::saveMatlabGraph(const char* filename) const { BZPRECONDITION(state_ == done); 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 = [ "; int 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 (int j=0; j < numImplementations_; ++j) { ofs << setprecision(12) << getMflops(j,i) << " "; } if (i != numParameters_ - 1) ofs << ";" << endl; } ofs << "] ;" << endl << endl; ofs << "semilogx(parm,Mf), title('" << description_ << "'), " << endl << " xlabel('" << parameterDescription_ << "'), " << "ylabel('" << rateDescription_ << "')" << endl << "legend("; for (int j=0; j < numImplementations_; ++j) { ofs << "'" << implementationDescriptions_(j) << "'"; if (j != numImplementations_ - 1) ofs << ", "; } ofs << ")" << endl; } BZ_NAMESPACE_END #endif // BZ_BENCHEXT_CC