You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1199 lines
48 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 Yves Renard
  4. This file is a part of GetFEM++
  5. GetFEM++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /** @file gmm_matrix.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Declaration of some matrix types (gmm::dense_matrix,
  30. gmm::row_matrix, gmm::col_matrix, gmm::csc_matrix, etc.)
  31. */
  32. #ifndef GMM_MATRIX_H__
  33. #define GMM_MATRIX_H__
  34. #include "gmm_vector.h"
  35. #include "gmm_sub_vector.h"
  36. #include "gmm_sub_matrix.h"
  37. #include "gmm_transposed.h"
  38. namespace gmm
  39. {
  40. /* ******************************************************************** */
  41. /* */
  42. /* Identity matrix */
  43. /* */
  44. /* ******************************************************************** */
  45. struct identity_matrix {
  46. template <class MAT> void build_with(const MAT &) {}
  47. };
  48. template <typename M> inline
  49. void add(const identity_matrix&, M &v1) {
  50. size_type n = std::min(gmm::mat_nrows(v1), gmm::mat_ncols(v1));
  51. for (size_type i = 0; i < n; ++i)
  52. v1(i,i) += typename linalg_traits<M>::value_type(1);
  53. }
  54. template <typename M> inline
  55. void add(const identity_matrix &II, const M &v1)
  56. { add(II, linalg_const_cast(v1)); }
  57. template <typename V1, typename V2> inline
  58. void mult(const identity_matrix&, const V1 &v1, V2 &v2)
  59. { copy(v1, v2); }
  60. template <typename V1, typename V2> inline
  61. void mult(const identity_matrix&, const V1 &v1, const V2 &v2)
  62. { copy(v1, v2); }
  63. template <typename V1, typename V2, typename V3> inline
  64. void mult(const identity_matrix&, const V1 &v1, const V2 &v2, V3 &v3)
  65. { add(v1, v2, v3); }
  66. template <typename V1, typename V2, typename V3> inline
  67. void mult(const identity_matrix&, const V1 &v1, const V2 &v2, const V3 &v3)
  68. { add(v1, v2, v3); }
  69. template <typename V1, typename V2> inline
  70. void left_mult(const identity_matrix&, const V1 &v1, V2 &v2)
  71. { copy(v1, v2); }
  72. template <typename V1, typename V2> inline
  73. void left_mult(const identity_matrix&, const V1 &v1, const V2 &v2)
  74. { copy(v1, v2); }
  75. template <typename V1, typename V2> inline
  76. void right_mult(const identity_matrix&, const V1 &v1, V2 &v2)
  77. { copy(v1, v2); }
  78. template <typename V1, typename V2> inline
  79. void right_mult(const identity_matrix&, const V1 &v1, const V2 &v2)
  80. { copy(v1, v2); }
  81. template <typename V1, typename V2> inline
  82. void transposed_left_mult(const identity_matrix&, const V1 &v1, V2 &v2)
  83. { copy(v1, v2); }
  84. template <typename V1, typename V2> inline
  85. void transposed_left_mult(const identity_matrix&, const V1 &v1,const V2 &v2)
  86. { copy(v1, v2); }
  87. template <typename V1, typename V2> inline
  88. void transposed_right_mult(const identity_matrix&, const V1 &v1, V2 &v2)
  89. { copy(v1, v2); }
  90. template <typename V1, typename V2> inline
  91. void transposed_right_mult(const identity_matrix&,const V1 &v1,const V2 &v2)
  92. { copy(v1, v2); }
  93. template <typename M> void copy_ident(const identity_matrix&, M &m) {
  94. size_type i = 0, n = std::min(mat_nrows(m), mat_ncols(m));
  95. clear(m);
  96. for (; i < n; ++i) m(i,i) = typename linalg_traits<M>::value_type(1);
  97. }
  98. template <typename M> inline void copy(const identity_matrix&, M &m)
  99. { copy_ident(identity_matrix(), m); }
  100. template <typename M> inline void copy(const identity_matrix &, const M &m)
  101. { copy_ident(identity_matrix(), linalg_const_cast(m)); }
  102. template <typename V1, typename V2> inline
  103. typename linalg_traits<V1>::value_type
  104. vect_sp(const identity_matrix &, const V1 &v1, const V2 &v2)
  105. { return vect_sp(v1, v2); }
  106. template <typename V1, typename V2> inline
  107. typename linalg_traits<V1>::value_type
  108. vect_hp(const identity_matrix &, const V1 &v1, const V2 &v2)
  109. { return vect_hp(v1, v2); }
  110. template<typename M> inline bool is_identity(const M&) { return false; }
  111. inline bool is_identity(const identity_matrix&) { return true; }
  112. /* ******************************************************************** */
  113. /* */
  114. /* Row matrix */
  115. /* */
  116. /* ******************************************************************** */
  117. template<typename V> class row_matrix {
  118. protected :
  119. std::vector<V> li; /* array of rows. */
  120. size_type nc;
  121. public :
  122. typedef typename linalg_traits<V>::reference reference;
  123. typedef typename linalg_traits<V>::value_type value_type;
  124. row_matrix(size_type r, size_type c) : li(r, V(c)), nc(c) {}
  125. row_matrix(void) : nc(0) {}
  126. reference operator ()(size_type l, size_type c)
  127. { return li[l][c]; }
  128. value_type operator ()(size_type l, size_type c) const
  129. { return li[l][c]; }
  130. void clear_mat();
  131. void resize(size_type m, size_type n);
  132. typename std::vector<V>::iterator begin(void)
  133. { return li.begin(); }
  134. typename std::vector<V>::iterator end(void)
  135. { return li.end(); }
  136. typename std::vector<V>::const_iterator begin(void) const
  137. { return li.begin(); }
  138. typename std::vector<V>::const_iterator end(void) const
  139. { return li.end(); }
  140. V& row(size_type i) { return li[i]; }
  141. const V& row(size_type i) const { return li[i]; }
  142. V& operator[](size_type i) { return li[i]; }
  143. const V& operator[](size_type i) const { return li[i]; }
  144. inline size_type nrows(void) const { return li.size(); }
  145. inline size_type ncols(void) const { return nc; }
  146. void swap(row_matrix<V> &m) { std::swap(li, m.li); std::swap(nc, m.nc); }
  147. void swap_row(size_type i, size_type j) { std::swap(li[i], li[j]); }
  148. };
  149. template<typename V> void row_matrix<V>::resize(size_type m, size_type n) {
  150. size_type nr = std::min(nrows(), m);
  151. li.resize(m);
  152. for (size_type i=nr; i < m; ++i) gmm::resize(li[i], n);
  153. if (n != nc) {
  154. for (size_type i=0; i < nr; ++i) gmm::resize(li[i], n);
  155. nc = n;
  156. }
  157. }
  158. template<typename V> void row_matrix<V>::clear_mat()
  159. { for (size_type i=0; i < nrows(); ++i) clear(li[i]); }
  160. template <typename V> struct linalg_traits<row_matrix<V> > {
  161. typedef row_matrix<V> this_type;
  162. typedef this_type origin_type;
  163. typedef linalg_false is_reference;
  164. typedef abstract_matrix linalg_type;
  165. typedef typename linalg_traits<V>::value_type value_type;
  166. typedef typename linalg_traits<V>::reference reference;
  167. typedef typename linalg_traits<V>::storage_type storage_type;
  168. typedef V & sub_row_type;
  169. typedef const V & const_sub_row_type;
  170. typedef typename std::vector<V>::iterator row_iterator;
  171. typedef typename std::vector<V>::const_iterator const_row_iterator;
  172. typedef abstract_null_type sub_col_type;
  173. typedef abstract_null_type const_sub_col_type;
  174. typedef abstract_null_type col_iterator;
  175. typedef abstract_null_type const_col_iterator;
  176. typedef row_major sub_orientation;
  177. typedef linalg_true index_sorted;
  178. static size_type nrows(const this_type &m) { return m.nrows(); }
  179. static size_type ncols(const this_type &m) { return m.ncols(); }
  180. static row_iterator row_begin(this_type &m) { return m.begin(); }
  181. static row_iterator row_end(this_type &m) { return m.end(); }
  182. static const_row_iterator row_begin(const this_type &m)
  183. { return m.begin(); }
  184. static const_row_iterator row_end(const this_type &m)
  185. { return m.end(); }
  186. static const_sub_row_type row(const const_row_iterator &it)
  187. { return const_sub_row_type(*it); }
  188. static sub_row_type row(const row_iterator &it)
  189. { return sub_row_type(*it); }
  190. static origin_type* origin(this_type &m) { return &m; }
  191. static const origin_type* origin(const this_type &m) { return &m; }
  192. static void do_clear(this_type &m) { m.clear_mat(); }
  193. static value_type access(const const_row_iterator &itrow, size_type j)
  194. { return (*itrow)[j]; }
  195. static reference access(const row_iterator &itrow, size_type j)
  196. { return (*itrow)[j]; }
  197. static void resize(this_type &v, size_type m, size_type n)
  198. { v.resize(m, n); }
  199. static void reshape(this_type &, size_type, size_type)
  200. { GMM_ASSERT1(false, "Sorry, to be done"); }
  201. };
  202. template<typename V> std::ostream &operator <<
  203. (std::ostream &o, const row_matrix<V>& m) { gmm::write(o,m); return o; }
  204. /* ******************************************************************** */
  205. /* */
  206. /* Column matrix */
  207. /* */
  208. /* ******************************************************************** */
  209. template<typename V> class col_matrix {
  210. protected :
  211. std::vector<V> li; /* array of columns. */
  212. size_type nr;
  213. public :
  214. typedef typename linalg_traits<V>::reference reference;
  215. typedef typename linalg_traits<V>::value_type value_type;
  216. col_matrix(size_type r, size_type c) : li(c, V(r)), nr(r) { }
  217. col_matrix(void) : nr(0) {}
  218. reference operator ()(size_type l, size_type c)
  219. { return li[c][l]; }
  220. value_type operator ()(size_type l, size_type c) const
  221. { return li[c][l]; }
  222. void clear_mat();
  223. void resize(size_type, size_type);
  224. V& col(size_type i) { return li[i]; }
  225. const V& col(size_type i) const { return li[i]; }
  226. V& operator[](size_type i) { return li[i]; }
  227. const V& operator[](size_type i) const { return li[i]; }
  228. typename std::vector<V>::iterator begin(void)
  229. { return li.begin(); }
  230. typename std::vector<V>::iterator end(void)
  231. { return li.end(); }
  232. typename std::vector<V>::const_iterator begin(void) const
  233. { return li.begin(); }
  234. typename std::vector<V>::const_iterator end(void) const
  235. { return li.end(); }
  236. inline size_type ncols(void) const { return li.size(); }
  237. inline size_type nrows(void) const { return nr; }
  238. void swap(col_matrix<V> &m) { std::swap(li, m.li); std::swap(nr, m.nr); }
  239. void swap_col(size_type i, size_type j) { std::swap(li[i], li[j]); }
  240. };
  241. template<typename V> void col_matrix<V>::resize(size_type m, size_type n) {
  242. size_type nc = std::min(ncols(), n);
  243. li.resize(n);
  244. for (size_type i=nc; i < n; ++i) gmm::resize(li[i], m);
  245. if (m != nr) {
  246. for (size_type i=0; i < nc; ++i) gmm::resize(li[i], m);
  247. nr = m;
  248. }
  249. }
  250. template<typename V> void col_matrix<V>::clear_mat()
  251. { for (size_type i=0; i < ncols(); ++i) clear(li[i]); }
  252. template <typename V> struct linalg_traits<col_matrix<V> > {
  253. typedef col_matrix<V> this_type;
  254. typedef this_type origin_type;
  255. typedef linalg_false is_reference;
  256. typedef abstract_matrix linalg_type;
  257. typedef typename linalg_traits<V>::value_type value_type;
  258. typedef typename linalg_traits<V>::reference reference;
  259. typedef typename linalg_traits<V>::storage_type storage_type;
  260. typedef V &sub_col_type;
  261. typedef const V &const_sub_col_type;
  262. typedef typename std::vector<V>::iterator col_iterator;
  263. typedef typename std::vector<V>::const_iterator const_col_iterator;
  264. typedef abstract_null_type sub_row_type;
  265. typedef abstract_null_type const_sub_row_type;
  266. typedef abstract_null_type row_iterator;
  267. typedef abstract_null_type const_row_iterator;
  268. typedef col_major sub_orientation;
  269. typedef linalg_true index_sorted;
  270. static size_type nrows(const this_type &m) { return m.nrows(); }
  271. static size_type ncols(const this_type &m) { return m.ncols(); }
  272. static col_iterator col_begin(this_type &m) { return m.begin(); }
  273. static col_iterator col_end(this_type &m) { return m.end(); }
  274. static const_col_iterator col_begin(const this_type &m)
  275. { return m.begin(); }
  276. static const_col_iterator col_end(const this_type &m)
  277. { return m.end(); }
  278. static const_sub_col_type col(const const_col_iterator &it)
  279. { return *it; }
  280. static sub_col_type col(const col_iterator &it)
  281. { return *it; }
  282. static origin_type* origin(this_type &m) { return &m; }
  283. static const origin_type* origin(const this_type &m) { return &m; }
  284. static void do_clear(this_type &m) { m.clear_mat(); }
  285. static value_type access(const const_col_iterator &itcol, size_type j)
  286. { return (*itcol)[j]; }
  287. static reference access(const col_iterator &itcol, size_type j)
  288. { return (*itcol)[j]; }
  289. static void resize(this_type &v, size_type m, size_type n)
  290. { v.resize(m,n); }
  291. static void reshape(this_type &, size_type, size_type)
  292. { GMM_ASSERT1(false, "Sorry, to be done"); }
  293. };
  294. template<typename V> std::ostream &operator <<
  295. (std::ostream &o, const col_matrix<V>& m) { gmm::write(o,m); return o; }
  296. /* ******************************************************************** */
  297. /* */
  298. /* Dense matrix */
  299. /* */
  300. /* ******************************************************************** */
  301. template<typename T> class dense_matrix : public std::vector<T> {
  302. public:
  303. typedef typename std::vector<T>::size_type size_type;
  304. typedef typename std::vector<T>::iterator iterator;
  305. typedef typename std::vector<T>::const_iterator const_iterator;
  306. typedef typename std::vector<T>::reference reference;
  307. typedef typename std::vector<T>::const_reference const_reference;
  308. protected:
  309. size_type nbc, nbl;
  310. public:
  311. inline const_reference operator ()(size_type l, size_type c) const {
  312. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  313. return *(this->begin() + c*nbl+l);
  314. }
  315. inline reference operator ()(size_type l, size_type c) {
  316. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  317. return *(this->begin() + c*nbl+l);
  318. }
  319. std::vector<T> &as_vector(void) { return *this; }
  320. const std::vector<T> &as_vector(void) const { return *this; }
  321. void resize(size_type, size_type);
  322. void base_resize(size_type, size_type);
  323. void reshape(size_type, size_type);
  324. void fill(T a, T b = T(0));
  325. inline size_type nrows(void) const { return nbl; }
  326. inline size_type ncols(void) const { return nbc; }
  327. void swap(dense_matrix<T> &m)
  328. { std::vector<T>::swap(m); std::swap(nbc, m.nbc); std::swap(nbl, m.nbl); }
  329. dense_matrix(size_type l, size_type c)
  330. : std::vector<T>(c*l), nbc(c), nbl(l) {}
  331. dense_matrix(void) { nbl = nbc = 0; }
  332. };
  333. template<typename T> void dense_matrix<T>::reshape(size_type m,size_type n) {
  334. GMM_ASSERT2(n*m == nbl*nbc, "dimensions mismatch");
  335. nbl = m; nbc = n;
  336. }
  337. template<typename T> void dense_matrix<T>::base_resize(size_type m,
  338. size_type n)
  339. { std::vector<T>::resize(n*m); nbl = m; nbc = n; }
  340. template<typename T> void dense_matrix<T>::resize(size_type m, size_type n) {
  341. if (n*m > nbc*nbl) std::vector<T>::resize(n*m);
  342. if (m < nbl) {
  343. for (size_type i = 1; i < std::min(nbc, n); ++i)
  344. std::copy(this->begin()+i*nbl, this->begin()+(i*nbl+m),
  345. this->begin()+i*m);
  346. for (size_type i = std::min(nbc, n); i < n; ++i)
  347. std::fill(this->begin()+(i*m), this->begin()+(i+1)*m, T(0));
  348. }
  349. else if (m > nbl) { /* do nothing when the nb of rows does not change */
  350. for (size_type i = std::min(nbc, n); i > 1; --i)
  351. std::copy(this->begin()+(i-1)*nbl, this->begin()+i*nbl,
  352. this->begin()+(i-1)*m);
  353. for (size_type i = 0; i < std::min(nbc, n); ++i)
  354. std::fill(this->begin()+(i*m+nbl), this->begin()+(i+1)*m, T(0));
  355. }
  356. if (n*m < nbc*nbl) std::vector<T>::resize(n*m);
  357. nbl = m; nbc = n;
  358. }
  359. template<typename T> void dense_matrix<T>::fill(T a, T b) {
  360. std::fill(this->begin(), this->end(), b);
  361. size_type n = std::min(nbl, nbc);
  362. if (a != b) for (size_type i = 0; i < n; ++i) (*this)(i,i) = a;
  363. }
  364. template <typename T> struct linalg_traits<dense_matrix<T> > {
  365. typedef dense_matrix<T> this_type;
  366. typedef this_type origin_type;
  367. typedef linalg_false is_reference;
  368. typedef abstract_matrix linalg_type;
  369. typedef T value_type;
  370. typedef T& reference;
  371. typedef abstract_dense storage_type;
  372. typedef tab_ref_reg_spaced_with_origin<typename this_type::iterator,
  373. this_type> sub_row_type;
  374. typedef tab_ref_reg_spaced_with_origin<typename this_type::const_iterator,
  375. this_type> const_sub_row_type;
  376. typedef dense_compressed_iterator<typename this_type::iterator,
  377. typename this_type::iterator,
  378. this_type *> row_iterator;
  379. typedef dense_compressed_iterator<typename this_type::const_iterator,
  380. typename this_type::iterator,
  381. const this_type *> const_row_iterator;
  382. typedef tab_ref_with_origin<typename this_type::iterator,
  383. this_type> sub_col_type;
  384. typedef tab_ref_with_origin<typename this_type::const_iterator,
  385. this_type> const_sub_col_type;
  386. typedef dense_compressed_iterator<typename this_type::iterator,
  387. typename this_type::iterator,
  388. this_type *> col_iterator;
  389. typedef dense_compressed_iterator<typename this_type::const_iterator,
  390. typename this_type::iterator,
  391. const this_type *> const_col_iterator;
  392. typedef col_and_row sub_orientation;
  393. typedef linalg_true index_sorted;
  394. static size_type nrows(const this_type &m) { return m.nrows(); }
  395. static size_type ncols(const this_type &m) { return m.ncols(); }
  396. static const_sub_row_type row(const const_row_iterator &it)
  397. { return const_sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  398. static const_sub_col_type col(const const_col_iterator &it)
  399. { return const_sub_col_type(*it, *it + it.nrows, it.origin); }
  400. static sub_row_type row(const row_iterator &it)
  401. { return sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  402. static sub_col_type col(const col_iterator &it)
  403. { return sub_col_type(*it, *it + it.nrows, it.origin); }
  404. static row_iterator row_begin(this_type &m)
  405. { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
  406. static row_iterator row_end(this_type &m)
  407. { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
  408. static const_row_iterator row_begin(const this_type &m)
  409. { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
  410. static const_row_iterator row_end(const this_type &m)
  411. { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
  412. static col_iterator col_begin(this_type &m)
  413. { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
  414. static col_iterator col_end(this_type &m)
  415. { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), m.ncols(), &m); }
  416. static const_col_iterator col_begin(const this_type &m)
  417. { return const_col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
  418. static const_col_iterator col_end(const this_type &m)
  419. { return const_col_iterator(m.begin(),m.nrows(),m.nrows(),m.ncols(),m.ncols(), &m); }
  420. static origin_type* origin(this_type &m) { return &m; }
  421. static const origin_type* origin(const this_type &m) { return &m; }
  422. static void do_clear(this_type &m) { m.fill(value_type(0)); }
  423. static value_type access(const const_col_iterator &itcol, size_type j)
  424. { return (*itcol)[j]; }
  425. static reference access(const col_iterator &itcol, size_type j)
  426. { return (*itcol)[j]; }
  427. static void resize(this_type &v, size_type m, size_type n)
  428. { v.resize(m,n); }
  429. static void reshape(this_type &v, size_type m, size_type n)
  430. { v.reshape(m, n); }
  431. };
  432. template<typename T> std::ostream &operator <<
  433. (std::ostream &o, const dense_matrix<T>& m) { gmm::write(o,m); return o; }
  434. /* ******************************************************************** */
  435. /* */
  436. /* Read only compressed sparse column matrix */
  437. /* */
  438. /* ******************************************************************** */
  439. template <typename T, int shift = 0>
  440. struct csc_matrix {
  441. typedef unsigned int IND_TYPE;
  442. std::vector<T> pr;
  443. std::vector<IND_TYPE> ir;
  444. std::vector<IND_TYPE> jc;
  445. size_type nc, nr;
  446. typedef T value_type;
  447. typedef T& access_type;
  448. template <typename Matrix> void init_with_good_format(const Matrix &B);
  449. template <typename Matrix> void init_with(const Matrix &A);
  450. void init_with(const col_matrix<gmm::rsvector<T> > &B)
  451. { init_with_good_format(B); }
  452. void init_with(const col_matrix<wsvector<T> > &B)
  453. { init_with_good_format(B); }
  454. template <typename PT1, typename PT2, typename PT3, int cshift>
  455. void init_with(const csc_matrix_ref<PT1,PT2,PT3,cshift>& B)
  456. { init_with_good_format(B); }
  457. template <typename U, int cshift>
  458. void init_with(const csc_matrix<U, cshift>& B)
  459. { init_with_good_format(B); }
  460. void init_with_identity(size_type n);
  461. csc_matrix(void) : nc(0), nr(0) {}
  462. csc_matrix(size_type nnr, size_type nnc);
  463. size_type nrows(void) const { return nr; }
  464. size_type ncols(void) const { return nc; }
  465. void swap(csc_matrix<T, shift> &m) {
  466. std::swap(pr, m.pr);
  467. std::swap(ir, m.ir); std::swap(jc, m.jc);
  468. std::swap(nc, m.nc); std::swap(nr, m.nr);
  469. }
  470. value_type operator()(size_type i, size_type j) const
  471. { return mat_col(*this, j)[i]; }
  472. };
  473. template <typename T, int shift> template<typename Matrix>
  474. void csc_matrix<T, shift>::init_with_good_format(const Matrix &B) {
  475. typedef typename linalg_traits<Matrix>::const_sub_col_type col_type;
  476. nc = mat_ncols(B); nr = mat_nrows(B);
  477. jc.resize(nc+1);
  478. jc[0] = shift;
  479. for (size_type j = 0; j < nc; ++j) {
  480. jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_col(B, j)));
  481. }
  482. pr.resize(jc[nc]);
  483. ir.resize(jc[nc]);
  484. for (size_type j = 0; j < nc; ++j) {
  485. col_type col = mat_const_col(B, j);
  486. typename linalg_traits<typename org_type<col_type>::t>::const_iterator
  487. it = vect_const_begin(col), ite = vect_const_end(col);
  488. for (size_type k = 0; it != ite; ++it, ++k) {
  489. pr[jc[j]-shift+k] = *it;
  490. ir[jc[j]-shift+k] = IND_TYPE(it.index() + shift);
  491. }
  492. }
  493. }
  494. template <typename T, int shift> template <typename Matrix>
  495. void csc_matrix<T, shift>::init_with(const Matrix &A) {
  496. col_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
  497. copy(A, B);
  498. init_with_good_format(B);
  499. }
  500. template <typename T, int shift>
  501. void csc_matrix<T, shift>::init_with_identity(size_type n) {
  502. nc = nr = n;
  503. pr.resize(nc); ir.resize(nc); jc.resize(nc+1);
  504. for (size_type j = 0; j < nc; ++j)
  505. { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
  506. jc[nc] = shift + nc;
  507. }
  508. template <typename T, int shift>
  509. csc_matrix<T, shift>::csc_matrix(size_type nnr, size_type nnc)
  510. : nc(nnc), nr(nnr) {
  511. pr.resize(1); ir.resize(1); jc.resize(nc+1);
  512. for (size_type j = 0; j <= nc; ++j) jc[j] = shift;
  513. }
  514. template <typename T, int shift>
  515. struct linalg_traits<csc_matrix<T, shift> > {
  516. typedef csc_matrix<T, shift> this_type;
  517. typedef typename this_type::IND_TYPE IND_TYPE;
  518. typedef linalg_const is_reference;
  519. typedef abstract_matrix linalg_type;
  520. typedef T value_type;
  521. typedef T origin_type;
  522. typedef T reference;
  523. typedef abstract_sparse storage_type;
  524. typedef abstract_null_type sub_row_type;
  525. typedef abstract_null_type const_sub_row_type;
  526. typedef abstract_null_type row_iterator;
  527. typedef abstract_null_type const_row_iterator;
  528. typedef abstract_null_type sub_col_type;
  529. typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
  530. const_sub_col_type;
  531. typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
  532. const IND_TYPE *, shift>
  533. const_col_iterator;
  534. typedef abstract_null_type col_iterator;
  535. typedef col_major sub_orientation;
  536. typedef linalg_true index_sorted;
  537. static size_type nrows(const this_type &m) { return m.nrows(); }
  538. static size_type ncols(const this_type &m) { return m.ncols(); }
  539. static const_col_iterator col_begin(const this_type &m)
  540. { return const_col_iterator(&m.pr[0],&m.ir[0],&m.jc[0], m.nr, &m.pr[0]); }
  541. static const_col_iterator col_end(const this_type &m) {
  542. return const_col_iterator(&m.pr[0],&m.ir[0],&m.jc[0]+m.nc,
  543. m.nr,&m.pr[0]);
  544. }
  545. static const_sub_col_type col(const const_col_iterator &it) {
  546. return const_sub_col_type(it.pr + *(it.jc) - shift,
  547. it.ir + *(it.jc) - shift,
  548. *(it.jc + 1) - *(it.jc), it.n);
  549. }
  550. static const origin_type* origin(const this_type &m) { return &m.pr[0]; }
  551. static void do_clear(this_type &m) { m.do_clear(); }
  552. static value_type access(const const_col_iterator &itcol, size_type j)
  553. { return col(itcol)[j]; }
  554. };
  555. template <typename T, int shift>
  556. std::ostream &operator <<
  557. (std::ostream &o, const csc_matrix<T, shift>& m)
  558. { gmm::write(o,m); return o; }
  559. template <typename T, int shift>
  560. inline void copy(const identity_matrix &, csc_matrix<T, shift>& M)
  561. { M.init_with_identity(mat_nrows(M)); }
  562. template <typename Matrix, typename T, int shift>
  563. inline void copy(const Matrix &A, csc_matrix<T, shift>& M)
  564. { M.init_with(A); }
  565. /* ******************************************************************** */
  566. /* */
  567. /* Read only compressed sparse row matrix */
  568. /* */
  569. /* ******************************************************************** */
  570. template <typename T, int shift = 0>
  571. struct csr_matrix {
  572. typedef unsigned int IND_TYPE;
  573. std::vector<T> pr; // values.
  574. std::vector<IND_TYPE> ir; // col indices.
  575. std::vector<IND_TYPE> jc; // row repartition on pr and ir.
  576. size_type nc, nr;
  577. typedef T value_type;
  578. typedef T& access_type;
  579. template <typename Matrix> void init_with_good_format(const Matrix &B);
  580. void init_with(const row_matrix<wsvector<T> > &B)
  581. { init_with_good_format(B); }
  582. void init_with(const row_matrix<rsvector<T> > &B)
  583. { init_with_good_format(B); }
  584. template <typename PT1, typename PT2, typename PT3, int cshift>
  585. void init_with(const csr_matrix_ref<PT1,PT2,PT3,cshift>& B)
  586. { init_with_good_format(B); }
  587. template <typename U, int cshift>
  588. void init_with(const csr_matrix<U, cshift>& B)
  589. { init_with_good_format(B); }
  590. template <typename Matrix> void init_with(const Matrix &A);
  591. void init_with_identity(size_type n);
  592. csr_matrix(void) : nc(0), nr(0) {}
  593. csr_matrix(size_type nnr, size_type nnc);
  594. size_type nrows(void) const { return nr; }
  595. size_type ncols(void) const { return nc; }
  596. void swap(csr_matrix<T, shift> &m) {
  597. std::swap(pr, m.pr);
  598. std::swap(ir,m.ir); std::swap(jc, m.jc);
  599. std::swap(nc, m.nc); std::swap(nr,m.nr);
  600. }
  601. value_type operator()(size_type i, size_type j) const
  602. { return mat_row(*this, i)[j]; }
  603. };
  604. template <typename T, int shift> template <typename Matrix>
  605. void csr_matrix<T, shift>::init_with_good_format(const Matrix &B) {
  606. typedef typename linalg_traits<Matrix>::const_sub_row_type row_type;
  607. nc = mat_ncols(B); nr = mat_nrows(B);
  608. jc.resize(nr+1);
  609. jc[0] = shift;
  610. for (size_type j = 0; j < nr; ++j) {
  611. jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_row(B, j)));
  612. }
  613. pr.resize(jc[nr]);
  614. ir.resize(jc[nr]);
  615. for (size_type j = 0; j < nr; ++j) {
  616. row_type row = mat_const_row(B, j);
  617. typename linalg_traits<typename org_type<row_type>::t>::const_iterator
  618. it = vect_const_begin(row), ite = vect_const_end(row);
  619. for (size_type k = 0; it != ite; ++it, ++k) {
  620. pr[jc[j]-shift+k] = *it;
  621. ir[jc[j]-shift+k] = IND_TYPE(it.index()+shift);
  622. }
  623. }
  624. }
  625. template <typename T, int shift> template <typename Matrix>
  626. void csr_matrix<T, shift>::init_with(const Matrix &A) {
  627. row_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
  628. copy(A, B);
  629. init_with_good_format(B);
  630. }
  631. template <typename T, int shift>
  632. void csr_matrix<T, shift>::init_with_identity(size_type n) {
  633. nc = nr = n;
  634. pr.resize(nr); ir.resize(nr); jc.resize(nr+1);
  635. for (size_type j = 0; j < nr; ++j)
  636. { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
  637. jc[nr] = shift + nr;
  638. }
  639. template <typename T, int shift>
  640. csr_matrix<T, shift>::csr_matrix(size_type nnr, size_type nnc)
  641. : nc(nnc), nr(nnr) {
  642. pr.resize(1); ir.resize(1); jc.resize(nr+1);
  643. for (size_type j = 0; j < nr; ++j) jc[j] = shift;
  644. jc[nr] = shift;
  645. }
  646. template <typename T, int shift>
  647. struct linalg_traits<csr_matrix<T, shift> > {
  648. typedef csr_matrix<T, shift> this_type;
  649. typedef typename this_type::IND_TYPE IND_TYPE;
  650. typedef linalg_const is_reference;
  651. typedef abstract_matrix linalg_type;
  652. typedef T value_type;
  653. typedef T origin_type;
  654. typedef T reference;
  655. typedef abstract_sparse storage_type;
  656. typedef abstract_null_type sub_col_type;
  657. typedef abstract_null_type const_sub_col_type;
  658. typedef abstract_null_type col_iterator;
  659. typedef abstract_null_type const_col_iterator;
  660. typedef abstract_null_type sub_row_type;
  661. typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
  662. const_sub_row_type;
  663. typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
  664. const IND_TYPE *, shift>
  665. const_row_iterator;
  666. typedef abstract_null_type row_iterator;
  667. typedef row_major sub_orientation;
  668. typedef linalg_true index_sorted;
  669. static size_type nrows(const this_type &m) { return m.nrows(); }
  670. static size_type ncols(const this_type &m) { return m.ncols(); }
  671. static const_row_iterator row_begin(const this_type &m)
  672. { return const_row_iterator(&m.pr[0], &m.ir[0], &m.jc[0], m.nc, &m.pr[0]); }
  673. static const_row_iterator row_end(const this_type &m)
  674. { return const_row_iterator(&m.pr[0], &m.ir[0], &m.jc[0] + m.nr, m.nc, &m.pr[0]); }
  675. static const_sub_row_type row(const const_row_iterator &it) {
  676. return const_sub_row_type(it.pr + *(it.jc) - shift,
  677. it.ir + *(it.jc) - shift,
  678. *(it.jc + 1) - *(it.jc), it.n);
  679. }
  680. static const origin_type* origin(const this_type &m) { return &m.pr[0]; }
  681. static void do_clear(this_type &m) { m.do_clear(); }
  682. static value_type access(const const_row_iterator &itrow, size_type j)
  683. { return row(itrow)[j]; }
  684. };
  685. template <typename T, int shift>
  686. std::ostream &operator <<
  687. (std::ostream &o, const csr_matrix<T, shift>& m)
  688. { gmm::write(o,m); return o; }
  689. template <typename T, int shift>
  690. inline void copy(const identity_matrix &, csr_matrix<T, shift>& M)
  691. { M.init_with_identity(mat_nrows(M)); }
  692. template <typename Matrix, typename T, int shift>
  693. inline void copy(const Matrix &A, csr_matrix<T, shift>& M)
  694. { M.init_with(A); }
  695. /* ******************************************************************** */
  696. /* */
  697. /* Block matrix */
  698. /* */
  699. /* ******************************************************************** */
  700. template <typename MAT> class block_matrix {
  701. protected :
  702. std::vector<MAT> blocks;
  703. size_type nrowblocks_;
  704. size_type ncolblocks_;
  705. std::vector<sub_interval> introw, intcol;
  706. public :
  707. typedef typename linalg_traits<MAT>::value_type value_type;
  708. typedef typename linalg_traits<MAT>::reference reference;
  709. size_type nrows(void) const { return introw[nrowblocks_-1].max; }
  710. size_type ncols(void) const { return intcol[ncolblocks_-1].max; }
  711. size_type nrowblocks(void) const { return nrowblocks_; }
  712. size_type ncolblocks(void) const { return ncolblocks_; }
  713. const sub_interval &subrowinterval(size_type i) const { return introw[i]; }
  714. const sub_interval &subcolinterval(size_type i) const { return intcol[i]; }
  715. const MAT &block(size_type i, size_type j) const
  716. { return blocks[j*ncolblocks_+i]; }
  717. MAT &block(size_type i, size_type j)
  718. { return blocks[j*ncolblocks_+i]; }
  719. void do_clear(void);
  720. // to be done : read and write access to a component
  721. value_type operator() (size_type i, size_type j) const {
  722. size_type k, l;
  723. for (k = 0; k < nrowblocks_; ++k)
  724. if (i >= introw[k].min && i < introw[k].max) break;
  725. for (l = 0; l < nrowblocks_; ++l)
  726. if (j >= introw[l].min && j < introw[l].max) break;
  727. return (block(k, l))(i - introw[k].min, j - introw[l].min);
  728. }
  729. reference operator() (size_type i, size_type j) {
  730. size_type k, l;
  731. for (k = 0; k < nrowblocks_; ++k)
  732. if (i >= introw[k].min && i < introw[k].max) break;
  733. for (l = 0; l < nrowblocks_; ++l)
  734. if (j >= introw[l].min && j < introw[l].max) break;
  735. return (block(k, l))(i - introw[k].min, j - introw[l].min);
  736. }
  737. template <typename CONT> void resize(const CONT &c1, const CONT &c2);
  738. template <typename CONT> block_matrix(const CONT &c1, const CONT &c2)
  739. { resize(c1, c2); }
  740. block_matrix(void) {}
  741. };
  742. template <typename MAT> struct linalg_traits<block_matrix<MAT> > {
  743. typedef block_matrix<MAT> this_type;
  744. typedef linalg_false is_reference;
  745. typedef abstract_matrix linalg_type;
  746. typedef this_type origin_type;
  747. typedef typename linalg_traits<MAT>::value_type value_type;
  748. typedef typename linalg_traits<MAT>::reference reference;
  749. typedef typename linalg_traits<MAT>::storage_type storage_type;
  750. typedef abstract_null_type sub_row_type; // to be done ...
  751. typedef abstract_null_type const_sub_row_type; // to be done ...
  752. typedef abstract_null_type row_iterator; // to be done ...
  753. typedef abstract_null_type const_row_iterator; // to be done ...
  754. typedef abstract_null_type sub_col_type; // to be done ...
  755. typedef abstract_null_type const_sub_col_type; // to be done ...
  756. typedef abstract_null_type col_iterator; // to be done ...
  757. typedef abstract_null_type const_col_iterator; // to be done ...
  758. typedef abstract_null_type sub_orientation; // to be done ...
  759. typedef linalg_true index_sorted;
  760. static size_type nrows(const this_type &m) { return m.nrows(); }
  761. static size_type ncols(const this_type &m) { return m.ncols(); }
  762. static origin_type* origin(this_type &m) { return &m; }
  763. static const origin_type* origin(const this_type &m) { return &m; }
  764. static void do_clear(this_type &m) { m.do_clear(); }
  765. // access to be done ...
  766. static void resize(this_type &, size_type , size_type)
  767. { GMM_ASSERT1(false, "Sorry, to be done"); }
  768. static void reshape(this_type &, size_type , size_type)
  769. { GMM_ASSERT1(false, "Sorry, to be done"); }
  770. };
  771. template <typename MAT> void block_matrix<MAT>::do_clear(void) {
  772. for (size_type j = 0, l = 0; j < ncolblocks_; ++j)
  773. for (size_type i = 0, k = 0; i < nrowblocks_; ++i)
  774. clear(block(i,j));
  775. }
  776. template <typename MAT> template <typename CONT>
  777. void block_matrix<MAT>::resize(const CONT &c1, const CONT &c2) {
  778. nrowblocks_ = c1.size(); ncolblocks_ = c2.size();
  779. blocks.resize(nrowblocks_ * ncolblocks_);
  780. intcol.resize(ncolblocks_);
  781. introw.resize(nrowblocks_);
  782. for (size_type j = 0, l = 0; j < ncolblocks_; ++j) {
  783. intcol[j] = sub_interval(l, c2[j]); l += c2[j];
  784. for (size_type i = 0, k = 0; i < nrowblocks_; ++i) {
  785. if (j == 0) { introw[i] = sub_interval(k, c1[i]); k += c1[i]; }
  786. block(i, j) = MAT(c1[i], c2[j]);
  787. }
  788. }
  789. }
  790. template <typename M1, typename M2>
  791. void copy(const block_matrix<M1> &m1, M2 &m2) {
  792. for (size_type j = 0; j < m1.ncolblocks(); ++j)
  793. for (size_type i = 0; i < m1.nrowblocks(); ++i)
  794. copy(m1.block(i,j), sub_matrix(m2, m1.subrowinterval(i),
  795. m1.subcolinterval(j)));
  796. }
  797. template <typename M1, typename M2>
  798. void copy(const block_matrix<M1> &m1, const M2 &m2)
  799. { copy(m1, linalg_const_cast(m2)); }
  800. template <typename MAT, typename V1, typename V2>
  801. void mult(const block_matrix<MAT> &m, const V1 &v1, V2 &v2) {
  802. clear(v2);
  803. typename sub_vector_type<V2 *, sub_interval>::vector_type sv;
  804. for (size_type i = 0; i < m.nrowblocks() ; ++i)
  805. for (size_type j = 0; j < m.ncolblocks() ; ++j) {
  806. sv = sub_vector(v2, m.subrowinterval(i));
  807. mult(m.block(i,j),
  808. sub_vector(v1, m.subcolinterval(j)), sv, sv);
  809. }
  810. }
  811. template <typename MAT, typename V1, typename V2, typename V3>
  812. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2, V3 &v3) {
  813. typename sub_vector_type<V3 *, sub_interval>::vector_type sv;
  814. for (size_type i = 0; i < m.nrowblocks() ; ++i)
  815. for (size_type j = 0; j < m.ncolblocks() ; ++j) {
  816. sv = sub_vector(v3, m.subrowinterval(i));
  817. if (j == 0)
  818. mult(m.block(i,j),
  819. sub_vector(v1, m.subcolinterval(j)),
  820. sub_vector(v2, m.subrowinterval(i)), sv);
  821. else
  822. mult(m.block(i,j),
  823. sub_vector(v1, m.subcolinterval(j)), sv, sv);
  824. }
  825. }
  826. template <typename MAT, typename V1, typename V2>
  827. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2)
  828. { mult(m, v1, linalg_const_cast(v2)); }
  829. template <typename MAT, typename V1, typename V2, typename V3>
  830. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2,
  831. const V3 &v3)
  832. { mult_const(m, v1, v2, linalg_const_cast(v3)); }
  833. }
  834. /* ******************************************************************** */
  835. /* */
  836. /* Distributed matrices */
  837. /* */
  838. /* ******************************************************************** */
  839. #ifdef GMM_USES_MPI
  840. # include <mpi.h>
  841. namespace gmm {
  842. template <typename T> inline MPI_Datatype mpi_type(T)
  843. { GMM_ASSERT1(false, "Sorry unsupported type"); return MPI_FLOAT; }
  844. inline MPI_Datatype mpi_type(double) { return MPI_DOUBLE; }
  845. inline MPI_Datatype mpi_type(float) { return MPI_FLOAT; }
  846. inline MPI_Datatype mpi_type(long double) { return MPI_LONG_DOUBLE; }
  847. #ifndef LAM_MPI
  848. inline MPI_Datatype mpi_type(std::complex<float>) { return MPI_COMPLEX; }
  849. inline MPI_Datatype mpi_type(std::complex<double>) { return MPI_DOUBLE_COMPLEX; }
  850. #endif
  851. inline MPI_Datatype mpi_type(int) { return MPI_INT; }
  852. inline MPI_Datatype mpi_type(unsigned int) { return MPI_UNSIGNED; }
  853. inline MPI_Datatype mpi_type(long) { return MPI_LONG; }
  854. inline MPI_Datatype mpi_type(unsigned long) { return MPI_UNSIGNED_LONG; }
  855. template <typename MAT> struct mpi_distributed_matrix {
  856. MAT M;
  857. mpi_distributed_matrix(size_type n, size_type m) : M(n, m) {}
  858. mpi_distributed_matrix() {}
  859. const MAT &local_matrix(void) const { return M; }
  860. MAT &local_matrix(void) { return M; }
  861. };
  862. template <typename MAT> inline MAT &eff_matrix(MAT &m) { return m; }
  863. template <typename MAT> inline
  864. const MAT &eff_matrix(const MAT &m) { return m; }
  865. template <typename MAT> inline
  866. MAT &eff_matrix(mpi_distributed_matrix<MAT> &m) { return m.M; }
  867. template <typename MAT> inline
  868. const MAT &eff_matrix(const mpi_distributed_matrix<MAT> &m) { return m.M; }
  869. template <typename MAT1, typename MAT2>
  870. inline void copy(const mpi_distributed_matrix<MAT1> &m1,
  871. mpi_distributed_matrix<MAT2> &m2)
  872. { copy(eff_matrix(m1), eff_matrix(m2)); }
  873. template <typename MAT1, typename MAT2>
  874. inline void copy(const mpi_distributed_matrix<MAT1> &m1,
  875. const mpi_distributed_matrix<MAT2> &m2)
  876. { copy(m1.M, m2.M); }
  877. template <typename MAT1, typename MAT2>
  878. inline void copy(const mpi_distributed_matrix<MAT1> &m1, MAT2 &m2)
  879. { copy(m1.M, m2); }
  880. template <typename MAT1, typename MAT2>
  881. inline void copy(const mpi_distributed_matrix<MAT1> &m1, const MAT2 &m2)
  882. { copy(m1.M, m2); }
  883. template <typename MATSP, typename V1, typename V2> inline
  884. typename strongest_value_type3<V1,V2,MATSP>::value_type
  885. vect_sp(const mpi_distributed_matrix<MATSP> &ps, const V1 &v1,
  886. const V2 &v2) {
  887. typedef typename strongest_value_type3<V1,V2,MATSP>::value_type T;
  888. T res = vect_sp(ps.M, v1, v2), rest;
  889. MPI_Allreduce(&res, &rest, 1, mpi_type(T()), MPI_SUM,MPI_COMM_WORLD);
  890. return rest;
  891. }
  892. template <typename MAT, typename V1, typename V2>
  893. inline void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  894. V2 &v2) {
  895. typedef typename linalg_traits<V2>::value_type T;
  896. std::vector<T> v3(vect_size(v2)), v4(vect_size(v2));
  897. static double tmult_tot = 0.0;
  898. static double tmult_tot2 = 0.0;
  899. double t_ref = MPI_Wtime();
  900. gmm::mult(m.M, v1, v3);
  901. if (is_sparse(v2)) GMM_WARNING2("Using a plain temporary, here.");
  902. double t_ref2 = MPI_Wtime();
  903. MPI_Allreduce(&(v3[0]), &(v4[0]),gmm::vect_size(v2), mpi_type(T()),
  904. MPI_SUM,MPI_COMM_WORLD);
  905. tmult_tot2 = MPI_Wtime()-t_ref2;
  906. cout << "reduce mult mpi = " << tmult_tot2 << endl;
  907. gmm::add(v4, v2);
  908. tmult_tot = MPI_Wtime()-t_ref;
  909. cout << "tmult mpi = " << tmult_tot << endl;
  910. }
  911. template <typename MAT, typename V1, typename V2>
  912. void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  913. const V2 &v2_)
  914. { mult_add(m, v1, const_cast<V2 &>(v2_)); }
  915. template <typename MAT, typename V1, typename V2>
  916. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  917. const V2 &v2_)
  918. { V2 &v2 = const_cast<V2 &>(v2_); clear(v2); mult_add(m, v1, v2); }
  919. template <typename MAT, typename V1, typename V2>
  920. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  921. V2 &v2)
  922. { clear(v2); mult_add(m, v1, v2); }
  923. template <typename MAT, typename V1, typename V2, typename V3>
  924. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  925. const V2 &v2, const V3 &v3_)
  926. { V3 &v3 = const_cast<V3 &>(v3_); gmm::copy(v2, v3); mult_add(m, v1, v3); }
  927. template <typename MAT, typename V1, typename V2, typename V3>
  928. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  929. const V2 &v2, V3 &v3)
  930. { gmm::copy(v2, v3); mult_add(m, v1, v3); }
  931. template <typename MAT> inline
  932. size_type mat_nrows(const mpi_distributed_matrix<MAT> &M)
  933. { return mat_nrows(M.M); }
  934. template <typename MAT> inline
  935. size_type mat_ncols(const mpi_distributed_matrix<MAT> &M)
  936. { return mat_nrows(M.M); }
  937. template <typename MAT> inline
  938. void resize(mpi_distributed_matrix<MAT> &M, size_type m, size_type n)
  939. { resize(M.M, m, n); }
  940. template <typename MAT> inline void clear(mpi_distributed_matrix<MAT> &M)
  941. { clear(M.M); }
  942. // For compute reduced system
  943. template <typename MAT1, typename MAT2> inline
  944. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  945. mpi_distributed_matrix<MAT2> &M3)
  946. { mult(M1, M2.M, M3.M); }
  947. template <typename MAT1, typename MAT2> inline
  948. void mult(const mpi_distributed_matrix<MAT2> &M2,
  949. const MAT1 &M1, mpi_distributed_matrix<MAT2> &M3)
  950. { mult(M2.M, M1, M3.M); }
  951. template <typename MAT1, typename MAT2, typename MAT3> inline
  952. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  953. MAT3 &M3)
  954. { mult(M1, M2.M, M3); }
  955. template <typename MAT1, typename MAT2, typename MAT3> inline
  956. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  957. const MAT3 &M3)
  958. { mult(M1, M2.M, M3); }
  959. template <typename M, typename SUBI1, typename SUBI2>
  960. struct sub_matrix_type<const mpi_distributed_matrix<M> *, SUBI1, SUBI2>
  961. { typedef abstract_null_type matrix_type; };
  962. template <typename M, typename SUBI1, typename SUBI2>
  963. struct sub_matrix_type<mpi_distributed_matrix<M> *, SUBI1, SUBI2>
  964. { typedef abstract_null_type matrix_type; };
  965. template <typename M, typename SUBI1, typename SUBI2> inline
  966. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI2>
  967. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>::matrix_type,
  968. M *>::return_type
  969. sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1, const SUBI2 &si2)
  970. { return sub_matrix(m.M, si1, si2); }
  971. template <typename MAT, typename SUBI1, typename SUBI2> inline
  972. typename select_return<typename sub_matrix_type<const MAT *, SUBI1, SUBI2>
  973. ::matrix_type, typename sub_matrix_type<MAT *, SUBI1, SUBI2>::matrix_type,
  974. const MAT *>::return_type
  975. sub_matrix(const mpi_distributed_matrix<MAT> &m, const SUBI1 &si1,
  976. const SUBI2 &si2)
  977. { return sub_matrix(m.M, si1, si2); }
  978. template <typename M, typename SUBI1> inline
  979. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  980. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  981. M *>::return_type
  982. sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1)
  983. { return sub_matrix(m.M, si1, si1); }
  984. template <typename M, typename SUBI1> inline
  985. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  986. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  987. const M *>::return_type
  988. sub_matrix(const mpi_distributed_matrix<M> &m, const SUBI1 &si1)
  989. { return sub_matrix(m.M, si1, si1); }
  990. template <typename L> struct transposed_return<const mpi_distributed_matrix<L> *>
  991. { typedef abstract_null_type return_type; };
  992. template <typename L> struct transposed_return<mpi_distributed_matrix<L> *>
  993. { typedef abstract_null_type return_type; };
  994. template <typename L> inline typename transposed_return<const L *>::return_type
  995. transposed(const mpi_distributed_matrix<L> &l)
  996. { return transposed(l.M); }
  997. template <typename L> inline typename transposed_return<L *>::return_type
  998. transposed(mpi_distributed_matrix<L> &l)
  999. { return transposed(l.M); }
  1000. template <typename MAT>
  1001. struct linalg_traits<mpi_distributed_matrix<MAT> > {
  1002. typedef mpi_distributed_matrix<MAT> this_type;
  1003. typedef MAT origin_type;
  1004. typedef linalg_false is_reference;
  1005. typedef abstract_matrix linalg_type;
  1006. typedef typename linalg_traits<MAT>::value_type value_type;
  1007. typedef typename linalg_traits<MAT>::reference reference;
  1008. typedef typename linalg_traits<MAT>::storage_type storage_type;
  1009. typedef abstract_null_type sub_row_type;
  1010. typedef abstract_null_type const_sub_row_type;
  1011. typedef abstract_null_type row_iterator;
  1012. typedef abstract_null_type const_row_iterator;
  1013. typedef abstract_null_type sub_col_type;
  1014. typedef abstract_null_type const_sub_col_type;
  1015. typedef abstract_null_type col_iterator;
  1016. typedef abstract_null_type const_col_iterator;
  1017. typedef abstract_null_type sub_orientation;
  1018. typedef abstract_null_type index_sorted;
  1019. static size_type nrows(const this_type &m) { return nrows(m.M); }
  1020. static size_type ncols(const this_type &m) { return ncols(m.M); }
  1021. static void do_clear(this_type &m) { clear(m.M); }
  1022. };
  1023. }
  1024. #endif // GMM_USES_MPI
  1025. namespace std {
  1026. template <typename V>
  1027. void swap(gmm::row_matrix<V> &m1, gmm::row_matrix<V> &m2)
  1028. { m1.swap(m2); }
  1029. template <typename V>
  1030. void swap(gmm::col_matrix<V> &m1, gmm::col_matrix<V> &m2)
  1031. { m1.swap(m2); }
  1032. template <typename T>
  1033. void swap(gmm::dense_matrix<T> &m1, gmm::dense_matrix<T> &m2)
  1034. { m1.swap(m2); }
  1035. template <typename T, int shift> void
  1036. swap(gmm::csc_matrix<T,shift> &m1, gmm::csc_matrix<T,shift> &m2)
  1037. { m1.swap(m2); }
  1038. template <typename T, int shift> void
  1039. swap(gmm::csr_matrix<T,shift> &m1, gmm::csr_matrix<T,shift> &m2)
  1040. { m1.swap(m2); }
  1041. }
  1042. #endif /* GMM_MATRIX_H__ */