16#ifndef dealii_la_parallel_vector_h
17#define dealii_la_parallel_vector_h
54 class ReadWriteVector;
57# ifdef DEAL_II_WITH_PETSC
67# ifdef DEAL_II_WITH_TRILINOS
249 template <
typename Number,
typename MemorySpace = MemorySpace::Host>
266 std::is_same<MemorySpace, ::MemorySpace::Host>::value ||
267 std::is_same<MemorySpace, ::MemorySpace::CUDA>::value,
268 "MemorySpace should be Host or CUDA");
334 const std::shared_ptr<const Utilities::MPI::Partitioner> &
partitioner);
358 template <
typename Number2>
361 const bool omit_zeroing_entries =
false);
404 const std::shared_ptr<const Utilities::MPI::Partitioner> &
partitioner,
472 template <
typename Number2>
563 const unsigned int communication_channel = 0,
606 const unsigned int communication_channel = 0)
const;
671 template <
typename Number2>
685 template <
typename MemorySpace2>
703 const bool omit_zeroing_entries =
false)
override;
743 std::shared_ptr<const Utilities::MPI::CommunicationPatternBase>
744 communication_pattern = {})
override;
756 add(
const Number a)
override;
778 add(
const std::vector<size_type> &indices,
779 const std::vector<Number> & values);
882 const unsigned int precision = 3,
883 const bool scientific =
true,
884 const bool across =
true)
const override;
910 template <
typename OtherNumber>
912 add(
const std::vector<size_type> & indices,
913 const ::Vector<OtherNumber> &values);
919 template <
typename OtherNumber>
923 const OtherNumber *values);
1098 template <
typename OtherNumber>
1101 std::vector<OtherNumber> & values)
const;
1130 template <
typename ForwardIterator,
typename OutputIterator>
1133 const ForwardIterator indices_end,
1134 OutputIterator values_begin)
const;
1175 const std::shared_ptr<const Utilities::MPI::Partitioner> &
1218 const std::vector<ArrayView<const Number>> &
1244 <<
"Called compress(VectorOperation::insert), but"
1245 <<
" the element received from a remote processor, value "
1246 << std::setprecision(16) << arg1
1247 <<
", does not match with the value "
1248 << std::setprecision(16) << arg2
1249 <<
" on the owner processor " << arg3);
1260 <<
"You tried to access element " << arg1
1261 <<
" of a distributed vector, but this element is not "
1262 <<
"stored on the current processor. Note: The range of "
1263 <<
"locally owned elements is [" << arg2 <<
',' << arg3
1264 <<
"], and there are " << arg4 <<
" ghost elements "
1265 <<
"that this vector can access."
1267 <<
"A common source for this kind of problem is that you "
1268 <<
"are passing a 'fully distributed' vector into a function "
1269 <<
"that needs read access to vector elements that correspond "
1270 <<
"to degrees of freedom on ghost cells (or at least to "
1271 <<
"'locally active' degrees of freedom that are not also "
1272 <<
"'locally owned'). You need to pass a vector that has these "
1273 <<
"elements as ghost entries.");
1295 template <
typename Number2>
1354 mutable ::MemorySpace::MemorySpaceData<Number, MemorySpace>
data;
1360 mutable std::shared_ptr<::parallel::internal::TBBPartitioner>
1367 mutable ::MemorySpace::MemorySpaceData<Number, MemorySpace>
1379#ifdef DEAL_II_WITH_MPI
1426 template <
typename Number2,
typename MemorySpace2>
1430 template <
typename Number2>
1442 template <
typename Number,
typename MemorySpace>
1453 const ::MemorySpace::MemorySpaceData<Number, MemorySpace> &)
1458 static inline Number *
1468 template <
typename Number>
1473 begin(::MemorySpace::
1474 MemorySpaceData<Number, ::MemorySpace::Host> &data)
1476 return data.values.get();
1481 begin(const ::MemorySpace::
1482 MemorySpaceData<Number, ::MemorySpace::Host> &data)
1484 return data.values.get();
1487 static inline Number *
1488 get_values(::MemorySpace::
1489 MemorySpaceData<Number, ::MemorySpace::Host> &data)
1491 return data.values.get();
1497 template <
typename Number>
1502 begin(::MemorySpace::
1503 MemorySpaceData<Number, ::MemorySpace::CUDA> &data)
1505 return data.values_dev.get();
1510 begin(const ::MemorySpace::
1511 MemorySpaceData<Number, ::MemorySpace::CUDA> &data)
1513 return data.values_dev.get();
1516 static inline Number *
1517 get_values(::MemorySpace::
1518 MemorySpaceData<Number, ::MemorySpace::CUDA> &data)
1520 return data.values_dev.get();
1526 template <
typename Number,
typename MemorySpace>
1530 return vector_is_ghosted;
1535 template <
typename Number,
typename MemorySpace>
1539 return partitioner->size();
1544 template <
typename Number,
typename MemorySpace>
1548 return locally_owned_size();
1553 template <
typename Number,
typename MemorySpace>
1557 return partitioner->locally_owned_size();
1562 template <
typename Number,
typename MemorySpace>
1565 const size_type global_index)
const
1567 return partitioner->in_local_range(global_index);
1572 template <
typename Number,
typename MemorySpace>
1578 is.add_range(partitioner->local_range().first,
1579 partitioner->local_range().second);
1586 template <
typename Number,
typename MemorySpace>
1590 return internal::Policy<Number, MemorySpace>::begin(data);
1595 template <
typename Number,
typename MemorySpace>
1599 return internal::Policy<Number, MemorySpace>::begin(data);
1604 template <
typename Number,
typename MemorySpace>
1608 return internal::Policy<Number, MemorySpace>::begin(data) +
1609 partitioner->locally_owned_size();
1614 template <
typename Number,
typename MemorySpace>
1618 return internal::Policy<Number, MemorySpace>::begin(data) +
1619 partitioner->locally_owned_size();
1624 template <
typename Number,
typename MemorySpace>
1625 const std::vector<ArrayView<const Number>> &
1628 return data.values_sm;
1633 template <
typename Number,
typename MemorySpace>
1637 Assert((std::is_same<MemorySpace, ::MemorySpace::Host>::value),
1639 "This function is only implemented for the Host memory space"));
1641 partitioner->in_local_range(global_index) ||
1642 partitioner->ghost_indices().is_element(global_index),
1643 ExcAccessToNonLocalElement(global_index,
1644 partitioner->local_range().first,
1645 partitioner->local_range().second == 0 ?
1647 (partitioner->local_range().second - 1),
1648 partitioner->ghost_indices().n_elements()));
1650 Assert(partitioner->in_local_range(global_index) ||
1651 vector_is_ghosted ==
true,
1652 ExcMessage(
"You tried to read a ghost element of this vector, "
1653 "but it has not imported its ghost values."));
1654 return data.values[partitioner->global_to_local(global_index)];
1659 template <
typename Number,
typename MemorySpace>
1663 Assert((std::is_same<MemorySpace, ::MemorySpace::Host>::value),
1665 "This function is only implemented for the Host memory space"));
1667 partitioner->in_local_range(global_index) ||
1668 partitioner->ghost_indices().is_element(global_index),
1669 ExcAccessToNonLocalElement(global_index,
1670 partitioner->local_range().first,
1671 partitioner->local_range().second == 0 ?
1673 (partitioner->local_range().second - 1),
1674 partitioner->ghost_indices().n_elements()));
1681 return data.values[partitioner->global_to_local(global_index)];
1686 template <
typename Number,
typename MemorySpace>
1690 return operator()(global_index);
1695 template <
typename Number,
typename MemorySpace>
1699 return operator()(global_index);
1704 template <
typename Number,
typename MemorySpace>
1707 const size_type local_index)
const
1709 Assert((std::is_same<MemorySpace, ::MemorySpace::Host>::value),
1711 "This function is only implemented for the Host memory space"));
1713 partitioner->locally_owned_size() +
1714 partitioner->n_ghost_indices());
1716 Assert(local_index < locally_owned_size() || vector_is_ghosted ==
true,
1717 ExcMessage(
"You tried to read a ghost element of this vector, "
1718 "but it has not imported its ghost values."));
1720 return data.values[local_index];
1725 template <
typename Number,
typename MemorySpace>
1729 Assert((std::is_same<MemorySpace, ::MemorySpace::Host>::value),
1731 "This function is only implemented for the Host memory space"));
1734 partitioner->locally_owned_size() +
1735 partitioner->n_ghost_indices());
1737 return data.values[local_index];
1742 template <
typename Number,
typename MemorySpace>
1746 return internal::Policy<Number, MemorySpace>::get_values(data);
1751 template <
typename Number,
typename MemorySpace>
1752 template <
typename OtherNumber>
1755 const std::vector<size_type> &indices,
1756 std::vector<OtherNumber> & values)
const
1758 for (size_type i = 0; i < indices.size(); ++i)
1759 values[i] =
operator()(indices[i]);
1764 template <
typename Number,
typename MemorySpace>
1765 template <
typename ForwardIterator,
typename OutputIterator>
1768 ForwardIterator indices_begin,
1769 const ForwardIterator indices_end,
1770 OutputIterator values_begin)
const
1772 while (indices_begin != indices_end)
1774 *values_begin = operator()(*indices_begin);
1782 template <
typename Number,
typename MemorySpace>
1783 template <
typename OtherNumber>
1786 const std::vector<size_type> & indices,
1787 const ::Vector<OtherNumber> &values)
1790 for (size_type i = 0; i < indices.size(); ++i)
1795 "The given value is not finite but either infinite or Not A Number (NaN)"));
1796 this->operator()(indices[i]) +=
values(i);
1802 template <
typename Number,
typename MemorySpace>
1803 template <
typename OtherNumber>
1806 const size_type * indices,
1807 const OtherNumber *values)
1809 for (size_type i = 0; i < n_elements; ++i, ++indices, ++
values)
1814 "The given value is not finite but either infinite or Not A Number (NaN)"));
1815 this->operator()(*indices) += *
values;
1821 template <
typename Number,
typename MemorySpace>
1825 return partitioner->get_mpi_communicator();
1830 template <
typename Number,
typename MemorySpace>
1831 inline const std::shared_ptr<const Utilities::MPI::Partitioner> &
1839 template <
typename Number,
typename MemorySpace>
1843 vector_is_ghosted = ghosted;
1859template <
typename Number,
typename MemorySpace>
1871template <
typename Number,
typename MemorySpace>
1880 namespace LinearOperatorImplementation
1889 template <
typename Number>
1895 template <
typename T>
1897 decltype(std::declval<T>().get_mpi_communicator());
1899 template <
typename T>
1900 static constexpr bool has_get_mpi_communicator =
1901 is_supported_operation<get_mpi_communicator_t, T>;
1905 template <
typename T>
1907 decltype(std::declval<T>().locally_owned_domain_indices());
1909 template <
typename T>
1910 static constexpr bool has_locally_owned_domain_indices =
1911 is_supported_operation<locally_owned_domain_indices_t, T>;
1915 template <
typename T>
1917 decltype(std::declval<T>().locally_owned_range_indices());
1919 template <
typename T>
1920 static constexpr bool has_locally_owned_range_indices =
1921 is_supported_operation<locally_owned_range_indices_t, T>;
1925 template <
typename T>
1927 decltype(std::declval<T>().initialize_dof_vector(
1930 template <
typename T>
1931 static constexpr bool has_initialize_dof_vector =
1932 is_supported_operation<initialize_dof_vector_t, T>;
1936 typename MatrixType,
1937#if !defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1900
1938 typename std::enable_if<has_get_mpi_communicator<MatrixType> &&
1939 has_locally_owned_domain_indices<MatrixType>,
1942 typename std::enable_if<
1943 is_supported_operation<get_mpi_communicator_t, MatrixType> &&
1944 is_supported_operation<locally_owned_domain_indices_t, MatrixType>,
1946 MatrixType>::type * =
nullptr>
1952 vec.
reinit(mat.locally_owned_domain_indices(),
1953 mat.get_mpi_communicator());
1957 template <
typename MatrixType,
1958#if !defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1900
1959 typename std::enable_if<has_initialize_dof_vector<MatrixType>,
1962 typename std::enable_if<
1963 is_supported_operation<initialize_dof_vector_t, MatrixType>,
1965 MatrixType>::type * =
nullptr>
1969 bool omit_zeroing_entries)
1971 mat.initialize_dof_vector(vec);
1972 if (!omit_zeroing_entries)
1978 typename MatrixType,
1979#if !defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1900
1980 typename std::enable_if<has_get_mpi_communicator<MatrixType> &&
1981 has_locally_owned_range_indices<MatrixType>,
1984 typename std::enable_if<
1985 is_supported_operation<get_mpi_communicator_t, MatrixType> &&
1986 is_supported_operation<locally_owned_range_indices_t, MatrixType>,
1988 MatrixType>::type * =
nullptr>
1994 vec.
reinit(mat.locally_owned_range_indices(),
1995 mat.get_mpi_communicator());
1999 template <
typename MatrixType,
2000#if !defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1900
2001 typename std::enable_if<has_initialize_dof_vector<MatrixType>,
2004 typename std::enable_if<
2005 is_supported_operation<initialize_dof_vector_t, MatrixType>,
2007 MatrixType>::type * =
nullptr>
2011 bool omit_zeroing_entries)
2013 mat.initialize_dof_vector(vec);
2014 if (!omit_zeroing_entries)
void swap(LinearAlgebra::distributed::Vector< Number, MemorySpace > &u, LinearAlgebra::distributed::Vector< Number, MemorySpace > &v)
static void reinit_range_vector(MatrixType &mat, LinearAlgebra::distributed::Vector< Number > &vec, bool omit_zeroing_entries)
static void reinit_domain_vector(MatrixType &mat, LinearAlgebra::distributed::Vector< Number > &vec, bool omit_zeroing_entries)
#define DEAL_II_DEPRECATED
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define DeclException0(Exception0)
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static ::ExceptionBase & ExcMessage(std::string arg1)
Number & operator()(const size_type global_index)
void zero_out_ghost_values() const
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
Number mean_value_local() const
virtual real_type l1_norm() const override
void resize_val(const size_type new_allocated_size, const MPI_Comm &comm_sm=MPI_COMM_SELF)
bool partitioners_are_compatible(const Utilities::MPI::Partitioner &part) const
decltype(std::declval< T >().initialize_dof_vector(std::declval< LinearAlgebra::distributed::Vector< Number > & >())) initialize_dof_vector_t
virtual Vector< Number, MemorySpace > & operator*=(const Number factor) override
bool has_ghost_elements() const
void reinit(const std::shared_ptr< const Utilities::MPI::Partitioner > &partitioner, const MPI_Comm &comm_sm=MPI_COMM_SELF)
const value_type & const_reference
Number local_element(const size_type local_index) const
Number add_and_dot_local(const Number a, const Vector< Number, MemorySpace > &V, const Vector< Number, MemorySpace > &W)
real_type linfty_norm_local() const
virtual void add(const Number a, const VectorSpaceVector< Number > &V, const Number b, const VectorSpaceVector< Number > &W) override
Number * get_values() const
void update_ghost_values() const
mutable ::MemorySpace::MemorySpaceData< Number, MemorySpace > import_data
void swap(Vector< Number, MemorySpace > &v)
real_type l1_norm_local() const
virtual real_type linfty_norm() const override
mutable ::MemorySpace::MemorySpaceData< Number, MemorySpace > data
types::global_dof_index size_type
Vector(const IndexSet &local_range, const IndexSet &ghost_indices, const MPI_Comm &communicator)
decltype(std::declval< T >().get_mpi_communicator()) get_mpi_communicator_t
const value_type * const_iterator
typename numbers::NumberTraits< Number >::real_type real_type
const_iterator end() const
Vector(const size_type size)
Vector(Vector< Number, MemorySpace > &&in_vector)
size_type locally_owned_size() const
static void reinit_range_vector(MatrixType &mat, LinearAlgebra::distributed::Vector< Number > &vec, bool)
std::shared_ptr< const Utilities::MPI::Partitioner > partitioner
void add(const size_type n_elements, const size_type *indices, const OtherNumber *values)
virtual void equ(const Number a, const VectorSpaceVector< Number > &V) override
void sadd(const Number s, const Vector< Number, MemorySpace > &V)
Vector(const IndexSet &local_range, const MPI_Comm &communicator)
Vector< Number, MemorySpace > & operator=(const Vector< Number, MemorySpace > &in_vector)
real_type norm_sqr_local() const
virtual std::size_t memory_consumption() const override
const value_type * const_iterator
void clear_mpi_requests()
decltype(std::declval< T >().locally_owned_domain_indices()) locally_owned_domain_indices_t
static ::ExceptionBase & ExcVectorTypeNotCompatible()
Number operator[](const size_type global_index) const
virtual bool all_zero() const override
Vector(const Vector< Number, MemorySpace > &in_vector)
virtual Number mean_value() const override
void set_ghost_state(const bool ghosted) const
static ::ExceptionBase & ExcNonMatchingElements(Number arg1, Number arg2, unsigned int arg3)
virtual Vector< Number, MemorySpace > & operator=(const Number s) override
decltype(std::declval< T >().locally_owned_range_indices()) locally_owned_range_indices_t
void reinit(const IndexSet &local_range, const IndexSet &ghost_indices, const MPI_Comm &communicator)
void add(const std::vector< size_type > &indices, const ::Vector< OtherNumber > &values)
virtual void reinit(const VectorSpaceVector< Number > &V, const bool omit_zeroing_entries=false) override
Number & operator[](const size_type global_index)
virtual Number operator*(const VectorSpaceVector< Number > &V) const override
const value_type * const_pointer
static ::ExceptionBase & ExcNotAllowedForCuda()
virtual void add(const std::vector< size_type > &indices, const std::vector< Number > &values)
virtual void compress(::VectorOperation::values operation) override
void reinit(const Vector< Number2, MemorySpace > &in_vector, const bool omit_zeroing_entries=false)
real_type lp_norm(const real_type p) const
bool partitioners_are_globally_compatible(const Utilities::MPI::Partitioner &part) const
const std::shared_ptr< const Utilities::MPI::Partitioner > & get_partitioner() const
virtual size_type size() const override
virtual ~Vector() override
virtual Vector< Number, MemorySpace > & operator-=(const VectorSpaceVector< Number > &V) override
void add_local(const Number a, const VectorSpaceVector< Number > &V)
std::shared_ptr<::parallel::internal::TBBPartitioner > thread_loop_partitioner
virtual void sadd(const Number s, const Number a, const VectorSpaceVector< Number > &V) override
void compress_start(const unsigned int communication_channel=0, ::VectorOperation::values operation=VectorOperation::add)
const MPI_Comm & get_mpi_communicator() const
virtual void scale(const VectorSpaceVector< Number > &scaling_factors) override
virtual void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const override
void update_ghost_values_finish() const
const std::vector< ArrayView< const Number > > & shared_vector_data() const
void update_ghost_values_start(const unsigned int communication_channel=0) const
void copy_locally_owned_data_from(const Vector< Number2, MemorySpace > &src)
Vector< Number, MemorySpace > & operator=(const Vector< Number2, MemorySpace > &in_vector)
virtual void add(const Number a, const VectorSpaceVector< Number > &V) override
size_type local_size() const
virtual Vector< Number, MemorySpace > & operator/=(const Number factor) override
virtual void add(const Number a) override
void zero_out_ghosts() const
const value_type * const_iterator
Number operator()(const size_type global_index) const
virtual Vector< Number, MemorySpace > & operator+=(const VectorSpaceVector< Number > &V) override
real_type lp_norm_local(const real_type p) const
void reinit(const types::global_dof_index local_size, const types::global_dof_index ghost_size, const MPI_Comm &comm, const MPI_Comm &comm_sm=MPI_COMM_SELF)
void sadd_local(const Number s, const Number a, const VectorSpaceVector< Number > &V)
void reinit(const IndexSet &local_range, const MPI_Comm &communicator)
void reinit(const size_type size, const bool omit_zeroing_entries=false)
static void reinit_domain_vector(MatrixType &mat, LinearAlgebra::distributed::Vector< Number > &vec, bool)
void extract_subvector_to(const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
void compress_finish(::VectorOperation::values operation)
std::vector< MPI_Request > update_ghost_values_requests
const_iterator begin() const
std::vector< MPI_Request > compress_requests
bool in_local_range(const size_type global_index) const
void extract_subvector_to(ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
Number inner_product_local(const Vector< Number2, MemorySpace > &V) const
Number & local_element(const size_type local_index)
real_type norm_sqr() const
virtual Number add_and_dot(const Number a, const VectorSpaceVector< Number > &V, const VectorSpaceVector< Number > &W) override
virtual real_type l2_norm() const override
Vector(const std::shared_ptr< const Utilities::MPI::Partitioner > &partitioner)
virtual ::IndexSet locally_owned_elements() const override
VectorType::value_type * begin(VectorType &V)
bool is_finite(const double x)
unsigned int global_dof_index