operation_blocked.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #ifndef _BOOST_UBLAS_OPERATION_BLOCKED_
  13. #define _BOOST_UBLAS_OPERATION_BLOCKED_
  14. #include <boost/numeric/ublas/traits.hpp>
  15. #include <boost/numeric/ublas/detail/vector_assign.hpp> // indexing_vector_assign
  16. #include <boost/numeric/ublas/detail/matrix_assign.hpp> // indexing_matrix_assign
  17. namespace boost { namespace numeric { namespace ublas {
  18. template<class V, typename V::size_type BS, class E1, class E2>
  19. BOOST_UBLAS_INLINE
  20. V
  21. block_prod (const matrix_expression<E1> &e1,
  22. const vector_expression<E2> &e2) {
  23. typedef V vector_type;
  24. typedef const E1 expression1_type;
  25. typedef const E2 expression2_type;
  26. typedef typename V::size_type size_type;
  27. typedef typename V::value_type value_type;
  28. const size_type block_size = BS;
  29. V v (e1 ().size1 ());
  30. #if BOOST_UBLAS_TYPE_CHECK
  31. vector<value_type> cv (v.size ());
  32. typedef typename type_traits<value_type>::real_type real_type;
  33. real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2));
  34. indexing_vector_assign<scalar_assign> (cv, prod (e1, e2));
  35. #endif
  36. size_type i_size = e1 ().size1 ();
  37. size_type j_size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size ());
  38. for (size_type i_begin = 0; i_begin < i_size; i_begin += block_size) {
  39. size_type i_end = i_begin + (std::min) (i_size - i_begin, block_size);
  40. // FIX: never ignore Martin Weiser's advice ;-(
  41. #ifdef BOOST_UBLAS_NO_CACHE
  42. vector_range<vector_type> v_range (v, range (i_begin, i_end));
  43. #else
  44. // vector<value_type, bounded_array<value_type, block_size> > v_range (i_end - i_begin);
  45. vector<value_type> v_range (i_end - i_begin);
  46. #endif
  47. v_range.assign (zero_vector<value_type> (i_end - i_begin));
  48. for (size_type j_begin = 0; j_begin < j_size; j_begin += block_size) {
  49. size_type j_end = j_begin + (std::min) (j_size - j_begin, block_size);
  50. #ifdef BOOST_UBLAS_NO_CACHE
  51. const matrix_range<expression1_type> e1_range (e1 (), range (i_begin, i_end), range (j_begin, j_end));
  52. const vector_range<expression2_type> e2_range (e2 (), range (j_begin, j_end));
  53. v_range.plus_assign (prod (e1_range, e2_range));
  54. #else
  55. // const matrix<value_type, row_major, bounded_array<value_type, block_size * block_size> > e1_range (project (e1 (), range (i_begin, i_end), range (j_begin, j_end)));
  56. // const vector<value_type, bounded_array<value_type, block_size> > e2_range (project (e2 (), range (j_begin, j_end)));
  57. const matrix<value_type, row_major> e1_range (project (e1 (), range (i_begin, i_end), range (j_begin, j_end)));
  58. const vector<value_type> e2_range (project (e2 (), range (j_begin, j_end)));
  59. v_range.plus_assign (prod (e1_range, e2_range));
  60. #endif
  61. }
  62. #ifndef BOOST_UBLAS_NO_CACHE
  63. project (v, range (i_begin, i_end)).assign (v_range);
  64. #endif
  65. }
  66. #if BOOST_UBLAS_TYPE_CHECK
  67. BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits<real_type>::epsilon () * verrorbound, internal_logic ());
  68. #endif
  69. return v;
  70. }
  71. template<class V, typename V::size_type BS, class E1, class E2>
  72. BOOST_UBLAS_INLINE
  73. V
  74. block_prod (const vector_expression<E1> &e1,
  75. const matrix_expression<E2> &e2) {
  76. typedef V vector_type;
  77. typedef const E1 expression1_type;
  78. typedef const E2 expression2_type;
  79. typedef typename V::size_type size_type;
  80. typedef typename V::value_type value_type;
  81. const size_type block_size = BS;
  82. V v (e2 ().size2 ());
  83. #if BOOST_UBLAS_TYPE_CHECK
  84. vector<value_type> cv (v.size ());
  85. typedef typename type_traits<value_type>::real_type real_type;
  86. real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2));
  87. indexing_vector_assign<scalar_assign> (cv, prod (e1, e2));
  88. #endif
  89. size_type i_size = BOOST_UBLAS_SAME (e1 ().size (), e2 ().size1 ());
  90. size_type j_size = e2 ().size2 ();
  91. for (size_type j_begin = 0; j_begin < j_size; j_begin += block_size) {
  92. size_type j_end = j_begin + (std::min) (j_size - j_begin, block_size);
  93. // FIX: never ignore Martin Weiser's advice ;-(
  94. #ifdef BOOST_UBLAS_NO_CACHE
  95. vector_range<vector_type> v_range (v, range (j_begin, j_end));
  96. #else
  97. // vector<value_type, bounded_array<value_type, block_size> > v_range (j_end - j_begin);
  98. vector<value_type> v_range (j_end - j_begin);
  99. #endif
  100. v_range.assign (zero_vector<value_type> (j_end - j_begin));
  101. for (size_type i_begin = 0; i_begin < i_size; i_begin += block_size) {
  102. size_type i_end = i_begin + (std::min) (i_size - i_begin, block_size);
  103. #ifdef BOOST_UBLAS_NO_CACHE
  104. const vector_range<expression1_type> e1_range (e1 (), range (i_begin, i_end));
  105. const matrix_range<expression2_type> e2_range (e2 (), range (i_begin, i_end), range (j_begin, j_end));
  106. #else
  107. // const vector<value_type, bounded_array<value_type, block_size> > e1_range (project (e1 (), range (i_begin, i_end)));
  108. // const matrix<value_type, column_major, bounded_array<value_type, block_size * block_size> > e2_range (project (e2 (), range (i_begin, i_end), range (j_begin, j_end)));
  109. const vector<value_type> e1_range (project (e1 (), range (i_begin, i_end)));
  110. const matrix<value_type, column_major> e2_range (project (e2 (), range (i_begin, i_end), range (j_begin, j_end)));
  111. #endif
  112. v_range.plus_assign (prod (e1_range, e2_range));
  113. }
  114. #ifndef BOOST_UBLAS_NO_CACHE
  115. project (v, range (j_begin, j_end)).assign (v_range);
  116. #endif
  117. }
  118. #if BOOST_UBLAS_TYPE_CHECK
  119. BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits<real_type>::epsilon () * verrorbound, internal_logic ());
  120. #endif
  121. return v;
  122. }
  123. template<class M, typename M::size_type BS, class E1, class E2>
  124. BOOST_UBLAS_INLINE
  125. M
  126. block_prod (const matrix_expression<E1> &e1,
  127. const matrix_expression<E2> &e2,
  128. row_major_tag) {
  129. typedef M matrix_type;
  130. typedef const E1 expression1_type;
  131. typedef const E2 expression2_type;
  132. typedef typename M::size_type size_type;
  133. typedef typename M::value_type value_type;
  134. const size_type block_size = BS;
  135. M m (e1 ().size1 (), e2 ().size2 ());
  136. #if BOOST_UBLAS_TYPE_CHECK
  137. matrix<value_type, row_major> cm (m.size1 (), m.size2 ());
  138. typedef typename type_traits<value_type>::real_type real_type;
  139. real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2));
  140. indexing_matrix_assign<scalar_assign> (cm, prod (e1, e2), row_major_tag ());
  141. disable_type_check<bool>::value = true;
  142. #endif
  143. size_type i_size = e1 ().size1 ();
  144. size_type j_size = e2 ().size2 ();
  145. size_type k_size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ());
  146. for (size_type i_begin = 0; i_begin < i_size; i_begin += block_size) {
  147. size_type i_end = i_begin + (std::min) (i_size - i_begin, block_size);
  148. for (size_type j_begin = 0; j_begin < j_size; j_begin += block_size) {
  149. size_type j_end = j_begin + (std::min) (j_size - j_begin, block_size);
  150. // FIX: never ignore Martin Weiser's advice ;-(
  151. #ifdef BOOST_UBLAS_NO_CACHE
  152. matrix_range<matrix_type> m_range (m, range (i_begin, i_end), range (j_begin, j_end));
  153. #else
  154. // matrix<value_type, row_major, bounded_array<value_type, block_size * block_size> > m_range (i_end - i_begin, j_end - j_begin);
  155. matrix<value_type, row_major> m_range (i_end - i_begin, j_end - j_begin);
  156. #endif
  157. m_range.assign (zero_matrix<value_type> (i_end - i_begin, j_end - j_begin));
  158. for (size_type k_begin = 0; k_begin < k_size; k_begin += block_size) {
  159. size_type k_end = k_begin + (std::min) (k_size - k_begin, block_size);
  160. #ifdef BOOST_UBLAS_NO_CACHE
  161. const matrix_range<expression1_type> e1_range (e1 (), range (i_begin, i_end), range (k_begin, k_end));
  162. const matrix_range<expression2_type> e2_range (e2 (), range (k_begin, k_end), range (j_begin, j_end));
  163. #else
  164. // const matrix<value_type, row_major, bounded_array<value_type, block_size * block_size> > e1_range (project (e1 (), range (i_begin, i_end), range (k_begin, k_end)));
  165. // const matrix<value_type, column_major, bounded_array<value_type, block_size * block_size> > e2_range (project (e2 (), range (k_begin, k_end), range (j_begin, j_end)));
  166. const matrix<value_type, row_major> e1_range (project (e1 (), range (i_begin, i_end), range (k_begin, k_end)));
  167. const matrix<value_type, column_major> e2_range (project (e2 (), range (k_begin, k_end), range (j_begin, j_end)));
  168. #endif
  169. m_range.plus_assign (prod (e1_range, e2_range));
  170. }
  171. #ifndef BOOST_UBLAS_NO_CACHE
  172. project (m, range (i_begin, i_end), range (j_begin, j_end)).assign (m_range);
  173. #endif
  174. }
  175. }
  176. #if BOOST_UBLAS_TYPE_CHECK
  177. disable_type_check<bool>::value = false;
  178. BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits<real_type>::epsilon () * merrorbound, internal_logic ());
  179. #endif
  180. return m;
  181. }
  182. template<class M, typename M::size_type BS, class E1, class E2>
  183. BOOST_UBLAS_INLINE
  184. M
  185. block_prod (const matrix_expression<E1> &e1,
  186. const matrix_expression<E2> &e2,
  187. column_major_tag) {
  188. typedef M matrix_type;
  189. typedef const E1 expression1_type;
  190. typedef const E2 expression2_type;
  191. typedef typename M::size_type size_type;
  192. typedef typename M::value_type value_type;
  193. const size_type block_size = BS;
  194. M m (e1 ().size1 (), e2 ().size2 ());
  195. #if BOOST_UBLAS_TYPE_CHECK
  196. matrix<value_type, column_major> cm (m.size1 (), m.size2 ());
  197. typedef typename type_traits<value_type>::real_type real_type;
  198. real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2));
  199. indexing_matrix_assign<scalar_assign> (cm, prod (e1, e2), column_major_tag ());
  200. disable_type_check<bool>::value = true;
  201. #endif
  202. size_type i_size = e1 ().size1 ();
  203. size_type j_size = e2 ().size2 ();
  204. size_type k_size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ());
  205. for (size_type j_begin = 0; j_begin < j_size; j_begin += block_size) {
  206. size_type j_end = j_begin + (std::min) (j_size - j_begin, block_size);
  207. for (size_type i_begin = 0; i_begin < i_size; i_begin += block_size) {
  208. size_type i_end = i_begin + (std::min) (i_size - i_begin, block_size);
  209. // FIX: never ignore Martin Weiser's advice ;-(
  210. #ifdef BOOST_UBLAS_NO_CACHE
  211. matrix_range<matrix_type> m_range (m, range (i_begin, i_end), range (j_begin, j_end));
  212. #else
  213. // matrix<value_type, column_major, bounded_array<value_type, block_size * block_size> > m_range (i_end - i_begin, j_end - j_begin);
  214. matrix<value_type, column_major> m_range (i_end - i_begin, j_end - j_begin);
  215. #endif
  216. m_range.assign (zero_matrix<value_type> (i_end - i_begin, j_end - j_begin));
  217. for (size_type k_begin = 0; k_begin < k_size; k_begin += block_size) {
  218. size_type k_end = k_begin + (std::min) (k_size - k_begin, block_size);
  219. #ifdef BOOST_UBLAS_NO_CACHE
  220. const matrix_range<expression1_type> e1_range (e1 (), range (i_begin, i_end), range (k_begin, k_end));
  221. const matrix_range<expression2_type> e2_range (e2 (), range (k_begin, k_end), range (j_begin, j_end));
  222. #else
  223. // const matrix<value_type, row_major, bounded_array<value_type, block_size * block_size> > e1_range (project (e1 (), range (i_begin, i_end), range (k_begin, k_end)));
  224. // const matrix<value_type, column_major, bounded_array<value_type, block_size * block_size> > e2_range (project (e2 (), range (k_begin, k_end), range (j_begin, j_end)));
  225. const matrix<value_type, row_major> e1_range (project (e1 (), range (i_begin, i_end), range (k_begin, k_end)));
  226. const matrix<value_type, column_major> e2_range (project (e2 (), range (k_begin, k_end), range (j_begin, j_end)));
  227. #endif
  228. m_range.plus_assign (prod (e1_range, e2_range));
  229. }
  230. #ifndef BOOST_UBLAS_NO_CACHE
  231. project (m, range (i_begin, i_end), range (j_begin, j_end)).assign (m_range);
  232. #endif
  233. }
  234. }
  235. #if BOOST_UBLAS_TYPE_CHECK
  236. disable_type_check<bool>::value = false;
  237. BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits<real_type>::epsilon () * merrorbound, internal_logic ());
  238. #endif
  239. return m;
  240. }
  241. // Dispatcher
  242. template<class M, typename M::size_type BS, class E1, class E2>
  243. BOOST_UBLAS_INLINE
  244. M
  245. block_prod (const matrix_expression<E1> &e1,
  246. const matrix_expression<E2> &e2) {
  247. typedef typename M::orientation_category orientation_category;
  248. return block_prod<M, BS> (e1, e2, orientation_category ());
  249. }
  250. }}}
  251. #endif