blob: 9b8e1070e69b19f9d9a15cdb466776bcdf57cbea [file] [log] [blame]
Brian Silvermandc6866b2018-08-05 00:18:23 -07001//
2// Copyright (c) 2000-2002
3// Joerg Walter, Mathias Koch
4//
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)
8//
9// The authors gratefully acknowledge the support of
10// GeNeSys mbH & Co. KG in producing this work.
11//
12
13#include "bench3.hpp"
14
15template<class T, int N>
16struct bench_c_matrix_prod {
17 typedef T value_type;
18
19 void operator () (int runs) const {
20 try {
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);
24 boost::timer t;
25 for (int i = 0; i < runs; ++ i) {
26 for (int j = 0; j < N; ++ j) {
27 for (int k = 0; k < N; ++ k) {
28 m3 [j] [k] = 0;
29 for (int l = 0; l < N; ++ l) {
30 m3 [j] [k] += m1 [j] [l] * m2 [l] [k];
31 }
32 }
33 }
34// sink_c_matrix<T, N, N> () (m3);
35 }
36 footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ());
37 }
38 catch (std::exception &e) {
39 std::cout << e.what () << std::endl;
40 }
41 }
42};
43template<class M, int N>
44struct bench_my_matrix_prod {
45 typedef typename M::value_type value_type;
46
47 void operator () (int runs, safe_tag) const {
48 try {
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);
55 boost::timer t;
56 for (int i = 0; i < runs; ++ i) {
57 mr3 = ublas::prod (mr1, mr2);
58// sink_matrix (mr3);
59 }
60 footer<value_type> () (N * N * N, N * N * (N - 1), 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 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);
74 boost::timer t;
75 for (int i = 0; i < runs; ++ i) {
76 mr3.assign (ublas::prod (mr1, mr2));
77// sink_matrix (mr3);
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_C_ARRAY
125 header ("c_matrix safe");
126 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ());
127
128 header ("c_matrix fast");
129 bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ());
130#endif
131
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 ());
135
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 ());
138#endif
139
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 ());
143
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 ());
146#endif
147
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 ());
151
152 header ("matrix<std::valarray> fast");
153 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ());
154#endif
155
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 ());
159
160 header ("matrix<std::vector> fast");
161 bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ());
162#endif
163
164#ifdef USE_STD_VALARRAY
165 header ("std::valarray");
166 bench_cpp_matrix_prod<std::valarray<T>, N> () (runs);
167#endif
168}
169
170#ifdef USE_FLOAT
171template struct bench_3<float, 3>;
172template struct bench_3<float, 10>;
173template struct bench_3<float, 30>;
174template struct bench_3<float, 100>;
175#endif
176
177#ifdef USE_DOUBLE
178template struct bench_3<double, 3>;
179template struct bench_3<double, 10>;
180template struct bench_3<double, 30>;
181template struct bench_3<double, 100>;
182#endif
183
184#ifdef USE_STD_COMPLEX
185#ifdef USE_FLOAT
186template struct bench_3<std::complex<float>, 3>;
187template struct bench_3<std::complex<float>, 10>;
188template struct bench_3<std::complex<float>, 30>;
189template struct bench_3<std::complex<float>, 100>;
190#endif
191
192#ifdef USE_DOUBLE
193template struct bench_3<std::complex<double>, 3>;
194template struct bench_3<std::complex<double>, 10>;
195template struct bench_3<std::complex<double>, 30>;
196template struct bench_3<std::complex<double>, 100>;
197#endif
198#endif