DOLFIN
DOLFIN C++ interface
TpetraMatrix.h
1 // Copyright (C) 2014
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 // First added: 2014
19 
20 #ifndef __TPETRA_MATRIX_H
21 #define __TPETRA_MATRIX_H
22 
23 #ifdef HAS_TRILINOS
24 
25 #include <array>
26 #include <cstdint>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 
31 #include <Teuchos_GlobalMPISession.hpp>
32 #include <Teuchos_oblackholestream.hpp>
33 #include <Tpetra_CrsMatrix.hpp>
34 #include <Tpetra_DefaultPlatform.hpp>
35 #include <Tpetra_Vector.hpp>
36 #include <Tpetra_Version.hpp>
37 
38 #include <dolfin/common/types.h>
39 #include "GenericMatrix.h"
40 
41 namespace dolfin
42 {
43 
44  class GenericLinearAlgebraFactory;
45  class IndexMap;
46  class TensorLayout;
47  class TpetraVector;
48 
57 
58  class TpetraMatrix : public GenericMatrix
59  {
60 
61  public:
62 
64  typedef Tpetra::CrsMatrix<double, int, dolfin::la_index> matrix_type;
66  typedef Tpetra::CrsGraph<int, dolfin::la_index> graph_type;
68  typedef Tpetra::Map<int, dolfin::la_index> map_type;
69 
71  TpetraMatrix();
72 
74  explicit TpetraMatrix(Teuchos::RCP<matrix_type> A);
75 
77  TpetraMatrix(const TpetraMatrix& A);
78 
80  virtual ~TpetraMatrix();
81 
82  //--- Implementation of the GenericTensor interface ---
83 
85  void init(const TensorLayout& tensor_layout);
86 
88  bool empty() const;
89 
91  std::size_t size(std::size_t dim) const;
92 
94  std::pair<std::int64_t, std::int64_t> local_range(std::size_t dim) const;
95 
96  // Number of non-zero entries
97  std::size_t nnz() const;
98 
100  virtual void zero();
101 
103  virtual void apply(std::string mode);
104 
106  MPI_Comm mpi_comm() const;
107 
109  virtual std::string str(bool verbose) const;
110 
111  //--- Implementation of the GenericMatrix interface --
112 
114  virtual std::shared_ptr<GenericMatrix> copy() const;
115 
124  virtual void init_vector(GenericVector& z, std::size_t dim) const;
125 
127  virtual void get(double* block,
128  std::size_t m, const dolfin::la_index* rows,
129  std::size_t n, const dolfin::la_index* cols) const;
130 
132  virtual void set(const double* block,
133  std::size_t m, const dolfin::la_index* rows,
134  std::size_t n, const dolfin::la_index* cols);
135 
137  virtual void set_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 
142  virtual void add(const double* block,
143  std::size_t m, const dolfin::la_index* rows,
144  std::size_t n, const dolfin::la_index* cols);
145 
147  virtual void add_local(const double* block,
148  std::size_t m, const dolfin::la_index* rows,
149  std::size_t n, const dolfin::la_index* cols);
150 
152  virtual void axpy(double a, const GenericMatrix& A,
153  bool same_nonzero_pattern);
154 
156  virtual void getrow(std::size_t row,
157  std::vector<std::size_t>& columns,
158  std::vector<double>& values) const;
159 
161  virtual void setrow(std::size_t row,
162  const std::vector<std::size_t>& columns,
163  const std::vector<double>& values);
164 
166  virtual void zero(std::size_t m, const dolfin::la_index* rows);
167 
169  virtual void zero_local(std::size_t m, const dolfin::la_index* rows);
170 
172  virtual void ident(std::size_t m, const dolfin::la_index* rows);
173 
175  virtual void ident_local(std::size_t m, const dolfin::la_index* rows);
176 
177  // Matrix-vector product, y = Ax
178  virtual void mult(const GenericVector& x, GenericVector& y) const;
179 
180  // Matrix-vector product, y = A^T x
181  virtual void transpmult(const GenericVector& x, GenericVector& y) const;
182 
184  virtual void get_diagonal(GenericVector& x) const;
185 
187  virtual void set_diagonal(const GenericVector& x);
188 
190  virtual const TpetraMatrix& operator*= (double a);
191 
193  virtual const TpetraMatrix& operator/= (double a);
194 
196  virtual const GenericMatrix& operator= (const GenericMatrix& A);
197 
199  virtual bool is_symmetric(double tol) const;
200 
201  //--- Special functions ---
202 
204  virtual GenericLinearAlgebraFactory& factory() const;
205 
206  //--- Special TpetraFunctions ---
207 
209  double norm(std::string norm_type) const;
210 
212  const TpetraMatrix& operator= (const TpetraMatrix& A);
213 
215  Teuchos::RCP<matrix_type> mat()
216  { return _matA; }
217 
219  Teuchos::RCP<const matrix_type> mat() const
220  { return _matA; }
221 
223  static void graphdump(const Teuchos::RCP<const graph_type> graph);
224 
225  private:
226 
227  // The matrix
228  Teuchos::RCP<matrix_type> _matA;
229 
230  // Row and Column maps to allow local indexing of off-process
231  // entries needed in add_local() and set_local()
232  std::array<std::shared_ptr<const IndexMap>, 2> index_map;
233 
234  };
235 
236 }
237 
238 #endif
239 
240 #endif
virtual void mult(const GenericVector &x, GenericVector &y) const
Compute matrix-vector product y = Ax.
Definition: TpetraMatrix.cpp:572
double norm(std::string norm_type) const
Return norm of matrix.
Definition: TpetraMatrix.cpp:695
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: TpetraMatrix.cpp:788
virtual void ident_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to identity matrix.
Definition: TpetraMatrix.cpp:538
Definition: adapt.h:29
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: TpetraMatrix.cpp:365
Tpetra::CrsGraph< int, dolfin::la_index > graph_type
Graph type (local index, global index)
Definition: TpetraMatrix.h:66
std::size_t size(std::size_t dim) const
Return size of given dimension.
Definition: TpetraMatrix.cpp:184
std::pair< std::int64_t, std::int64_t > local_range(std::size_t dim) const
Return local ownership range.
Definition: TpetraMatrix.cpp:205
virtual bool is_symmetric(double tol) const
Test if matrix is symmetric.
Definition: TpetraMatrix.cpp:772
virtual void zero_local(std::size_t m, const dolfin::la_index *rows)
Set given rows (local row indices) to zero.
Definition: TpetraMatrix.cpp:492
Tpetra::Map< int, dolfin::la_index > map_type
Map type (local index, global index)
Definition: TpetraMatrix.h:68
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: TpetraMatrix.cpp:318
Definition: TpetraMatrix.h:58
virtual void init_vector(GenericVector &z, std::size_t dim) const
Definition: TpetraMatrix.cpp:249
virtual std::shared_ptr< GenericMatrix > copy() const
Return copy of matrix.
Definition: TpetraMatrix.cpp:71
Teuchos::RCP< matrix_type > mat()
Return Teuchos reference counted pointer to raw matrix.
Definition: TpetraMatrix.h:215
Definition: TensorLayout.h:41
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: TpetraMatrix.cpp:737
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: TpetraMatrix.cpp:349
Teuchos::RCP< const matrix_type > mat() const
Return Teuchos reference counted pointer to raw matrix (const)
Definition: TpetraMatrix.h:219
void init(const TensorLayout &tensor_layout)
Initialize zero tensor using tensor layout.
Definition: TpetraMatrix.cpp:76
virtual const TpetraMatrix & operator/=(double a)
Divide matrix by given number.
Definition: TpetraMatrix.cpp:756
virtual void set_diagonal(const GenericVector &x)
Set diagonal of a matrix.
Definition: TpetraMatrix.cpp:659
virtual void ident(std::size_t m, const dolfin::la_index *rows)
Set given rows (global row indices) to identity matrix.
Definition: TpetraMatrix.cpp:510
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: TpetraMatrix.cpp:778
virtual const TpetraMatrix & operator*=(double a)
Multiply matrix by given number.
Definition: TpetraMatrix.cpp:746
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: TpetraMatrix.cpp:442
virtual void axpy(double a, const GenericMatrix &A, bool same_nonzero_pattern)
Add multiple of given matrix (AXPY operation)
Definition: TpetraMatrix.cpp:397
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: TpetraMatrix.cpp:726
This class defines a common interface for matrices.
Definition: GenericMatrix.h:46
static void graphdump(const Teuchos::RCP< const graph_type > graph)
Print out a graph, for debugging.
Definition: TpetraMatrix.cpp:802
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
virtual void transpmult(const GenericVector &x, GenericVector &y) const
Definition: TpetraMatrix.cpp:601
bool empty() const
Return true if empty.
Definition: TpetraMatrix.cpp:244
TpetraMatrix()
Create empty matrix.
Definition: TpetraMatrix.cpp:43
virtual const GenericMatrix & operator=(const GenericMatrix &A)
Assignment operator.
Definition: TpetraMatrix.cpp:766
virtual void get_diagonal(GenericVector &x) const
Get diagonal of a matrix.
Definition: TpetraMatrix.cpp:629
Tpetra::CrsMatrix< double, int, dolfin::la_index > matrix_type
Matrix type (scalar, local index, global index)
Definition: TpetraMatrix.h:64
virtual void apply(std::string mode)
Finalize assembly of tensor. The mode parameter is ignored.
Definition: TpetraMatrix.cpp:711
virtual ~TpetraMatrix()
Destructor.
Definition: TpetraMatrix.cpp:66
std::size_t nnz() const
Return number of non-zero entries in matrix (collective)
Definition: TpetraMatrix.cpp:236
This class defines a common interface for vectors.
Definition: GenericVector.h:47
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: TpetraMatrix.cpp:414