ug4
mapsparsematrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__MAP_ALGEBRA__SPARSEMATRIX__
34 #define __H__UG__MAP_ALGEBRA__SPARSEMATRIX__
35 
36 #include "math.h"
37 #include "common/common.h"
38 
39 #include "../common/operations_mat/operations_mat.h"
40 
41 namespace ug{
42 
45 
46 
53 template<typename TValueType> class MapSparseMatrix
54 {
55 public:
56  typedef TValueType value_type;
57  enum {rows_sorted=true};
58 
60 
61 
62 public:
63  struct connection
64  {
65  size_t iIndex; // index to
66  value_type dValue; // smallmatrix value;
68  connection(size_t i, const value_type &v)
69  : iIndex(i), dValue(v) {}
70 
71  void print(){std::cout << *this;}
72  friend std::ostream &operator<<(std::ostream &output, const connection &c)
73  {
74  output << "(" << c.iIndex << "-> ";
75  output << c.dValue;
76  output << ")";
77  return output;
78  }
79 
80  void operator = (const connection &other)
81  {
82  iIndex = other.iIndex;
83  dValue = other.dValue;
84  }
85 
86  int operator < (const connection &c) const
87  {
88  return iIndex < c.iIndex;
89  }
90  };
91 
92 public:
93  // construction etc
94  //----------------------
95 
99  virtual ~MapSparseMatrix () {}
100 
101 
108  bool resize(size_t newRows, size_t newCols);
109 
110 
117  bool set_as_transpose_of(const SparseMatrix<value_type> &B, double scale=1.0);
118 
125  bool set_as_copy_of(const MapSparseMatrix<value_type> &B, double scale=1.0);
127  {
128  set_as_copy_of(B);
129  return *this;
130  }
131 
132 
133 public:
135  template<typename vector_t>
136  bool axpy(vector_t &dest,
137  const number &alpha1, const vector_t &v1,
138  const number &beta1, const vector_t &w1) const;
139 
141  template<typename vector_t>
142  bool axpy_transposed(vector_t &dest,
143  const number &alpha1, const vector_t &v1,
144  const number &beta1, const vector_t &w1) const;
145 
146 
147  // DEPRECATED!
149  // apply is deprecated because of axpy(res, 0.0, res, 1.0, beta, w1)
150  template<typename Vector_type>
151  bool apply(Vector_type &res, const Vector_type &x) const
152  {
153  return axpy(res, 0.0, res, 1.0, x);
154  }
155 
157  // apply is deprecated because of axpy(res, 0.0, res, 1.0, beta, w1)
158  template<typename Vector_type>
159  bool apply_transposed(Vector_type &res, const Vector_type &x) const
160  {
161  return axpy_transposed(res, 0.0, res, 1.0, x);
162  }
163 
164  // matmult_minus is deprecated because of axpy(res, 1.0, res, -1.0, x);
166  template<typename Vector_type>
167  bool matmul_minus(Vector_type &res, const Vector_type &x) const
168  {
169  return axpy(res, 1.0, res, -1.0, x);
170  }
171 
172 
173 
179  inline bool is_isolated(size_t i) const;
180 
181  bool scale(double d);
182  SparseMatrix<value_type> &operator *= (double d) { scale(d); return *this; }
183 
184  // submatrix set/get functions
185  //-------------------------------
186 
198  template<typename M>
199  void add(const M &mat);
200  template<typename M>
202  void set(const M &mat);
204  template<typename M>
205  void get(M &mat) const;
206 
207  // finalizing functions
208  //----------------------
209 
210 
211 
212 
214  bool set(double a);
215 
223  const value_type &operator () (size_t r, size_t c) const
224  {
225  typename std::map<size_t, TValueType>::const_iterator it = data[r].find(c);
226  if(it != data[r].end(c))
227  {
228  static value_type v(0.0);
229  return v;
230  }
231  return (*it).second;
232 
233  }
234 
243  value_type &operator() (size_t r, size_t c)
244  {
245  return data[r][c];
246  }
247 
248 public:
249  // row functions
250 
258  void set_matrix_row(size_t row, connection *c, size_t nr);
259 
271  void add_matrix_row(size_t row, connection *c, size_t nr);
272 
274  inline size_t num_connections(size_t i) const
275  {
276  return data[i].size();
277  }
278 
280  template<typename vector_t>
281  inline void mat_mult_add_row(size_t row, typename vector_t::value_type &dest, double alpha, const vector_t &v) const;
282 public:
283  // accessor functions
284  //----------------------
285 
287  size_t num_rows() const { return data.size(); }
288 
290  size_t num_cols() const { return cols; }
291 
293  size_t total_num_connections() const
294  {
295  size_t nnz=0;
296  for(size_t i=0; i<data.size(); i++)
297  nnz += num_connections(i);
298  return nnz;
299  }
300 
301 public:
302 
303  // Iterators
304  //---------------------------
305 
306  // const_row_iterator
307 
308 
309  //typedef const connection * const_row_iterator;
310  //typedef connection * const_row_iterator;
315  // a row_iterator has to suppport
316  // operator ++, operator +=, index() const, const value_type &value() const, value_type &value()
317  // a const_row_iterator has to suppport
318  // operator ++, operator +=, index() const, const value_type &value() const
319 
325  class row_iterator : public std::map<size_t, TValueType>::iterator
326  {
327  typedef typename std::map<size_t, TValueType>::iterator super;
328  public:
329  row_iterator(super it) : super(it) {}
330  value_type &value() { return super::operator*().second; }
331  size_t index() const { return super::operator*().first; }
332  };
333  class const_row_iterator : public std::map<size_t, TValueType>::const_iterator
334  {
335  typedef typename std::map<size_t, TValueType>::const_iterator super;
336  public:
338  const value_type &value() { return super::operator*().second; }
339  size_t index() const { return super::operator*().first; }
340  };
341 
342 
343  row_iterator begin_row(size_t r) { return row_iterator(data[r].begin()); }
344  row_iterator end_row(size_t r) { return row_iterator(data[r].end()); }
345  const_row_iterator begin_row(size_t r) const { return const_row_iterator(data[r].begin()); }
346  const_row_iterator end_row(size_t r) const { return const_row_iterator(data[r].end()); }
347 
348 public:
349  // connectivity functions
350  //-------------------------
351 
352 
358  const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
359  {
361  bFound = (it != end_row(r));
362  return it;
363  }
369  row_iterator get_connection(size_t r, size_t c, bool &bFound)
370  {
371  row_iterator it = row_iterator(data[r].find(c));
372  bFound = (it != end_row(r));
373  return it;
374  }
375 
381  const_row_iterator get_connection(size_t r, size_t c) const
382  {
383  return const_row_iterator(data[r].find(c));
384  }
391  row_iterator get_connection(size_t r, size_t c)
392  {
393  return row_iterator(data[r].find(c));
394  }
395 
396 
397  void defragment()
398  {
399 
400  }
401 
402 public:
403  // output functions
404  //----------------------
405 
406  void print(const char * const name = NULL) const;
407  void printtype() const;
408 
409  void print_to_file(const char *filename) const;
410  void printrow(size_t row) const;
411 
412  friend std::ostream &operator<<(std::ostream &out, const MapSparseMatrix &m)
413  {
414  out << "MapSparseMatrix " //<< m.name
415  << " [ " << m.num_rows() << " x " << m.num_cols() << " ]";
416  return out;
417  }
418 
419 
420  void p() const { print(); } // for use in gdb
421  void pr(size_t row) const {printrow(row); } // for use in gdb
422 
423 private:
424  // disallowed operations (not defined):
425  //---------------------------------------
427 
428 
429 protected:
430  std::vector<std::map<size_t, TValueType> > data;
431  size_t cols;
432 };
433 
434 
435 template<typename T>
437 {
438  static const int type = MATRIX_USE_ROW_FUNCTIONS;
439 };
440 
442 template<typename vector_t, typename matrix_t>
443 inline void MatMultTransposedAdd(vector_t &dest,
444  const number &alpha1, const vector_t &v1,
445  const number &beta1, const MapSparseMatrix<matrix_t> &A1, const vector_t &w1)
446 {
447  A1.axpy_transposed(dest, alpha1, v1, beta1, w1);
448 }
449 
451 } // namespace ug
452 
453 //#include "matrixrow.h"
454 #include "mapsparsematrix_impl.h"
455 #include "mapsparsematrix_print.h"
456 
457 #endif
location name
Definition: checkpoint_util.lua:128
Definition: mapsparsematrix.h:334
size_t index() const
Definition: mapsparsematrix.h:339
const_row_iterator(super it)
Definition: mapsparsematrix.h:337
std::map< size_t, TValueType >::const_iterator super
Definition: mapsparsematrix.h:335
const value_type & value()
Definition: mapsparsematrix.h:338
Definition: mapsparsematrix.h:326
size_t index() const
Definition: mapsparsematrix.h:331
std::map< size_t, TValueType >::iterator super
Definition: mapsparsematrix.h:327
value_type & value()
Definition: mapsparsematrix.h:330
row_iterator(super it)
Definition: mapsparsematrix.h:329
sparse matrix for big, variable sparse matrices.
Definition: mapsparsematrix.h:54
void print(const char *const name=NULL) const
Definition: mapsparsematrix_print.h:44
friend std::ostream & operator<<(std::ostream &out, const MapSparseMatrix &m)
Definition: mapsparsematrix.h:412
bool axpy_transposed(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
calculate dest = alpha1*v1 + beta1*A^T*w1 (A = this matrix)
Definition: mapsparsematrix_impl.h:135
row_iterator get_connection(size_t r, size_t c)
Definition: mapsparsematrix.h:391
SparseMatrix< value_type > this_type
Definition: mapsparsematrix.h:59
void p() const
Definition: mapsparsematrix.h:420
size_t total_num_connections() const
returns the total number of connections
Definition: mapsparsematrix.h:293
bool matmul_minus(Vector_type &res, const Vector_type &x) const
calculate res -= A x
Definition: mapsparsematrix.h:167
bool set_as_copy_of(const MapSparseMatrix< value_type > &B, double scale=1.0)
create/recreate this as a copy of SparseMatrix B
Definition: mapsparsematrix_impl.h:211
size_t num_connections(size_t i) const
returns number of connections of row row.
Definition: mapsparsematrix.h:274
@ rows_sorted
Definition: mapsparsematrix.h:57
void defragment()
Definition: mapsparsematrix.h:397
SparseMatrix< value_type > & operator=(const SparseMatrix< value_type > &B)
Definition: mapsparsematrix.h:126
const_row_iterator end_row(size_t r) const
Definition: mapsparsematrix.h:346
void printtype() const
Definition: mapsparsematrix_print.h:69
const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
Definition: mapsparsematrix.h:358
size_t cols
Definition: mapsparsematrix.h:431
TValueType value_type
Definition: mapsparsematrix.h:56
void mat_mult_add_row(size_t row, typename vector_t::value_type &dest, double alpha, const vector_t &v) const
calculates dest += alpha * A[row, .] v;
Definition: mapsparsematrix_impl.h:80
void set(const M &mat)
set local matrix
void get(M &mat) const
get local matrix
bool resize(size_t newRows, size_t newCols)
resizes the SparseMatrix
Definition: mapsparsematrix_impl.h:57
size_t num_cols() const
returns the number of cols
Definition: mapsparsematrix.h:290
size_t num_rows() const
returns number of rows
Definition: mapsparsematrix.h:287
void print_to_file(const char *filename) const
bool apply_transposed(Vector_type &res, const Vector_type &x) const
calculate res = A.T x
Definition: mapsparsematrix.h:159
virtual ~MapSparseMatrix()
destructor
Definition: mapsparsematrix.h:99
bool is_isolated(size_t i) const
check for isolated condition of an index
Definition: mapsparsematrix_impl.h:182
row_iterator begin_row(size_t r)
Definition: mapsparsematrix.h:343
MapSparseMatrix()
constructor for empty SparseMatrix
Definition: mapsparsematrix_impl.h:52
row_iterator get_connection(size_t r, size_t c, bool &bFound)
Definition: mapsparsematrix.h:369
bool axpy(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const vector_t &w1) const
calculate dest = alpha1*v1 + beta1*A*w1 (A = this matrix)
Definition: mapsparsematrix_impl.h:90
const_row_iterator begin_row(size_t r) const
Definition: mapsparsematrix.h:345
const_row_iterator get_connection(size_t r, size_t c) const
Definition: mapsparsematrix.h:381
const value_type & operator()(size_t r, size_t c) const
Definition: mapsparsematrix.h:223
row_iterator end_row(size_t r)
Definition: mapsparsematrix.h:344
void pr(size_t row) const
Definition: mapsparsematrix.h:421
SparseMatrix< value_type > & operator*=(double d)
Definition: mapsparsematrix.h:182
void printrow(size_t row) const
Definition: mapsparsematrix_print.h:55
std::vector< std::map< size_t, TValueType > > data
Definition: mapsparsematrix.h:430
void set_matrix_row(size_t row, connection *c, size_t nr)
Definition: mapsparsematrix_impl.h:194
void add_matrix_row(size_t row, connection *c, size_t nr)
Definition: mapsparsematrix_impl.h:202
MapSparseMatrix(MapSparseMatrix &)
disallow copy operator
bool set_as_transpose_of(const SparseMatrix< value_type > &B, double scale=1.0)
write in a empty SparseMatrix (this) the transpose SparseMatrix of B.
Definition: mapsparsematrix_impl.h:67
bool apply(Vector_type &res, const Vector_type &x) const
calculate res = A x
Definition: mapsparsematrix.h:151
bool scale(double d)
Definition: mapsparsematrix_impl.h:224
void add(const M &mat)
sparse matrix for big, variable sparse matrices.
Definition: sparsematrix.h:99
number alpha
double number
Definition: types.h:124
the ug namespace
IndexLayout::Interface::iterator find(IndexLayout::Interface &interface, size_t i)
Definition: parallel_index_layout.h:77
bool MatMultTransposedAdd(vector_t &dest, const number &alpha1, const vector_t &v1, const number &beta1, const matrix_t &A1, const vector_t &w1)
calculates dest = alpha1*v1 + beta1 * A1 *w1;
Definition: operations_mat.h:121
MatVec_Expression< L, R > operator*(const AlphaMat_Expression< L > &l, const R &r)
create a MatVec_Expression by (alpha*MATRIX) * VECTOR
Definition: template_expressions.h:223
@ MATRIX_USE_ROW_FUNCTIONS
Definition: matrix_algebra_types.h:68
T value_type
Definition: sparsematrix_interface.h:2
Definition: mapsparsematrix.h:64
connection()
Definition: mapsparsematrix.h:67
int operator<(const connection &c) const
Definition: mapsparsematrix.h:86
connection(size_t i, const value_type &v)
Definition: mapsparsematrix.h:68
void print()
Definition: mapsparsematrix.h:71
friend std::ostream & operator<<(std::ostream &output, const connection &c)
Definition: mapsparsematrix.h:72
void operator=(const connection &other)
Definition: mapsparsematrix.h:80
size_t iIndex
Definition: mapsparsematrix.h:65
value_type dValue
Definition: mapsparsematrix.h:66
Definition: matrix_algebra_types.h:79
static const int type
Definition: matrix_algebra_types.h:80