From: msteigemann Date: Thu, 21 May 2015 05:53:24 +0000 (+0200) Subject: Add a clear function for PETSc serial and parallel vectors X-Git-Tag: v8.3.0-rc1~156^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F947%2Fhead;p=dealii.git Add a clear function for PETSc serial and parallel vectors --- diff --git a/doc/news/changes.h b/doc/news/changes.h index 1d9097ce29..82eb390009 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -512,6 +512,12 @@ inconvenience this causes.

Specific improvements

    +
  1. New: Add a clear function to the PETSc::Vector + and PETSc::MPI::Vector classes similar to the Trilinos vector classes. +
    + (Martin Steigemann 2015/05/22) +
  2. +
  3. New: Three new quadrature formulas in quadrature_lib, based on Chebyshev quadrature rules. See functions QGaussChebyshev, QGaussRadauChebyshev and QGaussLobattoChebyshev. diff --git a/include/deal.II/lac/petsc_parallel_vector.h b/include/deal.II/lac/petsc_parallel_vector.h index 1b987c3417..ca7a3449db 100644 --- a/include/deal.II/lac/petsc_parallel_vector.h +++ b/include/deal.II/lac/petsc_parallel_vector.h @@ -262,6 +262,12 @@ namespace PETScWrappers explicit Vector (const IndexSet &local, const MPI_Comm &communicator); + /** + * Release all memory and return to a state just like after having called + * the default constructor. + */ + void clear (); + /** * Copy the given vector. Resize the present vector if necessary. Also * take over the MPI communicator of @p v. diff --git a/include/deal.II/lac/petsc_vector.h b/include/deal.II/lac/petsc_vector.h index c923ffc22e..5c907f0f83 100644 --- a/include/deal.II/lac/petsc_vector.h +++ b/include/deal.II/lac/petsc_vector.h @@ -118,6 +118,12 @@ namespace PETScWrappers */ explicit Vector (const MPI::Vector &v); + /** + * Release all memory and return to a state just like after having called + * the default constructor. + */ + void clear (); + /** * Copy the given vector. Resize the present vector if necessary. */ diff --git a/include/deal.II/lac/petsc_vector_base.h b/include/deal.II/lac/petsc_vector_base.h index 4d60dfb11a..e17617ef06 100644 --- a/include/deal.II/lac/petsc_vector_base.h +++ b/include/deal.II/lac/petsc_vector_base.h @@ -260,6 +260,12 @@ namespace PETScWrappers */ virtual ~VectorBase (); + /** + * Release all memory and return to a state just like after having called + * the default constructor. + */ + virtual void clear (); + /** * Compress the underlying representation of the PETSc object, i.e. flush * the buffers of the vector object if it has any. This function is diff --git a/source/lac/petsc_parallel_vector.cc b/source/lac/petsc_parallel_vector.cc index 87fde1ad6e..5037b9fc64 100644 --- a/source/lac/petsc_parallel_vector.cc +++ b/source/lac/petsc_parallel_vector.cc @@ -80,6 +80,7 @@ namespace PETScWrappers } + Vector::Vector (const IndexSet &local, const MPI_Comm &communicator) : @@ -90,6 +91,23 @@ namespace PETScWrappers } + + void + Vector::clear () + { + // destroy the PETSc Vec and create an invalid empty vector, + // so we can just as well create a sequential one to avoid + // all the overhead incurred by parallelism + attained_ownership = true; + VectorBase::clear (); + + const int n = 0; + int ierr = VecCreateSeq (PETSC_COMM_SELF, n, &vector); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + } + + + void Vector::reinit (const MPI_Comm &comm, const size_type n, diff --git a/source/lac/petsc_vector.cc b/source/lac/petsc_vector.cc index 0cf745a7b2..8baa0aeecf 100644 --- a/source/lac/petsc_vector.cc +++ b/source/lac/petsc_vector.cc @@ -61,6 +61,15 @@ namespace PETScWrappers + void + Vector::clear () + { + VectorBase::clear (); + Vector::create_vector (0); + } + + + void Vector::reinit (const size_type n, const bool fast) diff --git a/source/lac/petsc_vector_base.cc b/source/lac/petsc_vector_base.cc index b127024d25..0feeaef91a 100644 --- a/source/lac/petsc_vector_base.cc +++ b/source/lac/petsc_vector_base.cc @@ -210,6 +210,27 @@ namespace PETScWrappers + void + VectorBase::clear () + { + if (attained_ownership) + { +#if DEAL_II_PETSC_VERSION_LT(3,2,0) + const int ierr = VecDestroy (vector); +#else + const int ierr = VecDestroy (&vector); +#endif + AssertThrow (ierr == 0, ExcPETScError(ierr)); + } + + ghosted = false; + ghost_indices.clear (); + last_action = ::dealii::VectorOperation::unknown; + attained_ownership = true; + } + + + VectorBase & VectorBase::operator = (const PetscScalar s) {