]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Added a new constructor for PETScWrappers::Vector that takes an existing PETSc Vec...
authorheister <heister@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 3 Dec 2010 22:43:02 +0000 (22:43 +0000)
committerheister <heister@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 3 Dec 2010 22:43:02 +0000 (22:43 +0000)
git-svn-id: https://svn.dealii.org/trunk@22914 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/lac/petsc_vector.h
deal.II/include/deal.II/lac/petsc_vector_base.h
deal.II/source/lac/petsc_vector.cc
deal.II/source/lac/petsc_vector_base.cc

index dbb11970f30ce85c7e26f5406513366e7255a1ab..0ea8e105ad8a191177546960fcf67f8030ebfb07 100644 (file)
@@ -94,6 +94,13 @@ through DoFHandler::get_tria() and DoFHandler::get_fe(), respectively.
 <h3>General</h3>
 
 <ol>
+  <li><p>New: Added a new constructor for PETScWrappers::Vector that takes
+  an existing PETSc Vec that was created by the user and is only wrapped for
+  usage.
+  <br>
+  (TH, 2010/12/03)
+  </p></li>
+
   <li><p>Updated: The version of the <a href="http://www.threadingbuildingblocks.org">Threading
   Building Blocks (TBB)</a> shipped with deal.II has been updated
   to release 3.0 Update 3 (Commercially aligned version).
index 166570736ddf25cf57b79eb39ba7747e9254ae60..dd35878af07394949318b540fd83ca0e76541a5e 100644 (file)
@@ -79,6 +79,18 @@ namespace PETScWrappers
       template <typename Number>
       explicit Vector (const dealii::Vector<Number> &v);
 
+                                      /**
+                                       * Construct it from an existing PETSc
+                                       * Vector of type Vec. Note: this does
+                                       * not copy the contents and just keeps
+                                       * a pointer. You need to make sure the
+                                       * vector is not used twice at the same
+                                       * time or destroyed while in use. This
+                                       * class does not destroy the PETSc
+                                       * object. Handle with care!
+                                       */
+      explicit Vector (const Vec & v);
+      
                                        /**
                                         * Copy-constructor the values from a
                                         * PETSc wrapper vector class.
@@ -226,6 +238,13 @@ namespace PETScWrappers
     *this = v;
   }
 
+
+
+  inline
+  Vector::Vector (const Vec & v)
+                 :
+                 VectorBase(v)
+  {}
   
   
   inline
@@ -259,12 +278,18 @@ namespace PETScWrappers
   Vector &
   Vector::operator = (const MPI::Vector &v)
   {
+    int ierr;
+    if (attained_ownership)
+      {
                                      // the petsc function we call wants to
                                      // generate the vector itself, so destroy
                                      // the old one first
-    int ierr = VecDestroy (vector);
-    AssertThrow (ierr == 0, ExcPETScError(ierr));
+       ierr = VecDestroy (vector);
+       AssertThrow (ierr == 0, ExcPETScError(ierr));
+      }
 
+    attained_ownership = true;
+    
                                      // then do the gather
                                      // operation. <rant>petsc has changed its
                                      // interface again, and replaced a single
index 1274105ef6ab2f4908ea49cffca818f858fab78b..a74c5b0a53b3849cf177e8ea36887fbcfdb99efc 100644 (file)
@@ -254,6 +254,15 @@ namespace PETScWrappers
                                         */
       VectorBase (const VectorBase &v);
 
+                                      /**
+                                       * Initialize a Vector from a PETSc Vec
+                                       * object. Note that we do not copy the
+                                       * vector and we do not attain
+                                       * ownership, so we do not destroy the
+                                       * PETSc object in the destructor.
+                                       */
+      explicit VectorBase (const Vec & v);
+      
                                        /**
                                         * Destructor
                                         */
@@ -803,6 +812,15 @@ namespace PETScWrappers
                                         */
       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 destructed in
+                                       * the destructor.
+                                       */
+      bool attained_ownership;
+      
                                       /**
                                         * Collective set or add
                                         * operation: This function is
index 10fa556a39ff13987ae1cfcae7866bfd605ea806..5a3b476dc7bfdebeee370db4367606b0e9b80e65 100644 (file)
@@ -76,9 +76,11 @@ namespace PETScWrappers
 //         AssertThrow (ierr == 0, ExcPETScError(ierr));
 
                                          // so let's go the slow way:
-        int ierr;
-        ierr = VecDestroy (vector);
-        AssertThrow (ierr == 0, ExcPETScError(ierr));
+       if (attained_ownership)
+         {
+           int ierr = VecDestroy (vector);
+           AssertThrow (ierr == 0, ExcPETScError(ierr));
+         }
 
         create_vector (n);
       }
@@ -106,6 +108,7 @@ namespace PETScWrappers
     const int ierr
       = VecCreateSeq (PETSC_COMM_SELF, n, &vector);
     AssertThrow (ierr == 0, ExcPETScError(ierr));
+    attained_ownership = true;
   }
 }
 
index 412169855144e80d8912cdf5036852d4731489cb..3ada9aee1d05b06980b7aff687c42a4eeb35f694 100644 (file)
@@ -177,7 +177,8 @@ namespace PETScWrappers
   VectorBase::VectorBase ()
                   :
                  ghosted(false),
-                  last_action (LastAction::none)
+                  last_action (LastAction::none),
+                 attained_ownership(true)
   {}
 
 
@@ -187,7 +188,8 @@ namespace PETScWrappers
                  Subscriptor (),
                  ghosted(v.ghosted),
                  ghost_indices(v.ghost_indices),
-                  last_action (LastAction::none)
+                  last_action (LastAction::none),
+                 attained_ownership(true)
   {
     int ierr = VecDuplicate (v.vector, &vector);
     AssertThrow (ierr == 0, ExcPETScError(ierr));
@@ -197,10 +199,25 @@ namespace PETScWrappers
   }
 
 
+  
+  VectorBase::VectorBase (const Vec & v)
+                 :
+                 Subscriptor (),
+                 vector(v),
+                 ghosted(false),
+                  last_action (LastAction::none),
+                 attained_ownership(false)
+  {}
+
+  
+
   VectorBase::~VectorBase ()
   {
-    const int ierr = VecDestroy (vector);
-    AssertThrow (ierr == 0, ExcPETScError(ierr));
+    if (attained_ownership)
+      {        
+       const int ierr = VecDestroy (vector);
+       AssertThrow (ierr == 0, ExcPETScError(ierr));
+      }
   }
 
 

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.