Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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