2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
15 template<class T, int N>
16 struct bench_c_matrix_prod {
19 void operator () (int runs) const {
21 static typename c_matrix_traits<T, N, N>::type m1, m2, m3;
22 initialize_c_matrix<T, N, N> () (m1);
23 initialize_c_matrix<T, N, N> () (m2);
25 for (int i = 0; i < runs; ++ i) {
26 for (int j = 0; j < N; ++ j) {
27 for (int k = 0; k < N; ++ k) {
29 for (int l = 0; l < N; ++ l) {
30 m3 [j] [k] += m1 [j] [l] * m2 [l] [k];
34 // sink_c_matrix<T, N, N> () (m3);
36 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
38 catch (std::exception &e) {
39 std::cout << e.what () << std::endl;
43 template<class M, int N>
44 struct bench_my_matrix_prod {
45 typedef typename M::value_type value_type;
47 void operator () (int runs, safe_tag) const {
49 static M m1 (N, N), m2 (N, N), m3 (N, N);
50 ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
51 mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
52 mr3 (m3, ublas::range (0, N), ublas::range (0, N));
53 initialize_matrix (mr1);
54 initialize_matrix (mr2);
56 for (int i = 0; i < runs; ++ i) {
57 mr3 = ublas::prod (mr1, mr2);
60 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
62 catch (std::exception &e) {
63 std::cout << e.what () << std::endl;
66 void operator () (int runs, fast_tag) const {
68 static M m1 (N, N), m2 (N, N), m3 (N, N);
69 ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
70 mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
71 mr3 (m3, ublas::range (0, N), ublas::range (0, N));
72 initialize_matrix (mr1);
73 initialize_matrix (mr2);
75 for (int i = 0; i < runs; ++ i) {
76 mr3.assign (ublas::prod (mr1, mr2));
79 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
81 catch (std::exception &e) {
82 std::cout << e.what () << std::endl;
86 template<class M, int N>
87 struct bench_cpp_matrix_prod {
88 typedef typename M::value_type value_type;
90 void operator () (int runs) const {
92 static M m1 (N * N), m2 (N * N), m3 (N * N);
93 initialize_vector (m1);
94 initialize_vector (m2);
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 ();
106 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
108 catch (std::exception &e) {
109 std::cout << e.what () << std::endl;
114 // Benchmark O (n ^ 3)
115 template<class T, int N>
116 void bench_3<T, N>::operator () (int runs) {
119 header ("prod (matrix, matrix)");
122 bench_c_matrix_prod<T, N> () (runs);
125 header ("c_matrix safe");
126 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
128 header ("c_matrix fast");
129 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
132 #ifdef USE_BOUNDED_ARRAY
133 header ("matrix<bounded_array> safe");
134 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ());
136 header ("matrix<bounded_array> fast");
137 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ());
140 #ifdef USE_UNBOUNDED_ARRAY
141 header ("matrix<unbounded_array> safe");
142 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ());
144 header ("matrix<unbounded_array> fast");
145 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ());
148 #ifdef USE_STD_VALARRAY
149 header ("matrix<std::valarray> safe");
150 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ());
152 header ("matrix<std::valarray> fast");
153 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
156 #ifdef USE_STD_VECTOR
157 header ("matrix<std::vector> safe");
158 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ());
160 header ("matrix<std::vector> fast");
161 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
164 #ifdef USE_STD_VALARRAY
165 header ("std::valarray");
166 bench_cpp_matrix_prod<std::valarray<T>, N> () (runs);
171 template struct bench_3<float, 3>;
172 template struct bench_3<float, 10>;
173 template struct bench_3<float, 30>;
174 template struct bench_3<float, 100>;
178 template struct bench_3<double, 3>;
179 template struct bench_3<double, 10>;
180 template struct bench_3<double, 30>;
181 template struct bench_3<double, 100>;
184 #ifdef USE_STD_COMPLEX
186 template struct bench_3<std::complex<float>, 3>;
187 template struct bench_3<std::complex<float>, 10>;
188 template struct bench_3<std::complex<float>, 30>;
189 template struct bench_3<std::complex<float>, 100>;
193 template struct bench_3<std::complex<double>, 3>;
194 template struct bench_3<std::complex<double>, 10>;
195 template struct bench_3<std::complex<double>, 30>;
196 template struct bench_3<std::complex<double>, 100>;