libMesh
petsc_matrix.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PETSC_MATRIX_H
21 #define LIBMESH_PETSC_MATRIX_H
22 
23 #include "libmesh/libmesh_common.h"
24 
25 #ifdef LIBMESH_HAVE_PETSC
26 
27 // Local includes
28 #include "libmesh/sparse_matrix.h"
29 #include "libmesh/petsc_macro.h"
30 
31 // C++ includes
32 #include <algorithm>
33 #ifdef LIBMESH_HAVE_CXX11_THREAD
34 #include <atomic>
35 #include <mutex>
36 #endif
37 
38 // Macro to identify and debug functions which should be called in
39 // parallel on parallel matrices but which may be called in serial on
40 // serial matrices. This macro will only be valid inside non-static
41 // PetscMatrix methods
42 #undef semiparallel_only
43 #ifndef NDEBUG
44 #include <cstring>
45 
46 #define semiparallel_only() do { if (this->initialized()) { const char * mytype; \
47  MatGetType(_mat,&mytype); \
48  if (!strcmp(mytype, MATSEQAIJ)) \
49  parallel_object_only(); } } while (0)
50 #else
51 #define semiparallel_only()
52 #endif
53 
54 
55 // Petsc include files.
56 #ifdef I
57 # define LIBMESH_SAW_I
58 #endif
59 #include <petscmat.h>
60 #ifndef LIBMESH_SAW_I
61 # undef I // Avoid complex.h contamination
62 #endif
63 
64 
65 
66 namespace libMesh
67 {
68 
69 // Forward Declarations
70 template <typename T> class DenseMatrix;
71 
72 enum PetscMatrixType : int {
73  AIJ=0,
75 
76 
86 template <typename T>
87 class PetscMatrix final : public SparseMatrix<T>
88 {
89 public:
100  explicit
101  PetscMatrix (const Parallel::Communicator & comm_in);
102 
110  explicit
111  PetscMatrix (Mat m,
112  const Parallel::Communicator & comm_in);
113 
119  PetscMatrix (PetscMatrix &&) = delete;
120  PetscMatrix (const PetscMatrix &) = delete;
121  PetscMatrix & operator= (const PetscMatrix &) = delete;
122  PetscMatrix & operator= (PetscMatrix &&) = delete;
123  virtual ~PetscMatrix ();
124 
125  void set_matrix_type(PetscMatrixType mat_type);
126 
127  virtual void init (const numeric_index_type m,
128  const numeric_index_type n,
129  const numeric_index_type m_l,
130  const numeric_index_type n_l,
131  const numeric_index_type nnz=30,
132  const numeric_index_type noz=10,
133  const numeric_index_type blocksize=1) override;
134 
146  void init (const numeric_index_type m,
147  const numeric_index_type n,
148  const numeric_index_type m_l,
149  const numeric_index_type n_l,
150  const std::vector<numeric_index_type> & n_nz,
151  const std::vector<numeric_index_type> & n_oz,
152  const numeric_index_type blocksize=1);
153 
154  virtual void init () override;
155 
162 
166  void reset_preallocation();
167 
168  virtual void clear () override;
169 
170  virtual void zero () override;
171 
172  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0) override;
173 
174  virtual void close () override;
175 
176  virtual void flush () override;
177 
178  virtual numeric_index_type m () const override;
179 
180  numeric_index_type local_m () const final;
181 
182  virtual numeric_index_type n () const override;
183 
187  numeric_index_type local_n () const;
188 
195 
196  virtual numeric_index_type row_start () const override;
197 
198  virtual numeric_index_type row_stop () const override;
199 
200  virtual void set (const numeric_index_type i,
201  const numeric_index_type j,
202  const T value) override;
203 
204  virtual void add (const numeric_index_type i,
205  const numeric_index_type j,
206  const T value) override;
207 
208  virtual void add_matrix (const DenseMatrix<T> & dm,
209  const std::vector<numeric_index_type> & rows,
210  const std::vector<numeric_index_type> & cols) override;
211 
212  virtual void add_matrix (const DenseMatrix<T> & dm,
213  const std::vector<numeric_index_type> & dof_indices) override;
214 
215  virtual void add_block_matrix (const DenseMatrix<T> & dm,
216  const std::vector<numeric_index_type> & brows,
217  const std::vector<numeric_index_type> & bcols) override;
218 
219  virtual void add_block_matrix (const DenseMatrix<T> & dm,
220  const std::vector<numeric_index_type> & dof_indices) override
221  { this->add_block_matrix (dm, dof_indices, dof_indices); }
222 
236  virtual void add (const T a, const SparseMatrix<T> & X) override;
237 
238  virtual T operator () (const numeric_index_type i,
239  const numeric_index_type j) const override;
240 
241  virtual Real l1_norm () const override;
242 
243  virtual Real linfty_norm () const override;
244 
245  virtual bool closed() const override;
246 
251  virtual void set_destroy_mat_on_exit(bool destroy = true);
252 
259  virtual void print_personal(std::ostream & os=libMesh::out) const override;
260 
261  virtual void print_matlab(const std::string & name = "") const override;
262 
263  virtual void get_diagonal (NumericVector<T> & dest) const override;
264 
265  virtual void get_transpose (SparseMatrix<T> & dest) const override;
266 
271  void swap (PetscMatrix<T> &);
272 
281  Mat mat () { libmesh_assert (_mat); return _mat; }
282 
284  std::vector<numeric_index_type> & indices,
285  std::vector<T> & values) const override;
286 protected:
287 
298  virtual void _get_submatrix(SparseMatrix<T> & submatrix,
299  const std::vector<numeric_index_type> & rows,
300  const std::vector<numeric_index_type> & cols,
301  const bool reuse_submatrix) const override;
302 
303 private:
304 
308  Mat _mat;
309 
315 
317 
318 #ifdef LIBMESH_HAVE_CXX11_THREAD
319  mutable std::mutex _petsc_matrix_mutex;
320 #else
322 #endif
323 };
324 
325 } // namespace libMesh
326 
327 #endif // #ifdef LIBMESH_HAVE_PETSC
328 #endif // LIBMESH_PETSC_MATRIX_H
libMesh::PetscMatrix::~PetscMatrix
virtual ~PetscMatrix()
Definition: petsc_matrix.C:117
libMesh::PetscMatrix::_petsc_matrix_mutex
Threads::spin_mutex _petsc_matrix_mutex
Definition: petsc_matrix.h:321
libMesh::PetscMatrix::clear
virtual void clear() override
Restores the SparseMatrix<T> to a pristine state.
Definition: petsc_matrix.C:560
libMesh::PetscMatrix::_get_submatrix
virtual void _get_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols, const bool reuse_submatrix) const override
This function either creates or re-initializes a matrix called submatrix which is defined by the indi...
Definition: petsc_matrix.C:881
libMesh::PetscMatrix::print_matlab
virtual void print_matlab(const std::string &name="") const override
Print the contents of the matrix in Matlab's sparse matrix format.
Definition: petsc_matrix.C:624
libMesh::PetscMatrix::local_m
numeric_index_type local_m() const final
Get the number of rows owned by this process.
Definition: petsc_matrix.C:1038
libMesh::AIJ
Definition: petsc_matrix.h:73
libMesh::PetscMatrix::operator()
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
Definition: petsc_matrix.C:1203
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscMatrix::row_stop
virtual numeric_index_type row_stop() const override
Definition: petsc_matrix.C:1109
libMesh::PetscMatrix::mat
Mat mat()
Definition: petsc_matrix.h:281
libMesh::PetscMatrix::PetscMatrix
PetscMatrix(const Parallel::Communicator &comm_in)
Constructor; initializes the matrix to be empty, without any structure, i.e.
Definition: petsc_matrix.C:92
libMesh::DenseMatrix
Defines a dense matrix for use in Finite Element-type computations.
Definition: dof_map.h:73
libMesh::PetscMatrix::init
virtual void init() override
Initialize this matrix using the sparsity structure computed by dof_map.
Definition: petsc_matrix.C:358
libMesh::PetscMatrix::update_preallocation_and_zero
void update_preallocation_and_zero()
Update the sparsity pattern based on dof_map, and set the matrix to zero.
Definition: petsc_matrix.C:486
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::PetscMatrix::linfty_norm
virtual Real linfty_norm() const override
Definition: petsc_matrix.C:601
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::PetscMatrix::zero
virtual void zero() override
Set all entries to 0.
Definition: petsc_matrix.C:507
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::PetscMatrix::add
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
Definition: petsc_matrix.C:1143
libMesh::TriangleWrapper::destroy
void destroy(triangulateio &t, IO_Type)
Frees any memory which has been dynamically allocated by Triangle.
libMesh::PetscMatrix::get_row
void get_row(numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const override
Get a row from the matrix.
Definition: petsc_matrix.C:1260
libMesh::PetscMatrix::zero_rows
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0) override
Sets all row entries to 0 then puts diag_value in the diagonal entry.
Definition: petsc_matrix.C:528
libMesh::PetscMatrix::_petsc_matrix_mutex
std::mutex _petsc_matrix_mutex
Definition: petsc_matrix.h:319
libMesh::PetscMatrix::add_block_matrix
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols) override
Add the full matrix dm to the SparseMatrix.
Definition: petsc_matrix.C:837
libMesh::PetscMatrix::local_n
numeric_index_type local_n() const
Get the number of columns owned by this process.
Definition: petsc_matrix.C:1065
libMesh::PetscMatrix::get_diagonal
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
Definition: petsc_matrix.C:940
libMesh::PetscMatrix::print_personal
virtual void print_personal(std::ostream &os=libMesh::out) const override
Print the contents of the matrix to the screen with the PETSc viewer.
Definition: petsc_matrix.C:703
libMesh::PetscMatrix::set_destroy_mat_on_exit
virtual void set_destroy_mat_on_exit(bool destroy=true)
If set to false, we don't delete the Mat on destruction and allow instead for PETSc to manage it.
Definition: petsc_matrix.C:1331
libMesh::PetscMatrix::_mat_type
PetscMatrixType _mat_type
Definition: petsc_matrix.h:316
libMesh::PetscMatrix::swap
void swap(PetscMatrix< T > &)
Swaps the internal data pointers of two PetscMatrices, no actual values are swapped.
Definition: petsc_matrix.C:1338
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::PetscMatrix::set
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
Definition: petsc_matrix.C:1125
libMesh::PetscMatrix::m
virtual numeric_index_type m() const override
Definition: petsc_matrix.C:1024
value
static const bool value
Definition: xdr_io.C:56
libMesh::PetscMatrix
This class provides a nice interface to the PETSc C-based data structures for parallel,...
Definition: petsc_matrix.h:87
libMesh::PetscMatrix::reset_preallocation
void reset_preallocation()
Reset matrix to use the original nonzero pattern provided by users.
Definition: petsc_matrix.C:492
libMesh::PetscMatrix::l1_norm
virtual Real l1_norm() const override
Definition: petsc_matrix.C:578
libMesh::PetscMatrix::close
virtual void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
Definition: petsc_matrix.C:989
libMesh::PetscMatrix::_destroy_mat_on_exit
bool _destroy_mat_on_exit
This boolean value should only be set to false for the constructor which takes a PETSc Mat object.
Definition: petsc_matrix.h:314
libMesh::PetscMatrix::get_local_size
void get_local_size(numeric_index_type &m, numeric_index_type &n) const
Get the number of rows and columns owned by this process.
Definition: petsc_matrix.C:1078
libMesh::PetscMatrix::closed
virtual bool closed() const override
Definition: petsc_matrix.C:1317
libMesh::PetscMatrix::n
virtual numeric_index_type n() const override
Definition: petsc_matrix.C:1051
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::HYPRE
Definition: petsc_matrix.h:74
libMesh::PetscMatrix::flush
virtual void flush() override
For PETSc matrix , this function is similar to close but without shrinking memory.
Definition: petsc_matrix.C:1009
libMesh::out
OStreamProxy out
libMesh::PetscMatrix::operator=
PetscMatrix & operator=(const PetscMatrix &)=delete
libMesh::PetscMatrix::get_transpose
virtual void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
Definition: petsc_matrix.C:953
libMesh::PetscMatrix::add_matrix
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
Add the full matrix dm to the SparseMatrix.
Definition: petsc_matrix.C:810
libMesh::Threads::spin_mutex
Spin mutex.
Definition: threads_none.h:127
libMesh::PetscMatrix::row_start
virtual numeric_index_type row_start() const override
Definition: petsc_matrix.C:1093
libMesh::PetscMatrixType
PetscMatrixType
Definition: petsc_matrix.h:72
libMesh::PetscMatrix::_mat
Mat _mat
PETSc matrix datatype to store values.
Definition: petsc_matrix.h:308
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::PetscMatrix::add_block_matrix
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices) override
Same as add_block_matrix(), but assumes the row and column maps are the same.
Definition: petsc_matrix.h:219
libMesh::PetscMatrix::set_matrix_type
void set_matrix_type(PetscMatrixType mat_type)
Definition: petsc_matrix.C:123