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