Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/numeric/ublas/bench3/bench32.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 17.9 KB
Line 
1//
2//  Copyright (c) 2000-2002
3//  Joerg Walter, Mathias Koch
4//
5//  Permission to use, copy, modify, distribute and sell this software
6//  and its documentation for any purpose is hereby granted without fee,
7//  provided that the above copyright notice appear in all copies and
8//  that both that copyright notice and this permission notice appear
9//  in supporting documentation.  The authors make no representations
10//  about the suitability of this software for any purpose.
11//  It is provided "as is" without express or implied warranty.
12//
13//  The authors gratefully acknowledge the support of
14//  GeNeSys mbH & Co. KG in producing this work.
15//
16
17#include "bench3.hpp"
18
19template<class T, int N>
20struct bench_c_outer_prod {
21    typedef T value_type;
22
23    void operator () (int runs) const {
24        try {
25            static typename c_matrix_traits<T, N, N>::type m;
26            static typename c_vector_traits<T, N>::type v1, v2;
27            initialize_c_vector<T, N> () (v1);
28            initialize_c_vector<T, N> () (v2);
29            boost::timer t;
30            for (int i = 0; i < runs; ++ i) {
31                for (int j = 0; j < N; ++ j) {
32                    for (int k = 0; k < N; ++ k) {
33                        m [j] [k] = - v1 [j] * v2 [k];
34                    }
35                }
36//                sink_c_matrix<T, N, N> () (m);
37            }
38            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
39        }
40        catch (std::exception &e) {
41            std::cout << e.what () << std::endl;
42        }
43    }
44};
45template<class M, class V, int N>
46struct bench_my_outer_prod {
47    typedef typename M::value_type value_type;
48
49    void operator () (int runs, safe_tag) const {
50        try {
51            static M m (N, N);
52            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
53            static V v1 (N), v2 (N);
54            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
55                                   vr2 (v2, ublas::range (0, N));
56            initialize_vector (vr1);
57            initialize_vector (vr2);
58            boost::timer t;
59            for (int i = 0; i < runs; ++ i) {
60                mr = - ublas::outer_prod (vr1, vr2);
61//                sink_matrix (mr);
62            }
63            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
64        }
65        catch (std::exception &e) {
66            std::cout << e.what () << std::endl;
67        }
68    }
69    void operator () (int runs, fast_tag) const {
70        try {
71            static M m (N, N);
72            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
73            static V v1 (N), v2 (N);
74            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
75                                   vr2 (v2, ublas::range (0, N));
76            initialize_vector (vr1);
77            initialize_vector (vr2);
78            boost::timer t;
79            for (int i = 0; i < runs; ++ i) {
80                mr.assign (- ublas::outer_prod (vr1, vr2));
81//                sink_matrix (mr);
82            }
83            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
84        }
85        catch (std::exception &e) {
86            std::cout << e.what () << std::endl;
87        }
88    }
89};
90template<class M, class V, int N>
91struct bench_cpp_outer_prod {
92    typedef typename M::value_type value_type;
93
94    void operator () (int runs) const {
95        try {
96            static M m (N * N);
97            static V v1 (N), v2 (N);
98            initialize_vector (v1);
99            initialize_vector (v2);
100            boost::timer t;
101            for (int i = 0; i < runs; ++ i) {
102                for (int j = 0; j < N; ++ j) {
103                    for (int k = 0; k < N; ++ k) {
104                        m [N * j + k] = - v1 [j] * v2 [k];
105                    }
106                }
107//                sink_vector (m);
108            }
109            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
110        }
111        catch (std::exception &e) {
112            std::cout << e.what () << std::endl;
113        }
114    }
115};
116
117template<class T, int N>
118struct bench_c_matrix_vector_prod {
119    typedef T value_type;
120
121    void operator () (int runs) const {
122        try {
123            static typename c_matrix_traits<T, N, N>::type m;
124            static typename c_vector_traits<T, N>::type v1, v2;
125            initialize_c_matrix<T, N, N> () (m);
126            initialize_c_vector<T, N> () (v1);
127            boost::timer t;
128            for (int i = 0; i < runs; ++ i) {
129                for (int j = 0; j < N; ++ j) {
130                    v2 [j] = 0;
131                    for (int k = 0; k < N; ++ k) {
132                        v2 [j] += m [j] [k] * v1 [k];
133                    }
134                }
135//                sink_c_vector<T, N> () (v2);
136            }
137            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
138        }
139        catch (std::exception &e) {
140            std::cout << e.what () << std::endl;
141        }
142    }
143};
144template<class M, class V, int N>
145struct bench_my_matrix_vector_prod {
146    typedef typename M::value_type value_type;
147
148    void operator () (int runs, safe_tag) const {
149        try {
150            static M m (N, N);
151            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
152            static V v1 (N), v2 (N);
153            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
154                                   vr2 (v2, ublas::range (0, N));
155            initialize_matrix (mr);
156            initialize_vector (vr1);
157            boost::timer t;
158            for (int i = 0; i < runs; ++ i) {
159                vr2 = ublas::prod (mr, vr1);
160//                sink_vector (vr2);
161            }
162            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
163        }
164        catch (std::exception &e) {
165            std::cout << e.what () << std::endl;
166        }
167    }
168    void operator () (int runs, fast_tag) const {
169        try {
170            static M m (N, N);
171            ublas::matrix_range<M> mr (m, ublas::range (0, N), ublas::range (0, N));
172            static V v1 (N), v2 (N);
173            ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
174                                   vr2 (v2, ublas::range (0, N));
175            initialize_matrix (mr);
176            initialize_vector (vr1);
177            boost::timer t;
178            for (int i = 0; i < runs; ++ i) {
179                vr2.assign (ublas::prod (mr, vr1));
180//                sink_vector (vr2);
181            }
182            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
183        }
184        catch (std::exception &e) {
185            std::cout << e.what () << std::endl;
186        }
187    }
188};
189template<class M, class V, int N>
190struct bench_cpp_matrix_vector_prod {
191    typedef typename M::value_type value_type;
192
193    void operator () (int runs) const {
194        try {
195            static M m (N * N);
196            static V v1 (N), v2 (N);
197            initialize_vector (m);
198            initialize_vector (v1);
199            boost::timer t;
200            for (int i = 0; i < runs; ++ i) {
201                for (int j = 0; j < N; ++ j) {
202                    std::valarray<value_type> row (m [std::slice (N * j, N, 1)]);
203                    v2 [j] = (row * v1).sum ();
204                }
205//                sink_vector (v2);
206            }
207            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
208        }
209        catch (std::exception &e) {
210            std::cout << e.what () << std::endl;
211        }
212    }
213};
214
215template<class T, int N>
216struct bench_c_matrix_add {
217    typedef T value_type;
218
219    void operator () (int runs) const {
220        try {
221            static typename c_matrix_traits<T, N, N>::type m1, m2, m3;
222            initialize_c_matrix<T, N, N> () (m1);
223            initialize_c_matrix<T, N, N> () (m2);
224            boost::timer t;
225            for (int i = 0; i < runs; ++ i) {
226                for (int j = 0; j < N; ++ j) {
227                    for (int k = 0; k < N; ++ k) {
228                        m3 [j] [k] = - (m1 [j] [k] + m2 [j] [k]);
229                    }
230                }
231//                sink_c_matrix<T, N, N> () (m3);
232            }
233            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
234        }
235        catch (std::exception &e) {
236            std::cout << e.what () << std::endl;
237        }
238    }
239};
240template<class M, int N>
241struct bench_my_matrix_add {
242    typedef typename M::value_type value_type;
243
244    void operator () (int runs, safe_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 = - (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    void operator () (int runs, fast_tag) const {
261        try {
262            static M m1 (N, N), m2 (N, N), m3 (N, N);
263            initialize_matrix (m1);
264            initialize_matrix (m2);
265            boost::timer t;
266            for (int i = 0; i < runs; ++ i) {
267                m3.assign (- (m1 + m2));
268//                sink_matrix (m3);
269            }
270            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
271        }
272        catch (std::exception &e) {
273            std::cout << e.what () << std::endl;
274        }
275    }
276};
277template<class M, int N>
278struct bench_cpp_matrix_add {
279    typedef typename M::value_type value_type;
280
281    void operator () (int runs) const {
282        try {
283            static M m1 (N * N), m2 (N * N), m3 (N * N);
284            initialize_vector (m1);
285            initialize_vector (m2);
286            boost::timer t;
287            for (int i = 0; i < runs; ++ i) {
288                m3 = - (m1 + m2);
289//                sink_vector (m3);
290            }
291            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
292        }
293        catch (std::exception &e) {
294            std::cout << e.what () << std::endl;
295        }
296    }
297};
298
299// Benchmark O (n ^ 2)
300template<class T, int N>
301void bench_2<T, N>::operator () (int runs) {
302    header ("bench_2");
303
304    header ("outer_prod");
305
306    header ("C array");
307    bench_c_outer_prod<T, N> () (runs);
308
309#ifdef USE_C_ARRAY
310    header ("c_matrix, c_vector safe");
311    bench_my_outer_prod<ublas::c_matrix<T, N, N>,
312                        ublas::c_vector<T, N>, N> () (runs, safe_tag ());
313
314    header ("c_matrix, c_vector fast");
315    bench_my_outer_prod<ublas::c_matrix<T, N, N>,
316                        ublas::c_vector<T, N>, N> () (runs, fast_tag ());
317#endif
318
319#ifdef USE_BOUNDED_ARRAY
320    header ("matrix<bounded_array>, vector<bounded_array> safe");
321    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
322                        ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, safe_tag ());
323
324    header ("matrix<bounded_array>, vector<bounded_array> fast");
325    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
326                        ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, fast_tag ());
327#endif
328
329#ifdef USE_UNBOUNDED_ARRAY
330    header ("matrix<unbounded_array>, vector<unbounded_array> safe");
331    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
332                        ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
333
334    header ("matrix<unbounded_array>, vector<unbounded_array> fast");
335    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
336                        ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
337#endif
338
339#ifdef USE_STD_VALARRAY
340    header ("matrix<std::valarray>, vector<std::valarray> safe");
341    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
342                        ublas::vector<T, std::valarray<T> >, N> () (runs, safe_tag ());
343
344    header ("matrix<std::valarray>, vector<std::valarray> fast");
345    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
346                        ublas::vector<T, std::valarray<T> >, N> () (runs, fast_tag ());
347#endif
348
349#ifdef USE_STD_VECTOR
350    header ("matrix<std::vector>, vector<std::vector> safe");
351    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
352                        ublas::vector<T, std::vector<T> >, N> () (runs, safe_tag ());
353
354    header ("matrix<std::vector>, vector<std::vector> fast");
355    bench_my_outer_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
356                        ublas::vector<T, std::vector<T> >, N> () (runs, fast_tag ());
357#endif
358
359#ifdef USE_STD_VALARRAY
360    header ("std::valarray");
361    bench_cpp_outer_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
362#endif
363
364    header ("prod (matrix, vector)");
365
366    header ("C array");
367    bench_c_matrix_vector_prod<T, N> () (runs);
368
369#ifdef USE_C_ARRAY
370    header ("c_matrix, c_vector safe");
371    bench_my_matrix_vector_prod<ublas::c_matrix<T, N, N>,
372                                ublas::c_vector<T, N>, N> () (runs, safe_tag ());
373
374    header ("c_matrix, c_vector fast");
375    bench_my_matrix_vector_prod<ublas::c_matrix<T, N, N>,
376                                ublas::c_vector<T, N>, N> () (runs, fast_tag ());
377#endif
378
379#ifdef USE_BOUNDED_ARRAY
380    header ("matrix<bounded_array>, vector<bounded_array> safe");
381    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
382                                ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, safe_tag ());
383
384    header ("matrix<bounded_array>, vector<bounded_array> fast");
385    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >,
386                                ublas::vector<T, ublas::bounded_array<T, N> >, N> () (runs, fast_tag ());
387#endif
388
389#ifdef USE_UNBOUNDED_ARRAY
390    header ("matrix<unbounded_array>, vector<unbounded_array> safe");
391    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
392                                ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
393
394    header ("matrix<unbounded_array>, vector<unbounded_array> fast");
395    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >,
396                                ublas::vector<T, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
397#endif
398
399#ifdef USE_STD_VALARRAY
400    header ("matrix<std::valarray>, vector<std::valarray> safe");
401    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
402                                ublas::vector<T, std::valarray<T> >, N> () (runs, safe_tag ());
403
404    header ("matrix<std::valarray>, vector<std::valarray> fast");
405    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >,
406                                ublas::vector<T, std::valarray<T> >, N> () (runs, fast_tag ());
407#endif
408
409#ifdef USE_STD_VECTOR
410    header ("matrix<std::vector>, vector<std::vector> safe");
411    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
412                                ublas::vector<T, std::vector<T> >, N> () (runs, safe_tag ());
413
414    header ("matrix<std::vector>, vector<std::vector> fast");
415    bench_my_matrix_vector_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >,
416                                ublas::vector<T, std::vector<T> >, N> () (runs, fast_tag ());
417#endif
418
419#ifdef USE_STD_VALARRAY
420    header ("std::valarray");
421    bench_cpp_matrix_vector_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
422#endif
423
424    header ("matrix + matrix");
425
426    header ("C array");
427    bench_c_matrix_add<T, N> () (runs);
428
429#ifdef USE_C_ARRAY
430    header ("c_matrix safe");
431    bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
432
433    header ("c_matrix fast");
434    bench_my_matrix_add<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
435#endif
436
437#ifdef USE_BOUNDED_ARRAY
438    header ("matrix<bounded_array> safe");
439    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ());
440
441    header ("matrix<bounded_array> fast");
442    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ());
443#endif
444
445#ifdef USE_UNBOUNDED_ARRAY
446    header ("matrix<unbounded_array> safe");
447    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
448
449    header ("matrix<unbounded_array> fast");
450    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
451#endif
452
453#ifdef USE_STD_VALARRAY
454    header ("matrix<std::valarray> safe");
455    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ());
456
457    header ("matrix<std::valarray> fast");
458    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
459#endif
460
461#ifdef USE_STD_VECTOR
462    header ("matrix<std::vector> safe");
463    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ());
464
465    header ("matrix<std::vector> fast");
466    bench_my_matrix_add<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
467#endif
468
469#ifdef USE_STD_VALARRAY
470    header ("std::valarray");
471    bench_cpp_matrix_add<std::valarray<T>, N> () (runs);
472#endif
473}
474
475#ifdef USE_FLOAT
476template struct bench_2<float, 3>;
477template struct bench_2<float, 10>;
478template struct bench_2<float, 30>;
479template struct bench_2<float, 100>;
480#endif
481
482#ifdef USE_DOUBLE
483template struct bench_2<double, 3>;
484template struct bench_2<double, 10>;
485template struct bench_2<double, 30>;
486template struct bench_2<double, 100>;
487#endif
488
489#ifdef USE_STD_COMPLEX
490#ifdef USE_FLOAT
491template struct bench_2<std::complex<float>, 3>;
492template struct bench_2<std::complex<float>, 10>;
493template struct bench_2<std::complex<float>, 30>;
494template struct bench_2<std::complex<float>, 100>;
495#endif
496
497#ifdef USE_DOUBLE
498template struct bench_2<std::complex<double>, 3>;
499template struct bench_2<std::complex<double>, 10>;
500template struct bench_2<std::complex<double>, 30>;
501template struct bench_2<std::complex<double>, 100>;
502#endif
503#endif
Note: See TracBrowser for help on using the repository browser.