Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added boost

File size: 7.0 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_matrix_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 m1, m2, m3;
26            initialize_c_matrix<T, N, N> () (m1);
27            initialize_c_matrix<T, N, N> () (m2);
28            boost::timer t;
29            for (int i = 0; i < runs; ++ i) {
30                for (int j = 0; j < N; ++ j) {
31                    for (int k = 0; k < N; ++ k) {
32                        m3 [j] [k] = 0;
33                        for (int l = 0; l < N; ++ l) {
34                            m3 [j] [k] += m1 [j] [l] * m2 [l] [k];
35                        }
36                    }
37                }
38//                sink_c_matrix<T, N, N> () (m3);
39            }
40            footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
41        }
42        catch (std::exception &e) {
43            std::cout << e.what () << std::endl;
44        }
45    }
46};
47template<class M, int N>
48struct bench_my_matrix_prod {
49    typedef typename M::value_type value_type;
50
51    void operator () (int runs, safe_tag) const {
52        try {
53            static M m1 (N, N), m2 (N, N), m3 (N, N);
54            ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
55                                   mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
56                                   mr3 (m3, ublas::range (0, N), ublas::range (0, N));
57            initialize_matrix (mr1);
58            initialize_matrix (mr2);
59            boost::timer t;
60            for (int i = 0; i < runs; ++ i) {
61                mr3 = ublas::prod (mr1, mr2);
62//                sink_matrix (mr3);
63            }
64            footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
65        }
66        catch (std::exception &e) {
67            std::cout << e.what () << std::endl;
68        }
69    }
70    void operator () (int runs, fast_tag) const {
71        try {
72            static M m1 (N, N), m2 (N, N), m3 (N, N);
73            ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
74                                   mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
75                                   mr3 (m3, ublas::range (0, N), ublas::range (0, N));
76            initialize_matrix (mr1);
77            initialize_matrix (mr2);
78            boost::timer t;
79            for (int i = 0; i < runs; ++ i) {
80                mr3.assign (ublas::prod (mr1, mr2));
81//                sink_matrix (mr3);
82            }
83            footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
84        }
85        catch (std::exception &e) {
86            std::cout << e.what () << std::endl;
87        }
88    }
89};
90template<class M, int N>
91struct bench_cpp_matrix_prod {
92    typedef typename M::value_type value_type;
93
94    void operator () (int runs) const {
95        try {
96            static M m1 (N * N), m2 (N * N), m3 (N * N);
97            initialize_vector (m1);
98            initialize_vector (m2);
99            boost::timer t;
100            for (int i = 0; i < runs; ++ i) {
101                for (int j = 0; j < N; ++ j) {
102                    std::valarray<value_type> row (m1 [std::slice (N * j, N, 1)]);
103                    for (int k = 0; k < N; ++ k) {
104                        std::valarray<value_type> column (m2 [std::slice (k, N, N)]);
105                        m3 [N * j + k] = (row * column).sum ();
106                    }
107                }
108//                sink_vector (m3);
109            }
110            footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
111        }
112        catch (std::exception &e) {
113            std::cout << e.what () << std::endl;
114        }
115    }
116};
117
118// Benchmark O (n ^ 3)
119template<class T, int N>
120void bench_3<T, N>::operator () (int runs) {
121    header ("bench_3");
122
123    header ("prod (matrix, matrix)");
124
125    header ("C array");
126    bench_c_matrix_prod<T, N> () (runs);
127
128#ifdef USE_C_ARRAY
129    header ("c_matrix safe");
130    bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
131
132    header ("c_matrix fast");
133    bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
134#endif
135
136#ifdef USE_BOUNDED_ARRAY
137    header ("matrix<bounded_array> safe");
138    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ());
139
140    header ("matrix<bounded_array> fast");
141    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ());
142#endif
143
144#ifdef USE_UNBOUNDED_ARRAY
145    header ("matrix<unbounded_array> safe");
146    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
147
148    header ("matrix<unbounded_array> fast");
149    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
150#endif
151
152#ifdef USE_STD_VALARRAY
153    header ("matrix<std::valarray> safe");
154    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ());
155
156    header ("matrix<std::valarray> fast");
157    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
158#endif
159
160#ifdef USE_STD_VECTOR
161    header ("matrix<std::vector> safe");
162    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ());
163
164    header ("matrix<std::vector> fast");
165    bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
166#endif
167
168#ifdef USE_STD_VALARRAY
169    header ("std::valarray");
170    bench_cpp_matrix_prod<std::valarray<T>, N> () (runs);
171#endif
172}
173
174#ifdef USE_FLOAT
175template struct bench_3<float, 3>;
176template struct bench_3<float, 10>;
177template struct bench_3<float, 30>;
178template struct bench_3<float, 100>;
179#endif
180
181#ifdef USE_DOUBLE
182template struct bench_3<double, 3>;
183template struct bench_3<double, 10>;
184template struct bench_3<double, 30>;
185template struct bench_3<double, 100>;
186#endif
187
188#ifdef USE_STD_COMPLEX
189#ifdef USE_FLOAT
190template struct bench_3<std::complex<float>, 3>;
191template struct bench_3<std::complex<float>, 10>;
192template struct bench_3<std::complex<float>, 30>;
193template struct bench_3<std::complex<float>, 100>;
194#endif
195
196#ifdef USE_DOUBLE
197template struct bench_3<std::complex<double>, 3>;
198template struct bench_3<std::complex<double>, 10>;
199template struct bench_3<std::complex<double>, 30>;
200template struct bench_3<std::complex<double>, 100>;
201#endif
202#endif
Note: See TracBrowser for help on using the repository browser.