Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/numeric/ublas/bench2/bench22.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: 16.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_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, N * N);
52            static V v1 (N, N), v2 (N, N);
53            initialize_vector (v1);
54            initialize_vector (v2);
55            boost::timer t;
56            for (int i = 0; i < runs; ++ i) {
57                m = - ublas::outer_prod (v1, v2);
58//                sink_matrix (m);
59            }
60            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
61        }
62        catch (std::exception &e) {
63            std::cout << e.what () << std::endl;
64        }
65    }
66    void operator () (int runs, fast_tag) const {
67        try {
68            static M m (N, N, N * N);
69            static V v1 (N, N), v2 (N, N);
70            initialize_vector (v1);
71            initialize_vector (v2);
72            boost::timer t;
73            for (int i = 0; i < runs; ++ i) {
74                m.assign (- ublas::outer_prod (v1, v2));
75//                sink_matrix (m);
76            }
77            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
78        }
79        catch (std::exception &e) {
80            std::cout << e.what () << std::endl;
81        }
82    }
83};
84template<class M, class V, int N>
85struct bench_cpp_outer_prod {
86    typedef typename M::value_type value_type;
87
88    void operator () (int runs) const {
89        try {
90            static M m (N * N);
91            static V v1 (N), v2 (N);
92            initialize_vector (v1);
93            initialize_vector (v2);
94            boost::timer t;
95            for (int i = 0; i < runs; ++ i) {
96                for (int j = 0; j < N; ++ j) {
97                    for (int k = 0; k < N; ++ k) {
98                        m [N * j + k] = - v1 [j] * v2 [k];
99                    }
100                }
101//                sink_vector (m);
102            }
103            footer<value_type> () (N * N, N * N, runs, t.elapsed ());
104        }
105        catch (std::exception &e) {
106            std::cout << e.what () << std::endl;
107        }
108    }
109};
110
111template<class T, int N>
112struct bench_c_matrix_vector_prod {
113    typedef T value_type;
114
115    void operator () (int runs) const {
116        try {
117            static typename c_matrix_traits<T, N, N>::type m;
118            static typename c_vector_traits<T, N>::type v1, v2;
119            initialize_c_matrix<T, N, N> () (m);
120            initialize_c_vector<T, N> () (v1);
121            boost::timer t;
122            for (int i = 0; i < runs; ++ i) {
123                for (int j = 0; j < N; ++ j) {
124                    v2 [j] = 0;
125                    for (int k = 0; k < N; ++ k) {
126                        v2 [j] += m [j] [k] * v1 [k];
127                    }
128                }
129//                sink_c_vector<T, N> () (v2);
130            }
131            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
132        }
133        catch (std::exception &e) {
134            std::cout << e.what () << std::endl;
135        }
136    }
137};
138template<class M, class V, int N>
139struct bench_my_matrix_vector_prod {
140    typedef typename M::value_type value_type;
141
142    void operator () (int runs, safe_tag) const {
143        try {
144            static M m (N, N, N * N);
145            static V v1 (N, N), v2 (N, N);
146            initialize_matrix (m);
147            initialize_vector (v1);
148            boost::timer t;
149            for (int i = 0; i < runs; ++ i) {
150                v2 = ublas::prod (m, v1);
151//                sink_vector (v2);
152            }
153            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
154        }
155        catch (std::exception &e) {
156            std::cout << e.what () << std::endl;
157        }
158    }
159    void operator () (int runs, fast_tag) const {
160        try {
161            static M m (N, N, N * N);
162            static V v1 (N, N), v2 (N, N);
163            initialize_matrix (m);
164            initialize_vector (v1);
165            boost::timer t;
166            for (int i = 0; i < runs; ++ i) {
167                v2.assign (ublas::prod (m, v1));
168//                sink_vector (v2);
169            }
170            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
171        }
172        catch (std::exception &e) {
173            std::cout << e.what () << std::endl;
174        }
175    }
176};
177template<class M, class V, int N>
178struct bench_cpp_matrix_vector_prod {
179    typedef typename M::value_type value_type;
180
181    void operator () (int runs) const {
182        try {
183            static M m (N * N);
184            static V v1 (N), v2 (N);
185            initialize_vector (m);
186            initialize_vector (v1);
187            boost::timer t;
188            for (int i = 0; i < runs; ++ i) {
189                for (int j = 0; j < N; ++ j) {
190                    std::valarray<value_type> row (m [std::slice (N * j, N, 1)]);
191                    v2 [j] = (row * v1).sum ();
192                }
193//                sink_vector (v2);
194            }
195            footer<value_type> () (N * N, N * (N - 1), runs, t.elapsed ());
196        }
197        catch (std::exception &e) {
198            std::cout << e.what () << std::endl;
199        }
200    }
201};
202
203template<class T, int N>
204struct bench_c_matrix_add {
205    typedef T value_type;
206
207    void operator () (int runs) const {
208        try {
209            static typename c_matrix_traits<T, N, N>::type m1, m2, m3;
210            initialize_c_matrix<T, N, N> () (m1);
211            initialize_c_matrix<T, N, N> () (m2);
212            boost::timer t;
213            for (int i = 0; i < runs; ++ i) {
214                for (int j = 0; j < N; ++ j) {
215                    for (int k = 0; k < N; ++ k) {
216                        m3 [j] [k] = - (m1 [j] [k] + m2 [j] [k]);
217                    }
218                }
219//                sink_c_matrix<T, N, N> () (m3);
220            }
221            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
222        }
223        catch (std::exception &e) {
224            std::cout << e.what () << std::endl;
225        }
226    }
227};
228template<class M, int N>
229struct bench_my_matrix_add {
230    typedef typename M::value_type value_type;
231
232    void operator () (int runs, safe_tag) const {
233        try {
234            static M m1 (N, N, N * N), m2 (N, N, N * N), m3 (N, N, N * N);
235            initialize_matrix (m1);
236            initialize_matrix (m2);
237            boost::timer t;
238            for (int i = 0; i < runs; ++ i) {
239                m3 = - (m1 + m2);
240//                sink_matrix (m3);
241            }
242            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
243        }
244        catch (std::exception &e) {
245            std::cout << e.what () << std::endl;
246        }
247    }
248    void operator () (int runs, fast_tag) const {
249        try {
250            static M m1 (N, N, N * N), m2 (N, N, N * N), m3 (N, N, N * N);
251            initialize_matrix (m1);
252            initialize_matrix (m2);
253            boost::timer t;
254            for (int i = 0; i < runs; ++ i) {
255                m3.assign (- (m1 + m2));
256//                sink_matrix (m3);
257            }
258            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
259        }
260        catch (std::exception &e) {
261            std::cout << e.what () << std::endl;
262        }
263    }
264};
265template<class M, int N>
266struct bench_cpp_matrix_add {
267    typedef typename M::value_type value_type;
268
269    void operator () (int runs) const {
270        try {
271            static M m1 (N * N), m2 (N * N), m3 (N * N);
272            initialize_vector (m1);
273            initialize_vector (m2);
274            boost::timer t;
275            for (int i = 0; i < runs; ++ i) {
276                m3 = - (m1 + m2);
277//                sink_vector (m3);
278            }
279            footer<value_type> () (0, 2 * N * N, runs, t.elapsed ());
280        }
281        catch (std::exception &e) {
282            std::cout << e.what () << std::endl;
283        }
284    }
285};
286
287// Benchmark O (n ^ 2)
288template<class T, int N>
289void bench_2<T, N>::operator () (int runs) {
290    header ("bench_2");
291
292    header ("outer_prod");
293
294    header ("C array");
295    bench_c_outer_prod<T, N> () (runs);
296
297#ifdef USE_SPARSE_MATRIX
298#ifdef USE_MAP_ARRAY
299    header ("sparse_matrix<map_array>, sparse_vector<map_array> safe");
300    bench_my_outer_prod<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >,
301                        ublas::sparse_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, safe_tag ());
302
303    header ("sparse_matrix<map_array>, sparse_vector<map_array> fast");
304    bench_my_outer_prod<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >,
305                        ublas::sparse_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, fast_tag ());
306#endif
307
308#ifdef USE_STD_MAP
309    header ("sparse_matrix<std::map>, sparse_vector<std::map> safe");
310    bench_my_outer_prod<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >,
311                        ublas::sparse_vector<T, std::map<std::size_t, T> >, N> () (runs, safe_tag ());
312
313    header ("sparse_matrix<std::map>, sparse_vector<std::map> fast");
314    bench_my_outer_prod<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >,
315                        ublas::sparse_vector<T, std::map<std::size_t, T> >, N> () (runs, fast_tag ());
316#endif
317#endif
318
319#ifdef USE_COMPRESSED_MATRIX
320    header ("compressed_matrix, compressed_vector safe");
321    bench_my_outer_prod<ublas::compressed_matrix<T, ublas::row_major>,
322                        ublas::compressed_vector<T>, N> () (runs, safe_tag ());
323
324    header ("compressed_matrix, compressed_vector fast");
325    bench_my_outer_prod<ublas::compressed_matrix<T, ublas::row_major>,
326                        ublas::compressed_vector<T>, N> () (runs, fast_tag ());
327#endif
328
329#ifdef USE_COORDINATE_MATRIX
330    header ("coordinate_matrix, coordinate_vector safe");
331    bench_my_outer_prod<ublas::coordinate_matrix<T, ublas::row_major>,
332                        ublas::coordinate_vector<T>, N> () (runs, safe_tag ());
333
334    header ("coordinate_matrix, coordinate_vector fast");
335    bench_my_outer_prod<ublas::coordinate_matrix<T, ublas::row_major>,
336                        ublas::coordinate_vector<T>, N> () (runs, fast_tag ());
337#endif
338
339#ifdef USE_STD_VALARRAY
340    header ("std::valarray");
341    bench_cpp_outer_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
342#endif
343
344    header ("prod (matrix, vector)");
345
346    header ("C array");
347    bench_c_matrix_vector_prod<T, N> () (runs);
348
349#ifdef USE_SPARSE_MATRIX
350#ifdef USE_MAP_ARRAY
351    header ("sparse_matrix<map_array>, sparse_vector<map_array> safe");
352    bench_my_matrix_vector_prod<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >,
353                                ublas::sparse_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, safe_tag ());
354
355    header ("sparse_matrix<map_array>, sparse_vector<map_array> fast");
356    bench_my_matrix_vector_prod<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >,
357                                ublas::sparse_vector<T, ublas::map_array<std::size_t, T> >, N> () (runs, fast_tag ());
358#endif
359
360#ifdef USE_STD_MAP
361    header ("sparse_matrix<std::map>, sparse_vector<std::map> safe");
362    bench_my_matrix_vector_prod<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >,
363                                ublas::sparse_vector<T, std::map<std::size_t, T> >, N> () (runs, safe_tag ());
364
365    header ("sparse_matrix<std::map>, sparse_vector<std::map> fast");
366    bench_my_matrix_vector_prod<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >,
367                                ublas::sparse_vector<T, std::map<std::size_t, T> >, N> () (runs, fast_tag ());
368#endif
369#endif
370
371#ifdef USE_COMPRESSED_MATRIX
372    header ("compressed_matrix, compressed_vector safe");
373    bench_my_matrix_vector_prod<ublas::compressed_matrix<T, ublas::row_major>,
374                                ublas::compressed_vector<T>, N> () (runs, safe_tag ());
375
376    header ("compressed_matrix, compressed_vector fast");
377    bench_my_matrix_vector_prod<ublas::compressed_matrix<T, ublas::row_major>,
378                                ublas::compressed_vector<T>, N> () (runs, fast_tag ());
379#endif
380
381#ifdef USE_COORDINATE_MATRIX
382    header ("coordinate_matrix, coordinate_vector safe");
383    bench_my_matrix_vector_prod<ublas::coordinate_matrix<T, ublas::row_major>,
384                                ublas::coordinate_vector<T>, N> () (runs, safe_tag ());
385
386    header ("coordinate_matrix, coordinate_vector fast");
387    bench_my_matrix_vector_prod<ublas::coordinate_matrix<T, ublas::row_major>,
388                                ublas::coordinate_vector<T>, N> () (runs, fast_tag ());
389#endif
390
391#ifdef USE_STD_VALARRAY
392    header ("std::valarray");
393    bench_cpp_matrix_vector_prod<std::valarray<T>, std::valarray<T>, N> () (runs);
394#endif
395
396    header ("matrix + matrix");
397
398    header ("C array");
399    bench_c_matrix_add<T, N> () (runs);
400
401#ifdef USE_SPARSE_MATRIX
402#ifdef USE_MAP_ARRAY
403    header ("sparse_matrix<map_array> safe");
404    bench_my_matrix_add<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >, N> () (runs, safe_tag ());
405
406    header ("sparse_matrix<map_array> fast");
407    bench_my_matrix_add<ublas::sparse_matrix<T, ublas::row_major, ublas::map_array<std::size_t, T> >, N> () (runs, fast_tag ());
408#endif
409
410#ifdef USE_STD_MAP
411    header ("sparse_matrix<std::map> safe");
412    bench_my_matrix_add<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >, N> () (runs, safe_tag ());
413
414    header ("sparse_matrix<std::map> fast");
415    bench_my_matrix_add<ublas::sparse_matrix<T, ublas::row_major, std::map<std::size_t, T> >, N> () (runs, fast_tag ());
416#endif
417#endif
418
419#ifdef USE_COMPRESSED_MATRIX
420    header ("compressed_matrix safe");
421    bench_my_matrix_add<ublas::compressed_matrix<T, ublas::row_major>, N> () (runs, safe_tag ());
422
423    header ("compressed_matrix fast");
424    bench_my_matrix_add<ublas::compressed_matrix<T, ublas::row_major>, N> () (runs, fast_tag ());
425#endif
426
427#ifdef USE_COORDINATE_MATRIX
428    header ("coordinate_matrix safe");
429    bench_my_matrix_add<ublas::coordinate_matrix<T, ublas::row_major>, N> () (runs, safe_tag ());
430
431    header ("coordinate_matrix fast");
432    bench_my_matrix_add<ublas::coordinate_matrix<T, ublas::row_major>, N> () (runs, fast_tag ());
433#endif
434
435#ifdef USE_STD_VALARRAY
436    header ("std::valarray");
437    bench_cpp_matrix_add<std::valarray<T>, N> () (runs);
438#endif
439}
440
441#ifdef USE_FLOAT
442template struct bench_2<float, 3>;
443template struct bench_2<float, 10>;
444template struct bench_2<float, 30>;
445template struct bench_2<float, 100>;
446#endif
447
448#ifdef USE_DOUBLE
449template struct bench_2<double, 3>;
450template struct bench_2<double, 10>;
451template struct bench_2<double, 30>;
452template struct bench_2<double, 100>;
453#endif
454
455#ifdef USE_STD_COMPLEX
456#ifdef USE_FLOAT
457template struct bench_2<std::complex<float>, 3>;
458template struct bench_2<std::complex<float>, 10>;
459template struct bench_2<std::complex<float>, 30>;
460template struct bench_2<std::complex<float>, 100>;
461#endif
462
463#ifdef USE_DOUBLE
464template struct bench_2<std::complex<double>, 3>;
465template struct bench_2<std::complex<double>, 10>;
466template struct bench_2<std::complex<double>, 30>;
467template struct bench_2<std::complex<double>, 100>;
468#endif
469#endif
Note: See TracBrowser for help on using the repository browser.