DOLFIN
DOLFIN C++ interface
Matrix.h
1 // Copyright (C) 2006-2008 Anders Logg and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __MATRIX_H
19 #define __MATRIX_H
20 
21 #include <memory>
22 #include <tuple>
23 #include "DefaultFactory.h"
24 #include "GenericMatrix.h"
25 
26 namespace dolfin
27 {
28 
29  class GenericVector;
30  class TensorLayout;
31 
34 
35  class Matrix : public GenericMatrix
36  {
37  public:
38 
40  Matrix() : Matrix(MPI_COMM_WORLD) {}
41 
43  explicit Matrix(MPI_Comm comm)
44  {
46  matrix = factory.create_matrix(comm);
47  }
48 
50  Matrix(const Matrix& A) : matrix(A.matrix->copy()) {}
51 
53  Matrix(const GenericMatrix& A) : matrix(A.copy()) {}
54 
56  virtual ~Matrix() {}
57 
58  //--- Implementation of the GenericTensor interface ---
59 
61  virtual void init(const TensorLayout& tensor_layout)
62  { matrix->init(tensor_layout); }
63 
65  virtual bool empty() const
66  { return matrix->empty(); }
67 
69  virtual std::size_t size(std::size_t dim) const
70  { return matrix->size(dim); }
71 
73  virtual std::pair<std::int64_t, std::int64_t>
74  local_range(std::size_t dim) const
75  { return matrix->local_range(dim); }
76 
78  virtual std::size_t nnz() const
79  { return matrix->nnz(); }
80 
82  virtual void zero()
83  { matrix->zero(); }
84 
86  virtual void apply(std::string mode)
87  { matrix->apply(mode); }
88 
90  MPI_Comm mpi_comm() const
91  { return matrix->mpi_comm(); }
92 
94  virtual std::string str(bool verbose) const
95  { return "<Matrix wrapper of " + matrix->str(verbose) + ">"; }
96 
97  //--- Implementation of the GenericMatrix interface ---
98 
100  virtual std::shared_ptr<GenericMatrix> copy() const
101  {
102  std::shared_ptr<Matrix> A(new Matrix(*this));
103  return A;
104  }
105 
109  virtual void init_vector(GenericVector& y, std::size_t dim) const
110  { matrix->init_vector(y, dim); }
111 
113  virtual void get(double* block,
114  std::size_t m, const dolfin::la_index* rows,
115  std::size_t n, const dolfin::la_index* cols) const
116  { matrix->get(block, m, rows, n, cols); }
117 
119  virtual void set(const double* block,
120  std::size_t m, const dolfin::la_index* rows,
121  std::size_t n, const dolfin::la_index* cols)
122  { matrix->set(block, m, rows, n, cols); }
123 
125  virtual void set_local(const double* block,
126  std::size_t m, const dolfin::la_index* rows,
127  std::size_t n, const dolfin::la_index* cols)
128  { matrix->set_local(block, m, rows, n, cols); }
129 
131  virtual void add(const double* block,
132  std::size_t m, const dolfin::la_index* rows,
133  std::size_t n, const dolfin::la_index* cols)
134  { matrix->add(block, m, rows, n, cols); }
135 
137  virtual void add_local(const double* block,
138  std::size_t m, const dolfin::la_index* rows,
139  std::size_t n, const dolfin::la_index* cols)
140  { matrix->add_local(block, m, rows, n, cols); }
141 
143  virtual void axpy(double a, const GenericMatrix& A,
144  bool same_nonzero_pattern)
145  { matrix->axpy(a, A, same_nonzero_pattern); }
146 
148  virtual double norm(std::string norm_type) const
149  { return matrix->norm(norm_type); }
150 
152  virtual void getrow(std::size_t row, std::vector<std::size_t>& columns,
153  std::vector<double>& values) const
154  { matrix->getrow(row, columns, values); }
155 
157  virtual void setrow(std::size_t row,
158  const std::vector<std::size_t>& columns,
159  const std::vector<double>& values)
160  { matrix->setrow(row, columns, values); }
161 
163  virtual void zero(std::size_t m, const dolfin::la_index* rows)
164  { matrix->zero(m, rows); }
165 
167  virtual void zero_local(std::size_t m, const dolfin::la_index* rows)
168  { matrix->zero_local(m, rows); }
169 
171  virtual void ident(std::size_t m, const dolfin::la_index* rows)
172  { matrix->ident(m, rows); }
173 
175  virtual void ident_local(std::size_t m, const dolfin::la_index* rows)
176  { matrix->ident_local(m, rows); }
177 
178  // Matrix-vector product, y = Ax
179  virtual void mult(const GenericVector& x, GenericVector& y) const
180  { matrix->mult(x, y); }
181 
182  // Matrix-vector product, y = Ax
183  virtual void transpmult(const GenericVector& x, GenericVector& y) const
184  { matrix->transpmult(x, y); }
185 
187  virtual void get_diagonal(GenericVector& x) const
188  { matrix->get_diagonal(x); }
189 
191  virtual void set_diagonal(const GenericVector& x)
192  { matrix->set_diagonal(x); }
193 
195  virtual const Matrix& operator*= (double a)
196  { *matrix *= a; return *this; }
197 
199  virtual const Matrix& operator/= (double a)
200  { *matrix /= a; return *this; }
201 
203  virtual const GenericMatrix& operator= (const GenericMatrix& A)
204  { *matrix = A; return *this; }
205 
207  virtual bool is_symmetric(double tol) const
208  { return matrix->is_symmetric(tol); }
209 
210  //--- Special functions ---
211 
214  { return matrix->factory(); }
215 
216  //--- Special functions, intended for library use only ---
217 
219  virtual const GenericMatrix* instance() const
220  { return matrix.get() ; }
221 
224  { return matrix.get(); }
225 
226  virtual std::shared_ptr<const LinearAlgebraObject> shared_instance() const
227  { return matrix; }
228 
229  virtual std::shared_ptr<LinearAlgebraObject> shared_instance()
230  { return matrix; }
231 
232  //--- Special Matrix functions ---
233 
235  const Matrix& operator= (const Matrix& A)
236  { *matrix = *A.matrix; return *this; }
237 
238  private:
239 
240  // Pointer to concrete implementation
241  std::shared_ptr<GenericMatrix> matrix;
242 
243  };
244 
245 }
246 
247 #endif
virtual void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition: Matrix.h:61
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation)
Definition: Matrix.h:143
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Set block of values using local indices.
Definition: Matrix.h:125
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition: Matrix.h:207
virtual const Matrix & operator/=(double a)
Divide matrix by given number.
Definition: Matrix.h:199
virtual bool empty() const
Return true if matrix is empty.
Definition: Matrix.h:65
virtual void zero(std::size_t m, const dolfin::la_index *rows)
Set given rows to zero.
Definition: Matrix.h:163
virtual void setrow(std::size_t row, const std::vector< std::size_t > &columns, const std::vector< double > &values)
Set values for given row.
Definition: Matrix.h:157
Definition: adapt.h:29
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using global indices.
Definition: Matrix.h:131
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: Matrix.h:90
virtual const Matrix & operator*=(double a)
Multiply matrix by given number.
Definition: Matrix.h:195
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition: Matrix.h:191
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition: Matrix.h:179
virtual void getrow(std::size_t row, std::vector< std::size_t > &columns, std::vector< double > &values) const
Get non-zero values of given row.
Definition: Matrix.h:152
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition: Matrix.h:171
virtual std::size_t nnz() const
Return number of non-zero entries in matrix (collective)
Definition: Matrix.h:78
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: Matrix.h:82
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition: Matrix.h:187
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition: Matrix.h:100
Matrix(const GenericMatrix &A)
Create a Vector from a GenericVector.
Definition: Matrix.h:53
virtual GenericMatrix * instance()
Return concrete instance / unwrap (non-const version)
Definition: Matrix.h:223
Definition: TensorLayout.h:41
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition: Matrix.h:203
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition: Matrix.h:183
Matrix(MPI_Comm comm)
Create empty matrix.
Definition: Matrix.h:43
virtual std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition: Matrix.h:69
Matrix()
Create empty matrix.
Definition: Matrix.h:40
virtual std::shared_ptr< LinearAlgebraObject > shared_instance()
Return concrete shared ptr instance / unwrap (non-const version)
Definition: Matrix.h:229
Definition: Matrix.h:35
Matrix(const Matrix &A)
Copy constructor.
Definition: Matrix.h:50
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to identity matrix.
Definition: Matrix.h:175
This class defines a common interface for matrices.
Definition: GenericMatrix.h:46
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition: Matrix.h:86
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual const GenericMatrix * instance() const
Return concrete instance / unwrap (const version)
Definition: Matrix.h:219
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: Matrix.h:94
virtual void init_vector(GenericVector &y, std::size_t dim) const
Definition: Matrix.h:109
virtual double norm(std::string norm_type) const
Return norm of matrix.
Definition: Matrix.h:148
virtual std::shared_ptr< const LinearAlgebraObject > shared_instance() const
Return concrete shared ptr instance / unwrap (const version)
Definition: Matrix.h:226
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows, std::size_t n, const dolfin::la_index *cols)
Add block of values using local indices.
Definition: Matrix.h:137
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: Matrix.h:213
virtual std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition: Matrix.h:74
This class defines a common interface for vectors.
Definition: GenericVector.h:47
virtual std::shared_ptr< GenericMatrix > create_matrix(MPI_Comm comm) const
Create empty matrix.
Definition: DefaultFactory.cpp:32
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition: Matrix.h:167
Default linear algebra factory based on global parameter "linear_algebra_backend".
Definition: DefaultFactory.h:35
virtual ~Matrix()
Destructor.
Definition: Matrix.h:56