AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
MatrixBackend.hpp
1 #pragma once
2 
3 #include <list>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include <boost/numeric/mtl/matrix/compressed2D.hpp>
9 #include <boost/numeric/mtl/matrix/inserter.hpp>
10 #include <boost/numeric/mtl/utility/property_map.hpp>
11 #include <boost/numeric/mtl/utility/range_wrapper.hpp>
12 
13 #include <amdis/Output.hpp>
14 #include <amdis/linearalgebra/SymmetryStructure.hpp>
15 #include <amdis/linearalgebra/mtl/SlotSize.hpp>
16 
17 namespace AMDiS
18 {
20  template <class T>
22  {
23  public:
25  using BaseMatrix = mtl::compressed2D<T>;
26 
28  using value_type = typename BaseMatrix::value_type;
29 
31  using size_type = typename BaseMatrix::size_type;
32 
33  private:
35  using Inserter = mtl::mat::inserter<BaseMatrix, mtl::operations::update_plus<value_type>>;
36 
38  using Pattern = SlotSize;
39 
40  public:
42  template <class Basis>
43  explicit MTLSparseMatrix(Basis const& /*rowBasis*/, Basis const& /*colBasis*/) {}
44 
47  {
48  assert( !inserter_ );
49  return matrix_;
50  }
51 
53  BaseMatrix const& matrix() const
54  {
55  assert( !inserter_ );
56  return matrix_;
57  }
58 
61  void init(Pattern const& pattern)
62  {
63  test_exit(!inserter_, "Matrix already in insertion mode!");
64 
65  std::size_t slotSize = nnz() > 0 ? 6*nnz() / (5*num_rows(matrix_))
66  : pattern.rowSizeEstimate();
67  matrix_.change_dim(pattern.rows(), pattern.cols());
68  set_to_zero(matrix_);
69 
70  symmetry_ = pattern.symmetry();
71  inserter_ = new Inserter(matrix_, slotSize);
72  }
73 
76  void init()
77  {
78  test_exit(!inserter_, "Matrix already in insertion mode!");
79 
80  std::size_t slotSize = 6*nnz() / (5*num_rows(matrix_));
81  set_to_zero(matrix_);
82 
83  inserter_ = new Inserter(matrix_, slotSize);
84  }
85 
86 
89  void finish()
90  {
91  delete inserter_;
92  inserter_ = nullptr;
93  }
94 
95 
99  void insert(size_type r, size_type c, value_type const& value)
100  {
101  test_exit_dbg(inserter_, "Inserter not initilized!");
102  test_exit_dbg(r < num_rows(matrix_) && c < num_cols(matrix_),
103  "Indices out of range [0,{})x[0,{})", num_rows(matrix_), num_cols(matrix_));
104  if (value != value_type(0) || r == c)
105  (*inserter_)[r][c] += value;
106  }
107 
108  template <class Ind, class LocalMat>
109  void scatter(Ind const& idx, LocalMat const& mat)
110  {
111  scatter(idx, idx, mat);
112  }
113 
114  template <class RowInd, class ColInd, class LocalMat>
115  void scatter(RowInd const& rows, ColInd const& cols, LocalMat const& mat)
116  {
117  test_exit_dbg(inserter_, "Inserter not initilized!");
118  for (size_type i = 0; i < size_type(rows.size()); ++i)
119  for (size_type j = 0; j < size_type(cols.size()); ++j)
120  if (mat[i][j] != value_type(0) || i == j)
121  (*inserter_)[rows[i]][cols[j]] += mat[i][j];
122  }
123 
125  std::size_t nnz() const
126  {
127  return matrix_.nnz();
128  }
129 
131  SymmetryStructure symmetry() const
132  {
133  return symmetry_;
134  }
135 
136  private:
138  BaseMatrix matrix_;
139 
141  Inserter* inserter_ = nullptr;
142 
144  SymmetryStructure symmetry_ = SymmetryStructure::unknown;
145  };
146 
147 } // end namespace AMDiS
BaseMatrix & matrix()
Return a reference to the data-matrix matrix.
Definition: MatrixBackend.hpp:46
void init()
Definition: MatrixBackend.hpp:76
SymmetryStructure symmetry() const
Symmetry of the matrix entries.
Definition: MatrixBackend.hpp:131
std::size_t rowSizeEstimate() const
Estimate of the non-zeros per row.
Definition: SlotSize.hpp:41
void test_exit_dbg(bool condition, Args &&... args)
call assert_msg, in debug mode only
Definition: Output.hpp:205
mtl::compressed2D< T > BaseMatrix
The matrix type of the underlying base matrix.
Definition: MatrixBackend.hpp:25
BaseMatrix const & matrix() const
Return a reference to the data-matrix matrix.
Definition: MatrixBackend.hpp:53
MTLSparseMatrix(Basis const &, Basis const &)
Constructor. Constructs new BaseMatrix.
Definition: MatrixBackend.hpp:43
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
std::size_t cols() const
Number of columns in the matrix.
Definition: SlotSize.hpp:35
typename BaseMatrix::size_type size_type
The index/size - type.
Definition: MatrixBackend.hpp:31
std::size_t rows() const
Number of rows in the matrix.
Definition: SlotSize.hpp:29
SymmetryStructure symmetry() const
Symmetry of the matrix entries.
Definition: SlotSize.hpp:47
void init(Pattern const &pattern)
Definition: MatrixBackend.hpp:61
The basic container that stores a base matrix.
Definition: MatrixBackend.hpp:21
std::size_t nnz() const
Return the number of nonzeros in the matrix.
Definition: MatrixBackend.hpp:125
Definition: SlotSize.hpp:15
typename BaseMatrix::value_type value_type
The type of the elements of the DOFMatrix.
Definition: MatrixBackend.hpp:28
void insert(size_type r, size_type c, value_type const &value)
Returns an update-proxy of the inserter, to insert/update a value at position (r, c) in the matrix...
Definition: MatrixBackend.hpp:99
void test_exit(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message and exit
Definition: Output.hpp:163
void finish()
Definition: MatrixBackend.hpp:89