15#ifdef DEAL_II_WITH_PETSC
23# include <boost/container/small_vector.hpp>
31#ifdef DEAL_II_WITH_PETSC
39 operator PetscScalar()
const
53 VecGetOwnershipRange(vector.vector, &
begin, &
end);
56 Vec locally_stored_elements =
nullptr;
57 ierr = VecGhostGetLocalForm(vector.vector, &locally_stored_elements);
61 ierr = VecGetSize(locally_stored_elements, &lsize);
64 const PetscScalar *ptr;
65 ierr = VecGetArrayRead(locally_stored_elements, &ptr);
70 if (index >=
static_cast<size_type
>(
begin) &&
71 index <
static_cast<size_type
>(
end))
74 value = *(ptr + index -
begin);
79 Assert(vector.ghost_indices.is_element(index),
81 "You are trying to access an element of a vector "
82 "that is neither a locally owned element nor a "
83 "ghost element of the vector."));
84 const size_type ghostidx =
85 vector.ghost_indices.index_within_set(index);
88 value = *(ptr + ghostidx +
end -
begin);
91 ierr = VecRestoreArrayRead(locally_stored_elements, &ptr);
95 VecGhostRestoreLocalForm(vector.vector, &locally_stored_elements);
107 PetscErrorCode ierr = VecGetOwnershipRange(vector.vector, &
begin, &
end);
111 (index <
static_cast<size_type
>(
end)),
112 ExcAccessToNonlocalElement(index,
begin,
end - 1));
114 const PetscScalar *ptr;
116 ierr = VecGetArrayRead(vector.vector, &ptr);
118 value = *(ptr + index -
begin);
119 ierr = VecRestoreArrayRead(vector.vector, &ptr);
131 , ghost_vector(nullptr)
132 , ghost_vector_array(nullptr)
139 , ghost_indices(v.ghost_indices)
141 , ghost_vector(nullptr)
142 , ghost_vector_array(nullptr)
159 , ghost_vector(nullptr)
160 , ghost_vector_array(nullptr)
162 const PetscErrorCode ierr =
163 PetscObjectReference(
reinterpret_cast<PetscObject
>(
vector));
177 const PetscErrorCode ierr = VecDestroy(&
vector);
192 PetscErrorCode ierr =
193 PetscObjectReference(
reinterpret_cast<PetscObject
>(v));
195 ierr = VecDestroy(&
vector);
207 template <
typename Iterator,
typename OutType>
208 class ConvertingIterator
214 typename std::iterator_traits<Iterator>::difference_type;
218 using iterator_category = std::forward_iterator_tag;
220 ConvertingIterator(
const Iterator &iterator)
227 return static_cast<OutType
>(std::real(*m_iterator));
240 ConvertingIterator old = *
this;
246 operator==(
const ConvertingIterator &other)
const
248 return this->m_iterator == other.m_iterator;
252 operator!=(
const ConvertingIterator &other)
const
254 return this->m_iterator != other.m_iterator;
274 ierr = VecGhostGetLocalForm(
vector, &ghosted_vec);
276 if (ghosted_vec && ghosted_vec !=
vector)
280 PetscInt ghost_start_index, end_index, n_elements_stored_locally;
282 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
285 ierr = VecGetOwnershipRange(
vector, &ghost_start_index, &end_index);
287 ierr = VecDuplicate(
vector, &tvector);
289 ierr = VecGetArray(tvector, &array);
297 for (PetscInt i = 0; i < end_index - ghost_start_index; i++)
299 Assert(
static_cast<PetscInt
>(std::real(
static_cast<PetscScalar
>(
300 ghost_start_index + i))) == (ghost_start_index + i),
302 array[i] = ghost_start_index + i;
305 ierr = VecRestoreArray(tvector, &array);
307 ierr = VecGhostUpdateBegin(tvector, INSERT_VALUES, SCATTER_FORWARD);
309 ierr = VecGhostUpdateEnd(tvector, INSERT_VALUES, SCATTER_FORWARD);
311 ierr = VecGhostGetLocalForm(tvector, &ghosted_vec);
313 ierr = VecGetLocalSize(ghosted_vec, &n_elements_stored_locally);
315 ierr = VecGetArrayRead(ghosted_vec, (
const PetscScalar **)&array);
327 ConvertingIterator<PetscScalar *, types::global_dof_index> begin_ghosts(
328 &array[end_index - ghost_start_index]);
329 ConvertingIterator<PetscScalar *, types::global_dof_index> end_ghosts(
330 &array[n_elements_stored_locally]);
331 if (std::is_sorted(&array[end_index - ghost_start_index],
332 &array[n_elements_stored_locally],
333 [](PetscScalar left, PetscScalar right) {
334 return static_cast<PetscInt
>(std::real(left)) <
335 static_cast<PetscInt
>(std::real(right));
342 std::vector<PetscInt> sorted_indices(begin_ghosts, end_ghosts);
343 std::sort(sorted_indices.begin(), sorted_indices.end());
345 sorted_indices.end());
349 ierr = VecRestoreArrayRead(ghosted_vec, (
const PetscScalar **)&array);
351 ierr = VecGhostRestoreLocalForm(tvector, &ghosted_vec);
353 ierr = VecDestroy(&tvector);
358 ierr = VecGhostRestoreLocalForm(
vector, &ghosted_vec);
372 "Ghost vector is already acquired for the vector."));
388 PetscErrorCode ierr =
406 const PetscErrorCode ierr = VecDestroy(&
vector);
421 PetscErrorCode ierr = VecCopy(v,
vector);
432 if (s != PetscScalar(0))
440 PetscErrorCode ierr = VecSet(
vector, s);
450 ierr = VecGhostGetLocalForm(
vector, &ghost);
453 ierr = VecSet(ghost, s);
456 ierr = VecGhostRestoreLocalForm(
vector, &ghost);
471 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
474 return (flag == PETSC_TRUE);
485 const PetscErrorCode ierr = VecEqual(
vector, v.
vector, &flag);
488 return (flag == PETSC_FALSE);
497 const PetscErrorCode ierr = VecGetSize(
vector, &sz);
509 const PetscErrorCode ierr = VecGetLocalSize(
vector, &sz);
517 std::pair<VectorBase::size_type, VectorBase::size_type>
521 const PetscErrorCode ierr =
522 VecGetOwnershipRange(
static_cast<const Vec &
>(
vector), &
begin, &
end);
532 const std::vector<PetscScalar> &values)
534 Assert(indices.size() == values.size(),
535 ExcMessage(
"Function called with arguments of different sizes"));
543 const std::vector<PetscScalar> &values)
545 Assert(indices.size() == values.size(),
546 ExcMessage(
"Function called with arguments of different sizes"));
554 const ::Vector<PetscScalar> &values)
556 Assert(indices.size() == values.size(),
557 ExcMessage(
"Function called with arguments of different sizes"));
566 const PetscScalar *values)
587 const PetscErrorCode ierr = VecDot(vec.
vector,
vector, &result);
610 ExcMessage(
"Calling compress() is only useful if a vector "
611 "has been written into, but this is a vector with ghost "
612 "elements and consequently is read-only. It does "
613 "not make sense to call compress() for such "
623 int all_int_last_action;
625 const int ierr = MPI_Allreduce(&my_int_last_action,
626 &all_int_last_action,
636 "Error: not all processors agree on the last "
637 "VectorOperation before this compress() call."));
644 "Missing compress() or calling with wrong VectorOperation argument."));
660 PetscErrorCode ierr = VecAssemblyBegin(
vector);
662 ierr = VecAssemblyEnd(
vector);
690 const PetscErrorCode ierr = VecSum(
vector, &sum);
692 return sum /
static_cast<PetscReal
>(
size());
697 const PetscScalar *start_ptr;
698 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
701 PetscScalar mean = 0;
703 PetscScalar sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
708 const PetscScalar *ptr = start_ptr;
723 static_cast<PetscReal
>(
size());
728 ierr = VecRestoreArrayRead(
vector, &start_ptr);
740 const PetscErrorCode ierr = VecNorm(
vector, NORM_1, &d);
753 const PetscErrorCode ierr = VecNorm(
vector, NORM_2, &d);
766 const PetscScalar *start_ptr;
767 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
772 real_type sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
777 const PetscScalar *ptr = start_ptr;
797 ierr = VecRestoreArrayRead(
vector, &start_ptr);
810 const PetscErrorCode ierr = VecNorm(
vector, NORM_INFINITY, &d);
821 const PetscScalar *start_ptr;
822 PetscErrorCode ierr = VecGetArrayRead(
vector, &start_ptr);
825 const bool local_all_zero =
826 std::all_of(start_ptr,
828 numbers::value_is_zero<PetscScalar>);
830 ierr = VecRestoreArrayRead(
vector, &start_ptr);
844 const PetscErrorCode ierr = VecScale(
vector, a);
858 const PetscScalar factor = 1. / a;
861 const PetscErrorCode ierr = VecScale(
vector, factor);
873 const PetscErrorCode ierr = VecAXPY(
vector, 1, v);
885 const PetscErrorCode ierr = VecAXPY(
vector, -1, v);
899 const PetscErrorCode ierr = VecShift(
vector, s);
911 const PetscErrorCode ierr = VecAXPY(
vector, a, v);
927 const PetscScalar weights[2] = {a, b};
928 Vec addends[2] = {v.
vector, w.vector};
930 const PetscErrorCode ierr = VecMAXPY(
vector, 2, weights, addends);
942 const PetscErrorCode ierr = VecAYPX(
vector, s, v);
970 const PetscErrorCode ierr = VecPointwiseMult(
vector, factors,
vector);
984 const PetscErrorCode ierr = VecAXPBY(
vector, a, 0.0, v.
vector);
997 PetscErrorCode ierr =
998 PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(
comm), format);
1002 ierr = VecView(
vector, PETSC_VIEWER_STDOUT_(
comm));
1004 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(
comm));
1012 const unsigned int precision,
1013 const bool scientific,
1014 const bool across)
const
1020 const PetscScalar *val;
1021 PetscErrorCode ierr = VecGetArrayRead(
vector, &val);
1026 const std::ios::fmtflags old_flags = out.flags();
1027 const unsigned int old_precision = out.precision(precision);
1029 out.precision(precision);
1031 out.setf(std::ios::scientific, std::ios::floatfield);
1033 out.setf(std::ios::fixed, std::ios::floatfield);
1037 out << val[i] <<
' ';
1040 out << val[i] << std::endl;
1044 out.flags(old_flags);
1045 out.precision(old_precision);
1049 ierr = VecRestoreArrayRead(
vector, &val);
1060 std::swap(this->vector, v.vector);
1061 std::swap(this->ghosted, v.ghosted);
1062 std::swap(this->last_action, v.last_action);
1065 this->ghost_indices = v.ghost_indices;
1066 v.ghost_indices = std::move(t);
1067 std::swap(this->ghost_vector, v.ghost_vector);
1068 std::swap(this->ghost_vector_array, v.ghost_vector_array);
1073 operator
const Vec &()
const
1089 std::size_t mem =
sizeof(Vec) +
sizeof(
last_action) +
1111 const PetscScalar *values,
1112 const bool add_values)
1117 internal::VectorReference::ExcWrongMode(action,
last_action));
1120 boost::container::small_vector<PetscInt, 200> petsc_indices(n_elements);
1121 for (
size_type i = 0; i < n_elements; ++i)
1123 const auto petsc_index =
static_cast<PetscInt
>(indices[i]);
1125 petsc_indices[i] = petsc_index;
1128 const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES);
1129 const PetscErrorCode ierr = VecSetValues(
1130 vector, petsc_indices.size(), petsc_indices.data(), values, mode);
* * reference operator*() const
std::ptrdiff_t difference_type
* * iterator & operator++()
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
size_type n_elements() const
void set_size(const size_type size)
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
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)
bool operator!=(const VectorBase &v) const
std::size_t memory_consumption() const
VectorBase & operator/=(const PetscScalar factor)
MPI_Comm get_mpi_communicator() const
void scale(const VectorBase &scaling_factors)
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 PetscScalar * ghost_vector_array
real_type linfty_norm() const
void swap(VectorBase &v) noexcept
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
void acquire_ghost_form()
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 release_ghost_form()
size_type size() const override
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
#define AssertIntegerConversion(index1, index2)
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertThrowMPI(error_code)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
T sum(const T &t, const MPI_Comm mpi_communicator)
bool logical_and(const bool t, const MPI_Comm mpi_communicator)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)