| 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 "bench1.hpp" |
|---|
| 18 | |
|---|
| 19 | template<class T, int N> |
|---|
| 20 | struct bench_c_matrix_prod { |
|---|
| 21 | typedef T value_type; |
|---|
| 22 | |
|---|
| 23 | void operator () (int runs) const { |
|---|
| 24 | try { |
|---|
| 25 | static typename c_matrix_traits<T, N, N>::type m1, m2, m3; |
|---|
| 26 | initialize_c_matrix<T, N, N> () (m1); |
|---|
| 27 | initialize_c_matrix<T, N, N> () (m2); |
|---|
| 28 | boost::timer t; |
|---|
| 29 | for (int i = 0; i < runs; ++ i) { |
|---|
| 30 | for (int j = 0; j < N; ++ j) { |
|---|
| 31 | for (int k = 0; k < N; ++ k) { |
|---|
| 32 | m3 [j] [k] = 0; |
|---|
| 33 | for (int l = 0; l < N; ++ l) { |
|---|
| 34 | m3 [j] [k] += m1 [j] [l] * m2 [l] [k]; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | // sink_c_matrix<T, N, N> () (m3); |
|---|
| 39 | } |
|---|
| 40 | footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); |
|---|
| 41 | } |
|---|
| 42 | catch (std::exception &e) { |
|---|
| 43 | std::cout << e.what () << std::endl; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | }; |
|---|
| 47 | template<class M, int N> |
|---|
| 48 | struct bench_my_matrix_prod { |
|---|
| 49 | typedef typename M::value_type value_type; |
|---|
| 50 | |
|---|
| 51 | void operator () (int runs, safe_tag) const { |
|---|
| 52 | try { |
|---|
| 53 | static M m1 (N, N), m2 (N, N), m3 (N, N); |
|---|
| 54 | initialize_matrix (m1); |
|---|
| 55 | initialize_matrix (m2); |
|---|
| 56 | boost::timer t; |
|---|
| 57 | for (int i = 0; i < runs; ++ i) { |
|---|
| 58 | m3 = ublas::prod (m1, m2); |
|---|
| 59 | // sink_matrix (m3); |
|---|
| 60 | } |
|---|
| 61 | footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); |
|---|
| 62 | } |
|---|
| 63 | catch (std::exception &e) { |
|---|
| 64 | std::cout << e.what () << std::endl; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | void operator () (int runs, fast_tag) const { |
|---|
| 68 | try { |
|---|
| 69 | static M m1 (N, N), m2 (N, N), m3 (N, N); |
|---|
| 70 | initialize_matrix (m1); |
|---|
| 71 | initialize_matrix (m2); |
|---|
| 72 | boost::timer t; |
|---|
| 73 | for (int i = 0; i < runs; ++ i) { |
|---|
| 74 | m3.assign (ublas::prod (m1, m2)); |
|---|
| 75 | // sink_matrix (m3); |
|---|
| 76 | } |
|---|
| 77 | footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); |
|---|
| 78 | } |
|---|
| 79 | catch (std::exception &e) { |
|---|
| 80 | std::cout << e.what () << std::endl; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | }; |
|---|
| 84 | template<class M, int N> |
|---|
| 85 | struct bench_cpp_matrix_prod { |
|---|
| 86 | typedef typename M::value_type value_type; |
|---|
| 87 | |
|---|
| 88 | void operator () (int runs) const { |
|---|
| 89 | try { |
|---|
| 90 | static M m1 (N * N), m2 (N * N), m3 (N * N); |
|---|
| 91 | initialize_vector (m1); |
|---|
| 92 | initialize_vector (m2); |
|---|
| 93 | boost::timer t; |
|---|
| 94 | for (int i = 0; i < runs; ++ i) { |
|---|
| 95 | for (int j = 0; j < N; ++ j) { |
|---|
| 96 | std::valarray<value_type> row (m1 [std::slice (N * j, N, 1)]); |
|---|
| 97 | for (int k = 0; k < N; ++ k) { |
|---|
| 98 | std::valarray<value_type> column (m2 [std::slice (k, N, N)]); |
|---|
| 99 | m3 [N * j + k] = (row * column).sum (); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | // sink_vector (m3); |
|---|
| 103 | } |
|---|
| 104 | footer<value_type> () (N * N * N, N * N * (N - 1), runs, t.elapsed ()); |
|---|
| 105 | } |
|---|
| 106 | catch (std::exception &e) { |
|---|
| 107 | std::cout << e.what () << std::endl; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | // Benchmark O (n ^ 3) |
|---|
| 113 | template<class T, int N> |
|---|
| 114 | void bench_3<T, N>::operator () (int runs) { |
|---|
| 115 | header ("bench_3"); |
|---|
| 116 | |
|---|
| 117 | header ("prod (matrix, matrix)"); |
|---|
| 118 | |
|---|
| 119 | header ("C array"); |
|---|
| 120 | bench_c_matrix_prod<T, N> () (runs); |
|---|
| 121 | |
|---|
| 122 | #ifdef USE_C_ARRAY |
|---|
| 123 | header ("c_matrix safe"); |
|---|
| 124 | bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, safe_tag ()); |
|---|
| 125 | |
|---|
| 126 | header ("c_matrix fast"); |
|---|
| 127 | bench_my_matrix_prod<ublas::c_matrix<T, N, N>, N> () (runs, fast_tag ()); |
|---|
| 128 | #endif |
|---|
| 129 | |
|---|
| 130 | #ifdef USE_BOUNDED_ARRAY |
|---|
| 131 | header ("matrix<bounded_array> safe"); |
|---|
| 132 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, safe_tag ()); |
|---|
| 133 | |
|---|
| 134 | header ("matrix<bounded_array> fast"); |
|---|
| 135 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::bounded_array<T, N * N> >, N> () (runs, fast_tag ()); |
|---|
| 136 | #endif |
|---|
| 137 | |
|---|
| 138 | #ifdef USE_UNBOUNDED_ARRAY |
|---|
| 139 | header ("matrix<unbounded_array> safe"); |
|---|
| 140 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, safe_tag ()); |
|---|
| 141 | |
|---|
| 142 | header ("matrix<unbounded_array> fast"); |
|---|
| 143 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, ublas::unbounded_array<T> >, N> () (runs, fast_tag ()); |
|---|
| 144 | #endif |
|---|
| 145 | |
|---|
| 146 | #ifdef USE_STD_VALARRAY |
|---|
| 147 | header ("matrix<std::valarray> safe"); |
|---|
| 148 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, safe_tag ()); |
|---|
| 149 | |
|---|
| 150 | header ("matrix<std::valarray> fast"); |
|---|
| 151 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::valarray<T> >, N> () (runs, fast_tag ()); |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | #ifdef USE_STD_VECTOR |
|---|
| 155 | header ("matrix<std::vector> safe"); |
|---|
| 156 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, safe_tag ()); |
|---|
| 157 | |
|---|
| 158 | header ("matrix<std::vector> fast"); |
|---|
| 159 | bench_my_matrix_prod<ublas::matrix<T, ublas::row_major, std::vector<T> >, N> () (runs, fast_tag ()); |
|---|
| 160 | #endif |
|---|
| 161 | |
|---|
| 162 | #ifdef USE_STD_VALARRAY |
|---|
| 163 | header ("std::valarray"); |
|---|
| 164 | bench_cpp_matrix_prod<std::valarray<T>, N> () (runs); |
|---|
| 165 | #endif |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | #ifdef USE_FLOAT |
|---|
| 169 | template struct bench_3<float, 3>; |
|---|
| 170 | template struct bench_3<float, 10>; |
|---|
| 171 | template struct bench_3<float, 30>; |
|---|
| 172 | template struct bench_3<float, 100>; |
|---|
| 173 | #endif |
|---|
| 174 | |
|---|
| 175 | #ifdef USE_DOUBLE |
|---|
| 176 | template struct bench_3<double, 3>; |
|---|
| 177 | template struct bench_3<double, 10>; |
|---|
| 178 | template struct bench_3<double, 30>; |
|---|
| 179 | template struct bench_3<double, 100>; |
|---|
| 180 | #endif |
|---|
| 181 | |
|---|
| 182 | #ifdef USE_STD_COMPLEX |
|---|
| 183 | #ifdef USE_FLOAT |
|---|
| 184 | template struct bench_3<std::complex<float>, 3>; |
|---|
| 185 | template struct bench_3<std::complex<float>, 10>; |
|---|
| 186 | template struct bench_3<std::complex<float>, 30>; |
|---|
| 187 | template struct bench_3<std::complex<float>, 100>; |
|---|
| 188 | #endif |
|---|
| 189 | |
|---|
| 190 | #ifdef USE_DOUBLE |
|---|
| 191 | template struct bench_3<std::complex<double>, 3>; |
|---|
| 192 | template struct bench_3<std::complex<double>, 10>; |
|---|
| 193 | template struct bench_3<std::complex<double>, 30>; |
|---|
| 194 | template struct bench_3<std::complex<double>, 100>; |
|---|
| 195 | #endif |
|---|
| 196 | #endif |
|---|