1 // -*- C++ -*-
2 /***************************************************************************
3  * blitz/benchext.h      BenchmarkExt classes (Benchmarks with external
4  *                       control)
5  *
6  * $Id$
7  *
8  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
9  *
10  * This file is a part of Blitz.
11  *
12  * Blitz is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation, either version 3
15  * of the License, or (at your option) any later version.
16  *
17  * Blitz is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
24  *
25  * Suggestions:          blitz-devel@lists.sourceforge.net
26  * Bugs:                 blitz-support@lists.sourceforge.net
27  *
28  * For more information, please see the Blitz++ Home Page:
29  *    https://sourceforge.net/projects/blitz/
30  *
31  ***************************************************************************/
32 
33 #ifndef BZ_BENCHEXT_H
34 #define BZ_BENCHEXT_H
35 
36 //#ifndef BZ_MATRIX_H
37 #include <blitz/array.h>
38 //#endif
39  #include <vector>
40 
41 #ifndef BZ_TIMER_H
42  #include <blitz/timer.h>
43 #endif
44 
45 #include <math.h>
46 #include <string>
47 
48 // NEEDS_WORK: replace use of const char* with <string>, once standard
49 // library is widely supported.
50 
51 namespace blitz {
52 
53 // Declaration of class BenchmarkExt<T>
54 // The template parameter T is the parameter type which is varied in
55 // the benchmark.  Typically T will be an unsigned, and will represent
56 // the length of a vector, size of an array, etc.
57 
58 template<typename P_parameter = unsigned>
59 class BenchmarkExt {
60 
61 public:
62     typedef P_parameter T_parameter;
63 
64     BenchmarkExt(const char* description, int numImplementations);
65 
66     ~BenchmarkExt();
67 
68     void setNumParameters(int numParameters);
69   void setParameterVector(Array<T_parameter,1> parms);
70     void setParameterDescription(const char* string);
71   void setIterations(Array<long,1> iters);
72   void setOpsPerIteration(Array<double,1> flopsPerIteration);
73   void setDependentVariable(const char* string);
74 
75     void beginBenchmarking();
76 
77     void beginImplementation(const char* description);
78     bool doneImplementationBenchmark() const;
79   const string& currentImplementation() const;
80 
81     T_parameter getParameter() const;
82     long        getIterations() const;
83 
84     inline void start();
85     inline void stop();
86 
87     void startOverhead();
88     void stopOverhead();
89     void skip();
90 
91 
92     void endImplementation();
93 
94     void endBenchmarking();
95     double getMflops(unsigned implementation, unsigned parameterNum) const;
96     double getinstrperc(int implementation, int parameterNum) const;
97     double getflopsperc(int implementation, int parameterNum) const;
98 
99     void saveMatlabGraph(const char* filename, const char* graphType="semilogx") const;
100     void savePylabGraph(const char* filename, const char* graphType="semilogx") const;
101 
102 protected:
BenchmarkExt(const BenchmarkExt<P_parameter> &)103     BenchmarkExt(const BenchmarkExt<P_parameter>&) { }
104     void operator=(const BenchmarkExt<P_parameter>&) { }
105 
106     enum { initializing, benchmarking, benchmarkingImplementation,
107        running, runningOverhead, done } state_;
108 
109     unsigned numImplementations_;
110     unsigned implementationNumber_;
111 
112   std::string description_;
113   std::vector<std::string> implementationDescriptions_;
114 
115   Array<double,2> times_;       // Elapsed time
116   Array<long long,2> instr_;       // instructions according to timer
117   Array<long long,2> flops_;       // flops according to timer
118 
119   Array<T_parameter,1> parameters_;
120   Array<long,1> iterations_;
121   Array<double,1> flopsPerIteration_;
122 
123     Timer timer_;
124 
125   std::string parameterDescription_;
126   std::string depvar_;
127   double timerconversion_;
128 
129     unsigned numParameters_;
130     unsigned parameterNumber_;
131 };
132 
133 }
134 
135 #include <blitz/benchext.cc>
136 
137 #endif // BZ_BENCHEXT_H
138