From: Wolfgang Bangerth Date: Fri, 16 Apr 2004 20:49:34 +0000 (+0000) Subject: Have a function that copies a sequential into a parallel vector, but only the local... X-Git-Tag: v8.0.0~15319 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4cd93db24929a390ded2fad12221282e0df57ae6;p=dealii.git Have a function that copies a sequential into a parallel vector, but only the local part. git-svn-id: https://svn.dealii.org/trunk@9030 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/petsc_parallel_vector.h b/deal.II/lac/include/lac/petsc_parallel_vector.h index 2c3f9bd774..6eb511a7d0 100644 --- a/deal.II/lac/include/lac/petsc_parallel_vector.h +++ b/deal.II/lac/include/lac/petsc_parallel_vector.h @@ -130,6 +130,43 @@ namespace PETScWrappers */ Vector & operator = (const Vector &v); + /** + * Copy the given sequential + * (non-distributed) vector + * into the present parallel + * vector. It is assumed that + * they have the same size, + * and this operation does + * not change the + * partitioning of the + * parallel vector by which + * its elements are + * distributed across several + * MPI processes. What this + * operation therefore does + * is to copy that chunk of + * the given vector @p v that + * corresponds to elements of + * the target vector that are + * stored locally, and copies + * them. Elements that are + * not stored locally are not + * touched. + * + * This being a parallel + * vector, you must make sure + * that @em all processes + * call this function at the + * same time. It is not + * possible to change the + * local part of a parallel + * vector on only one + * process, independent of + * what other processes do, + * with this function. + */ + Vector & operator = (const PETScWrappers::Vector &v); + /** * Set all components of the vector to * the given number @p{s}. Simply pass diff --git a/deal.II/lac/source/petsc_parallel_vector.cc b/deal.II/lac/source/petsc_parallel_vector.cc index 733190a246..35491c5beb 100644 --- a/deal.II/lac/source/petsc_parallel_vector.cc +++ b/deal.II/lac/source/petsc_parallel_vector.cc @@ -13,8 +13,10 @@ #include +#include #include +#include #ifdef DEAL_II_USE_PETSC @@ -105,7 +107,38 @@ namespace PETScWrappers reinit (v.size(), v.local_size(), fast); } - + + + + Vector & + Vector::operator = (const PETScWrappers::Vector &v) + { + int ierr; + + // get a pointer to the local memory of this vector + PetscScalar *dest_array; + ierr = VecGetArray (vector, &dest_array); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + // then also a pointer to the source vector + PetscScalar *src_array; + ierr = VecGetArray (static_cast(v), &src_array); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + // then copy: + const std::pair local_elements = local_range (); + std::copy (src_array + local_elements.first, src_array + local_elements.second, + dest_array); + + // finally restore the arrays + ierr = VecRestoreArray (vector, &dest_array); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + ierr = VecRestoreArray (static_cast(v), &src_array); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + return *this; + } void