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