1 //
2 //  Copyright (c) 2000-2002
3 //  Joerg Walter, Mathias Koch
4 //
5 //  Distributed under the Boost Software License, Version 1.0. (See
6 //  accompanying file LICENSE_1_0.txt or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt)
8 //
9 //  The authors gratefully acknowledge the support of
10 //  GeNeSys mbH & Co. KG in producing this work.
11 //
12 
13 #include "bench2.hpp"
14 
15 template<class T, int N>
16 struct bench_c_inner_prod {
17     typedef T value_type;
18 
operator ()bench_c_inner_prod19     void operator () (int runs) const {
20         try {
21             static typename c_vector_traits<T, N>::type v1, v2;
22             initialize_c_vector<T, N> () (v1);
23             initialize_c_vector<T, N> () (v2);
24             boost::timer t;
25             for (int i = 0; i < runs; ++ i) {
26                 static value_type s (0);
27                 for (int j = 0; j < N; ++ j) {
28                     s += v1 [j] * v2 [j];
29                 }
30 //                sink_scalar (s);
31             }
32             footer<value_type> () (N, N - 1, runs, t.elapsed ());
33         }
34         catch (std::exception &e) {
35             std::cout << e.what () << std::endl;
36         }
37     }
38 };
39 template<class V, int N>
40 struct bench_my_inner_prod {
41     typedef typename V::value_type value_type;
42 
operator ()bench_my_inner_prod43     void operator () (int runs) const {
44         try {
45             static V v1 (N, N), v2 (N, N);
46             initialize_vector (v1);
47             initialize_vector (v2);
48             boost::timer t;
49             for (int i = 0; i < runs; ++ i) {
50                 static value_type s (0);
51                 s = ublas::inner_prod (v1, v2);
52 //                sink_scalar (s);
53                 BOOST_UBLAS_NOT_USED(s);
54             }
55             footer<value_type> () (N, N - 1, runs, t.elapsed ());
56         }
57         catch (std::exception &e) {
58             std::cout << e.what () << std::endl;
59         }
60     }
61 };
62 template<class V, int N>
63 struct bench_cpp_inner_prod {
64     typedef typename V::value_type value_type;
65 
operator ()bench_cpp_inner_prod66     void operator () (int runs) const {
67         try {
68             static V v1 (N), v2 (N);
69             initialize_vector (v1);
70             initialize_vector (v2);
71             boost::timer t;
72             for (int i = 0; i < runs; ++ i) {
73                 static value_type s (0);
74                 s = (v1 * v2).sum ();
75 //                sink_scalar (s);
76             }
77             footer<value_type> () (N, N - 1, runs, t.elapsed ());
78         }
79         catch (std::exception &e) {
80             std::cout << e.what () << std::endl;
81         }
82     }
83 };
84 
85 template<class T, int N>
86 struct bench_c_vector_add {
87     typedef T value_type;
88 
operator ()bench_c_vector_add89     void operator () (int runs) const {
90         try {
91             static typename c_vector_traits<T, N>::type v1, v2, v3;
92             initialize_c_vector<T, N> () (v1);
93             initialize_c_vector<T, N> () (v2);
94             boost::timer t;
95             for (int i = 0; i < runs; ++ i) {
96                 for (int j = 0; j < N; ++ j) {
97                     v3 [j] = - (v1 [j] + v2 [j]);
98                 }
99 //                sink_c_vector<T, N> () (v3);
100                 BOOST_UBLAS_NOT_USED(v3);
101             }
102             footer<value_type> () (0, 2 * N, runs, t.elapsed ());
103         }
104         catch (std::exception &e) {
105             std::cout << e.what () << std::endl;
106         }
107     }
108 };
109 template<class V, int N>
110 struct bench_my_vector_add {
111     typedef typename V::value_type value_type;
112 
operator ()bench_my_vector_add113     void operator () (int runs, safe_tag) const {
114         try {
115             static V v1 (N, N), v2 (N, N), v3 (N, N);
116             initialize_vector (v1);
117             initialize_vector (v2);
118             boost::timer t;
119             for (int i = 0; i < runs; ++ i) {
120                 v3 = - (v1 + v2);
121 //                sink_vector (v3);
122             }
123             footer<value_type> () (0, 2 * N, runs, t.elapsed ());
124         }
125         catch (std::exception &e) {
126             std::cout << e.what () << std::endl;
127         }
128     }
operator ()bench_my_vector_add129     void operator () (int runs, fast_tag) const {
130         try {
131             static V v1 (N, N), v2 (N, N), v3 (N, N);
132             initialize_vector (v1);
133             initialize_vector (v2);
134             boost::timer t;
135             for (int i = 0; i < runs; ++ i) {
136                 v3.assign (- (v1 + v2));
137 //                sink_vector (v3);
138             }
139             footer<value_type> () (0, 2 * N, runs, t.elapsed ());
140         }
141         catch (std::exception &e) {
142             std::cout << e.what () << std::endl;
143         }
144     }
145 };
146 template<class V, int N>
147 struct bench_cpp_vector_add {
148     typedef typename V::value_type value_type;
149 
operator ()bench_cpp_vector_add150     void operator () (int runs) const {
151         try {
152             static V v1 (N), v2 (N), v3 (N);
153             initialize_vector (v1);
154             initialize_vector (v2);
155             boost::timer t;
156             for (int i = 0; i < runs; ++ i) {
157                 v3 = - (v1 + v2);
158 //                sink_vector (v3);
159             }
160             footer<value_type> () (0, 2 * N, runs, t.elapsed ());
161         }
162         catch (std::exception &e) {
163             std::cout << e.what () << std::endl;
164         }
165     }
166 };
167 
168 // Benchmark O (n)
169 template<class T, int N>
operator ()(int runs)170 void bench_1<T, N>::operator () (int runs) {
171     header ("bench_1");
172 
173     header ("inner_prod");
174 
175     header ("C array");
176     bench_c_inner_prod<T, N> () (runs);
177 
178 #ifdef USE_MAPPED_VECTOR
179 #ifdef USE_MAP_ARRAY
180     header ("mapped_vector<map_array>");
181     bench_my_inner_prod<ublas::mapped_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs);
182 #endif
183 
184 #ifdef USE_STD_MAP
185     header ("mapped_vector<std::map>");
186     bench_my_inner_prod<ublas::mapped_vector<T, std::map<std::size_t, T> >, N> () (runs);
187 #endif
188 #endif
189 
190 #ifdef USE_COMPRESSED_VECTOR
191     header ("compressed_vector");
192     bench_my_inner_prod<ublas::compressed_vector<T>, N> () (runs);
193 #endif
194 
195 #ifdef USE_COORDINATE_VECTOR
196     header ("coordinate_vector");
197     bench_my_inner_prod<ublas::coordinate_vector<T>, N> () (runs);
198 #endif
199 
200 #ifdef USE_STD_VALARRAY
201     header ("std::valarray");
202     bench_cpp_inner_prod<std::valarray<T>, N> () (runs);
203 #endif
204 
205     header ("vector + vector");
206 
207     header ("C array");
208     bench_c_vector_add<T, N> () (runs);
209 
210 #ifdef USE_MAPPED_VECTOR
211 #ifdef USE_MAP_ARRAY
212     header ("mapped_vector<map_array> safe");
213     bench_my_vector_add<ublas::mapped_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, safe_tag ());
214 
215     header ("maped_vector<map_array> fast");
216     bench_my_vector_add<ublas::mapped_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, fast_tag ());
217 #endif
218 
219 #ifdef USE_STD_MAP
220     header ("mapped_vector<std::map> safe");
221     bench_my_vector_add<ublas::mapped_vector<T, std::map<std::size_t, T> >, N> () (runs, safe_tag ());
222 
223     header ("mapped_vector<std::map> fast");
224     bench_my_vector_add<ublas::mapped_vector<T, std::map<std::size_t, T> >, N> () (runs, fast_tag ());
225 #endif
226 #endif
227 
228 #ifdef USE_COMPRESSED_VECTOR
229 #ifdef USE_MAP_ARRAY
230     header ("compressed_vector safe");
231     bench_my_vector_add<ublas::compressed_vector<T>, N> () (runs, safe_tag ());
232 
233     header ("compressed_vector fast");
234     bench_my_vector_add<ublas::compressed_vector<T>, N> () (runs, fast_tag ());
235 #endif
236 #endif
237 
238 #ifdef USE_COORDINATE_VECTOR
239 #ifdef USE_MAP_ARRAY
240     header ("coordinate_vector safe");
241     bench_my_vector_add<ublas::coordinate_vector<T>, N> () (runs, safe_tag ());
242 
243     header ("coordinate_vector fast");
244     bench_my_vector_add<ublas::coordinate_vector<T>, N> () (runs, fast_tag ());
245 #endif
246 #endif
247 
248 #ifdef USE_STD_VALARRAY
249     header ("std::valarray");
250     bench_cpp_vector_add<std::valarray<T>, N> () (runs);
251 #endif
252 }
253 
254 #ifdef USE_FLOAT
255 template struct bench_1<float, 3>;
256 template struct bench_1<float, 10>;
257 template struct bench_1<float, 30>;
258 template struct bench_1<float, 100>;
259 #endif
260 
261 #ifdef USE_DOUBLE
262 template struct bench_1<double, 3>;
263 template struct bench_1<double, 10>;
264 template struct bench_1<double, 30>;
265 template struct bench_1<double, 100>;
266 #endif
267 
268 #ifdef USE_STD_COMPLEX
269 #ifdef USE_FLOAT
270 template struct bench_1<std::complex<float>, 3>;
271 template struct bench_1<std::complex<float>, 10>;
272 template struct bench_1<std::complex<float>, 30>;
273 template struct bench_1<std::complex<float>, 100>;
274 #endif
275 
276 #ifdef USE_DOUBLE
277 template struct bench_1<std::complex<double>, 3>;
278 template struct bench_1<std::complex<double>, 10>;
279 template struct bench_1<std::complex<double>, 30>;
280 template struct bench_1<std::complex<double>, 100>;
281 #endif
282 #endif
283