From: Marc Fehling Date: Fri, 4 Mar 2022 05:30:28 +0000 (-0700) Subject: Added `n_nonzero_elements` to PETSc `BlockSparseMatrix`. X-Git-Tag: v9.4.0-rc1~406^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7eb79d5f953d1bbb01884204b623efd349e1df5b;p=dealii.git Added `n_nonzero_elements` to PETSc `BlockSparseMatrix`. --- diff --git a/include/deal.II/lac/petsc_block_sparse_matrix.h b/include/deal.II/lac/petsc_block_sparse_matrix.h index d7ebe0819c..60ac2c3e93 100644 --- a/include/deal.II/lac/petsc_block_sparse_matrix.h +++ b/include/deal.II/lac/petsc_block_sparse_matrix.h @@ -250,6 +250,14 @@ namespace PETScWrappers std::vector locally_owned_range_indices() const; + /** + * Return the number of nonzero elements of this matrix. Actually, it + * returns the number of entries in the sparsity pattern; if any of the + * entries should happen to be zero, it is counted anyway. + */ + size_type + n_nonzero_elements() const; + /** * Return a reference to the MPI communicator object in use with this * matrix. diff --git a/source/lac/petsc_parallel_block_sparse_matrix.cc b/source/lac/petsc_parallel_block_sparse_matrix.cc index 598f3a57ff..0d0a553838 100644 --- a/source/lac/petsc_parallel_block_sparse_matrix.cc +++ b/source/lac/petsc_parallel_block_sparse_matrix.cc @@ -133,6 +133,17 @@ namespace PETScWrappers return index_sets; } + BlockSparseMatrix::size_type + BlockSparseMatrix::n_nonzero_elements() const + { + size_type n_nonzero = 0; + for (size_type rows = 0; rows < this->n_block_rows(); ++rows) + for (size_type cols = 0; cols < this->n_block_cols(); ++cols) + n_nonzero += this->block(rows, cols).n_nonzero_elements(); + + return n_nonzero; + } + const MPI_Comm & BlockSparseMatrix::get_mpi_communicator() const {