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.

1201 lines
48 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 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 &I, const M &v1)
  56. { add(I, 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 simple_vector_ref<V *> sub_row_type;
  169. typedef simple_vector_ref<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 simple_vector_ref<V *> sub_col_type;
  261. typedef simple_vector_ref<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 const_sub_col_type(*it); }
  280. static sub_col_type col(const col_iterator &it)
  281. { return sub_col_type(*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. void resize(size_type, size_type);
  320. void reshape(size_type, size_type);
  321. void fill(T a, T b = T(0));
  322. inline size_type nrows(void) const { return nbl; }
  323. inline size_type ncols(void) const { return nbc; }
  324. void swap(dense_matrix<T> &m)
  325. { std::vector<T>::swap(m); std::swap(nbc, m.nbc); std::swap(nbl, m.nbl); }
  326. dense_matrix(size_type l, size_type c)
  327. : std::vector<T>(c*l), nbc(c), nbl(l) {}
  328. dense_matrix(void) { nbl = nbc = 0; }
  329. };
  330. template<typename T> void dense_matrix<T>::reshape(size_type m,size_type n) {
  331. GMM_ASSERT2(n*m == nbl*nbc, "dimensions mismatch");
  332. nbl = m; nbc = n;
  333. }
  334. template<typename T> void dense_matrix<T>::resize(size_type m, size_type n) {
  335. if (n*m > nbc*nbl) std::vector<T>::resize(n*m);
  336. if (m < nbl) {
  337. for (size_type i = 1; i < std::min(nbc, n); ++i)
  338. std::copy(this->begin()+i*nbl, this->begin()+(i*nbl+m),
  339. this->begin()+i*m);
  340. for (size_type i = std::min(nbc, n); i < n; ++i)
  341. std::fill(this->begin()+(i*m), this->begin()+(i+1)*m, T(0));
  342. }
  343. else if (m > nbl) { /* do nothing when the nb of rows does not change */
  344. for (size_type i = std::min(nbc, n); i > 1; --i)
  345. std::copy(this->begin()+(i-1)*nbl, this->begin()+i*nbl,
  346. this->begin()+(i-1)*m);
  347. for (size_type i = 0; i < std::min(nbc, n); ++i)
  348. std::fill(this->begin()+(i*m+nbl), this->begin()+(i+1)*m, T(0));
  349. }
  350. if (n*m < nbc*nbl) std::vector<T>::resize(n*m);
  351. nbl = m; nbc = n;
  352. }
  353. template<typename T> void dense_matrix<T>::fill(T a, T b) {
  354. std::fill(this->begin(), this->end(), b);
  355. size_type n = std::min(nbl, nbc);
  356. if (a != b) for (size_type i = 0; i < n; ++i) (*this)(i,i) = a;
  357. }
  358. template <typename T> struct linalg_traits<dense_matrix<T> > {
  359. typedef dense_matrix<T> this_type;
  360. typedef this_type origin_type;
  361. typedef linalg_false is_reference;
  362. typedef abstract_matrix linalg_type;
  363. typedef T value_type;
  364. typedef T& reference;
  365. typedef abstract_dense storage_type;
  366. typedef tab_ref_reg_spaced_with_origin<typename this_type::iterator,
  367. this_type> sub_row_type;
  368. typedef tab_ref_reg_spaced_with_origin<typename this_type::const_iterator,
  369. this_type> const_sub_row_type;
  370. typedef dense_compressed_iterator<typename this_type::iterator,
  371. typename this_type::iterator,
  372. this_type *> row_iterator;
  373. typedef dense_compressed_iterator<typename this_type::const_iterator,
  374. typename this_type::iterator,
  375. const this_type *> const_row_iterator;
  376. typedef tab_ref_with_origin<typename this_type::iterator,
  377. this_type> sub_col_type;
  378. typedef tab_ref_with_origin<typename this_type::const_iterator,
  379. this_type> const_sub_col_type;
  380. typedef dense_compressed_iterator<typename this_type::iterator,
  381. typename this_type::iterator,
  382. this_type *> col_iterator;
  383. typedef dense_compressed_iterator<typename this_type::const_iterator,
  384. typename this_type::iterator,
  385. const this_type *> const_col_iterator;
  386. typedef col_and_row sub_orientation;
  387. typedef linalg_true index_sorted;
  388. static size_type nrows(const this_type &m) { return m.nrows(); }
  389. static size_type ncols(const this_type &m) { return m.ncols(); }
  390. static const_sub_row_type row(const const_row_iterator &it)
  391. { return const_sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  392. static const_sub_col_type col(const const_col_iterator &it)
  393. { return const_sub_col_type(*it, *it + it.nrows, it.origin); }
  394. static sub_row_type row(const row_iterator &it)
  395. { return sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  396. static sub_col_type col(const col_iterator &it)
  397. { return sub_col_type(*it, *it + it.nrows, it.origin); }
  398. static row_iterator row_begin(this_type &m)
  399. { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
  400. static row_iterator row_end(this_type &m)
  401. { return row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
  402. static const_row_iterator row_begin(const this_type &m)
  403. { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), 0, &m); }
  404. static const_row_iterator row_end(const this_type &m)
  405. { return const_row_iterator(m.begin(), m.size() ? 1 : 0, m.nrows(), m.ncols(), m.nrows(), &m); }
  406. static col_iterator col_begin(this_type &m)
  407. { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
  408. static col_iterator col_end(this_type &m)
  409. { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), m.ncols(), &m); }
  410. static const_col_iterator col_begin(const this_type &m)
  411. { return const_col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
  412. static const_col_iterator col_end(const this_type &m)
  413. { return const_col_iterator(m.begin(),m.nrows(),m.nrows(),m.ncols(),m.ncols(), &m); }
  414. static origin_type* origin(this_type &m) { return &m; }
  415. static const origin_type* origin(const this_type &m) { return &m; }
  416. static void do_clear(this_type &m) { m.fill(value_type(0)); }
  417. static value_type access(const const_col_iterator &itcol, size_type j)
  418. { return (*itcol)[j]; }
  419. static reference access(const col_iterator &itcol, size_type j)
  420. { return (*itcol)[j]; }
  421. static void resize(this_type &v, size_type m, size_type n)
  422. { v.resize(m,n); }
  423. static void reshape(this_type &v, size_type m, size_type n)
  424. { v.reshape(m, n); }
  425. };
  426. template<typename T> std::ostream &operator <<
  427. (std::ostream &o, const dense_matrix<T>& m) { gmm::write(o,m); return o; }
  428. /* ******************************************************************** */
  429. /* */
  430. /* Read only compressed sparse column matrix */
  431. /* */
  432. /* ******************************************************************** */
  433. template <typename T, int shift = 0>
  434. struct csc_matrix {
  435. typedef unsigned int IND_TYPE;
  436. std::vector<T> pr;
  437. std::vector<IND_TYPE> ir;
  438. std::vector<IND_TYPE> jc;
  439. size_type nc, nr;
  440. typedef T value_type;
  441. typedef T& access_type;
  442. template <typename Matrix> void init_with_good_format(const Matrix &B);
  443. template <typename Matrix> void init_with(const Matrix &A);
  444. void init_with(const col_matrix<gmm::rsvector<T> > &B)
  445. { init_with_good_format(B); }
  446. void init_with(const col_matrix<wsvector<T> > &B)
  447. { init_with_good_format(B); }
  448. template <typename PT1, typename PT2, typename PT3, int cshift>
  449. void init_with(const csc_matrix_ref<PT1,PT2,PT3,cshift>& B)
  450. { init_with_good_format(B); }
  451. template <typename U, int cshift>
  452. void init_with(const csc_matrix<U, cshift>& B)
  453. { init_with_good_format(B); }
  454. void init_with_identity(size_type n);
  455. csc_matrix(void) : nc(0), nr(0) {}
  456. csc_matrix(size_type nnr, size_type nnc);
  457. size_type nrows(void) const { return nr; }
  458. size_type ncols(void) const { return nc; }
  459. void swap(csc_matrix<T, shift> &m) {
  460. std::swap(pr, m.pr);
  461. std::swap(ir, m.ir); std::swap(jc, m.jc);
  462. std::swap(nc, m.nc); std::swap(nr, m.nr);
  463. }
  464. value_type operator()(size_type i, size_type j) const
  465. { return mat_col(*this, j)[i]; }
  466. };
  467. template <typename T, int shift> template<typename Matrix>
  468. void csc_matrix<T, shift>::init_with_good_format(const Matrix &B) {
  469. typedef typename linalg_traits<Matrix>::const_sub_col_type col_type;
  470. nc = mat_ncols(B); nr = mat_nrows(B);
  471. jc.resize(nc+1);
  472. jc[0] = shift;
  473. for (size_type j = 0; j < nc; ++j) {
  474. jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_col(B, j)));
  475. }
  476. pr.resize(jc[nc]);
  477. ir.resize(jc[nc]);
  478. for (size_type j = 0; j < nc; ++j) {
  479. col_type col = mat_const_col(B, j);
  480. typename linalg_traits<col_type>::const_iterator
  481. it = vect_const_begin(col), ite = vect_const_end(col);
  482. for (size_type k = 0; it != ite; ++it, ++k) {
  483. pr[jc[j]-shift+k] = *it;
  484. ir[jc[j]-shift+k] = IND_TYPE(it.index() + shift);
  485. }
  486. }
  487. }
  488. template <typename T, int shift> template <typename Matrix>
  489. void csc_matrix<T, shift>::init_with(const Matrix &A) {
  490. col_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
  491. copy(A, B);
  492. init_with_good_format(B);
  493. }
  494. template <typename T, int shift>
  495. void csc_matrix<T, shift>::init_with_identity(size_type n) {
  496. nc = nr = n;
  497. pr.resize(nc); ir.resize(nc); jc.resize(nc+1);
  498. for (size_type j = 0; j < nc; ++j)
  499. { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
  500. jc[nc] = shift + nc;
  501. }
  502. template <typename T, int shift>
  503. csc_matrix<T, shift>::csc_matrix(size_type nnr, size_type nnc)
  504. : nc(nnc), nr(nnr) {
  505. pr.resize(1); ir.resize(1); jc.resize(nc+1);
  506. for (size_type j = 0; j <= nc; ++j) jc[j] = shift;
  507. }
  508. template <typename T, int shift>
  509. struct linalg_traits<csc_matrix<T, shift> > {
  510. typedef csc_matrix<T, shift> this_type;
  511. typedef typename this_type::IND_TYPE IND_TYPE;
  512. typedef linalg_const is_reference;
  513. typedef abstract_matrix linalg_type;
  514. typedef T value_type;
  515. typedef T origin_type;
  516. typedef T reference;
  517. typedef abstract_sparse storage_type;
  518. typedef abstract_null_type sub_row_type;
  519. typedef abstract_null_type const_sub_row_type;
  520. typedef abstract_null_type row_iterator;
  521. typedef abstract_null_type const_row_iterator;
  522. typedef abstract_null_type sub_col_type;
  523. typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
  524. const_sub_col_type;
  525. typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
  526. const IND_TYPE *, shift>
  527. const_col_iterator;
  528. typedef abstract_null_type col_iterator;
  529. typedef col_major sub_orientation;
  530. typedef linalg_true index_sorted;
  531. static size_type nrows(const this_type &m) { return m.nrows(); }
  532. static size_type ncols(const this_type &m) { return m.ncols(); }
  533. static const_col_iterator col_begin(const this_type &m)
  534. { return const_col_iterator(&m.pr[0],&m.ir[0],&m.jc[0], m.nr, &m.pr[0]); }
  535. static const_col_iterator col_end(const this_type &m) {
  536. return const_col_iterator(&m.pr[0],&m.ir[0],&m.jc[0]+m.nc,
  537. m.nr,&m.pr[0]);
  538. }
  539. static const_sub_col_type col(const const_col_iterator &it) {
  540. return const_sub_col_type(it.pr + *(it.jc) - shift,
  541. it.ir + *(it.jc) - shift,
  542. *(it.jc + 1) - *(it.jc), it.n);
  543. }
  544. static const origin_type* origin(const this_type &m) { return &m.pr[0]; }
  545. static void do_clear(this_type &m) { m.do_clear(); }
  546. static value_type access(const const_col_iterator &itcol, size_type j)
  547. { return col(itcol)[j]; }
  548. };
  549. template <typename T, int shift>
  550. std::ostream &operator <<
  551. (std::ostream &o, const csc_matrix<T, shift>& m)
  552. { gmm::write(o,m); return o; }
  553. template <typename T, int shift>
  554. inline void copy(const identity_matrix &, csc_matrix<T, shift>& M)
  555. { M.init_with_identity(mat_nrows(M)); }
  556. template <typename Matrix, typename T, int shift>
  557. inline void copy(const Matrix &A, csc_matrix<T, shift>& M)
  558. { M.init_with(A); }
  559. /* ******************************************************************** */
  560. /* */
  561. /* Read only compressed sparse row matrix */
  562. /* */
  563. /* ******************************************************************** */
  564. template <typename T, int shift = 0>
  565. struct csr_matrix {
  566. typedef unsigned int IND_TYPE;
  567. std::vector<T> pr; // values.
  568. std::vector<IND_TYPE> ir; // col indices.
  569. std::vector<IND_TYPE> jc; // row repartition on pr and ir.
  570. size_type nc, nr;
  571. typedef T value_type;
  572. typedef T& access_type;
  573. template <typename Matrix> void init_with_good_format(const Matrix &B);
  574. void init_with(const row_matrix<wsvector<T> > &B)
  575. { init_with_good_format(B); }
  576. void init_with(const row_matrix<rsvector<T> > &B)
  577. { init_with_good_format(B); }
  578. template <typename PT1, typename PT2, typename PT3, int cshift>
  579. void init_with(const csr_matrix_ref<PT1,PT2,PT3,cshift>& B)
  580. { init_with_good_format(B); }
  581. template <typename U, int cshift>
  582. void init_with(const csr_matrix<U, cshift>& B)
  583. { init_with_good_format(B); }
  584. template <typename Matrix> void init_with(const Matrix &A);
  585. void init_with_identity(size_type n);
  586. csr_matrix(void) : nc(0), nr(0) {}
  587. csr_matrix(size_type nnr, size_type nnc);
  588. size_type nrows(void) const { return nr; }
  589. size_type ncols(void) const { return nc; }
  590. void swap(csr_matrix<T, shift> &m) {
  591. std::swap(pr, m.pr);
  592. std::swap(ir,m.ir); std::swap(jc, m.jc);
  593. std::swap(nc, m.nc); std::swap(nr,m.nr);
  594. }
  595. value_type operator()(size_type i, size_type j) const
  596. { return mat_row(*this, i)[j]; }
  597. };
  598. template <typename T, int shift> template <typename Matrix>
  599. void csr_matrix<T, shift>::init_with_good_format(const Matrix &B) {
  600. typedef typename linalg_traits<Matrix>::const_sub_row_type row_type;
  601. nc = mat_ncols(B); nr = mat_nrows(B);
  602. jc.resize(nr+1);
  603. jc[0] = shift;
  604. for (size_type j = 0; j < nr; ++j) {
  605. jc[j+1] = IND_TYPE(jc[j] + nnz(mat_const_row(B, j)));
  606. }
  607. pr.resize(jc[nr]);
  608. ir.resize(jc[nr]);
  609. for (size_type j = 0; j < nr; ++j) {
  610. row_type row = mat_const_row(B, j);
  611. typename linalg_traits<row_type>::const_iterator
  612. it = vect_const_begin(row), ite = vect_const_end(row);
  613. for (size_type k = 0; it != ite; ++it, ++k) {
  614. pr[jc[j]-shift+k] = *it;
  615. ir[jc[j]-shift+k] = IND_TYPE(it.index()+shift);
  616. }
  617. }
  618. }
  619. template <typename T, int shift> template <typename Matrix>
  620. void csr_matrix<T, shift>::init_with(const Matrix &A) {
  621. row_matrix<wsvector<T> > B(mat_nrows(A), mat_ncols(A));
  622. copy(A, B);
  623. init_with_good_format(B);
  624. }
  625. template <typename T, int shift>
  626. void csr_matrix<T, shift>::init_with_identity(size_type n) {
  627. nc = nr = n;
  628. pr.resize(nr); ir.resize(nr); jc.resize(nr+1);
  629. for (size_type j = 0; j < nr; ++j)
  630. { ir[j] = jc[j] = shift + j; pr[j] = T(1); }
  631. jc[nr] = shift + nr;
  632. }
  633. template <typename T, int shift>
  634. csr_matrix<T, shift>::csr_matrix(size_type nnr, size_type nnc)
  635. : nc(nnc), nr(nnr) {
  636. pr.resize(1); ir.resize(1); jc.resize(nr+1);
  637. for (size_type j = 0; j < nr; ++j) jc[j] = shift;
  638. jc[nr] = shift;
  639. }
  640. template <typename T, int shift>
  641. struct linalg_traits<csr_matrix<T, shift> > {
  642. typedef csr_matrix<T, shift> this_type;
  643. typedef typename this_type::IND_TYPE IND_TYPE;
  644. typedef linalg_const is_reference;
  645. typedef abstract_matrix linalg_type;
  646. typedef T value_type;
  647. typedef T origin_type;
  648. typedef T reference;
  649. typedef abstract_sparse storage_type;
  650. typedef abstract_null_type sub_col_type;
  651. typedef abstract_null_type const_sub_col_type;
  652. typedef abstract_null_type col_iterator;
  653. typedef abstract_null_type const_col_iterator;
  654. typedef abstract_null_type sub_row_type;
  655. typedef cs_vector_ref<const T *, const IND_TYPE *, shift>
  656. const_sub_row_type;
  657. typedef sparse_compressed_iterator<const T *, const IND_TYPE *,
  658. const IND_TYPE *, shift>
  659. const_row_iterator;
  660. typedef abstract_null_type row_iterator;
  661. typedef row_major sub_orientation;
  662. typedef linalg_true index_sorted;
  663. static size_type nrows(const this_type &m) { return m.nrows(); }
  664. static size_type ncols(const this_type &m) { return m.ncols(); }
  665. static const_row_iterator row_begin(const this_type &m)
  666. { return const_row_iterator(&m.pr[0], &m.ir[0], &m.jc[0], m.nc, &m.pr[0]); }
  667. static const_row_iterator row_end(const this_type &m)
  668. { return const_row_iterator(&m.pr[0], &m.ir[0], &m.jc[0] + m.nr, m.nc, &m.pr[0]); }
  669. static const_sub_row_type row(const const_row_iterator &it) {
  670. return const_sub_row_type(it.pr + *(it.jc) - shift,
  671. it.ir + *(it.jc) - shift,
  672. *(it.jc + 1) - *(it.jc), it.n);
  673. }
  674. static const origin_type* origin(const this_type &m) { return &m.pr[0]; }
  675. static void do_clear(this_type &m) { m.do_clear(); }
  676. static value_type access(const const_row_iterator &itrow, size_type j)
  677. { return row(itrow)[j]; }
  678. };
  679. template <typename T, int shift>
  680. std::ostream &operator <<
  681. (std::ostream &o, const csr_matrix<T, shift>& m)
  682. { gmm::write(o,m); return o; }
  683. template <typename T, int shift>
  684. inline void copy(const identity_matrix &, csr_matrix<T, shift>& M)
  685. { M.init_with_identity(mat_nrows(M)); }
  686. template <typename Matrix, typename T, int shift>
  687. inline void copy(const Matrix &A, csr_matrix<T, shift>& M)
  688. { M.init_with(A); }
  689. /* ******************************************************************** */
  690. /* */
  691. /* Block matrix */
  692. /* */
  693. /* ******************************************************************** */
  694. template <typename MAT> class block_matrix {
  695. protected :
  696. std::vector<MAT> blocks;
  697. size_type nrowblocks_;
  698. size_type ncolblocks_;
  699. std::vector<sub_interval> introw, intcol;
  700. public :
  701. typedef typename linalg_traits<MAT>::value_type value_type;
  702. typedef typename linalg_traits<MAT>::reference reference;
  703. size_type nrows(void) const { return introw[nrowblocks_-1].max; }
  704. size_type ncols(void) const { return intcol[ncolblocks_-1].max; }
  705. size_type nrowblocks(void) const { return nrowblocks_; }
  706. size_type ncolblocks(void) const { return ncolblocks_; }
  707. const sub_interval &subrowinterval(size_type i) const { return introw[i]; }
  708. const sub_interval &subcolinterval(size_type i) const { return intcol[i]; }
  709. const MAT &block(size_type i, size_type j) const
  710. { return blocks[j*ncolblocks_+i]; }
  711. MAT &block(size_type i, size_type j)
  712. { return blocks[j*ncolblocks_+i]; }
  713. void do_clear(void);
  714. // to be done : read and write access to a component
  715. value_type operator() (size_type i, size_type j) const {
  716. size_type k, l;
  717. for (k = 0; k < nrowblocks_; ++k)
  718. if (i >= introw[k].min && i < introw[k].max) break;
  719. for (l = 0; l < nrowblocks_; ++l)
  720. if (j >= introw[l].min && j < introw[l].max) break;
  721. return (block(k, l))(i - introw[k].min, j - introw[l].min);
  722. }
  723. reference operator() (size_type i, size_type j) {
  724. size_type k, l;
  725. for (k = 0; k < nrowblocks_; ++k)
  726. if (i >= introw[k].min && i < introw[k].max) break;
  727. for (l = 0; l < nrowblocks_; ++l)
  728. if (j >= introw[l].min && j < introw[l].max) break;
  729. return (block(k, l))(i - introw[k].min, j - introw[l].min);
  730. }
  731. template <typename CONT> void resize(const CONT &c1, const CONT &c2);
  732. template <typename CONT> block_matrix(const CONT &c1, const CONT &c2)
  733. { resize(c1, c2); }
  734. block_matrix(void) {}
  735. };
  736. template <typename MAT> struct linalg_traits<block_matrix<MAT> > {
  737. typedef block_matrix<MAT> this_type;
  738. typedef linalg_false is_reference;
  739. typedef abstract_matrix linalg_type;
  740. typedef this_type origin_type;
  741. typedef typename linalg_traits<MAT>::value_type value_type;
  742. typedef typename linalg_traits<MAT>::reference reference;
  743. typedef typename linalg_traits<MAT>::storage_type storage_type;
  744. typedef abstract_null_type sub_row_type; // to be done ...
  745. typedef abstract_null_type const_sub_row_type; // to be done ...
  746. typedef abstract_null_type row_iterator; // to be done ...
  747. typedef abstract_null_type const_row_iterator; // to be done ...
  748. typedef abstract_null_type sub_col_type; // to be done ...
  749. typedef abstract_null_type const_sub_col_type; // to be done ...
  750. typedef abstract_null_type col_iterator; // to be done ...
  751. typedef abstract_null_type const_col_iterator; // to be done ...
  752. typedef abstract_null_type sub_orientation; // to be done ...
  753. typedef linalg_true index_sorted;
  754. static size_type nrows(const this_type &m) { return m.nrows(); }
  755. static size_type ncols(const this_type &m) { return m.ncols(); }
  756. static origin_type* origin(this_type &m) { return &m; }
  757. static const origin_type* origin(const this_type &m) { return &m; }
  758. static void do_clear(this_type &m) { m.do_clear(); }
  759. // access to be done ...
  760. static void resize(this_type &, size_type , size_type)
  761. { GMM_ASSERT1(false, "Sorry, to be done"); }
  762. static void reshape(this_type &, size_type , size_type)
  763. { GMM_ASSERT1(false, "Sorry, to be done"); }
  764. };
  765. template <typename MAT> void block_matrix<MAT>::do_clear(void) {
  766. for (size_type j = 0, l = 0; j < ncolblocks_; ++j)
  767. for (size_type i = 0, k = 0; i < nrowblocks_; ++i)
  768. clear(block(i,j));
  769. }
  770. template <typename MAT> template <typename CONT>
  771. void block_matrix<MAT>::resize(const CONT &c1, const CONT &c2) {
  772. nrowblocks_ = c1.size(); ncolblocks_ = c2.size();
  773. blocks.resize(nrowblocks_ * ncolblocks_);
  774. intcol.resize(ncolblocks_);
  775. introw.resize(nrowblocks_);
  776. for (size_type j = 0, l = 0; j < ncolblocks_; ++j) {
  777. intcol[j] = sub_interval(l, c2[j]); l += c2[j];
  778. for (size_type i = 0, k = 0; i < nrowblocks_; ++i) {
  779. if (j == 0) { introw[i] = sub_interval(k, c1[i]); k += c1[i]; }
  780. block(i, j) = MAT(c1[i], c2[j]);
  781. }
  782. }
  783. }
  784. template <typename M1, typename M2>
  785. void copy(const block_matrix<M1> &m1, M2 &m2) {
  786. for (size_type j = 0; j < m1.ncolblocks(); ++j)
  787. for (size_type i = 0; i < m1.nrowblocks(); ++i)
  788. copy(m1.block(i,j), sub_matrix(m2, m1.subrowinterval(i),
  789. m1.subcolinterval(j)));
  790. }
  791. template <typename M1, typename M2>
  792. void copy(const block_matrix<M1> &m1, const M2 &m2)
  793. { copy(m1, linalg_const_cast(m2)); }
  794. template <typename MAT, typename V1, typename V2>
  795. void mult(const block_matrix<MAT> &m, const V1 &v1, V2 &v2) {
  796. clear(v2);
  797. typename sub_vector_type<V2 *, sub_interval>::vector_type sv;
  798. for (size_type i = 0; i < m.nrowblocks() ; ++i)
  799. for (size_type j = 0; j < m.ncolblocks() ; ++j) {
  800. sv = sub_vector(v2, m.subrowinterval(i));
  801. mult(m.block(i,j),
  802. sub_vector(v1, m.subcolinterval(j)), sv, sv);
  803. }
  804. }
  805. template <typename MAT, typename V1, typename V2, typename V3>
  806. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2, V3 &v3) {
  807. typename sub_vector_type<V3 *, sub_interval>::vector_type sv;
  808. for (size_type i = 0; i < m.nrowblocks() ; ++i)
  809. for (size_type j = 0; j < m.ncolblocks() ; ++j) {
  810. sv = sub_vector(v3, m.subrowinterval(i));
  811. if (j == 0)
  812. mult(m.block(i,j),
  813. sub_vector(v1, m.subcolinterval(j)),
  814. sub_vector(v2, m.subrowinterval(i)), sv);
  815. else
  816. mult(m.block(i,j),
  817. sub_vector(v1, m.subcolinterval(j)), sv, sv);
  818. }
  819. }
  820. template <typename MAT, typename V1, typename V2>
  821. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2)
  822. { mult(m, v1, linalg_const_cast(v2)); }
  823. template <typename MAT, typename V1, typename V2, typename V3>
  824. void mult(const block_matrix<MAT> &m, const V1 &v1, const V2 &v2,
  825. const V3 &v3)
  826. { mult_const(m, v1, v2, linalg_const_cast(v3)); }
  827. }
  828. /* ******************************************************************** */
  829. /* */
  830. /* Distributed matrices */
  831. /* */
  832. /* ******************************************************************** */
  833. #ifdef GMM_USES_MPI
  834. // Problem : GETFEM_HAVE_MPI_H not defined in gmm : NOT SATIFACTORY !!
  835. #include<getfem/getfem_arch_config.h>
  836. # if defined(GETFEM_HAVE_MPI_H)
  837. # include <mpi.h>
  838. # elif defined(GETFEM_HAVE_MPI_MPI_H)
  839. # include <mpi/mpi.h>
  840. # elif defined(GETFEM_HAVE_MPICH2_MPI_H)
  841. # include <mpich2/mpi.h>
  842. # endif
  843. namespace gmm {
  844. template <typename T> inline MPI_Datatype mpi_type(T)
  845. { GMM_ASSERT1(false, "Sorry unsupported type"); return MPI_FLOAT; }
  846. inline MPI_Datatype mpi_type(double) { return MPI_DOUBLE; }
  847. inline MPI_Datatype mpi_type(float) { return MPI_FLOAT; }
  848. inline MPI_Datatype mpi_type(long double) { return MPI_LONG_DOUBLE; }
  849. #ifndef LAM_MPI
  850. inline MPI_Datatype mpi_type(std::complex<float>) { return MPI_COMPLEX; }
  851. inline MPI_Datatype mpi_type(std::complex<double>) { return MPI_DOUBLE_COMPLEX; }
  852. #endif
  853. inline MPI_Datatype mpi_type(int) { return MPI_INT; }
  854. inline MPI_Datatype mpi_type(unsigned int) { return MPI_UNSIGNED; }
  855. inline MPI_Datatype mpi_type(long) { return MPI_LONG; }
  856. inline MPI_Datatype mpi_type(unsigned long) { return MPI_UNSIGNED_LONG; }
  857. template <typename MAT> struct mpi_distributed_matrix {
  858. MAT M;
  859. mpi_distributed_matrix(size_type n, size_type m) : M(n, m) {}
  860. mpi_distributed_matrix() {}
  861. const MAT &local_matrix(void) const { return M; }
  862. MAT &local_matrix(void) { return M; }
  863. };
  864. template <typename MAT> inline MAT &eff_matrix(MAT &m) { return m; }
  865. template <typename MAT> inline
  866. const MAT &eff_matrix(const MAT &m) { return m; }
  867. template <typename MAT> inline
  868. MAT &eff_matrix(mpi_distributed_matrix<MAT> &m) { return m.M; }
  869. template <typename MAT> inline
  870. const MAT &eff_matrix(const mpi_distributed_matrix<MAT> &m) { return m.M; }
  871. template <typename MAT1, typename MAT2>
  872. inline void copy(const mpi_distributed_matrix<MAT1> &m1,
  873. mpi_distributed_matrix<MAT2> &m2)
  874. { copy(eff_matrix(m1), eff_matrix(m2)); }
  875. template <typename MAT1, typename MAT2>
  876. inline void copy(const mpi_distributed_matrix<MAT1> &m1,
  877. const mpi_distributed_matrix<MAT2> &m2)
  878. { copy(m1.M, m2.M); }
  879. template <typename MAT1, typename MAT2>
  880. inline void copy(const mpi_distributed_matrix<MAT1> &m1, MAT2 &m2)
  881. { copy(m1.M, m2); }
  882. template <typename MAT1, typename MAT2>
  883. inline void copy(const mpi_distributed_matrix<MAT1> &m1, const MAT2 &m2)
  884. { copy(m1.M, m2); }
  885. template <typename MATSP, typename V1, typename V2> inline
  886. typename strongest_value_type3<V1,V2,MATSP>::value_type
  887. vect_sp(const mpi_distributed_matrix<MATSP> &ps, const V1 &v1,
  888. const V2 &v2) {
  889. typedef typename strongest_value_type3<V1,V2,MATSP>::value_type T;
  890. T res = vect_sp(ps.M, v1, v2), rest;
  891. MPI_Allreduce(&res, &rest, 1, mpi_type(T()), MPI_SUM,MPI_COMM_WORLD);
  892. return rest;
  893. }
  894. template <typename MAT, typename V1, typename V2>
  895. inline void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  896. V2 &v2) {
  897. typedef typename linalg_traits<V2>::value_type T;
  898. std::vector<T> v3(vect_size(v2)), v4(vect_size(v2));
  899. static double tmult_tot = 0.0;
  900. static double tmult_tot2 = 0.0;
  901. double t_ref = MPI_Wtime();
  902. gmm::mult(m.M, v1, v3);
  903. if (is_sparse(v2)) GMM_WARNING2("Using a plain temporary, here.");
  904. double t_ref2 = MPI_Wtime();
  905. MPI_Allreduce(&(v3[0]), &(v4[0]),gmm::vect_size(v2), mpi_type(T()),
  906. MPI_SUM,MPI_COMM_WORLD);
  907. tmult_tot2 = MPI_Wtime()-t_ref2;
  908. cout << "reduce mult mpi = " << tmult_tot2 << endl;
  909. gmm::add(v4, v2);
  910. tmult_tot = MPI_Wtime()-t_ref;
  911. cout << "tmult mpi = " << tmult_tot << endl;
  912. }
  913. template <typename MAT, typename V1, typename V2>
  914. void mult_add(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  915. const V2 &v2_)
  916. { mult_add(m, v1, const_cast<V2 &>(v2_)); }
  917. template <typename MAT, typename V1, typename V2>
  918. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  919. const V2 &v2_)
  920. { V2 &v2 = const_cast<V2 &>(v2_); clear(v2); mult_add(m, v1, v2); }
  921. template <typename MAT, typename V1, typename V2>
  922. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  923. V2 &v2)
  924. { clear(v2); mult_add(m, v1, v2); }
  925. template <typename MAT, typename V1, typename V2, typename V3>
  926. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  927. const V2 &v2, const V3 &v3_)
  928. { V3 &v3 = const_cast<V3 &>(v3_); gmm::copy(v2, v3); mult_add(m, v1, v3); }
  929. template <typename MAT, typename V1, typename V2, typename V3>
  930. inline void mult(const mpi_distributed_matrix<MAT> &m, const V1 &v1,
  931. const V2 &v2, V3 &v3)
  932. { gmm::copy(v2, v3); mult_add(m, v1, v3); }
  933. template <typename MAT> inline
  934. size_type mat_nrows(const mpi_distributed_matrix<MAT> &M)
  935. { return mat_nrows(M.M); }
  936. template <typename MAT> inline
  937. size_type mat_ncols(const mpi_distributed_matrix<MAT> &M)
  938. { return mat_nrows(M.M); }
  939. template <typename MAT> inline
  940. void resize(mpi_distributed_matrix<MAT> &M, size_type m, size_type n)
  941. { resize(M.M, m, n); }
  942. template <typename MAT> inline void clear(mpi_distributed_matrix<MAT> &M)
  943. { clear(M.M); }
  944. // For compute reduced system
  945. template <typename MAT1, typename MAT2> inline
  946. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  947. mpi_distributed_matrix<MAT2> &M3)
  948. { mult(M1, M2.M, M3.M); }
  949. template <typename MAT1, typename MAT2> inline
  950. void mult(const mpi_distributed_matrix<MAT2> &M2,
  951. const MAT1 &M1, mpi_distributed_matrix<MAT2> &M3)
  952. { mult(M2.M, M1, M3.M); }
  953. template <typename MAT1, typename MAT2, typename MAT3> inline
  954. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  955. MAT3 &M3)
  956. { mult(M1, M2.M, M3); }
  957. template <typename MAT1, typename MAT2, typename MAT3> inline
  958. void mult(const MAT1 &M1, const mpi_distributed_matrix<MAT2> &M2,
  959. const MAT3 &M3)
  960. { mult(M1, M2.M, M3); }
  961. template <typename M, typename SUBI1, typename SUBI2>
  962. struct sub_matrix_type<const mpi_distributed_matrix<M> *, SUBI1, SUBI2>
  963. { typedef abstract_null_type matrix_type; };
  964. template <typename M, typename SUBI1, typename SUBI2>
  965. struct sub_matrix_type<mpi_distributed_matrix<M> *, SUBI1, SUBI2>
  966. { typedef abstract_null_type matrix_type; };
  967. template <typename M, typename SUBI1, typename SUBI2> inline
  968. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI2>
  969. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI2>::matrix_type,
  970. M *>::return_type
  971. sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1, const SUBI2 &si2)
  972. { return sub_matrix(m.M, si1, si2); }
  973. template <typename MAT, typename SUBI1, typename SUBI2> inline
  974. typename select_return<typename sub_matrix_type<const MAT *, SUBI1, SUBI2>
  975. ::matrix_type, typename sub_matrix_type<MAT *, SUBI1, SUBI2>::matrix_type,
  976. const MAT *>::return_type
  977. sub_matrix(const mpi_distributed_matrix<MAT> &m, const SUBI1 &si1,
  978. const SUBI2 &si2)
  979. { return sub_matrix(m.M, si1, si2); }
  980. template <typename M, typename SUBI1> inline
  981. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  982. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  983. M *>::return_type
  984. sub_matrix(mpi_distributed_matrix<M> &m, const SUBI1 &si1)
  985. { return sub_matrix(m.M, si1, si1); }
  986. template <typename M, typename SUBI1> inline
  987. typename select_return<typename sub_matrix_type<const M *, SUBI1, SUBI1>
  988. ::matrix_type, typename sub_matrix_type<M *, SUBI1, SUBI1>::matrix_type,
  989. const M *>::return_type
  990. sub_matrix(const mpi_distributed_matrix<M> &m, const SUBI1 &si1)
  991. { return sub_matrix(m.M, si1, si1); }
  992. template <typename L> struct transposed_return<const mpi_distributed_matrix<L> *>
  993. { typedef abstract_null_type return_type; };
  994. template <typename L> struct transposed_return<mpi_distributed_matrix<L> *>
  995. { typedef abstract_null_type return_type; };
  996. template <typename L> inline typename transposed_return<const L *>::return_type
  997. transposed(const mpi_distributed_matrix<L> &l)
  998. { return transposed(l.M); }
  999. template <typename L> inline typename transposed_return<L *>::return_type
  1000. transposed(mpi_distributed_matrix<L> &l)
  1001. { return transposed(l.M); }
  1002. template <typename MAT>
  1003. struct linalg_traits<mpi_distributed_matrix<MAT> > {
  1004. typedef mpi_distributed_matrix<MAT> this_type;
  1005. typedef MAT origin_type;
  1006. typedef linalg_false is_reference;
  1007. typedef abstract_matrix linalg_type;
  1008. typedef typename linalg_traits<MAT>::value_type value_type;
  1009. typedef typename linalg_traits<MAT>::reference reference;
  1010. typedef typename linalg_traits<MAT>::storage_type storage_type;
  1011. typedef abstract_null_type sub_row_type;
  1012. typedef abstract_null_type const_sub_row_type;
  1013. typedef abstract_null_type row_iterator;
  1014. typedef abstract_null_type const_row_iterator;
  1015. typedef abstract_null_type sub_col_type;
  1016. typedef abstract_null_type const_sub_col_type;
  1017. typedef abstract_null_type col_iterator;
  1018. typedef abstract_null_type const_col_iterator;
  1019. typedef abstract_null_type sub_orientation;
  1020. typedef abstract_null_type index_sorted;
  1021. static size_type nrows(const this_type &m) { return nrows(m.M); }
  1022. static size_type ncols(const this_type &m) { return ncols(m.M); }
  1023. static void do_clear(this_type &m) { clear(m.M); }
  1024. };
  1025. }
  1026. #endif // GMM_USES_MPI
  1027. namespace std {
  1028. template <typename V>
  1029. void swap(gmm::row_matrix<V> &m1, gmm::row_matrix<V> &m2)
  1030. { m1.swap(m2); }
  1031. template <typename V>
  1032. void swap(gmm::col_matrix<V> &m1, gmm::col_matrix<V> &m2)
  1033. { m1.swap(m2); }
  1034. template <typename T>
  1035. void swap(gmm::dense_matrix<T> &m1, gmm::dense_matrix<T> &m2)
  1036. { m1.swap(m2); }
  1037. template <typename T, int shift> void
  1038. swap(gmm::csc_matrix<T,shift> &m1, gmm::csc_matrix<T,shift> &m2)
  1039. { m1.swap(m2); }
  1040. template <typename T, int shift> void
  1041. swap(gmm::csr_matrix<T,shift> &m1, gmm::csr_matrix<T,shift> &m2)
  1042. { m1.swap(m2); }
  1043. }
  1044. #endif /* GMM_MATRIX_H__ */