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