Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/numeric/ublas/bench2/bench21.cpp @ 29

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

updated boost from 1_33_1 to 1_34_1

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