1 | /*************************************************************************** |
---|
2 | * blitz/bench.h Benchmark classes |
---|
3 | * |
---|
4 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License |
---|
8 | * as published by the Free Software Foundation; either version 2 |
---|
9 | * of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * Suggestions: blitz-dev@oonumerics.org |
---|
17 | * Bugs: blitz-bugs@oonumerics.org |
---|
18 | * |
---|
19 | * For more information, please see the Blitz++ Home Page: |
---|
20 | * http://oonumerics.org/blitz/ |
---|
21 | * |
---|
22 | ***************************************************************************/ |
---|
23 | |
---|
24 | #ifndef BZ_BENCH_H |
---|
25 | #define BZ_BENCH_H |
---|
26 | |
---|
27 | #ifndef BZ_MATRIX_H |
---|
28 | #include <blitz/matrix.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifndef BZ_TIMER_H |
---|
32 | #include <blitz/timer.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #include <math.h> |
---|
36 | |
---|
37 | BZ_NAMESPACE(blitz) |
---|
38 | |
---|
39 | // Forward declaration |
---|
40 | template<typename P_parameter = unsigned> |
---|
41 | class BenchmarkImplementation; |
---|
42 | |
---|
43 | |
---|
44 | // Declaration of class Benchmark<T> |
---|
45 | // The template parameter T is the parameter type which is varied in |
---|
46 | // the benchmark. Typically T will be an unsigned, and will represent |
---|
47 | // the length of a vector, size of an array, etc. |
---|
48 | |
---|
49 | template<typename P_parameter = unsigned> |
---|
50 | class Benchmark { |
---|
51 | |
---|
52 | public: |
---|
53 | typedef P_parameter T_parameter; |
---|
54 | |
---|
55 | Benchmark(unsigned numImplementations); |
---|
56 | |
---|
57 | ~Benchmark(); |
---|
58 | |
---|
59 | void addImplementation(BenchmarkImplementation<T_parameter>* |
---|
60 | implementation); |
---|
61 | |
---|
62 | void run(ostream& log = cout); |
---|
63 | |
---|
64 | double getMflops(unsigned implementation, unsigned setting) const; |
---|
65 | |
---|
66 | double getRate(unsigned implementation, unsigned setting) const; |
---|
67 | |
---|
68 | void saveMatlabGraph(const char* filename) const; |
---|
69 | |
---|
70 | public: |
---|
71 | // Virtual functions |
---|
72 | |
---|
73 | virtual const char* description() const |
---|
74 | { return ""; } |
---|
75 | |
---|
76 | virtual const char* parameterDescription() const |
---|
77 | { return "Vector length"; } |
---|
78 | |
---|
79 | virtual unsigned numParameterSettings() const |
---|
80 | { return 19; } |
---|
81 | |
---|
82 | virtual T_parameter getParameterSetting(unsigned i) const |
---|
83 | { return ::pow(10.0, (i+1)/4.0); } |
---|
84 | |
---|
85 | virtual long getIterationSetting(unsigned i) const |
---|
86 | { return 1000000L / getParameterSetting(i); } |
---|
87 | |
---|
88 | private: |
---|
89 | Benchmark(const Benchmark<P_parameter>&) { } |
---|
90 | void operator=(const Benchmark<P_parameter>&) { } |
---|
91 | |
---|
92 | enum { uninitialized, initialized, running, done } state_; |
---|
93 | |
---|
94 | unsigned numImplementations_; |
---|
95 | unsigned numStoredImplementations_; |
---|
96 | |
---|
97 | BenchmarkImplementation<T_parameter>** implementations_; |
---|
98 | |
---|
99 | Matrix<double,RowMajor> rates_; // Iterations per second array |
---|
100 | Matrix<double,RowMajor> Mflops_; |
---|
101 | }; |
---|
102 | |
---|
103 | template<typename P_parameter> |
---|
104 | class BenchmarkImplementation { |
---|
105 | |
---|
106 | public: |
---|
107 | typedef P_parameter T_parameter; |
---|
108 | |
---|
109 | virtual void initialize(P_parameter parameter) { } |
---|
110 | |
---|
111 | virtual void done() { } |
---|
112 | |
---|
113 | virtual const char* implementationName() const |
---|
114 | { return ""; } |
---|
115 | |
---|
116 | virtual void run(long iterations) = 0; |
---|
117 | |
---|
118 | virtual void runOverhead(long iterations) |
---|
119 | { |
---|
120 | for (long i=0; i < iterations; ++i) |
---|
121 | { |
---|
122 | } |
---|
123 | }; |
---|
124 | |
---|
125 | virtual void tickle() { } |
---|
126 | |
---|
127 | virtual long flopsPerIteration() const |
---|
128 | { return 0; } |
---|
129 | }; |
---|
130 | |
---|
131 | BZ_NAMESPACE_END |
---|
132 | |
---|
133 | #include <blitz/bench.cc> |
---|
134 | |
---|
135 | #endif // BZ_BENCH_H |
---|