From 3aa8da1157ace4cfd7e4292370200ef0608655c9 Mon Sep 17 00:00:00 2001 From: heister Date: Fri, 3 Dec 2010 22:43:02 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.dealii.org/trunk@22914 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 7 +++++ deal.II/include/deal.II/lac/petsc_vector.h | 29 +++++++++++++++++-- .../include/deal.II/lac/petsc_vector_base.h | 18 ++++++++++++ deal.II/source/lac/petsc_vector.cc | 9 ++++-- deal.II/source/lac/petsc_vector_base.cc | 25 +++++++++++++--- 5 files changed, 79 insertions(+), 9 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index dbb11970f3..0ea8e105ad 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -94,6 +94,13 @@ through DoFHandler::get_tria() and DoFHandler::get_fe(), respectively.

General

    +
  1. 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. +
    + (TH, 2010/12/03) +

  2. +
  3. Updated: The version of the Threading Building Blocks (TBB) shipped with deal.II has been updated to release 3.0 Update 3 (Commercially aligned version). diff --git a/deal.II/include/deal.II/lac/petsc_vector.h b/deal.II/include/deal.II/lac/petsc_vector.h index 166570736d..dd35878af0 100644 --- a/deal.II/include/deal.II/lac/petsc_vector.h +++ b/deal.II/include/deal.II/lac/petsc_vector.h @@ -79,6 +79,18 @@ namespace PETScWrappers template explicit Vector (const dealii::Vector &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. petsc has changed its // interface again, and replaced a single diff --git a/deal.II/include/deal.II/lac/petsc_vector_base.h b/deal.II/include/deal.II/lac/petsc_vector_base.h index 1274105ef6..a74c5b0a53 100644 --- a/deal.II/include/deal.II/lac/petsc_vector_base.h +++ b/deal.II/include/deal.II/lac/petsc_vector_base.h @@ -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 diff --git a/deal.II/source/lac/petsc_vector.cc b/deal.II/source/lac/petsc_vector.cc index 10fa556a39..5a3b476dc7 100644 --- a/deal.II/source/lac/petsc_vector.cc +++ b/deal.II/source/lac/petsc_vector.cc @@ -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; } } diff --git a/deal.II/source/lac/petsc_vector_base.cc b/deal.II/source/lac/petsc_vector_base.cc index 4121698551..3ada9aee1d 100644 --- a/deal.II/source/lac/petsc_vector_base.cc +++ b/deal.II/source/lac/petsc_vector_base.cc @@ -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)); + } } -- 2.39.5