15#ifndef dealii_trilinos_tpetra_vector_h
16#define dealii_trilinos_tpetra_vector_h
25#ifdef DEAL_II_TRILINOS_WITH_TPETRA
37# include <Teuchos_Comm.hpp>
38# include <Teuchos_OrdinalTraits.hpp>
39# include <Tpetra_Core.hpp>
40# include <Tpetra_Vector.hpp>
41# include <Tpetra_Version.hpp>
53template <
typename Number>
57# ifdef HAVE_TPETRA_INST_FLOAT
63# ifdef HAVE_TPETRA_INST_DOUBLE
69# ifdef DEAL_II_WITH_COMPLEX_VALUES
70# ifdef HAVE_TPETRA_INST_COMPLEX_FLOAT
76# ifdef HAVE_TPETRA_INST_COMPLEX_DOUBLE
87 template <
typename Number>
88 class ReadWriteVector;
105 namespace TpetraWrappers
144 template <
typename Number,
146 class VectorReference
154 const size_type index);
160 VectorReference(
const VectorReference &) =
default;
173 const VectorReference &
174 operator=(
const VectorReference &r)
const;
180 operator=(
const VectorReference &r);
185 const VectorReference &
186 operator=(
const Number &s)
const;
191 const VectorReference &
192 operator+=(
const Number &s)
const;
197 const VectorReference &
198 operator-=(
const Number &s)
const;
203 const VectorReference &
204 operator*=(
const Number &s)
const;
209 const VectorReference &
210 operator/=(
const Number &s)
const;
216 operator Number()
const;
223 <<
"An error with error number " << arg1
224 <<
" occurred while calling a Trilinos function");
232 ExcAccessToNonLocalElement,
237 <<
"You are trying to access element " << arg1
238 <<
" of a distributed vector, but this element is not stored "
239 <<
"on the current processor. Note: There are " << arg2
240 <<
" elements stored "
241 <<
"on the current processor from within the range [" << arg3 <<
','
242 << arg4 <<
"] but Trilinos vectors need not store contiguous "
243 <<
"ranges on each processor, and not every element in "
244 <<
"this range may in fact be stored locally."
246 <<
"A common source for this kind of problem is that you "
247 <<
"are passing a 'fully distributed' vector into a function "
248 <<
"that needs read access to vector elements that correspond "
249 <<
"to degrees of freedom on ghost cells (or at least to "
250 <<
"'locally active' degrees of freedom that are not also "
251 <<
"'locally owned'). You need to pass a vector that has these "
252 <<
"elements as ghost entries.");
263 const size_type index;
287 template <
typename Number,
typename MemorySpace = ::MemorySpace::Host>
297 using reference = internal::VectorReference<Number, MemorySpace>;
299 const internal::VectorReference<Number, MemorySpace>;
351 const bool vector_writable =
false);
368 const MPI_Comm communicator = MPI_COMM_WORLD,
369 const bool omit_zeroing_entries =
false);
387 const IndexSet &locally_relevant_or_ghost_entries,
388 const MPI_Comm communicator = MPI_COMM_WORLD,
389 const bool vector_writable =
false);
397 const bool omit_zeroing_entries =
false);
462 template <
typename OtherNumber>
485 const Teuchos::RCP<const Utilities::MPI::CommunicationPatternBase>
486 &communication_pattern);
496 const std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
497 &communication_pattern);
516 std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
517 communication_pattern = {})
633 add(
const std::vector<size_type> &indices,
634 const std::vector<Number> &values);
645 const Number *values);
665 const Number *values);
834 std::pair<size_type, size_type>
918 Teuchos::RCP<const TpetraTypes::VectorType<Number, MemorySpace>>
925 Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>>
933 const unsigned int precision = 3,
934 const bool scientific =
true,
935 const bool across =
true)
const;
978 <<
"You are trying to access element " << arg1
979 <<
" of a distributed vector, but this element is not stored "
980 <<
"on the current processor. Note: There are " << arg2
981 <<
" elements stored "
982 <<
"on the current processor from within the range [" << arg3 <<
','
983 << arg4 <<
"] but Trilinos vectors need not store contiguous "
984 <<
"ranges on each processor, and not every element in "
985 <<
"this range may in fact be stored locally."
987 <<
"A common source for this kind of problem is that you "
988 <<
"are passing a 'fully distributed' vector into a function "
989 <<
"that needs read access to vector elements that correspond "
990 <<
"to degrees of freedom on ghost cells (or at least to "
991 <<
"'locally active' degrees of freedom that are not also "
992 <<
"'locally owned'). You need to pass a vector that has these "
993 <<
"elements as ghost entries.");
1001 "To compress a vector, a locally_relevant_dofs "
1002 "index set, and a locally_owned_dofs index set "
1003 "must be provided. These index sets must be "
1004 "provided either when the vector is initialized "
1005 "or when compress is called. See the documentation "
1006 "of compress() for more information.");
1015 <<
"An error with error number " << arg1
1016 <<
" occurred while calling a Trilinos function");
1047 Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>>
vector;
1054 Teuchos::RCP<TpetraTypes::VectorType<Number, MemorySpace>>
1066 Teuchos::RCP<const TpetraWrappers::CommunicationPattern<MemorySpace>>
1070 friend class internal::VectorReference<Number,
MemorySpace>;
1076 template <
typename Number,
typename MemorySpace>
1085 template <
typename Number,
typename MemorySpace>
1094 template <
typename Number,
typename MemorySpace>
1101 template <
typename Number,
typename MemorySpace>
1105 vector.swap(v.vector);
1109 template <
typename Number,
typename MemorySpace>
1112 const std::vector<Number> &values)
1118 add(indices.size(), indices.data(), values.data());
1123 template <
typename Number,
typename MemorySpace>
1127 const Number *values)
1133# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1134 auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>(
1135 Tpetra::Access::ReadWrite);
1137 vector->template sync<Kokkos::HostSpace>();
1139 auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>();
1148 auto vector_1d_local = Kokkos::subview(vector_2d_local, Kokkos::ALL(), 0);
1149 using ViewType1d =
decltype(vector_1d_local);
1150 std::optional<ViewType1d> vector_1d_nonlocal;
1152# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1155 vector->template modify<Kokkos::HostSpace>();
1158 for (
size_type i = 0; i < n_elements; ++i)
1166 vector->getMap()->getLocalElement(row);
1167 local_row != Teuchos::OrdinalTraits<int>::invalid())
1169 vector_1d_local(local_row) += values[i];
1174 if (nonlocal_vector.get() !=
nullptr)
1184 nonlocal_vector->getMap()->getLocalElement(row);
1186# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
1187 Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(),
1188 ExcAccessToNonLocalElement(
1190 vector->getMap()->getLocalNumElements(),
1191 vector->getMap()->getMinLocalIndex(),
1192 vector->getMap()->getMaxLocalIndex()));
1194 Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(),
1195 ExcAccessToNonLocalElement(
1197 vector->getMap()->getNodeNumElements(),
1198 vector->getMap()->getMinLocalIndex(),
1199 vector->getMap()->getMaxLocalIndex()));
1207 if (!vector_1d_nonlocal)
1209# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1210 auto vector_2d_nonlocal =
1211 nonlocal_vector->template getLocalView<Kokkos::HostSpace>(
1212 Tpetra::Access::ReadWrite);
1214 auto vector_2d_nonlocal =
1215 nonlocal_vector->template getLocalView<Kokkos::HostSpace>();
1218 vector_1d_nonlocal =
1219 Kokkos::subview(vector_2d_nonlocal, Kokkos::ALL(), 0);
1221# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1223 nonlocal_vector->template modify<Kokkos::HostSpace>();
1226 (*vector_1d_nonlocal)(nonlocal_row) += values[i];
1231# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1232 vector->template sync<
1233 typename Tpetra::Vector<Number, int, types::signed_global_dof_index>::
1234 device_type::memory_space>();
1238 if (vector_1d_nonlocal)
1239 nonlocal_vector->template sync<
1240 typename Tpetra::Vector<Number, int, types::signed_global_dof_index>::
1241 device_type::memory_space>();
1247 template <
typename Number,
typename MemorySpace>
1251 const Number *values)
1257# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1258 auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>(
1259 Tpetra::Access::ReadWrite);
1261 vector->template sync<Kokkos::HostSpace>();
1263 auto vector_2d_local = vector->template getLocalView<Kokkos::HostSpace>();
1272 auto vector_1d_local = Kokkos::subview(vector_2d_local, Kokkos::ALL(), 0);
1273 using ViewType1d =
decltype(vector_1d_local);
1274 std::optional<ViewType1d> vector_1d_nonlocal;
1276# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1279 vector->template modify<Kokkos::HostSpace>();
1282 for (
size_type i = 0; i < n_elements; ++i)
1290 vector->getMap()->getLocalElement(row);
1291 local_row != Teuchos::OrdinalTraits<int>::invalid())
1293 vector_1d_local(local_row) = values[i];
1298 if (nonlocal_vector.get() !=
nullptr)
1308 nonlocal_vector->getMap()->getLocalElement(row);
1310# if DEAL_II_TRILINOS_VERSION_GTE(14, 0, 0)
1311 Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(),
1312 ExcAccessToNonLocalElement(
1314 vector->getMap()->getLocalNumElements(),
1315 vector->getMap()->getMinLocalIndex(),
1316 vector->getMap()->getMaxLocalIndex()));
1318 Assert(nonlocal_row != Teuchos::OrdinalTraits<int>::invalid(),
1319 ExcAccessToNonLocalElement(
1321 vector->getMap()->getNodeNumElements(),
1322 vector->getMap()->getMinLocalIndex(),
1323 vector->getMap()->getMaxLocalIndex()));
1331 if (!vector_1d_nonlocal)
1333# if DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1334 auto vector_2d_nonlocal =
1335 nonlocal_vector->template getLocalView<Kokkos::HostSpace>(
1336 Tpetra::Access::ReadWrite);
1338 auto vector_2d_nonlocal =
1339 nonlocal_vector->template getLocalView<Kokkos::HostSpace>();
1342 vector_1d_nonlocal =
1343 Kokkos::subview(vector_2d_nonlocal, Kokkos::ALL(), 0);
1345# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1347 nonlocal_vector->template modify<Kokkos::HostSpace>();
1350 (*vector_1d_nonlocal)(nonlocal_row) = values[i];
1355# if !DEAL_II_TRILINOS_VERSION_GTE(13, 2, 0)
1356 vector->template sync<
1357 typename Tpetra::Vector<Number, int, types::signed_global_dof_index>::
1358 device_type::memory_space>();
1362 if (vector_1d_nonlocal)
1363 nonlocal_vector->template sync<
1364 typename Tpetra::Vector<Number, int, types::signed_global_dof_index>::
1365 device_type::memory_space>();
1371 template <
typename Number,
typename MemorySpace>
1372 inline internal::VectorReference<Number, MemorySpace>
1375 return internal::VectorReference(*
this, index);
1378 template <
typename Number,
typename MemorySpace>
1379 inline internal::VectorReference<Number, MemorySpace>
1382 return operator()(index);
1385 template <
typename Number,
typename MemorySpace>
1389 return operator()(index);
1397 template <
typename Number,
typename MemorySpace>
1398 inline VectorReference<Number, MemorySpace>::VectorReference(
1400 const size_type index)
1407 template <
typename Number,
typename MemorySpace>
1408 inline const VectorReference<Number, MemorySpace> &
1409 VectorReference<Number, MemorySpace>::operator=(
1410 const VectorReference<Number, MemorySpace> &r)
const
1416 *
this =
static_cast<Number
>(r);
1423 template <
typename Number,
typename MemorySpace>
1424 inline VectorReference<Number, MemorySpace> &
1425 VectorReference<Number, MemorySpace>::operator=(
1426 const VectorReference<Number, MemorySpace> &r)
1429 *
this =
static_cast<Number
>(r);
1436 template <
typename Number,
typename MemorySpace>
1437 inline const VectorReference<Number, MemorySpace> &
1438 VectorReference<Number, MemorySpace>::operator=(
const Number &value)
const
1440 vector.set(1, &index, &value);
1446 template <
typename Number,
typename MemorySpace>
1447 inline const VectorReference<Number, MemorySpace> &
1448 VectorReference<Number, MemorySpace>::operator+=(
1449 const Number &value)
const
1451 vector.add(1, &index, &value);
1457 template <
typename Number,
typename MemorySpace>
1458 inline const VectorReference<Number, MemorySpace> &
1459 VectorReference<Number, MemorySpace>::operator-=(
1460 const Number &value)
const
1462 Number new_value = -
value;
1463 vector.add(1, &index, &new_value);
1469 template <
typename Number,
typename MemorySpace>
1470 inline const VectorReference<Number, MemorySpace> &
1471 VectorReference<Number, MemorySpace>::operator*=(
1472 const Number &value)
const
1474 Number new_value =
static_cast<Number
>(*this) *
value;
1475 vector.set(1, &index, &new_value);
1481 template <
typename Number,
typename MemorySpace>
1482 inline const VectorReference<Number, MemorySpace> &
1483 VectorReference<Number, MemorySpace>::operator/=(
1484 const Number &value)
const
1486 Number new_value =
static_cast<Number
>(*this) /
value;
1487 vector.set(1, &index, &new_value);
1503 namespace LinearOperatorImplementation
1512 template <
typename Number,
typename MemorySpace>
1517 template <
typename Matrix>
1520 const Matrix &matrix,
1522 bool omit_zeroing_entries)
1524 v.
reinit(matrix.locally_owned_range_indices(),
1525 matrix.get_mpi_communicator(),
1526 omit_zeroing_entries);
1529 template <
typename Matrix>
1532 const Matrix &matrix,
1534 bool omit_zeroing_entries)
1536 v.
reinit(matrix.locally_owned_domain_indices(),
1537 matrix.get_mpi_communicator(),
1538 omit_zeroing_entries);
1548template <
typename Number,
typename MemorySpace>
1550 LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>> : std::false_type
void reinit(const Vector< Number, MemorySpace > &V, const bool omit_zeroing_entries=false)
void equ(const Number a, const Vector< Number, MemorySpace > &V)
Teuchos::RCP< const TpetraWrappers::CommunicationPattern< MemorySpace > > tpetra_comm_pattern
void reinit(const IndexSet &locally_owned_entries, const IndexSet &locally_relevant_or_ghost_entries, const MPI_Comm communicator=MPI_COMM_WORLD, const bool vector_writable=false)
void add(const Number a, const Vector< Number, MemorySpace > &V, const Number b, const Vector< Number, MemorySpace > &W)
bool is_non_negative() const
Number add_and_dot(const Number a, const Vector< Number, MemorySpace > &V, const Vector< Number, MemorySpace > &W)
TpetraTypes::VectorType< Number, MemorySpace > & trilinos_vector()
void reinit(const IndexSet ¶llel_partitioner, const MPI_Comm communicator=MPI_COMM_WORLD, const bool omit_zeroing_entries=false)
MPI_Comm get_mpi_communicator() const
Teuchos::RCP< TpetraTypes::VectorType< Number, MemorySpace > > vector
Number operator[](const size_type index) const
bool operator==(const Vector< Number, MemorySpace > &v) const
Vector(const IndexSet ¶llel_partitioner, const MPI_Comm communicator)
void scale(const Vector< Number, MemorySpace > &scaling_factors)
void set(const size_type n_elements, const size_type *indices, const Number *values)
void import_elements(const ReadWriteVector< Number > &V, VectorOperation::values operation, const std::shared_ptr< const Utilities::MPI::CommunicationPatternBase > &communication_pattern)
MPI_Comm mpi_comm() const
Vector(const Teuchos::RCP< TpetraTypes::VectorType< Number, MemorySpace > > V)
internal::VectorReference< Number, MemorySpace > reference
size_type locally_owned_size() const
reference operator()(const size_type index)
virtual void swap(Vector &v) noexcept
const internal::VectorReference< Number, MemorySpace > const_reference
void compress(const VectorOperation::values operation)
bool is_compressed() const
bool operator!=(const Vector< Number, MemorySpace > &v) const
Number operator()(const size_type index) const
Vector & operator/=(const Number factor)
std::pair< size_type, size_type > local_range() const
virtual void extract_subvector_to(const ArrayView< const types::global_dof_index > &indices, ArrayView< Number > &elements) const override
::IndexSet source_stored_elements
void sadd(const Number s, const Number a, const Vector< Number, MemorySpace > &V)
reference operator[](const size_type index)
void add(const Number a, const Vector< Number, MemorySpace > &V)
Vector & operator*=(const Number factor)
void add(const size_type n_elements, const size_type *indices, const Number *values)
void import_elements(const ReadWriteVector< Number > &V, VectorOperation::values operation, const Teuchos::RCP< const Utilities::MPI::CommunicationPatternBase > &communication_pattern)
real_type l2_norm() const
void add(const std::vector< size_type > &indices, const std::vector< Number > &values)
typename numbers::NumberTraits< Number >::real_type real_type
Vector & operator-=(const Vector< Number, MemorySpace > &V)
Vector & operator=(const ::Vector< OtherNumber > &V)
::IndexSet locally_owned_elements() const
Vector & operator=(const Vector &V)
Vector & operator=(const Number s)
Vector & operator+=(const Vector< Number, MemorySpace > &V)
const TpetraTypes::VectorType< Number, MemorySpace > & trilinos_vector() const
real_type linfty_norm() const
real_type norm_sqr() const
virtual size_type size() const override
void import_elements(const ReadWriteVector< Number > &V, VectorOperation::values operation)
void create_tpetra_comm_pattern(const IndexSet &source_index_set, const MPI_Comm mpi_comm)
real_type l1_norm() const
Teuchos::RCP< const TpetraTypes::VectorType< Number, MemorySpace > > trilinos_rcp() const
Number operator*(const Vector< Number, MemorySpace > &V) const
Number mean_value() const
std::size_t memory_consumption() const
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
bool has_ghost_elements() const
bool in_local_range(const size_type index) const
Vector(const IndexSet &locally_owned_entries, const IndexSet &ghost_entries, const MPI_Comm communicator, const bool vector_writable=false)
Teuchos::RCP< TpetraTypes::VectorType< Number, MemorySpace > > nonlocal_vector
Teuchos::RCP< TpetraTypes::VectorType< Number, MemorySpace > > trilinos_rcp()
static void reinit_range_vector(const Matrix &matrix, LinearAlgebra::TpetraWrappers::Vector< Number, MemorySpace > &v, bool omit_zeroing_entries)
static void reinit_domain_vector(const Matrix &matrix, LinearAlgebra::TpetraWrappers::Vector< Number, MemorySpace > &v, bool omit_zeroing_entries)
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define DeclException0(Exception0)
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
static ::ExceptionBase & ExcVectorTypeNotCompatible()
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
#define Assert(cond, exc)
static ::ExceptionBase & ExcMissingIndexSet()
#define AssertDimension(dim1, dim2)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcInternalError()
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcDifferentParallelPartitioning()
static ::ExceptionBase & ExcTrilinosError(int arg1)
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
void swap(Vector< Number, MemorySpace > &u, Vector< Number, MemorySpace > &v) noexcept
unsigned int global_dof_index