Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / numeric / ublas / bench1 / bench12.cpp
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 "bench1.hpp"
14
15 template<class T, int N>
16 struct bench_c_outer_prod {
17     typedef T value_type;
18
19     void operator () (int runs) const {
20         try {
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);
25             boost::timer t;
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];
30                     }
31                 }
32 //                sink_c_matrix<T, N, N> () (m);
33             }
34             footer<value_type> () (N * N, N * N, runs, t.elapsed ());
35         }
36         catch (std::exception &e) {
37             std::cout << e.what () << std::endl;
38         }
39     }
40 };
41 template<class M, class V, int N>
42 struct bench_my_outer_prod {
43     typedef typename M::value_type value_type;
44
45     void operator () (int runs, safe_tag) const {
46         try {
47             static M m (N, N);
48             static V v1 (N), v2 (N);
49             initialize_vector (v1);
50             initialize_vector (v2);
51             boost::timer t;
52             for (int i = 0; i < runs; ++ i) {
53                 m = - ublas::outer_prod (v1, v2);
54 //                sink_matrix (m);
55             }
56             footer<value_type> () (N * N, N * N, runs, t.elapsed ());
57         }
58         catch (std::exception &e) {
59             std::cout << e.what () << std::endl;
60         }
61     }
62     void operator () (int runs, fast_tag) const {
63         try {
64             static M m (N, N);
65             static V v1 (N), v2 (N);
66             initialize_vector (v1);
67             initialize_vector (v2);
68             boost::timer t;
69             for (int i = 0; i < runs; ++ i) {
70                 m.assign (- ublas::outer_prod (v1, v2));
71 //                sink_matrix (m);
72             }
73             footer<value_type> () (N * N, N * N, runs, t.elapsed ());
74         }
75         catch (std::exception &e) {
76             std::cout << e.what () << std::endl;
77         }
78     }
79 };
80 template<class M, class V, int N>
81 struct bench_cpp_outer_prod {
82     typedef typename M::value_type value_type;
83
84     void operator () (int runs) const {
85         try {
86             static M m (N * N);
87             static V v1 (N), v2 (N);
88             initialize_vector (v1);
89             initialize_vector (v2);
90             boost::timer t;
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];
95                     }
96                 }
97 //                sink_vector (m);
98             }
99             footer<value_type> () (N * N, N * N, runs, t.elapsed ());
100         }
101         catch (std::exception &e) {
102             std::cout << e.what () << std::endl;
103         }
104     }
105 };
106
107 template<class T, int N>
108 struct bench_c_matrix_vector_prod {
109     typedef T value_type;
110
111     void operator () (int runs) const {
112         try {
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);
117             boost::timer t;
118             for (int i = 0; i < runs; ++ i) {
119                 for (int j = 0; j < N; ++ j) {
120                     v2 [j] = 0;
121                     for (int k = 0; k < N; ++ k) {
122                         v2 [j] += m [j] [k] * v1 [k];
123                     }
124                 }
125 //                sink_c_vector<T, N> () (v2);
126             }
127             footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
128         }
129         catch (std::exception &e) {
130             std::cout << e.what () << std::endl;
131         }
132     }
133 };
134 template<class M, class V, int N>
135 struct bench_my_matrix_vector_prod {
136     typedef typename M::value_type value_type;
137
138     void operator () (int runs, safe_tag) const {
139         try {
140             static M m (N, N);
141             static V v1 (N), v2 (N);
142             initialize_matrix (m);
143             initialize_vector (v1);
144             boost::timer t;
145             for (int i = 0; i < runs; ++ i) {
146                 v2 = ublas::prod (m, v1);
147 //                sink_vector (v2);
148             }
149             footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
150         }
151         catch (std::exception &e) {
152             std::cout << e.what () << std::endl;
153         }
154     }
155     void operator () (int runs, fast_tag) const {
156         try {
157             static M m (N, N);
158             static V v1 (N), v2 (N);
159             initialize_matrix (m);
160             initialize_vector (v1);
161             boost::timer t;
162             for (int i = 0; i < runs; ++ i) {
163                 v2.assign (ublas::prod (m, v1));
164 //                sink_vector (v2);
165             }
166             footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
167         }
168         catch (std::exception &e) {
169             std::cout << e.what () << std::endl;
170         }
171     }
172 };
173 template<class M, class V, int N>
174 struct bench_cpp_matrix_vector_prod {
175     typedef typename M::value_type value_type;
176
177     void operator () (int runs) const {
178         try {
179             static M m (N * N);
180             static V v1 (N), v2 (N);
181             initialize_vector (m);
182             initialize_vector (v1);
183             boost::timer t;
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 ();
188                 }
189 //                sink_vector (v2);
190             }
191             footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
192         }
193         catch (std::exception &e) {
194             std::cout << e.what () << std::endl;
195         }
196     }
197 };
198
199 template<class T, int N>
200 struct bench_c_matrix_add {
201     typedef T value_type;
202
203     void operator () (int runs) const {
204         try {
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);
208             boost::timer t;
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]);
213                     }
214                 }
215 //                sink_c_matrix<T, N, N> () (m3);
216             }
217             footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
218         }
219         catch (std::exception &e) {
220             std::cout << e.what () << std::endl;
221         }
222     }
223 };
224 template<class M, int N>
225 struct bench_my_matrix_add {
226     typedef typename M::value_type value_type;
227
228     void operator () (int runs, safe_tag) const {
229         try {
230             static M m1 (N, N), m2 (N, N), m3 (N, N);
231             initialize_matrix (m1);
232             initialize_matrix (m2);
233             boost::timer t;
234             for (int i = 0; i < runs; ++ i) {
235                 m3 = - (m1 + m2);
236 //                sink_matrix (m3);
237             }
238             footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
239         }
240         catch (std::exception &e) {
241             std::cout << e.what () << std::endl;
242         }
243     }
244     void operator () (int runs, fast_tag) const {
245         try {
246             static M m1 (N, N), m2 (N, N), m3 (N, N);
247             initialize_matrix (m1);
248             initialize_matrix (m2);
249             boost::timer t;
250             for (int i = 0; i < runs; ++ i) {
251                 m3.assign (- (m1 + m2));
252 //                sink_matrix (m3);
253             }
254             footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
255         }
256         catch (std::exception &e) {
257             std::cout << e.what () << std::endl;
258         }
259     }
260 };
261 template<class M, int N>
262 struct bench_cpp_matrix_add {
263     typedef typename M::value_type value_type;
264
265     void operator () (int runs) const {
266         try {
267             static M m1 (N * N), m2 (N * N), m3 (N * N);
268             initialize_vector (m1);
269             initialize_vector (m2);
270             boost::timer t;
271             for (int i = 0; i < runs; ++ i) {
272                 m3 = - (m1 + m2);
273 //                sink_vector (m3);
274             }
275             footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
276         }
277         catch (std::exception &e) {
278             std::cout << e.what () << std::endl;
279         }
280     }
281 };
282
283 // Benchmark O (n ^ 2)
284 template<class T, int N>
285 void bench_2<T, N>::operator () (int runs) {
286     header ("bench_2");
287
288     header ("outer_prod");
289
290     header ("C array");
291     bench_c_outer_prod<T, N> () (runs);
292
293 #ifdef USE_C_ARRAY
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 ());
297
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 ());
301 #endif
302
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 ());
307
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 ());
311 #endif
312
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 ());
317
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 ());
321 #endif
322
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 ());
327
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 ());
331 #endif
332
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 ());
337
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 ());
341 #endif
342
343 #ifdef USE_STD_VALARRAY
344     header ("std::valarray");
345     bench_cpp_outer_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
346 #endif
347
348     header ("prod (matrix, vector)");
349
350     header ("C array");
351     bench_c_matrix_vector_prod<T, N> () (runs);
352
353 #ifdef USE_C_ARRAY
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 ());
357
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 ());
361 #endif
362
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 ());
367
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 ());
371 #endif
372
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 ());
377
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 ());
381 #endif
382
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 ());
387
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 ());
391 #endif
392
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 ());
397
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 ());
401 #endif
402
403 #ifdef USE_STD_VALARRAY
404     header ("std::valarray");
405     bench_cpp_matrix_vector_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
406 #endif
407
408     header ("matrix + matrix");
409
410     header ("C array");
411     bench_c_matrix_add<T, N> () (runs);
412
413 #ifdef USE_C_ARRAY
414     header ("c_matrix safe");
415     bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
416
417     header ("c_matrix fast");
418     bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
419 #endif
420
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 ());
424
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 ());
427 #endif
428
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 ());
432
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 ());
435 #endif
436
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 ());
440
441     header ("matrix<std::valarray> fast");
442     bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
443 #endif
444
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 ());
448
449     header ("matrix<std::vector> fast");
450     bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
451 #endif
452
453 #ifdef USE_STD_VALARRAY
454     header ("std::valarray");
455     bench_cpp_matrix_add<std::valarray<T>, N> () (runs);
456 #endif
457 }
458
459 #ifdef USE_FLOAT
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>;
464 #endif
465
466 #ifdef USE_DOUBLE
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>;
471 #endif
472
473 #ifdef USE_STD_COMPLEX
474 #ifdef USE_FLOAT
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>;
479 #endif
480
481 #ifdef USE_DOUBLE
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>;
486 #endif
487 #endif