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