Skip to content

Commit 4494fb8

Browse files
authored
Merge pull request #2169 from lindsayad/expand-petsc-interface
Expand PetscMatrix interface
2 parents b20455e + fd629fe commit 4494fb8

File tree

2 files changed

+84
-11
lines changed

2 files changed

+84
-11
lines changed

include/numerics/petsc_matrix.h

+22
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ class PetscMatrix final : public SparseMatrix<T>
151151
*/
152152
void update_preallocation_and_zero();
153153

154+
/**
155+
* Reset matrix to use the original nonzero pattern provided by users
156+
*/
157+
void reset_preallocation();
158+
154159
virtual void clear () override;
155160

156161
virtual void zero () override;
@@ -163,8 +168,25 @@ class PetscMatrix final : public SparseMatrix<T>
163168

164169
virtual numeric_index_type m () const override;
165170

171+
/**
172+
* Get the number of rows owned by this process
173+
*/
174+
numeric_index_type local_m () const;
175+
166176
virtual numeric_index_type n () const override;
167177

178+
/**
179+
* Get the number of columns owned by this process
180+
*/
181+
numeric_index_type local_n () const;
182+
183+
/**
184+
* Get the number of rows and columns owned by this process
185+
* @param i Row size
186+
* @param j Column size
187+
*/
188+
void get_local_size (numeric_index_type & m, numeric_index_type & n) const;
189+
168190
virtual numeric_index_type row_start () const override;
169191

170192
virtual numeric_index_type row_stop () const override;

src/numerics/petsc_matrix.C

+62-11
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ void PetscMatrix<T>::init (const numeric_index_type m_in,
172172

173173
ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
174174
LIBMESH_CHKERR(ierr);
175-
ierr = MatSeqBAIJSetPreallocation(_mat, blocksize, n_nz/blocksize, PETSC_NULL);
175+
ierr = MatSeqBAIJSetPreallocation(_mat, blocksize, n_nz/blocksize, NULL);
176176
LIBMESH_CHKERR(ierr);
177177
ierr = MatMPIBAIJSetPreallocation(_mat, blocksize,
178-
n_nz/blocksize, PETSC_NULL,
179-
n_oz/blocksize, PETSC_NULL);
178+
n_nz/blocksize, NULL,
179+
n_oz/blocksize, NULL);
180180
LIBMESH_CHKERR(ierr);
181181
}
182182
else
@@ -186,17 +186,17 @@ void PetscMatrix<T>::init (const numeric_index_type m_in,
186186
case AIJ:
187187
ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
188188
LIBMESH_CHKERR(ierr);
189-
ierr = MatSeqAIJSetPreallocation(_mat, n_nz, PETSC_NULL);
189+
ierr = MatSeqAIJSetPreallocation(_mat, n_nz, NULL);
190190
LIBMESH_CHKERR(ierr);
191-
ierr = MatMPIAIJSetPreallocation(_mat, n_nz, PETSC_NULL, n_oz, PETSC_NULL);
191+
ierr = MatMPIAIJSetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
192192
LIBMESH_CHKERR(ierr);
193193
break;
194194

195195
case HYPRE:
196196
#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
197197
ierr = MatSetType(_mat, MATHYPRE);
198198
LIBMESH_CHKERR(ierr);
199-
ierr = MatHYPRESetPreallocation(_mat, n_nz, PETSC_NULL, n_oz, PETSC_NULL);
199+
ierr = MatHYPRESetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
200200
LIBMESH_CHKERR(ierr);
201201
#else
202202
libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
@@ -470,6 +470,20 @@ void PetscMatrix<T>::update_preallocation_and_zero ()
470470
libmesh_not_implemented();
471471
}
472472

473+
template <typename T>
474+
void PetscMatrix<T>::reset_preallocation()
475+
{
476+
#if !PETSC_VERSION_LESS_THAN(3,8,0)
477+
libmesh_assert (this->initialized());
478+
479+
auto ierr = MatResetPreallocation(_mat);
480+
LIBMESH_CHKERR(ierr);
481+
#else
482+
libmesh_warning("Your version of PETSc doesn't support resetting of "
483+
"preallocation, so we will use your most recent sparsity "
484+
"pattern. This may result in a degradation of performance\n");
485+
#endif
486+
}
473487

474488
template <typename T>
475489
void PetscMatrix<T>::zero ()
@@ -506,7 +520,7 @@ void PetscMatrix<T>::zero_rows (std::vector<numeric_index_type> & rows, T diag_v
506520
ierr = MatZeroRows(_mat, rows.size(),
507521
numeric_petsc_cast(rows.data()), diag_value);
508522
else
509-
ierr = MatZeroRows(_mat, 0, PETSC_NULL, diag_value);
523+
ierr = MatZeroRows(_mat, 0, NULL, diag_value);
510524
#else
511525
// As of petsc-dev at the time of 3.1.0, MatZeroRows now takes two additional
512526
// optional arguments. The optional arguments (x,b) can be used to specify the
@@ -515,10 +529,10 @@ void PetscMatrix<T>::zero_rows (std::vector<numeric_index_type> & rows, T diag_v
515529
if (!rows.empty())
516530
ierr = MatZeroRows(_mat, cast_int<PetscInt>(rows.size()),
517531
numeric_petsc_cast(rows.data()), diag_value,
518-
PETSC_NULL, PETSC_NULL);
532+
NULL, NULL);
519533
else
520-
ierr = MatZeroRows(_mat, 0, PETSC_NULL, diag_value, PETSC_NULL,
521-
PETSC_NULL);
534+
ierr = MatZeroRows(_mat, 0, NULL, diag_value, NULL,
535+
NULL);
522536
#endif
523537

524538
LIBMESH_CHKERR(ierr);
@@ -931,7 +945,7 @@ void PetscMatrix<T>::get_transpose (SparseMatrix<T> & dest) const
931945
PetscErrorCode ierr;
932946
#if PETSC_VERSION_LESS_THAN(3,0,0)
933947
if (&petsc_dest == this)
934-
ierr = MatTranspose(_mat,PETSC_NULL);
948+
ierr = MatTranspose(_mat,NULL);
935949
else
936950
ierr = MatTranspose(_mat,&petsc_dest._mat);
937951
LIBMESH_CHKERR(ierr);
@@ -1002,7 +1016,18 @@ numeric_index_type PetscMatrix<T>::m () const
10021016
return static_cast<numeric_index_type>(petsc_m);
10031017
}
10041018

1019+
template <typename T>
1020+
numeric_index_type PetscMatrix<T>::local_m () const
1021+
{
1022+
libmesh_assert (this->initialized());
1023+
1024+
PetscInt m = 0;
10051025

1026+
auto ierr = MatGetLocalSize (_mat, &m, NULL);
1027+
LIBMESH_CHKERR(ierr);
1028+
1029+
return static_cast<numeric_index_type>(m);
1030+
}
10061031

10071032
template <typename T>
10081033
numeric_index_type PetscMatrix<T>::n () const
@@ -1018,7 +1043,33 @@ numeric_index_type PetscMatrix<T>::n () const
10181043
return static_cast<numeric_index_type>(petsc_n);
10191044
}
10201045

1046+
template <typename T>
1047+
numeric_index_type PetscMatrix<T>::local_n () const
1048+
{
1049+
libmesh_assert (this->initialized());
1050+
1051+
PetscInt n = 0;
1052+
1053+
auto ierr = MatGetLocalSize (_mat, NULL, &n);
1054+
LIBMESH_CHKERR(ierr);
1055+
1056+
return static_cast<numeric_index_type>(n);
1057+
}
10211058

1059+
template <typename T>
1060+
void PetscMatrix<T>::get_local_size (numeric_index_type & m,
1061+
numeric_index_type & n) const
1062+
{
1063+
libmesh_assert (this->initialized());
1064+
1065+
PetscInt petsc_m = 0, petsc_n = 0;
1066+
1067+
auto ierr = MatGetLocalSize (_mat, &petsc_m, &petsc_n);
1068+
LIBMESH_CHKERR(ierr);
1069+
1070+
m = static_cast<numeric_index_type>(petsc_m);
1071+
n = static_cast<numeric_index_type>(petsc_n);
1072+
}
10221073

10231074
template <typename T>
10241075
numeric_index_type PetscMatrix<T>::row_start () const

0 commit comments

Comments
 (0)