From 4bd75e0aa082fdcfb6f625d2ab1a3cd7acfd3634 Mon Sep 17 00:00:00 2001 From: Stefano Zampini Date: Mon, 17 Oct 2022 13:23:18 +0300 Subject: [PATCH] PETScWrappers::VectorBase: use PetscObjectReference now the deal object can outlive the Vec --- include/deal.II/lac/petsc_vector_base.h | 10 +-------- source/lac/petsc_parallel_vector.cc | 1 - source/lac/petsc_vector_base.cc | 27 ++++++++++--------------- 3 files changed, 12 insertions(+), 26 deletions(-) diff --git a/include/deal.II/lac/petsc_vector_base.h b/include/deal.II/lac/petsc_vector_base.h index 01fb81fa5c..9fe7ba6e8b 100644 --- a/include/deal.II/lac/petsc_vector_base.h +++ b/include/deal.II/lac/petsc_vector_base.h @@ -275,8 +275,7 @@ namespace PETScWrappers /** * Initialize a Vector from a PETSc Vec object. Note that we do not copy - * the vector and we do not obtain ownership, so we do not destroy the - * PETSc object in the destructor. + * the vector. */ explicit VectorBase(const Vec &v); @@ -822,13 +821,6 @@ namespace PETScWrappers // Make the reference class a friend. friend class internal::VectorReference; - /** - * Specifies if the vector is the owner of the PETSc Vec. This is true if - * it got created by this class and determines if it gets destroyed in - * the destructor. - */ - bool obtained_ownership; - /** * Collective set or add operation: This function is invoked by the * collective @p set and @p add with the @p add_values flag set to the diff --git a/source/lac/petsc_parallel_vector.cc b/source/lac/petsc_parallel_vector.cc index 87242539d7..794c687542 100644 --- a/source/lac/petsc_parallel_vector.cc +++ b/source/lac/petsc_parallel_vector.cc @@ -131,7 +131,6 @@ namespace PETScWrappers void Vector::clear() { - obtained_ownership = true; VectorBase::clear(); create_vector(0, 0); diff --git a/source/lac/petsc_vector_base.cc b/source/lac/petsc_vector_base.cc index 510b7a4fda..71cf16a3da 100644 --- a/source/lac/petsc_vector_base.cc +++ b/source/lac/petsc_vector_base.cc @@ -122,7 +122,6 @@ namespace PETScWrappers : vector(nullptr) , ghosted(false) , last_action(::dealii::VectorOperation::unknown) - , obtained_ownership(true) { Assert(MultithreadInfo::is_running_single_threaded(), ExcMessage("PETSc does not support multi-threaded access, set " @@ -136,7 +135,6 @@ namespace PETScWrappers , ghosted(v.ghosted) , ghost_indices(v.ghost_indices) , last_action(::dealii::VectorOperation::unknown) - , obtained_ownership(true) { Assert(MultithreadInfo::is_running_single_threaded(), ExcMessage("PETSc does not support multi-threaded access, set " @@ -156,23 +154,24 @@ namespace PETScWrappers , vector(v) , ghosted(false) , last_action(::dealii::VectorOperation::unknown) - , obtained_ownership(false) { Assert(MultithreadInfo::is_running_single_threaded(), ExcMessage("PETSc does not support multi-threaded access, set " "the thread limit to 1 in MPI_InitFinalize().")); + + const PetscErrorCode ierr = + PetscObjectReference(reinterpret_cast(vector)); + AssertNothrow(ierr == 0, ExcPETScError(ierr)); + (void)ierr; } VectorBase::~VectorBase() { - if (obtained_ownership) - { - const PetscErrorCode ierr = VecDestroy(&vector); - AssertNothrow(ierr == 0, ExcPETScError(ierr)); - (void)ierr; - } + const PetscErrorCode ierr = VecDestroy(&vector); + AssertNothrow(ierr == 0, ExcPETScError(ierr)); + (void)ierr; } @@ -180,16 +179,12 @@ namespace PETScWrappers void VectorBase::clear() { - if (obtained_ownership) - { - const PetscErrorCode ierr = VecDestroy(&vector); - AssertThrow(ierr == 0, ExcPETScError(ierr)); - } + const PetscErrorCode ierr = VecDestroy(&vector); + AssertThrow(ierr == 0, ExcPETScError(ierr)); ghosted = false; ghost_indices.clear(); - last_action = ::dealii::VectorOperation::unknown; - obtained_ownership = true; + last_action = ::dealii::VectorOperation::unknown; } -- 2.39.5