// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2004 - 2025 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_petsc_vector_base_h #define dealii_petsc_vector_base_h #include #ifdef DEAL_II_WITH_PETSC # include # include # include # include # include # include # include # include # include DEAL_II_NAMESPACE_OPEN // forward declaration # ifndef DOXYGEN template class Vector; namespace PETScWrappers { class VectorBase; } # endif /** * A namespace in which wrapper classes for PETSc objects reside. * * @ingroup PETScWrappers * @ingroup Vectors */ namespace PETScWrappers { /** * @cond internal */ /** * A namespace for internal implementation details of the PETScWrapper * members. * @ingroup PETScWrappers */ namespace internal { /** * Since access to PETSc vectors only goes through functions, rather than * by obtaining a reference to a vector element, we need a wrapper class * that acts as if it was a reference, and basically redirects all * accesses (read and write) to member functions of this class. * * This class implements such a wrapper: it is initialized with a vector * and an element within it, and has a conversion operator to extract the * scalar value of this element. It also has a variety of assignment * operator for writing to this one element. * @ingroup PETScWrappers */ class VectorReference { public: /** * Declare type for container size. */ using size_type = types::global_dof_index; private: /** * Constructor. It is made private so as to only allow the actual vector * class to create it. */ VectorReference(const VectorBase &vector, const size_type index); public: /* * Copy constructor. */ VectorReference(const VectorReference &vector) = default; /** * This looks like a copy operator, but does something different than * usual. In particular, it does not copy the member variables of this * reference. Rather, it handles the situation where we have two vectors * @p v and @p w, and assign elements like in v(i)=w(i). Here, * both left and right hand side of the assignment have data type * VectorReference, but what we really mean is to assign the vector * elements represented by the two references. This operator implements * this operation. Note also that this allows us to make the assignment * operator const. */ const VectorReference & operator=(const VectorReference &r) const; /** * The same function as above, but for non-const reference objects. The * function is needed since the compiler might otherwise automatically * generate a copy operator for non-const objects. */ VectorReference & operator=(const VectorReference &r); /** * Set the referenced element of the vector to s. */ const VectorReference & operator=(const PetscScalar &s) const; /** * Add s to the referenced element of the vector. */ const VectorReference & operator+=(const PetscScalar &s) const; /** * Subtract s from the referenced element of the vector. */ const VectorReference & operator-=(const PetscScalar &s) const; /** * Multiply the referenced element of the vector by s. */ const VectorReference & operator*=(const PetscScalar &s) const; /** * Divide the referenced element of the vector by s. */ const VectorReference & operator/=(const PetscScalar &s) const; /** * Return the real part of the value of the referenced element. */ PetscReal real() const; /** * Return the imaginary part of the value of the referenced element. * * @note This operation is not defined for real numbers and an exception * is thrown. */ PetscReal imag() const; /** * Convert the reference to an actual value, i.e. return the value of * the referenced element of the vector. */ operator PetscScalar() const; /** * Exception */ DeclException3( ExcAccessToNonlocalElement, int, int, int, << "You tried to access element " << arg1 << " of a distributed vector, but only elements in range [" << arg2 << ',' << arg3 << "] are stored locally and can be accessed." << "\n\n" << "A common source for this kind of problem is that you " << "are passing a 'fully distributed' vector into a function " << "that needs read access to vector elements that correspond " << "to degrees of freedom on ghost cells (or at least to " << "'locally active' degrees of freedom that are not also " << "'locally owned'). You need to pass a vector that has these " << "elements as ghost entries."); /** * Exception. */ DeclException2(ExcWrongMode, int, int, << "You tried to do a " << (arg1 == 1 ? "'set'" : (arg1 == 2 ? "'add'" : "???")) << " operation but the vector is currently in " << (arg2 == 1 ? "'set'" : (arg2 == 2 ? "'add'" : "???")) << " mode. You first have to call 'compress()'."); private: /** * Point to the vector we are referencing. */ const VectorBase &vector; /** * Index of the referenced element of the vector. */ const size_type index; // Make the vector class a friend, so that it can create objects of the // present type. friend class ::dealii::PETScWrappers::VectorBase; }; } // namespace internal /** * @endcond */ /** * Base class for all vector classes that are implemented on top of the * PETSc vector types. Since in PETSc all vector types (i.e. sequential and * parallel ones) are built by filling the contents of an abstract object * that is only referenced through a pointer of a type that is independent * of the actual vector type, we can implement almost all functionality of * vectors in this base class. As such, this class can also be used as a * deal.II-compatible wrapper for a PETSc Vec object of any * type. Derived classes will then only have to provide the functionality to * create one or the other kind of vector. * * The interface of this class is modeled after the existing Vector class in * deal.II. It has almost the same member functions, and is often * exchangeable. However, since PETSc only supports a single scalar type * (either double, float, or a complex data type), it is not templated, and * only works with whatever your PETSc installation has defined the data * type @p PetscScalar to. * * Note that PETSc only guarantees that operations do what you expect if the * functions @p VecAssemblyBegin and @p VecAssemblyEnd have been called * after vector assembly. Therefore, you need to call Vector::compress() * before you actually use the vector. * * @ingroup PETScWrappers */ class VectorBase : public ReadVector { public: /** * Declare some of the standard types used in all containers. These types * parallel those in the C++ standard libraries * vector<...> class. */ using value_type = PetscScalar; using real_type = PetscReal; using size_type = types::global_dof_index; using reference = internal::VectorReference; using const_reference = const internal::VectorReference; /** * Default constructor. It doesn't do anything, derived classes will have * to initialize the data. */ VectorBase(); /** * Copy constructor. Sets the dimension to that of the given vector, and * copies all elements. */ VectorBase(const VectorBase &v); /** * Initialize a Vector from a PETSc Vec object. Note that we do not copy * the vector. */ explicit VectorBase(const Vec &v); /** * Destructor. */ virtual ~VectorBase() override; /** * 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 * necessary after writing into a vector element-by-element and before * anything else can be done on it. * * See * @ref GlossCompress "Compressing distributed objects" * for more information. */ void compress(const VectorOperation::values operation); /** * The copy assignment operator. */ VectorBase & operator=(const VectorBase &); /** * Set all components of the vector to the given number @p s. Simply pass * this down to the individual block objects, but we still need to declare * this function to make the example given in the discussion about making * the constructor explicit work. * * * Since the semantics of assigning a scalar to a vector are not * immediately clear, this operator should really only be used if you want * to set the entire vector to zero. This allows the intuitive notation * v=0. Assigning other values is deprecated and may be * disallowed in the future. */ VectorBase & operator=(const PetscScalar s); /** * This method associates the PETSc Vec to the instance of the class. * This is particularly useful when performing PETSc to Deal.II operations * since it allows to reuse the Deal.II VectorBase and the PETSc Vec * without incurring in memory copies. */ void reinit(Vec v); /** * Test for equality. This function assumes that the present vector and * the one to compare with have the same size already, since comparing * vectors of different sizes makes not much sense anyway. */ bool operator==(const VectorBase &v) const; /** * Test for inequality. This function assumes that the present vector and * the one to compare with have the same size already, since comparing * vectors of different sizes makes not much sense anyway. */ bool operator!=(const VectorBase &v) const; /** * Return the global dimension of the vector. */ size_type size() const override; /** * Return the local dimension of the vector, i.e. the number of elements * stored on the present MPI process. For sequential vectors, this number * is the same as size(), but for parallel vectors it may be smaller. * * To figure out which elements exactly are stored locally, use * local_range() or locally_owned_elements(). */ size_type locally_owned_size() const; /** * Return a pair of indices indicating which elements of this vector are * stored locally. The first number is the index of the first element * stored, the second the index of the one past the last one that is * stored locally. If this is a sequential vector, then the result will be * the pair (0,N), otherwise it will be a pair (i,i+n), where * n=locally_owned_size(). */ std::pair local_range() const; /** * Return whether @p index is in the local range or not, see also * local_range(). */ bool in_local_range(const size_type index) const; /** * Return an index set that describes which elements of this vector are * owned by the current processor. Note that this index set does not * include elements this vector may store locally as ghost elements but * that are in fact owned by another processor. As a consequence, the * index sets returned on different processors if this is a distributed * vector will form disjoint sets that add up to the complete index set. * Obviously, if a vector is created on only one processor, then the * result would satisfy * @code * vec.locally_owned_elements() == complete_index_set (vec.size()) * @endcode */ IndexSet locally_owned_elements() const; /** * Return if the vector contains ghost elements. * * @see * @ref GlossGhostedVector "vectors with ghost elements" */ bool has_ghost_elements() const; /** * Return the IndexSet of ghost elements. */ const IndexSet & ghost_elements() const; /** * Update ghosted elements. */ void update_ghost_values() const; /** * Provide access to a given element, both read and write. */ reference operator()(const size_type index); /** * Provide read-only access to an element. */ PetscScalar operator()(const size_type index) const; /** * Provide access to a given element, both read and write. * * Exactly the same as operator(). */ reference operator[](const size_type index); /** * Provide read-only access to an element. * * Exactly the same as operator(). */ PetscScalar operator[](const size_type index) const; /** * A collective set operation: instead of setting individual elements of a * vector, this function allows to set a whole set of elements at once. * The indices of the elements to be set are stated in the first argument, * the corresponding values in the second. */ void set(const std::vector &indices, const std::vector &values); /** * Instead of getting individual elements of a vector via operator(), * this function allows getting a whole set of elements at once. The * indices of the elements to be read are stated in the first argument, the * corresponding values are returned in the second. * * If the current vector is called @p v, then this function is the equivalent * to the code * @code * for (unsigned int i=0; i &indices, std::vector &values) const; /** * Extract a range of elements all at once. */ virtual void extract_subvector_to( const ArrayView &indices, const ArrayView &elements) const override; /** * Instead of getting individual elements of a vector via operator(), * this function allows getting a whole set of elements at once. In * contrast to the previous function, this function obtains the * indices of the elements by dereferencing all elements of the iterator * range provided by the first two arguments, and puts the vector * values into memory locations obtained by dereferencing a range * of iterators starting at the location pointed to by the third * argument. * * If the current vector is called @p v, then this function is the equivalent * to the code * @code * ForwardIterator indices_p = indices_begin; * OutputIterator values_p = values_begin; * while (indices_p != indices_end) * { * *values_p = v[*indices_p]; * ++indices_p; * ++values_p; * } * @endcode * * @pre It must be possible to write into as many memory locations * starting at @p values_begin as there are iterators between * @p indices_begin and @p indices_end. */ template void extract_subvector_to(const ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const; /** * A collective add operation: This function adds a whole set of values * stored in @p values to the vector components specified by @p indices. */ void add(const std::vector &indices, const std::vector &values); /** * This is a second collective add operation. As a difference, this * function takes a deal.II vector of values. */ void add(const std::vector &indices, const ::dealii::Vector &values); /** * Take an address where n_elements are stored contiguously and * add them into the vector. Handles all cases which are not covered by * the other two add() functions above. */ void add(const size_type n_elements, const size_type *indices, const PetscScalar *values); /** * Return the scalar product of two vectors. The vectors must have the * same size. * * For complex valued vector, this gives$\left(v^\ast,vec\right)$. */ PetscScalar operator*(const VectorBase &vec) const; /** * Return the square of the $l_2$-norm. */ real_type norm_sqr() const; /** * Return the mean value of the elements of this vector. */ PetscScalar mean_value() const; /** * $l_1$-norm of the vector. The sum of the absolute values. * * @note In complex-valued PETSc priori to 3.7.0 this norm is implemented * as the sum of absolute values of real and imaginary parts of elements * of a complex vector. */ real_type l1_norm() const; /** * $l_2$-norm of the vector. The square root of the sum of the squares of * the elements. */ real_type l2_norm() const; /** * $l_p$-norm of the vector. The pth root of the sum of the pth powers of * the absolute values of the elements. */ real_type lp_norm(const real_type p) const; /** * $l_\infty$-norm of the vector. Return the value of the vector element * with the maximum absolute value. */ real_type linfty_norm() const; /** * Performs a combined operation of a vector addition and a subsequent * inner product, returning the value of the inner product. In other * words, the result of this function is the same as if the user called * @code * this->add(a, V); * return_value = *this * W; * @endcode * * The reason this function exists is for compatibility with deal.II's own * vector classes which can implement this functionality with less memory * transfer. However, for PETSc vectors such a combined operation is not * natively supported and thus the cost is completely equivalent as * calling the two methods separately. * * For complex-valued vectors, the scalar product in the second step is * implemented as * $\left=\sum_i v_i \bar{w_i}$. */ PetscScalar add_and_dot(const PetscScalar a, const VectorBase &V, const VectorBase &W); /** * Return whether the vector contains only elements with value zero. This * is a @ref GlossCollectiveOperation "collective operation". This function is expensive, because * potentially all elements have to be checked. */ bool all_zero() const; /** * Multiply the entire vector by a fixed factor. */ VectorBase & operator*=(const PetscScalar factor); /** * Divide the entire vector by a fixed factor. */ VectorBase & operator/=(const PetscScalar factor); /** * Add the given vector to the present one. */ VectorBase & operator+=(const VectorBase &V); /** * Subtract the given vector from the present one. */ VectorBase & operator-=(const VectorBase &V); /** * Addition of @p s to all components. Note that @p s is a scalar and not * a vector. */ void add(const PetscScalar s); /** * Simple addition of a multiple of a vector, i.e. *this += a*V. */ void add(const PetscScalar a, const VectorBase &V); /** * Multiple addition of scaled vectors, i.e. *this += a*V+b*W. */ void add(const PetscScalar a, const VectorBase &V, const PetscScalar b, const VectorBase &W); /** * Scaling and simple vector addition, i.e. *this = s*(*this)+V. */ void sadd(const PetscScalar s, const VectorBase &V); /** * Scaling and simple addition, i.e. *this = s*(*this)+a*V. */ void sadd(const PetscScalar s, const PetscScalar a, const VectorBase &V); /** * Scale each element of this vector by the corresponding element in the * argument. This function is mostly meant to simulate multiplication (and * immediate re-assignment) by a diagonal scaling matrix. */ void scale(const VectorBase &scaling_factors); /** * Assignment *this = a*V. */ void equ(const PetscScalar a, const VectorBase &V); /** * Prints the PETSc vector object values using PETSc internal vector * viewer function VecView. The default format prints the * vector's contents, including indices of vector elements. For other * valid view formats, consult * http://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecView.html */ void write_ascii(const PetscViewerFormat format = PETSC_VIEWER_DEFAULT); /** * Print to a stream. @p precision denotes the desired precision with * which values shall be printed, @p scientific whether scientific * notation shall be used. If @p across is @p true then the vector is * printed in a line, while if @p false then the elements are printed on a * separate line each. */ void print(std::ostream &out, const unsigned int precision = 3, const bool scientific = true, const bool across = true) const; /** * Write the data of this object to a stream for the purpose of * serialization using the [BOOST serialization * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). * * @note Each processor only serializes its own locally owned values. */ template void save(Archive &ar, const unsigned int version) const; /** * Read the data of this object from a stream for the purpose of * serialization using the [BOOST serialization * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). */ template void load(Archive &ar, const unsigned int version); # ifdef DOXYGEN /** * Write and read the data of this object from a stream for the purpose * of serialization using the [BOOST serialization * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). */ template void serialize(Archive &archive, const unsigned int version); # else // This macro defines the serialize() method that is compatible with // the templated save() and load() method that have been implemented. BOOST_SERIALIZATION_SPLIT_MEMBER() # endif /** * Swap the contents of this vector and the other vector @p v. One could * do this operation with a temporary variable and copying over the data * elements, but this function is significantly more efficient since it * only swaps the pointers to the data of the two vectors and therefore * does not need to allocate temporary storage and move data around. * * This function is analogous to the @p swap function of all C++ * standard containers. Also, there is a global function * swap(u,v) that simply calls u.swap(v), again in * analogy to standard functions. */ void swap(VectorBase &v) noexcept; /** * Conversion operator to gain access to the underlying PETSc type. If you * do this, you cut this class off some information it may need, so this * conversion operator should only be used if you know what you do. In * particular, it should only be used for read-only operations into the * vector. */ operator const Vec &() const; /** * Return a reference to the underlying PETSc type. It can be used to * modify the underlying data, so use it only when you know what you * are doing. */ Vec & petsc_vector(); /** * Estimate for the memory consumption (not implemented for this class). */ std::size_t memory_consumption() const; /** * Return the underlying MPI communicator. */ MPI_Comm get_mpi_communicator() const; protected: /** * A generic vector object in PETSc. The actual type, a sequential vector, * is set in the constructor. */ Vec vector; /** * Denotes if this vector has ghost indices associated with it. This means * that at least one of the processes in a parallel program has at least * one ghost index. */ bool ghosted; /** * This vector contains the global indices of the ghost values. The * location in this vector denotes the local numbering, which is used in * PETSc. */ IndexSet ghost_indices; /** * Store whether the last action was a write or add operation. This * variable is @p mutable so that the accessor classes can write to it, * even though the vector object they refer to is constant. */ mutable VectorOperation::values last_action; // Make the reference class a friend. friend class internal::VectorReference; /** * 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 * corresponding value. */ void do_set_add_operation(const size_type n_elements, const size_type *indices, const PetscScalar *values, const bool add_values); /** * Determine ghost indices from the internal PETSc Vec */ void determine_ghost_indices(); }; // ------------------- inline and template functions -------------- /** * Global function @p swap which overloads the default implementation of the * C++ standard library which uses a temporary object. The function simply * exchanges the data of the two vectors. * * @relatesalso PETScWrappers::VectorBase */ inline void swap(VectorBase &u, VectorBase &v) noexcept { u.swap(v); } # ifndef DOXYGEN namespace internal { inline VectorReference::VectorReference(const VectorBase &vector, const size_type index) : vector(vector) , index(index) {} inline const VectorReference & VectorReference::operator=(const VectorReference &r) const { // as explained in the class // documentation, this is not the copy // operator. so simply pass on to the // "correct" assignment operator *this = static_cast(r); return *this; } inline VectorReference & VectorReference::operator=(const VectorReference &r) { // as explained in the class // documentation, this is not the copy // operator. so simply pass on to the // "correct" assignment operator *this = static_cast(r); return *this; } inline const VectorReference & VectorReference::operator=(const PetscScalar &value) const { Assert((vector.last_action == VectorOperation::insert) || (vector.last_action == VectorOperation::unknown), ExcWrongMode(VectorOperation::insert, vector.last_action)); Assert(!vector.has_ghost_elements(), ExcGhostsPresent()); const PetscInt petsc_i = index; const PetscErrorCode ierr = VecSetValues(vector, 1, &petsc_i, &value, INSERT_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); vector.last_action = VectorOperation::insert; return *this; } inline const VectorReference & VectorReference::operator+=(const PetscScalar &value) const { Assert((vector.last_action == VectorOperation::add) || (vector.last_action == VectorOperation::unknown), ExcWrongMode(VectorOperation::add, vector.last_action)); Assert(!vector.has_ghost_elements(), ExcGhostsPresent()); vector.last_action = VectorOperation::add; // we have to do above actions in any // case to be consistent with the MPI // communication model (see the // comments in the documentation of // PETScWrappers::MPI::Vector), but we // can save some work if the addend is // zero if (value == PetscScalar()) return *this; // use the PETSc function to add something const PetscInt petsc_i = index; const PetscErrorCode ierr = VecSetValues(vector, 1, &petsc_i, &value, ADD_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); return *this; } inline const VectorReference & VectorReference::operator-=(const PetscScalar &value) const { Assert((vector.last_action == VectorOperation::add) || (vector.last_action == VectorOperation::unknown), ExcWrongMode(VectorOperation::add, vector.last_action)); Assert(!vector.has_ghost_elements(), ExcGhostsPresent()); vector.last_action = VectorOperation::add; // we have to do above actions in any // case to be consistent with the MPI // communication model (see the // comments in the documentation of // PETScWrappers::MPI::Vector), but we // can save some work if the addend is // zero if (value == PetscScalar()) return *this; // use the PETSc function to // add something const PetscInt petsc_i = index; const PetscScalar subtractand = -value; const PetscErrorCode ierr = VecSetValues(vector, 1, &petsc_i, &subtractand, ADD_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); return *this; } inline const VectorReference & VectorReference::operator*=(const PetscScalar &value) const { Assert((vector.last_action == VectorOperation::insert) || (vector.last_action == VectorOperation::unknown), ExcWrongMode(VectorOperation::insert, vector.last_action)); Assert(!vector.has_ghost_elements(), ExcGhostsPresent()); vector.last_action = VectorOperation::insert; // we have to do above actions in any // case to be consistent with the MPI // communication model (see the // comments in the documentation of // PETScWrappers::MPI::Vector), but we // can save some work if the factor is // one if (value == 1.) return *this; const PetscInt petsc_i = index; const PetscScalar new_value = static_cast(*this) * value; const PetscErrorCode ierr = VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); return *this; } inline const VectorReference & VectorReference::operator/=(const PetscScalar &value) const { Assert((vector.last_action == VectorOperation::insert) || (vector.last_action == VectorOperation::unknown), ExcWrongMode(VectorOperation::insert, vector.last_action)); Assert(!vector.has_ghost_elements(), ExcGhostsPresent()); vector.last_action = VectorOperation::insert; // we have to do above actions in any // case to be consistent with the MPI // communication model (see the // comments in the documentation of // PETScWrappers::MPI::Vector), but we // can save some work if the factor is // one if (value == 1.) return *this; const PetscInt petsc_i = index; const PetscScalar new_value = static_cast(*this) / value; const PetscErrorCode ierr = VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); return *this; } inline PetscReal VectorReference::real() const { # ifndef PETSC_USE_COMPLEX return static_cast(*this); # else return PetscRealPart(static_cast(*this)); # endif } inline PetscReal VectorReference::imag() const { # ifndef PETSC_USE_COMPLEX return PetscReal(0); # else return PetscImaginaryPart(static_cast(*this)); # endif } } // namespace internal inline bool VectorBase::in_local_range(const size_type index) const { PetscInt begin, end; const PetscErrorCode ierr = VecGetOwnershipRange(static_cast(vector), &begin, &end); AssertThrow(ierr == 0, ExcPETScError(ierr)); return ((index >= static_cast(begin)) && (index < static_cast(end))); } inline IndexSet VectorBase::locally_owned_elements() const { IndexSet is(size()); // PETSc only allows for contiguous local ranges, so this is simple const std::pair x = local_range(); is.add_range(x.first, x.second); return is; } inline bool VectorBase::has_ghost_elements() const { return ghosted; } inline const IndexSet & VectorBase::ghost_elements() const { return ghost_indices; } inline void VectorBase::update_ghost_values() const { if (ghosted) { PetscErrorCode ierr; ierr = VecGhostUpdateBegin(vector, INSERT_VALUES, SCATTER_FORWARD); AssertThrow(ierr == 0, ExcPETScError(ierr)); ierr = VecGhostUpdateEnd(vector, INSERT_VALUES, SCATTER_FORWARD); AssertThrow(ierr == 0, ExcPETScError(ierr)); } } inline internal::VectorReference VectorBase::operator()(const size_type index) { return internal::VectorReference(*this, index); } inline PetscScalar VectorBase::operator()(const size_type index) const { return static_cast(internal::VectorReference(*this, index)); } inline internal::VectorReference VectorBase::operator[](const size_type index) { return operator()(index); } inline PetscScalar VectorBase::operator[](const size_type index) const { return operator()(index); } inline MPI_Comm VectorBase::get_mpi_communicator() const { return PetscObjectComm(reinterpret_cast(vector)); } inline void VectorBase::extract_subvector_to(const std::vector &indices, std::vector &values) const { Assert(indices.size() <= values.size(), ExcDimensionMismatch(indices.size(), values.size())); extract_subvector_to(indices.begin(), indices.end(), values.begin()); } inline void VectorBase::extract_subvector_to( const ArrayView &indices, const ArrayView &elements) const { AssertDimension(indices.size(), elements.size()); extract_subvector_to(indices.begin(), indices.end(), elements.begin()); } template inline void VectorBase::extract_subvector_to(const ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const { if (indices_begin == indices_end) return; // if we are dealing // with a parallel vector if (ghosted) { // there is the possibility // that the vector has // ghost elements. in that // case, we first need to // figure out which // elements we own locally, // then get a pointer to // the elements that are // stored here (both the // ones we own as well as // the ghost elements). in // this array, the locally // owned elements come // first followed by the // ghost elements whose // position we can get from // an index set PetscInt begin, end; PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end); AssertThrow(ierr == 0, ExcPETScError(ierr)); Vec locally_stored_elements = nullptr; ierr = VecGhostGetLocalForm(vector, &locally_stored_elements); AssertThrow(ierr == 0, ExcPETScError(ierr)); PetscInt lsize; ierr = VecGetSize(locally_stored_elements, &lsize); AssertThrow(ierr == 0, ExcPETScError(ierr)); const PetscScalar *ptr; ierr = VecGetArrayRead(locally_stored_elements, &ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); auto input = indices_begin; auto output = values_begin; while (input != indices_end) { const auto index = static_cast(*input); AssertIntegerConversion(index, *input); if (index >= begin && index < end) { // local entry *output = *(ptr + index - begin); } else { // ghost entry const auto ghost_index = ghost_indices.index_within_set(*input); AssertIndexRange(ghost_index + end - begin, lsize); *output = *(ptr + ghost_index + end - begin); } ++input; ++output; } ierr = VecRestoreArrayRead(locally_stored_elements, &ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); ierr = VecGhostRestoreLocalForm(vector, &locally_stored_elements); AssertThrow(ierr == 0, ExcPETScError(ierr)); } // if the vector is local or the // caller, then simply access the // element we are interested in else { PetscInt begin, end; PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end); AssertThrow(ierr == 0, ExcPETScError(ierr)); const PetscScalar *ptr; ierr = VecGetArrayRead(vector, &ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); auto input = indices_begin; auto output = values_begin; while (input != indices_end) { const auto index = static_cast(*input); AssertIntegerConversion(index, *input); Assert(index >= begin && index < end, ExcMessage("You are accessing elements of a vector without " "ghost elements that are not actually owned by " "this vector. A typical case where this may " "happen is if you are passing a non-ghosted " "(completely distributed) vector to a function " "that expects a vector that stores ghost " "elements for all locally relevant or locally " "active vector entries.")); *output = *(ptr + index - begin); ++input; ++output; } ierr = VecRestoreArrayRead(vector, &ptr); AssertThrow(ierr == 0, ExcPETScError(ierr)); } } template inline void VectorBase::save(Archive &ar, const unsigned int) const { // forward to serialization function in the base class. ar &static_cast(*this); ar &size(); ar &local_range(); const PetscScalar *array = nullptr; int ierr = VecGetArrayRead(*this, &array); AssertThrow(ierr == 0, ExcPETScError(ierr)); boost::serialization::array_wrapper wrapper( array, locally_owned_size()); ar &wrapper; ierr = VecRestoreArrayRead(*this, &array); AssertThrow(ierr == 0, ExcPETScError(ierr)); } template inline void VectorBase::load(Archive &ar, const unsigned int) { ar &static_cast(*this); size_type size = 0; std::pair local_range; ar &size; Assert(size == this->size(), ExcMessage("The serialized value of size (" + std::to_string(size) + ") does not match the current size (" + std::to_string(this->size()) + ")")); ar &local_range; Assert(local_range == this->local_range(), ExcMessage("The serialized value of local_range (" + std::to_string(local_range.first) + ", " + std::to_string(local_range.second) + ") does not match the current local_range (" + std::to_string(this->local_range().first) + ", " + std::to_string(this->local_range().second) + ")")); PetscScalar *array = nullptr; int ierr = VecGetArray(petsc_vector(), &array); AssertThrow(ierr == 0, ExcPETScError(ierr)); boost::serialization::array_wrapper wrapper( array, locally_owned_size()); ar &wrapper; ierr = VecRestoreArray(petsc_vector(), &array); AssertThrow(ierr == 0, ExcPETScError(ierr)); } # endif // DOXYGEN } // namespace PETScWrappers DEAL_II_NAMESPACE_CLOSE #else // Make sure the scripts that create the C++20 module input files have // something to latch on if the preprocessor #ifdef above would // otherwise lead to an empty content of the file. DEAL_II_NAMESPACE_OPEN DEAL_II_NAMESPACE_CLOSE #endif // DEAL_II_WITH_PETSC #endif