]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::VectorBase: use PetscObjectReference
authorStefano Zampini <stefano.zampini@gmail.com>
Mon, 17 Oct 2022 10:23:18 +0000 (13:23 +0300)
committerLuca Heltai <luca.heltai@sissa.it>
Tue, 13 Dec 2022 16:32:26 +0000 (17:32 +0100)
now the deal object can outlive the Vec

include/deal.II/lac/petsc_vector_base.h
source/lac/petsc_parallel_vector.cc
source/lac/petsc_vector_base.cc

index 01fb81fa5cc8c9bb8a10e3ad3985b1237c8ed713..9fe7ba6e8b08b171f6ff4c33386a908ad61ba753 100644 (file)
@@ -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
index 87242539d7cb2469b77fb28e5cc2cda851e0aaac..794c687542be2b24b8d2e74c6d13dc0de74d0061 100644 (file)
@@ -131,7 +131,6 @@ namespace PETScWrappers
     void
     Vector::clear()
     {
-      obtained_ownership = true;
       VectorBase::clear();
 
       create_vector(0, 0);
index 510b7a4fda641949ece9e0b1968544827cca6e6e..71cf16a3daaa4eeb07392649fffb517756d42fe9 100644 (file)
@@ -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<PetscObject>(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;
   }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.