15#ifndef dealii_petsc_vector_base_h
16#define dealii_petsc_vector_base_h
21#ifdef DEAL_II_WITH_PETSC
29# include <boost/serialization/split_member.hpp>
30# include <boost/serialization/utility.hpp>
41template <
typename number>
94 VectorReference(
const VectorBase &vector,
const size_type index);
100 VectorReference(
const VectorReference &vector) =
default;
113 const VectorReference &
114 operator=(
const VectorReference &r)
const;
122 operator=(
const VectorReference &r);
127 const VectorReference &
128 operator=(
const PetscScalar &s)
const;
133 const VectorReference &
134 operator+=(
const PetscScalar &s)
const;
139 const VectorReference &
140 operator-=(
const PetscScalar &s)
const;
145 const VectorReference &
146 operator*=(
const PetscScalar &s)
const;
151 const VectorReference &
152 operator/=(
const PetscScalar &s)
const;
173 operator PetscScalar()
const;
178 ExcAccessToNonlocalElement,
182 <<
"You tried to access element " << arg1
183 <<
" of a distributed vector, but only elements in range [" << arg2
184 <<
',' << arg3 <<
"] are stored locally and can be accessed."
186 <<
"A common source for this kind of problem is that you "
187 <<
"are passing a 'fully distributed' vector into a function "
188 <<
"that needs read access to vector elements that correspond "
189 <<
"to degrees of freedom on ghost cells (or at least to "
190 <<
"'locally active' degrees of freedom that are not also "
191 <<
"'locally owned'). You need to pass a vector that has these "
192 <<
"elements as ghost entries.");
199 <<
"You tried to do a "
200 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
201 <<
" operation but the vector is currently in "
202 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
203 <<
" mode. You first have to call 'compress()'.");
209 const VectorBase &vector;
218 friend class ::PETScWrappers::VectorBase;
359 size()
const override;
380 std::pair<size_type, size_type>
462 set(
const std::vector<size_type> &indices,
463 const std::vector<PetscScalar> &values);
482 std::vector<PetscScalar> &values)
const;
519 template <
typename ForwardIterator,
typename OutputIterator>
530 add(
const std::vector<size_type> &indices,
531 const std::vector<PetscScalar> &values);
538 add(
const std::vector<size_type> &indices,
539 const ::Vector<PetscScalar> &values);
549 const PetscScalar *values);
662 add(
const PetscScalar s);
674 add(
const PetscScalar a,
689 sadd(
const PetscScalar s,
const PetscScalar a,
const VectorBase &V);
723 print(std::ostream &out,
724 const unsigned int precision = 3,
725 const bool scientific =
true,
726 const bool across =
true)
const;
735 template <
class Archive>
744 template <
class Archive>
754 template <
class Archive>
785 operator const Vec &()
const;
846 const PetscScalar *values,
876 inline VectorReference::VectorReference(
const VectorBase &vector,
877 const size_type index)
883 inline const VectorReference &
884 VectorReference::operator=(
const VectorReference &r)
const
890 *
this =
static_cast<PetscScalar
>(r);
897 inline VectorReference &
898 VectorReference::operator=(
const VectorReference &r)
904 *
this =
static_cast<PetscScalar
>(r);
911 inline const VectorReference &
912 VectorReference::operator=(
const PetscScalar &value)
const
920 const PetscInt petsc_i =
index;
922 const PetscErrorCode ierr =
923 VecSetValues(vector, 1, &petsc_i, &value, INSERT_VALUES);
933 inline const VectorReference &
934 VectorReference::operator+=(
const PetscScalar &value)
const
951 if (value == PetscScalar())
955 const PetscInt petsc_i =
index;
956 const PetscErrorCode ierr =
957 VecSetValues(vector, 1, &petsc_i, &value, ADD_VALUES);
966 inline const VectorReference &
967 VectorReference::operator-=(
const PetscScalar &value)
const
984 if (value == PetscScalar())
989 const PetscInt petsc_i =
index;
990 const PetscScalar subtractand = -
value;
991 const PetscErrorCode ierr =
992 VecSetValues(vector, 1, &petsc_i, &subtractand, ADD_VALUES);
1000 inline const VectorReference &
1001 VectorReference::operator*=(
const PetscScalar &value)
const
1021 const PetscInt petsc_i =
index;
1022 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) *
value;
1024 const PetscErrorCode ierr =
1025 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1033 inline const VectorReference &
1034 VectorReference::operator/=(
const PetscScalar &value)
const
1054 const PetscInt petsc_i =
index;
1055 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) /
value;
1057 const PetscErrorCode ierr =
1058 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1067 VectorReference::real()
const
1069# ifndef PETSC_USE_COMPLEX
1070 return static_cast<PetscScalar
>(*this);
1072 return PetscRealPart(
static_cast<PetscScalar
>(*
this));
1079 VectorReference::imag()
const
1081# ifndef PETSC_USE_COMPLEX
1082 return PetscReal(0);
1084 return PetscImaginaryPart(
static_cast<PetscScalar
>(*
this));
1091 VectorBase::in_local_range(
const size_type index)
const
1094 const PetscErrorCode ierr =
1095 VecGetOwnershipRange(
static_cast<const Vec &
>(vector), &begin, &end);
1098 return ((index >=
static_cast<size_type>(begin)) &&
1104 VectorBase::locally_owned_elements()
const
1109 const std::pair<size_type, size_type> x =
local_range();
1110 is.add_range(x.first, x.second);
1117 VectorBase::has_ghost_elements()
const
1124 VectorBase::ghost_elements()
const
1126 return ghost_indices;
1131 VectorBase::update_ghost_values()
const
1135 PetscErrorCode ierr;
1137 ierr = VecGhostUpdateBegin(vector, INSERT_VALUES, SCATTER_FORWARD);
1139 ierr = VecGhostUpdateEnd(vector, INSERT_VALUES, SCATTER_FORWARD);
1146 inline internal::VectorReference
1147 VectorBase::operator()(
const size_type index)
1149 return internal::VectorReference(*
this, index);
1155 VectorBase::operator()(
const size_type index)
const
1157 return static_cast<PetscScalar
>(internal::VectorReference(*
this, index));
1162 inline internal::VectorReference
1163 VectorBase::operator[](
const size_type index)
1165 return operator()(index);
1171 VectorBase::operator[](
const size_type index)
const
1173 return operator()(index);
1177 VectorBase::get_mpi_communicator()
const
1179 return PetscObjectComm(
reinterpret_cast<PetscObject
>(vector));
1183 VectorBase::extract_subvector_to(
const std::vector<size_type> &indices,
1184 std::vector<PetscScalar> &values)
const
1188 extract_subvector_to(indices.begin(), indices.end(),
values.begin());
1192 VectorBase::extract_subvector_to(
1197 extract_subvector_to(indices.
begin(), indices.
end(), elements.
begin());
1201 template <
typename ForwardIterator,
typename OutputIterator>
1203 VectorBase::extract_subvector_to(
const ForwardIterator indices_begin,
1204 const ForwardIterator indices_end,
1205 OutputIterator values_begin)
const
1207 if (indices_begin == indices_end)
1232 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1235 Vec locally_stored_elements =
nullptr;
1236 ierr = VecGhostGetLocalForm(vector, &locally_stored_elements);
1240 ierr = VecGetSize(locally_stored_elements, &lsize);
1243 const PetscScalar *ptr;
1244 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
1247 auto input = indices_begin;
1248 auto output = values_begin;
1249 while (input != indices_end)
1251 const auto index =
static_cast<PetscInt
>(*input);
1253 if (index >= begin && index < end)
1261 const auto ghost_index = ghost_indices.index_within_set(*input);
1264 *output = *(ptr + ghost_index +
end -
begin);
1271 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
1274 ierr = VecGhostRestoreLocalForm(vector, &locally_stored_elements);
1283 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1286 const PetscScalar *ptr;
1287 ierr = VecGetArrayRead(vector, &ptr);
1290 auto input = indices_begin;
1291 auto output = values_begin;
1292 while (input != indices_end)
1294 const auto index =
static_cast<PetscInt
>(*input);
1297 Assert(index >= begin && index < end,
1298 ExcMessage(
"You are accessing elements of a vector without "
1299 "ghost elements that are not actually owned by "
1300 "this vector. A typical case where this may "
1301 "happen is if you are passing a non-ghosted "
1302 "(completely distributed) vector to a function "
1303 "that expects a vector that stores ghost "
1304 "elements for all locally relevant or locally "
1305 "active vector entries."));
1313 ierr = VecRestoreArrayRead(vector, &ptr);
1318 template <
class Archive>
1320 VectorBase::save(Archive &ar,
const unsigned int)
const
1327 const PetscScalar *array =
nullptr;
1328 int ierr = VecGetArrayRead(*
this, &array);
1331 boost::serialization::array_wrapper<const PetscScalar> wrapper(
1335 ierr = VecRestoreArrayRead(*
this, &array);
1341 template <
class Archive>
1343 VectorBase::load(Archive &ar,
const unsigned int)
1352 ExcMessage(
"The serialized value of size (" + std::to_string(
size) +
1353 ") does not match the current size (" +
1354 std::to_string(this->
size()) +
")"));
1358 ExcMessage(
"The serialized value of local_range (" +
1361 ") does not match the current local_range (" +
1362 std::to_string(this->local_range().first) +
", " +
1363 std::to_string(this->local_range().second) +
")"));
1366 PetscScalar *array =
nullptr;
1367 int ierr = VecGetArray(petsc_vector(), &array);
1370 boost::serialization::array_wrapper<PetscScalar> wrapper(
1374 ierr = VecRestoreArray(petsc_vector(), &array);
real_type lp_norm(const real_type p) const
real_type l1_norm() const
VectorBase & operator+=(const VectorBase &V)
PetscScalar mean_value() const
void determine_ghost_indices()
VectorOperation::values last_action
PetscScalar operator*(const VectorBase &vec) const
bool operator==(const VectorBase &v) const
VectorBase & operator*=(const PetscScalar factor)
VectorBase & operator-=(const VectorBase &V)
void save(Archive &ar, const unsigned int version) const
bool operator!=(const VectorBase &v) const
IndexSet locally_owned_elements() const
bool in_local_range(const size_type index) const
void load(Archive &ar, const unsigned int version)
std::size_t memory_consumption() const
internal::VectorReference reference
VectorBase & operator/=(const PetscScalar factor)
friend class internal::VectorReference
PetscScalar operator[](const size_type index) const
MPI_Comm get_mpi_communicator() const
void scale(const VectorBase &scaling_factors)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< PetscScalar > &values) const
void update_ghost_values() const
std::pair< size_type, size_type > local_range() const
void sadd(const PetscScalar s, const VectorBase &V)
real_type norm_sqr() const
void compress(const VectorOperation::values operation)
void set(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
PetscScalar add_and_dot(const PetscScalar a, const VectorBase &V, const VectorBase &W)
virtual ~VectorBase() override
const IndexSet & ghost_elements() const
real_type linfty_norm() const
void swap(VectorBase &v) noexcept
reference operator()(const size_type index)
void swap(VectorBase &u, VectorBase &v) noexcept
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, const ArrayView< PetscScalar > &elements) const override
const internal::VectorReference const_reference
void serialize(Archive &archive, const unsigned int version)
void add(const std::vector< size_type > &indices, const std::vector< PetscScalar > &values)
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
bool has_ghost_elements() const
reference operator[](const size_type index)
size_type locally_owned_size() const
void equ(const PetscScalar a, const VectorBase &V)
void do_set_add_operation(const size_type n_elements, const size_type *indices, const PetscScalar *values, const bool add_values)
VectorBase & operator=(const VectorBase &)
real_type l2_norm() const
size_type size() const override
PetscScalar operator()(const size_type index) const
void extract_subvector_to(const ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define DeclException2(Exception2, type1, type2, outsequence)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
#define AssertIntegerConversion(index1, index2)
std::pair< types::global_dof_index, types::global_dof_index > local_range
types::global_dof_index locally_owned_size
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int global_dof_index