33 #ifndef __H__UG__CPU_ALGEBRA__SPARSEMATRIX__
34 #define __H__UG__CPU_ALGEBRA__SPARSEMATRIX__
38 #include "../algebra_common/sparsematrix_util.h"
43 #include "../algebra_common/connection.h"
44 #include "../algebra_common/matrixrow.h"
45 #include "../common/operations_mat/operations_mat.h"
47 #define PROFILE_SPMATRIX(name) PROFILE_BEGIN_GROUP(name, "SparseMatrix algebra")
50 #define CHECK_ROW_ITERATORS
160 template<
typename vector_t>
161 void axpy(vector_t &dest,
162 const number &alpha1,
const vector_t &v1,
163 const number &beta1,
const vector_t &w1)
const;
166 template<
typename vector_t>
168 const number &alpha1,
const vector_t &v1,
169 const number &beta1,
const vector_t &w1)
const;
172 template<
typename vector_t>
174 const number &beta1,
const vector_t &w1)
const;
177 template<
typename vector_t>
179 const number &beta1,
const vector_t &w1)
const;
184 template<
typename Vector_type>
185 bool apply(Vector_type &res,
const Vector_type &x)
const
187 axpy(res, 0.0, res, 1.0, x);
193 template<
typename Vector_type>
202 template<
typename Vector_type>
205 axpy(res, 1.0, res, -1.0, x);
218 void scale(
double d);
236 void add(
const M &mat);
239 void set(
const M &mat);
242 void get(M &mat)
const;
256 #ifdef CHECK_ROW_ITERATORS
328 template<
typename vector_t>
368 #ifdef CHECK_ROW_ITERATORS
374 #ifdef CHECK_ROW_ITERATORS
388 #ifdef CHECK_ROW_ITERATORS
399 #ifdef CHECK_ROW_ITERATORS
406 #ifdef CHECK_ROW_ITERATORS
423 #ifdef CHECK_ROW_ITERATORS
429 #ifdef CHECK_ROW_ITERATORS
443 #ifdef CHECK_ROW_ITERATORS
454 #ifdef CHECK_ROW_ITERATORS
463 #ifdef CHECK_ROW_ITERATORS
596 (
const_cast<this_type*
>(
this))->defragment();
608 std::vector<value_type> &argValues, std::vector<int> &argRowStart,
609 std::vector<int> &argColInd)
const
628 void get_crs(
size_t &numRows,
size_t &numCols,
629 value_type *&pValues,
size_t *pRowStart,
size_t *pColInd,
size_t &
nnz)
const
636 UG_THROW(
"SparseMatrix::get_crs() needs to be fixed.");
658 void print(
const char *
const name = NULL)
const;
666 out <<
"SparseMatrix "
684 #ifdef CHECK_ROW_ITERATORS
691 #ifdef CHECK_ROW_ITERATORS
726 assert(i<
cols.size());
750 #ifdef CHECK_ROW_ITERATORS
766 template<
typename vector_t,
typename matrix_t>
768 const number &alpha1,
const vector_t &v1,
location name
Definition: checkpoint_util.lua:128
Definition: connection.h:40
Definition: matrixrow.h:118
Definition: matrixrow.h:65
Definition: sparsematrix.h:421
size_t idx() const
Definition: sparsematrix.h:478
void operator++()
Definition: sparsematrix.h:473
const_row_iterator & operator=(const_row_iterator &&)=delete
void check() const
Definition: sparsematrix.h:428
const SparseMatrix & A
Definition: sparsematrix.h:422
size_t i
Definition: sparsematrix.h:426
void operator+=(int nr)
Definition: sparsematrix.h:474
bool operator==(const const_row_iterator &other) const
Definition: sparsematrix.h:475
size_t _row
Definition: sparsematrix.h:424
bool operator!=(const const_row_iterator &o) const
Definition: sparsematrix.h:472
~const_row_iterator()
Definition: sparsematrix.h:462
const_row_iterator(const const_row_iterator &other)
Definition: sparsematrix.h:438
const_row_iterator & operator=(const_row_iterator const &)=delete
const const_row_iterator * operator->() const
Definition: sparsematrix.h:469
size_t index() const
Definition: sparsematrix.h:471
const_row_iterator(const_row_iterator &&other)
Definition: sparsematrix.h:449
const value_type & value() const
Definition: sparsematrix.h:470
const_row_iterator(const SparseMatrix &_A, size_t row, size_t _i)
Definition: sparsematrix.h:433
Definition: sparsematrix.h:366
row_iterator * operator->()
Definition: sparsematrix.h:412
bool operator!=(const row_iterator &o) const
Definition: sparsematrix.h:415
~row_iterator()
Definition: sparsematrix.h:405
size_t i
Definition: sparsematrix.h:371
size_t index() const
Definition: sparsematrix.h:414
void operator+=(int nr)
Definition: sparsematrix.h:417
void operator++()
Definition: sparsematrix.h:416
row_iterator(const row_iterator &other)
Definition: sparsematrix.h:394
row_iterator(SparseMatrix &_A, size_t row, size_t _i)
Definition: sparsematrix.h:378
row_iterator(row_iterator &&other)
Definition: sparsematrix.h:383
size_t _row
Definition: sparsematrix.h:369
void check() const
Definition: sparsematrix.h:373
SparseMatrix & A
Definition: sparsematrix.h:367
bool operator==(const row_iterator &other) const
Definition: sparsematrix.h:418
value_type & value()
Definition: sparsematrix.h:413
sparse matrix for big, variable sparse matrices.
Definition: sparsematrix.h:99
bool has_connection(size_t r, size_t c) const
Definition: sparsematrix.h:496
bool apply_transposed(Vector_type &res, const Vector_type &x) const
calculate res = A.T x
Definition: sparsematrix.h:194
void apply_transposed_ignore_zero_rows(vector_t &dest, const number &beta1, const vector_t &w1) const
calculated dest = beta1*A*w1 . For empty cols of A (=empty rows of A^T), dest will not be changed
Definition: sparsematrix_impl.h:375
int get_index_internal(size_t row, int col) const
Definition: sparsematrix_impl.h:553
int get_index_const(int r, int c) const
Definition: sparsematrix_impl.h:584
void 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: sparsematrix_impl.h:294
std::vector< int > cols
Definition: sparsematrix.h:740
void p() const
Definition: sparsematrix.h:672
size_t nnz
Definition: sparsematrix.h:742
SparseMatrix< value_type > & operator*=(double d)
Definition: sparsematrix.h:219
void print_to_file(const char *filename) const
void check_rc(size_t r, size_t c) const
Definition: sparsematrix.h:249
void resize_and_clear(size_t newRows, size_t newCols)
resizes the SparseMatrix
Definition: sparsematrix_impl.h:91
const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
Definition: sparsematrix.h:527
void copyToNewSize(size_t newSize)
Definition: sparsematrix.h:716
void set_as_transpose_of2(const SparseMatrix< value_type > &B, double scale=1.0)
Definition: sparsematrix_impl.h:186
AlgebraicConnection< TValueType > connection
Definition: sparsematrix.h:107
const_row_type get_row(size_t r) const
Definition: sparsematrix.h:490
int col(size_t i) const
Definition: sparsematrix.h:725
void get_values(std::vector< value_type > &argValues) const
Definition: sparsematrix.h:647
SparseMatrix< value_type > & operator=(const SparseMatrix< value_type > &B)
Definition: sparsematrix.h:151
row_iterator get_connection(size_t r, size_t c)
Definition: sparsematrix.h:579
void resize_and_keep_values(size_t newRows, size_t newCols)
Definition: sparsematrix_impl.h:112
void defragment() const
Definition: sparsematrix.h:594
SparseMatrix(SparseMatrix &)
disallow copy operator
TValueType value_type
Definition: sparsematrix.h:101
void assureValuesSize(size_t s)
Definition: sparsematrix_impl.h:760
size_t num_rows() const
returns number of rows
Definition: sparsematrix.h:342
void add(const M &mat)
Definition: sparsematrix_impl.h:507
std::vector< int > rowEnd
Definition: sparsematrix.h:738
void check_row_modifiable(size_t r) const
Definition: sparsematrix.h:254
virtual ~SparseMatrix()
destructor
Definition: sparsematrix.h:118
void pr(size_t row) const
Definition: sparsematrix.h:673
const_row_iterator end_row(size_t r) const
Definition: sparsematrix.h:487
int maxValues
Definition: sparsematrix.h:746
std::vector< int > nrOfRowIterators
Definition: sparsematrix.h:752
void check_fragmentation() const
Definition: sparsematrix_impl.h:753
SparseMatrix()
constructor for empty SparseMatrix
Definition: sparsematrix_impl.h:59
size_t num_connections(size_t i) const
returns number of connections of row row.
Definition: sparsematrix.h:335
int iters() const
Definition: sparsematrix.h:731
void defragment()
Definition: sparsematrix.h:588
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: sparsematrix_impl.h:259
std::vector< value_type > values
Definition: sparsematrix.h:745
bool is_isolated(size_t i) const
check for isolated condition of an index
Definition: sparsematrix_impl.h:416
MatrixRow< this_type > row_type
Definition: sparsematrix.h:108
row_iterator end_row(size_t r)
Definition: sparsematrix.h:485
SparseMatrix< value_type > this_type
Definition: sparsematrix.h:104
void set_as_copy_of(const SparseMatrix< value_type > &B, double scale=1.0)
create/recreate this as a copy of SparseMatrix B
Definition: sparsematrix_impl.h:472
size_t total_num_connections() const
returns the total number of connections
Definition: sparsematrix.h:348
row_iterator get_iterator_or_next(size_t r, size_t c)
Definition: sparsematrix.h:509
const value_type & operator()(size_t r, size_t c) const
Definition: sparsematrix.h:272
void 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: sparsematrix_impl.h:149
friend std::ostream & operator<<(std::ostream &out, const SparseMatrix &m)
Definition: sparsematrix.h:664
void set_matrix_row(size_t row, connection *c, size_t nr)
Definition: sparsematrix_impl.h:429
bool matmul_minus(Vector_type &res, const Vector_type &x) const
calculate res -= A x
Definition: sparsematrix.h:203
void add_iterator(size_t row) const
Definition: sparsematrix.h:682
void add_matrix_row(size_t row, connection *c, size_t nr)
Definition: sparsematrix_impl.h:463
void apply_ignore_zero_rows(vector_t &dest, const number &beta1, const vector_t &w1) const
calculated dest = beta1*A*w1 . For empty rows, dest will not be changed
Definition: sparsematrix_impl.h:273
int get_index(int r, int c)
Definition: sparsematrix_impl.h:596
void check_row(size_t row, int i) const
Definition: sparsematrix.h:699
void scale(double d)
Definition: sparsematrix_impl.h:487
row_iterator get_connection(size_t r, size_t c, bool &bFound)
Definition: sparsematrix.h:547
bool apply(Vector_type &res, const Vector_type &x) const
calculate res = A x
Definition: sparsematrix.h:185
int m_numCols
Definition: sparsematrix.h:747
int iIterators
Definition: sparsematrix.h:748
size_t num_cols() const
returns the number of cols
Definition: sparsematrix.h:345
void get(M &mat) const
get local matrix
Definition: sparsematrix_impl.h:538
void remove_iterator(size_t row) const
Definition: sparsematrix.h:689
void clear_and_free()
Clears the matrix vectors and frees their memory.
Definition: sparsematrix_impl.h:72
size_t get_nnz() const
Definition: sparsematrix.h:704
@ rows_sorted
Definition: sparsematrix.h:102
void get_crs(size_t &numRows, size_t &numCols, value_type *&pValues, size_t *pRowStart, size_t *pColInd, size_t &nnz) const
Definition: sparsematrix.h:628
size_t fragmented
Definition: sparsematrix.h:741
std::vector< int > rowMax
Definition: sparsematrix.h:739
row_iterator begin_row(size_t r)
Definition: sparsematrix.h:484
void clear_retain_structure()
Definition: sparsematrix_impl.h:142
const_row_iterator get_connection(size_t r, size_t c) const
Definition: sparsematrix.h:568
void 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: sparsematrix_impl.h:344
int get_nnz_max_cols(size_t maxCols)
Definition: sparsematrix_impl.h:770
void copy_crs(size_t &numRows, size_t &numCols, std::vector< value_type > &argValues, std::vector< int > &argRowStart, std::vector< int > &argColInd) const
Definition: sparsematrix.h:607
bool bNeedsValues
Definition: sparsematrix.h:743
row_type get_row(size_t r)
Definition: sparsematrix.h:489
void set(const M &mat)
set local matrix
Definition: sparsematrix_impl.h:523
const_row_iterator begin_row(size_t r) const
Definition: sparsematrix.h:486
ConstMatrixRow< this_type > const_row_type
Definition: sparsematrix.h:109
std::vector< int > rowStart
Definition: sparsematrix.h:737
void print(const char *const name=NULL) const
Definition: sparsematrix_print.h:47
void printtype() const
Definition: sparsematrix_print.h:72
void printrow(size_t row) const
Definition: sparsematrix_print.h:58
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
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
@ MATRIX_USE_ROW_FUNCTIONS
Definition: matrix_algebra_types.h:68
#define CHECK_ROW_ITERATORS
Definition: sparsematrix.h:50
T value_type
Definition: sparsematrix_interface.h:2
Definition: matrix_algebra_types.h:79
static const int type
Definition: matrix_algebra_types.h:80