16#ifndef dealii_petsc_vector_base_h
17#define dealii_petsc_vector_base_h
22#ifdef DEAL_II_WITH_PETSC
40template <
typename number>
93 VectorReference(
const VectorBase &vector,
const size_type index);
99 VectorReference(
const VectorReference &vector) =
default;
112 const VectorReference &
113 operator=(
const VectorReference &r)
const;
121 operator=(
const VectorReference &r);
126 const VectorReference &
127 operator=(
const PetscScalar &s)
const;
132 const VectorReference &
133 operator+=(
const PetscScalar &s)
const;
138 const VectorReference &
139 operator-=(
const PetscScalar &s)
const;
144 const VectorReference &
145 operator*=(
const PetscScalar &s)
const;
150 const VectorReference &
151 operator/=(
const PetscScalar &s)
const;
172 operator PetscScalar()
const;
177 ExcAccessToNonlocalElement,
181 <<
"You tried to access element " << arg1
182 <<
" of a distributed vector, but only elements in range [" << arg2
183 <<
',' << arg3 <<
"] are stored locally and can be accessed."
185 <<
"A common source for this kind of problem is that you "
186 <<
"are passing a 'fully distributed' vector into a function "
187 <<
"that needs read access to vector elements that correspond "
188 <<
"to degrees of freedom on ghost cells (or at least to "
189 <<
"'locally active' degrees of freedom that are not also "
190 <<
"'locally owned'). You need to pass a vector that has these "
191 <<
"elements as ghost entries.");
198 <<
"You tried to do a "
199 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
200 <<
" operation but the vector is currently in "
201 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
202 <<
" mode. You first have to call 'compress()'.");
208 const VectorBase &vector;
217 friend class ::PETScWrappers::VectorBase;
393 std::pair<size_type, size_type>
475 set(
const std::vector<size_type> & indices,
476 const std::vector<PetscScalar> &values);
495 std::vector<PetscScalar> & values)
const;
524 template <
typename ForwardIterator,
typename OutputIterator>
527 const ForwardIterator indices_end,
528 OutputIterator values_begin)
const;
535 add(
const std::vector<size_type> & indices,
536 const std::vector<PetscScalar> &values);
543 add(
const std::vector<size_type> & indices,
544 const ::Vector<PetscScalar> &values);
554 const PetscScalar *values);
701 add(
const PetscScalar s);
713 add(
const PetscScalar a,
728 sadd(
const PetscScalar s,
const PetscScalar a,
const VectorBase &V);
752 write_ascii(
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT);
762 print(std::ostream & out,
763 const unsigned int precision = 3,
764 const bool scientific =
true,
765 const bool across =
true)
const;
789 operator const Vec &()
const;
850 const PetscScalar *values,
851 const bool add_values);
880 inline VectorReference::VectorReference(
const VectorBase &vector,
881 const size_type index)
887 inline const VectorReference &
888 VectorReference::operator=(
const VectorReference &r)
const
894 *
this =
static_cast<PetscScalar
>(r);
901 inline VectorReference &
902 VectorReference::operator=(
const VectorReference &r)
908 *
this =
static_cast<PetscScalar
>(r);
915 inline const VectorReference &
916 VectorReference::operator=(
const PetscScalar &value)
const
924 const PetscInt petsc_i =
index;
926 const PetscErrorCode ierr =
927 VecSetValues(vector, 1, &petsc_i, &value, INSERT_VALUES);
937 inline const VectorReference &
938 VectorReference::operator+=(
const PetscScalar &value)
const
955 if (value == PetscScalar())
959 const PetscInt petsc_i =
index;
960 const PetscErrorCode ierr =
961 VecSetValues(vector, 1, &petsc_i, &value, ADD_VALUES);
970 inline const VectorReference &
971 VectorReference::operator-=(
const PetscScalar &value)
const
988 if (value == PetscScalar())
993 const PetscInt petsc_i =
index;
994 const PetscScalar subtractand = -
value;
995 const PetscErrorCode ierr =
996 VecSetValues(vector, 1, &petsc_i, &subtractand, ADD_VALUES);
1004 inline const VectorReference &
1005 VectorReference::operator*=(
const PetscScalar &value)
const
1025 const PetscInt petsc_i =
index;
1026 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) *
value;
1028 const PetscErrorCode ierr =
1029 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1037 inline const VectorReference &
1038 VectorReference::operator/=(
const PetscScalar &value)
const
1058 const PetscInt petsc_i =
index;
1059 const PetscScalar new_value =
static_cast<PetscScalar
>(*this) /
value;
1061 const PetscErrorCode ierr =
1062 VecSetValues(vector, 1, &petsc_i, &new_value, INSERT_VALUES);
1071 VectorReference::real()
const
1073# ifndef PETSC_USE_COMPLEX
1074 return static_cast<PetscScalar
>(*this);
1076 return PetscRealPart(
static_cast<PetscScalar
>(*
this));
1083 VectorReference::imag()
const
1085# ifndef PETSC_USE_COMPLEX
1086 return PetscReal(0);
1088 return PetscImaginaryPart(
static_cast<PetscScalar
>(*
this));
1095 VectorBase::in_local_range(
const size_type index)
const
1098 const PetscErrorCode ierr =
1099 VecGetOwnershipRange(
static_cast<const Vec &
>(vector), &begin, &end);
1102 return ((index >=
static_cast<size_type>(begin)) &&
1108 VectorBase::locally_owned_elements()
const
1113 const std::pair<size_type, size_type> x = local_range();
1114 is.add_range(x.first, x.second);
1121 VectorBase::has_ghost_elements()
const
1128 VectorBase::ghost_elements()
const
1130 return ghost_indices;
1135 VectorBase::update_ghost_values()
const
1139 PetscErrorCode ierr;
1141 ierr = VecGhostUpdateBegin(vector, INSERT_VALUES, SCATTER_FORWARD);
1143 ierr = VecGhostUpdateEnd(vector, INSERT_VALUES, SCATTER_FORWARD);
1150 inline internal::VectorReference
1151 VectorBase::operator()(
const size_type index)
1153 return internal::VectorReference(*
this, index);
1159 VectorBase::operator()(
const size_type index)
const
1161 return static_cast<PetscScalar
>(internal::VectorReference(*
this, index));
1166 inline internal::VectorReference
1167 VectorBase::operator[](
const size_type index)
1169 return operator()(index);
1175 VectorBase::operator[](
const size_type index)
const
1177 return operator()(index);
1181 VectorBase::get_mpi_communicator()
const
1183 return PetscObjectComm(
reinterpret_cast<PetscObject
>(vector));
1187 VectorBase::extract_subvector_to(
const std::vector<size_type> &indices,
1188 std::vector<PetscScalar> & values)
const
1192 extract_subvector_to(indices.begin(), indices.end(),
values.begin());
1195 template <
typename ForwardIterator,
typename OutputIterator>
1197 VectorBase::extract_subvector_to(
const ForwardIterator indices_begin,
1198 const ForwardIterator indices_end,
1199 OutputIterator values_begin)
const
1201 const PetscInt n_idx =
static_cast<PetscInt
>(indices_end - indices_begin);
1227 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1230 Vec locally_stored_elements =
nullptr;
1231 ierr = VecGhostGetLocalForm(vector, &locally_stored_elements);
1235 ierr = VecGetSize(locally_stored_elements, &lsize);
1238 const PetscScalar *ptr;
1239 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
1242 for (PetscInt i = 0; i < n_idx; ++i)
1244 const unsigned int index = *(indices_begin + i);
1245 if (index >=
static_cast<unsigned int>(begin) &&
1246 index < static_cast<unsigned int>(end))
1249 *(values_begin + i) = *(ptr + index - begin);
1254 const unsigned int ghostidx =
1255 ghost_indices.index_within_set(index);
1258 *(values_begin + i) = *(ptr + ghostidx + end - begin);
1262 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
1265 ierr = VecGhostRestoreLocalForm(vector, &locally_stored_elements);
1274 PetscErrorCode ierr = VecGetOwnershipRange(vector, &begin, &end);
1277 const PetscScalar *ptr;
1278 ierr = VecGetArrayRead(vector, &ptr);
1281 for (PetscInt i = 0; i < n_idx; ++i)
1283 const unsigned int index = *(indices_begin + i);
1285 Assert(index >=
static_cast<unsigned int>(begin) &&
1286 index <
static_cast<unsigned int>(end),
1287 ExcMessage(
"You are accessing elements of a vector without "
1288 "ghost elements that are not actually owned by "
1289 "this vector. A typical case where this may "
1290 "happen is if you are passing a non-ghosted "
1291 "(completely distributed) vector to a function "
1292 "that expects a vector that stores ghost "
1293 "elements for all locally relevant or locally "
1294 "active vector entries."));
1296 *(values_begin + i) = *(ptr + index - begin);
1299 ierr = VecRestoreArrayRead(vector, &ptr);
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()
size_type local_size() const
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)
bool operator!=(const VectorBase &v) const
IndexSet locally_owned_elements() const
bool in_local_range(const size_type index) const
std::size_t memory_consumption() const
internal::VectorReference reference
VectorBase & operator/=(const PetscScalar factor)
bool is_non_negative() const
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
reference operator()(const size_type index)
const internal::VectorReference const_reference
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
void swap(VectorBase &u, VectorBase &v)
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_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define DeclException2(Exception2, type1, type2, outsequence)
#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)
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
VectorType::value_type * begin(VectorType &V)
unsigned int global_dof_index