2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
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)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
15 template<class T, int N>
16 struct bench_c_outer_prod {
19 void operator () (int runs) const {
21 static typename c_matrix_traits<T, N, N>::type m;
22 static typename c_vector_traits<T, N>::type v1, v2;
23 initialize_c_vector<T, N> () (v1);
24 initialize_c_vector<T, N> () (v2);
26 for (int i = 0; i < runs; ++ i) {
27 for (int j = 0; j < N; ++ j) {
28 for (int k = 0; k < N; ++ k) {
29 m [j] [k] = - v1 [j] * v2 [k];
32 // sink_c_matrix<T, N, N> () (m);
34 footer<value_type> () (N * N, N * N, runs, t.elapsed ());
36 catch (std::exception &e) {
37 std::cout << e.what () << std::endl;
41 template<class M, class V, int N>
42 struct bench_my_outer_prod {
43 typedef typename M::value_type value_type;
45 void operator () (int runs, safe_tag) const {
48 static V v1 (N), v2 (N);
49 initialize_vector (v1);
50 initialize_vector (v2);
52 for (int i = 0; i < runs; ++ i) {
53 m = - ublas::outer_prod (v1, v2);
56 footer<value_type> () (N * N, N * N, runs, t.elapsed ());
58 catch (std::exception &e) {
59 std::cout << e.what () << std::endl;
62 void operator () (int runs, fast_tag) const {
65 static V v1 (N), v2 (N);
66 initialize_vector (v1);
67 initialize_vector (v2);
69 for (int i = 0; i < runs; ++ i) {
70 m.assign (- ublas::outer_prod (v1, v2));
73 footer<value_type> () (N * N, N * N, runs, t.elapsed ());
75 catch (std::exception &e) {
76 std::cout << e.what () << std::endl;
80 template<class M, class V, int N>
81 struct bench_cpp_outer_prod {
82 typedef typename M::value_type value_type;
84 void operator () (int runs) const {
87 static V v1 (N), v2 (N);
88 initialize_vector (v1);
89 initialize_vector (v2);
91 for (int i = 0; i < runs; ++ i) {
92 for (int j = 0; j < N; ++ j) {
93 for (int k = 0; k < N; ++ k) {
94 m [N * j + k] = - v1 [j] * v2 [k];
99 footer<value_type> () (N * N, N * N, runs, t.elapsed ());
101 catch (std::exception &e) {
102 std::cout << e.what () << std::endl;
107 template<class T, int N>
108 struct bench_c_matrix_vector_prod {
109 typedef T value_type;
111 void operator () (int runs) const {
113 static typename c_matrix_traits<T, N, N>::type m;
114 static typename c_vector_traits<T, N>::type v1, v2;
115 initialize_c_matrix<T, N, N> () (m);
116 initialize_c_vector<T, N> () (v1);
118 for (int i = 0; i < runs; ++ i) {
119 for (int j = 0; j < N; ++ j) {
121 for (int k = 0; k < N; ++ k) {
122 v2 [j] += m [j] [k] * v1 [k];
125 // sink_c_vector<T, N> () (v2);
127 footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
129 catch (std::exception &e) {
130 std::cout << e.what () << std::endl;
134 template<class M, class V, int N>
135 struct bench_my_matrix_vector_prod {
136 typedef typename M::value_type value_type;
138 void operator () (int runs, safe_tag) const {
141 static V v1 (N), v2 (N);
142 initialize_matrix (m);
143 initialize_vector (v1);
145 for (int i = 0; i < runs; ++ i) {
146 v2 = ublas::prod (m, v1);
149 footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
151 catch (std::exception &e) {
152 std::cout << e.what () << std::endl;
155 void operator () (int runs, fast_tag) const {
158 static V v1 (N), v2 (N);
159 initialize_matrix (m);
160 initialize_vector (v1);
162 for (int i = 0; i < runs; ++ i) {
163 v2.assign (ublas::prod (m, v1));
166 footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
168 catch (std::exception &e) {
169 std::cout << e.what () << std::endl;
173 template<class M, class V, int N>
174 struct bench_cpp_matrix_vector_prod {
175 typedef typename M::value_type value_type;
177 void operator () (int runs) const {
180 static V v1 (N), v2 (N);
181 initialize_vector (m);
182 initialize_vector (v1);
184 for (int i = 0; i < runs; ++ i) {
185 for (int j = 0; j < N; ++ j) {
186 std::valarray<value_type> row (m [std::slice (N * j, N, 1)]);
187 v2 [j] = (row * v1).sum ();
191 footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
193 catch (std::exception &e) {
194 std::cout << e.what () << std::endl;
199 template<class T, int N>
200 struct bench_c_matrix_add {
201 typedef T value_type;
203 void operator () (int runs) const {
205 static typename c_matrix_traits<T, N, N>::type m1, m2, m3;
206 initialize_c_matrix<T, N, N> () (m1);
207 initialize_c_matrix<T, N, N> () (m2);
209 for (int i = 0; i < runs; ++ i) {
210 for (int j = 0; j < N; ++ j) {
211 for (int k = 0; k < N; ++ k) {
212 m3 [j] [k] = - (m1 [j] [k] + m2 [j] [k]);
215 // sink_c_matrix<T, N, N> () (m3);
217 footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
219 catch (std::exception &e) {
220 std::cout << e.what () << std::endl;
224 template<class M, int N>
225 struct bench_my_matrix_add {
226 typedef typename M::value_type value_type;
228 void operator () (int runs, safe_tag) const {
230 static M m1 (N, N), m2 (N, N), m3 (N, N);
231 initialize_matrix (m1);
232 initialize_matrix (m2);
234 for (int i = 0; i < runs; ++ i) {
238 footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
240 catch (std::exception &e) {
241 std::cout << e.what () << std::endl;
244 void operator () (int runs, fast_tag) const {
246 static M m1 (N, N), m2 (N, N), m3 (N, N);
247 initialize_matrix (m1);
248 initialize_matrix (m2);
250 for (int i = 0; i < runs; ++ i) {
251 m3.assign (- (m1 + m2));
254 footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
256 catch (std::exception &e) {
257 std::cout << e.what () << std::endl;
261 template<class M, int N>
262 struct bench_cpp_matrix_add {
263 typedef typename M::value_type value_type;
265 void operator () (int runs) const {
267 static M m1 (N * N), m2 (N * N), m3 (N * N);
268 initialize_vector (m1);
269 initialize_vector (m2);
271 for (int i = 0; i < runs; ++ i) {
275 footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
277 catch (std::exception &e) {
278 std::cout << e.what () << std::endl;
283 // Benchmark O (n ^ 2)
284 template<class T, int N>
285 void bench_2<T, N>::operator () (int runs) {
288 header ("outer_prod");
291 bench_c_outer_prod<T, N> () (runs);
294 header ("c_matrix, c_vector safe");
295 bench_my_outer_prod<ublas::c_matrix<T, N, N>,
296 ublas::c_vector<T, N>, N> () (runs, safe_tag ());
298 header ("c_matrix, c_vector fast");
299 bench_my_outer_prod<ublas::c_matrix<T, N, N>,
300 ublas::c_vector<T, N>, N> () (runs, fast_tag ());
303 #ifdef USE_BOUNDED_ARRAY
304 header ("matrix<bounded_array>, vector<bounded_array> safe");
305 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
306 ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, safe_tag ());
308 header ("matrix<bounded_array>, vector<bounded_array> fast");
309 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
310 ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, fast_tag ());
313 #ifdef USE_UNBOUNDED_ARRAY
314 header ("matrix<unbounded_array>, vector<unbounded_array> safe");
315 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
316 ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
318 header ("matrix<unbounded_array>, vector<unbounded_array> fast");
319 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
320 ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
323 #ifdef USE_STD_VALARRAY
324 header ("matrix<std::valarray>, vector<std::valarray> safe");
325 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
326 ublas::vector<T, std::valarray<T> >, N> () (runs, safe_tag ());
328 header ("matrix<std::valarray>, vector<std::valarray> fast");
329 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
330 ublas::vector<T, std::valarray<T> >, N> () (runs, fast_tag ());
333 #ifdef USE_STD_VECTOR
334 header ("matrix<std::vector>, vector<std::vector> safe");
335 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
336 ublas::vector<T, std::vector<T> >, N> () (runs, safe_tag ());
338 header ("matrix<std::vector>, vector<std::vector> fast");
339 bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
340 ublas::vector<T, std::vector<T> >, N> () (runs, fast_tag ());
343 #ifdef USE_STD_VALARRAY
344 header ("std::valarray");
345 bench_cpp_outer_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
348 header ("prod (matrix, vector)");
351 bench_c_matrix_vector_prod<T, N> () (runs);
354 header ("c_matrix, c_vector safe");
355 bench_my_matrix_vector_prod<ublas::c_matrix<T, N, N>,
356 ublas::c_vector<T, N>, N> () (runs, safe_tag ());
358 header ("c_matrix, c_vector fast");
359 bench_my_matrix_vector_prod<ublas::c_matrix<T, N, N>,
360 ublas::c_vector<T, N>, N> () (runs, fast_tag ());
363 #ifdef USE_BOUNDED_ARRAY
364 header ("matrix<bounded_array>, vector<bounded_array> safe");
365 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
366 ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, safe_tag ());
368 header ("matrix<bounded_array>, vector<bounded_array> fast");
369 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
370 ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, fast_tag ());
373 #ifdef USE_UNBOUNDED_ARRAY
374 header ("matrix<unbounded_array>, vector<unbounded_array> safe");
375 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
376 ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
378 header ("matrix<unbounded_array>, vector<unbounded_array> fast");
379 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
380 ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
383 #ifdef USE_STD_VALARRAY
384 header ("matrix<std::valarray>, vector<std::valarray> safe");
385 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
386 ublas::vector<T, std::valarray<T> >, N> () (runs, safe_tag ());
388 header ("matrix<std::valarray>, vector<std::valarray> fast");
389 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
390 ublas::vector<T, std::valarray<T> >, N> () (runs, fast_tag ());
393 #ifdef USE_STD_VECTOR
394 header ("matrix<std::vector>, vector<std::vector> safe");
395 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
396 ublas::vector<T, std::vector<T> >, N> () (runs, safe_tag ());
398 header ("matrix<std::vector>, vector<std::vector> fast");
399 bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
400 ublas::vector<T, std::vector<T> >, N> () (runs, fast_tag ());
403 #ifdef USE_STD_VALARRAY
404 header ("std::valarray");
405 bench_cpp_matrix_vector_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
408 header ("matrix + matrix");
411 bench_c_matrix_add<T, N> () (runs);
414 header ("c_matrix safe");
415 bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
417 header ("c_matrix fast");
418 bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
421 #ifdef USE_BOUNDED_ARRAY
422 header ("matrix<bounded_array> safe");
423 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ());
425 header ("matrix<bounded_array> fast");
426 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ());
429 #ifdef USE_UNBOUNDED_ARRAY
430 header ("matrix<unbounded_array> safe");
431 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
433 header ("matrix<unbounded_array> fast");
434 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
437 #ifdef USE_STD_VALARRAY
438 header ("matrix<std::valarray> safe");
439 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ());
441 header ("matrix<std::valarray> fast");
442 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
445 #ifdef USE_STD_VECTOR
446 header ("matrix<std::vector> safe");
447 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ());
449 header ("matrix<std::vector> fast");
450 bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
453 #ifdef USE_STD_VALARRAY
454 header ("std::valarray");
455 bench_cpp_matrix_add<std::valarray<T>, N> () (runs);
460 template struct bench_2<float, 3>;
461 template struct bench_2<float, 10>;
462 template struct bench_2<float, 30>;
463 template struct bench_2<float, 100>;
467 template struct bench_2<double, 3>;
468 template struct bench_2<double, 10>;
469 template struct bench_2<double, 30>;
470 template struct bench_2<double, 100>;
473 #ifdef USE_STD_COMPLEX
475 template struct bench_2<std::complex<float>, 3>;
476 template struct bench_2<std::complex<float>, 10>;
477 template struct bench_2<std::complex<float>, 30>;
478 template struct bench_2<std::complex<float>, 100>;
482 template struct bench_2<std::complex<double>, 3>;
483 template struct bench_2<std::complex<double>, 10>;
484 template struct bench_2<std::complex<double>, 30>;
485 template struct bench_2<std::complex<double>, 100>;