From: Stefano Zampini Date: Tue, 11 Apr 2023 08:54:28 +0000 (+0300) Subject: Implement Wolfgang suggestions X-Git-Tag: v9.5.0-rc1~336^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=feadecf4d46114576c4360aabaadef03055217ec;p=dealii.git Implement Wolfgang suggestions --- diff --git a/source/lac/petsc_matrix_base.cc b/source/lac/petsc_matrix_base.cc index 35a5da4207..5e1555468b 100644 --- a/source/lac/petsc_matrix_base.cc +++ b/source/lac/petsc_matrix_base.cc @@ -326,7 +326,7 @@ namespace PETScWrappers MatGetOwnershipRange(static_cast(matrix), &begin, &end); AssertThrow(ierr == 0, ExcPETScError(ierr)); - return std::make_pair(begin, end); + return {begin, end}; } @@ -355,7 +355,7 @@ namespace PETScWrappers &end); AssertThrow(ierr == 0, ExcPETScError(ierr)); - return std::make_pair(begin, end); + return {begin, end}; } diff --git a/source/lac/petsc_parallel_block_sparse_matrix.cc b/source/lac/petsc_parallel_block_sparse_matrix.cc index ef3d58a2b3..fe4c291c05 100644 --- a/source/lac/petsc_parallel_block_sparse_matrix.cc +++ b/source/lac/petsc_parallel_block_sparse_matrix.cc @@ -18,37 +18,41 @@ #ifdef DEAL_II_WITH_PETSC -// A dummy utility routine to create an empty matrix in case we import -// a MATNEST with NULL blocks -static void -createDummyMat(MPI_Comm comm, - PetscInt lr, - PetscInt gr, - PetscInt lc, - PetscInt gc, - Mat * dummy) +namespace { - PetscErrorCode ierr; - - ierr = MatCreate(comm, dummy); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatSetSizes(*dummy, lr, lc, gr, gc); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatSetType(*dummy, MATAIJ); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatSeqAIJSetPreallocation(*dummy, 0, nullptr); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatMPIAIJSetPreallocation(*dummy, 0, nullptr, 0, nullptr); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatSetUp(*dummy); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatSetOption(*dummy, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatAssemblyBegin(*dummy, MAT_FINAL_ASSEMBLY); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); - ierr = MatAssemblyEnd(*dummy, MAT_FINAL_ASSEMBLY); - AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); -} + // A dummy utility routine to create an empty matrix in case we import + // a MATNEST with NULL blocks + static Mat + create_dummy_mat(MPI_Comm comm, + PetscInt lr, + PetscInt gr, + PetscInt lc, + PetscInt gc) + { + Mat dummy; + PetscErrorCode ierr; + + ierr = MatCreate(comm, &dummy); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatSetSizes(dummy, lr, lc, gr, gc); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatSetType(dummy, MATAIJ); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatSeqAIJSetPreallocation(dummy, 0, nullptr); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatMPIAIJSetPreallocation(dummy, 0, nullptr, 0, nullptr); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatSetUp(dummy); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatSetOption(dummy, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatAssemblyBegin(dummy, MAT_FINAL_ASSEMBLY); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + ierr = MatAssemblyEnd(dummy, MAT_FINAL_ASSEMBLY); + AssertThrow(ierr == 0, dealii::ExcPETScError(ierr)); + return dummy; + } +} // namespace DEAL_II_NAMESPACE_OPEN @@ -188,15 +192,17 @@ namespace PETScWrappers { if (!this->sub_objects[r][c]) { - Mat dummy; - createDummyMat(comm, - static_cast(row_local_sizes[r]), - static_cast(row_sizes[r]), - static_cast(col_local_sizes[c]), - static_cast(col_sizes[c]), - &dummy); + Mat dummy = ::create_dummy_mat( + comm, + static_cast(row_local_sizes[r]), + static_cast(row_sizes[r]), + static_cast(col_local_sizes[c]), + static_cast(col_sizes[c])); this->sub_objects[r][c] = new BlockType(dummy); - ierr = MatDestroy(&dummy); + + // the new object got a reference on dummy, we can safely + // call destroy here + ierr = MatDestroy(&dummy); AssertThrow(ierr == 0, ExcPETScError(ierr)); } }