13#ifndef dealii_trilinos_tpetra_sparse_matrix_h
14#define dealii_trilinos_tpetra_sparse_matrix_h
18#ifdef DEAL_II_TRILINOS_WITH_TPETRA
31# include <Tpetra_Core.hpp>
32# include <Tpetra_CrsMatrix.hpp>
34# include <type_traits>
40#ifdef DEAL_II_TRILINOS_WITH_TPETRA
43template <
typename Number>
47template <
typename MatrixType>
52 namespace TpetraWrappers
54 template <
typename MemorySpace>
59 template <
typename Number,
typename MemorySpace,
bool Constness>
69 namespace TpetraWrappers
109 template <
typename Number,
typename MemorySpace = ::MemorySpace::Host>
123 <<
"You tried to access row " << arg1
124 <<
" of a non-contiguous locally owned row set."
125 <<
" The row " << arg1
126 <<
" is not stored locally and can't be accessed.");
185 const unsigned int n_max_entries_per_row);
196 const std::vector<unsigned int> &n_entries_per_row);
242 template <
typename SparsityPatternType>
244 reinit(
const SparsityPatternType &sparsity_pattern);
290 const unsigned int n_max_entries_per_row);
302 const std::vector<unsigned int> &n_entries_per_row);
319 const IndexSet &col_parallel_partitioning,
332 const IndexSet &col_parallel_partitioning,
334 const std::vector<unsigned int> &n_entries_per_row);
355 template <
typename SparsityPatternType>
357 !std::is_same_v<SparsityPatternType, ::SparseMatrix<double>>>
359 const SparsityPatternType &sparsity_pattern,
360 const MPI_Comm communicator = MPI_COMM_WORLD,
361 const bool exchange_data =
false);
375 template <
typename SparsityPatternType>
377 !std::is_same_v<SparsityPatternType, ::SparseMatrix<double>>>
379 const IndexSet &col_parallel_partitioning,
380 const SparsityPatternType &sparsity_pattern,
381 const MPI_Comm communicator = MPI_COMM_WORLD,
382 const bool exchange_data =
false);
402 const IndexSet &col_parallel_partitioning,
403 const ::SparseMatrix<Number> &dealii_sparse_matrix,
404 const MPI_Comm communicator = MPI_COMM_WORLD,
405 const double drop_tolerance = 1e-13,
406 const bool copy_values =
true,
407 const ::SparsityPattern *use_this_sparsity =
nullptr);
447 std::pair<size_type, size_type>
557 const Number *values,
558 const bool elide_zero_values =
true,
559 const bool col_indices_are_sorted =
false);
628 set(
const std::vector<size_type> &indices,
630 const bool elide_zero_values =
false);
638 set(
const std::vector<size_type> &row_indices,
639 const std::vector<size_type> &col_indices,
641 const bool elide_zero_values =
false);
672 const std::vector<size_type> &col_indices,
673 const std::vector<Number> &values,
674 const bool elide_zero_values =
false);
703 template <
typename OtherNumber>
708 const OtherNumber *values,
709 const bool elide_zero_values =
false);
762 const Number new_diag_value = 0);
834 template <
typename InputVectorType>
836 vmult(InputVectorType &dst,
const InputVectorType &src)
const;
845 template <
typename InputVectorType>
847 Tvmult(InputVectorType &dst,
const InputVectorType &src)
const;
855 template <
typename InputVectorType>
857 vmult_add(InputVectorType &dst,
const InputVectorType &src)
const;
866 template <
typename InputVectorType>
868 Tvmult_add(InputVectorType &dst,
const InputVectorType &src)
const;
988 const bool print_detailed_trilinos_information =
false)
const;
1060 Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1072 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
1214 "You are attempting an operation on two vectors that "
1215 "are the same object, but the operation requires that the "
1216 "two objects are in fact different.");
1222 "The column partitioning of a matrix does not match "
1223 "the partitioning of a vector you are trying to "
1224 "multiply it with. Are you multiplying the "
1225 "matrix with a vector that has ghost elements?");
1231 "The row partitioning of a matrix does not match "
1232 "the partitioning of a vector you are trying to "
1233 "put the result of a matrix-vector product in. "
1234 "Are you trying to put the product of the "
1235 "matrix with a vector into a vector that has "
1244 <<
"The entry with index <" << arg1 <<
',' << arg2
1245 <<
"> does not exist.");
1255 <<
"You tried to access element (" << arg1 <<
'/' << arg2
1257 <<
" of a distributed matrix, but only rows in range ["
1258 << arg3 <<
',' << arg4
1259 <<
"] are stored locally and can be accessed.");
1286 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
matrix;
1340 template <
typename Number,
typename MemorySpace>
1412 std::shared_ptr<std::vector<::types::signed_global_dof_index>>
1435 template <
typename Number,
typename MemorySpace,
bool Constness>
1454 template <
typename Number,
typename MemorySpace>
1482 template <
bool Other>
1495 template <
typename Number,
typename MemorySpace>
1510 operator Number()
const;
1516 operator=(
const Number n)
const;
1522 operator+=(
const Number n)
const;
1528 operator-=(
const Number n)
const;
1534 operator*=(
const Number n)
const;
1540 operator/=(
const Number n)
const;
1578 friend class Reference;
1594 template <
typename Number,
typename MemorySpace,
bool Constness>
1633 template <
bool Other>
1664 template <
bool OtherConstness>
1671 template <
bool OtherConstness>
1680 template <
bool OtherConstness>
1687 template <
bool OtherConstness>
1697 <<
"Attempt to access element " << arg2 <<
" of row "
1698 << arg1 <<
" which doesn't have that many elements.");
1720 template <
typename Number,
typename MemorySpace,
bool Constness>
1723 Iterator<Number, MemorySpace, Constness>>
1727 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1728 Iterator<Number, MemorySpace, Constness>::value_type;
1730 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1731 Iterator<Number, MemorySpace, Constness>::difference_type;
1742 namespace TpetraWrappers
1744 template <
typename Number,
typename MemorySpace>
1750 set(i, 1, &j, &value,
false);
1755 template <
typename Number,
typename MemorySpace>
1761 add(i, 1, &j, &value,
false);
1766 template <
typename Number,
typename MemorySpace>
1782 template <
typename Number,
typename MemorySpace>
1786 return matrix->getRowMap()->getGlobalNumElements();
1791 template <
typename Number,
typename MemorySpace>
1799 return column_space_map->getGlobalNumElements();
1804 template <
typename Number,
typename MemorySpace>
1809 sizeof(*this) +
sizeof(*matrix) +
sizeof(matrix->getGraph().get());
1811# if DEAL_II_TRILINOS_VERSION_GTE(13, 4, 0)
1812 matrix->getLocalNumEntries() +
1814 matrix->getNodeNumEntries() +
1816 sizeof(
int) * local_size() + static_memory);
1821 template <
typename Number,
typename MemorySpace>
1830 template <
typename Number,
typename MemorySpace>
1839 template <
typename Number,
typename MemorySpace>
1848 template <
typename Number,
typename MemorySpace>
1853 if (in_local_range(r) && (row_length(r) > 0))
1860 template <
typename Number,
typename MemorySpace>
1870 if (in_local_range(i) && (row_length(i) > 0))
1880 template <
typename Number,
typename MemorySpace>
1889 template <
typename Number,
typename MemorySpace>
1898 template <
typename Number,
typename MemorySpace>
1903 if (in_local_range(r) && (row_length(r) > 0))
1911 template <
typename Number,
typename MemorySpace>
1921 if (in_local_range(i) && (row_length(i) > 0))
1931 template <
typename Number,
typename MemorySpace>
1936 const size_type begin = matrix->getRowMap()->getMinGlobalIndex();
1937 const size_type end = matrix->getRowMap()->getMaxGlobalIndex() + 1;
1939 return ((index >=
begin) && (index <
end));
1944 template <
typename Number,
typename MemorySpace>
1948 auto n_entries = matrix->getNumEntriesInGlobalRow(row);
1950 Teuchos::OrdinalTraits<
decltype(n_entries)>::invalid(),
1951 ExcAccessToNonlocalRow(row));
1958 template <
typename Number,
typename MemorySpace>
1967 template <
typename Number,
typename MemorySpace>
1976 template <
typename Number,
typename MemorySpace>
1985 template <
typename Number,
typename MemorySpace>
1994 template <
typename Number,
typename MemorySpace>
1995 inline Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1998 return matrix.getConst();
2003 template <
typename Number,
typename MemorySpace>
2004 inline Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
2012 template <
typename Number,
typename MemorySpace>
2016 return IndexSet(matrix->getDomainMap());
2021 template <
typename Number,
typename MemorySpace>
2025 return IndexSet(matrix->getRangeMap());
2031 template <
typename Number,
typename MemorySpace>
2044 template <
typename Number,
typename MemorySpace>
2053 template <
typename Number,
typename MemorySpace>
2058 return (*colnum_cache)[a_index];
2062 template <
typename Number,
typename MemorySpace>
2072 template <
typename Number,
typename MemorySpace>
2082 if ((this->a_row == matrix->m()) ||
2083 (matrix->in_local_range(this->a_row) ==
false))
2085 colnum_cache.reset();
2086 value_cache.reset();
2094 matrix->row_length(this->a_row);
2095 if (value_cache.get() ==
nullptr)
2097 value_cache = std::make_shared<std::vector<Number>>(colnums);
2098 colnum_cache = std::make_shared<
2099 std::vector<::types::signed_global_dof_index>>(colnums);
2103 value_cache->resize(colnums);
2104 colnum_cache->resize(colnums);
2108 nonconst_global_inds_host_view_type col_indices(colnum_cache->data(),
2111 nonconst_values_host_view_type values(value_cache->data(), colnums);
2113 matrix->trilinos_matrix().getGlobalRowCopy(this->a_row,
2130 template <
typename Number,
typename MemorySpace>
2143 template <
typename Number,
typename MemorySpace>
2144 template <
bool Other>
2152 template <
typename Number,
typename MemorySpace>
2165 template <
typename Number,
typename MemorySpace>
2173 template <
typename Number,
typename MemorySpace>
2177 return (*accessor.value_cache)[accessor.a_index];
2182 template <
typename Number,
typename MemorySpace>
2185 const Number n)
const
2187 (*accessor.value_cache)[accessor.a_index] = n;
2188 accessor.matrix->set(accessor.row(),
2190 static_cast<Number
>(*
this));
2196 template <
typename Number,
typename MemorySpace>
2199 const Number n)
const
2201 (*accessor.value_cache)[accessor.a_index] += n;
2202 accessor.matrix->set(accessor.row(),
2204 static_cast<Number
>(*
this));
2210 template <
typename Number,
typename MemorySpace>
2213 const Number n)
const
2215 (*accessor.value_cache)[accessor.a_index] -= n;
2216 accessor.matrix->set(accessor.row(),
2218 static_cast<Number
>(*
this));
2223 template <
typename Number,
typename MemorySpace>
2226 const Number n)
const
2228 (*accessor.value_cache)[accessor.a_index] *= n;
2229 accessor.matrix->set(accessor.row(),
2231 static_cast<Number
>(*
this));
2236 template <
typename Number,
typename MemorySpace>
2239 const Number n)
const
2241 (*accessor.value_cache)[accessor.a_index] /= n;
2242 accessor.matrix->set(accessor.row(),
2244 static_cast<Number
>(*
this));
2249 template <
typename Number,
typename MemorySpace>
2258 template <
typename Number,
typename MemorySpace>
2270 template <
typename Number,
typename MemorySpace,
bool Constness>
2275 : accessor(matrix, row, index)
2279 template <
typename Number,
typename MemorySpace,
bool Constness>
2280 template <
bool Other>
2283 : accessor(other.accessor)
2288 template <
typename Number,
typename MemorySpace,
bool Constness>
2300 if (accessor.a_index >= accessor.colnum_cache->size())
2302 accessor.a_index = 0;
2306 (accessor.a_row < accessor.matrix->m()) &&
2307 ((accessor.matrix->in_local_range(accessor.a_row) ==
false) ||
2308 (accessor.matrix->row_length(accessor.a_row) == 0)))
2311 accessor.visit_present_row();
2317 template <
typename Number,
typename MemorySpace,
bool Constness>
2328 template <
typename Number,
typename MemorySpace,
bool Constness>
2337 template <
typename Number,
typename MemorySpace,
bool Constness>
2346 template <
typename Number,
typename MemorySpace,
bool Constness>
2347 template <
bool OtherConstness>
2352 return (accessor.a_row == other.
accessor.a_row &&
2353 accessor.a_index == other.
accessor.a_index);
2358 template <
typename Number,
typename MemorySpace,
bool Constness>
2359 template <
bool OtherConstness>
2364 return !(*
this == other);
2369 template <
typename Number,
typename MemorySpace,
bool Constness>
2370 template <
bool OtherConstness>
2375 return (accessor.row() < other.
accessor.row() ||
2376 (accessor.row() == other.
accessor.row() &&
2377 accessor.index() < other.
accessor.index()));
2381 template <
typename Number,
typename MemorySpace,
bool Constness>
2382 template <
bool OtherConstness>
2387 return (other < *
this);
*Â x_component_mask set(0, true)
SparseMatrix< Number, MemorySpace > * matrix
std::shared_ptr< std::vector< Number > > value_cache
std::shared_ptr< std::vector<::types::signed_global_dof_index > > colnum_cache
AccessorBase(SparseMatrix< Number, MemorySpace > *matrix, const size_type row, const size_type index)
typename AccessorBase< Number, MemorySpace >::size_type size_type
typename AccessorBase< Number, MemorySpace >::size_type size_type
bool operator!=(const Iterator< Number, MemorySpace, OtherConstness > &) const
bool operator<(const Iterator< Number, MemorySpace, OtherConstness > &) const
bool operator>(const Iterator< Number, MemorySpace, OtherConstness > &) const
Iterator(MatrixType *matrix, const size_type row, const size_type index)
Accessor< Number, MemorySpace, Constness > accessor
const Accessor< Number, MemorySpace, Constness > * operator->() const
Iterator< Number, MemorySpace, Constness > & operator++()
typename Accessor< Number, MemorySpace, Constness >::MatrixType MatrixType
const Accessor< Number, MemorySpace, Constness > & operator*() const
bool operator==(const Iterator< Number, MemorySpace, OtherConstness > &) const
virtual ~SparseMatrix() override=default
unsigned int row_length(const size_type row) const
SparseMatrix(const SparseMatrix< Number, MemorySpace > &)=delete
void clear_row(const size_type row, const Number new_diag_value=0)
void print(std::ostream &out, const bool print_detailed_trilinos_information=false) const
IndexSet locally_owned_domain_indices() const
iterator begin(const size_type r)
SparseMatrix< Number, MemorySpace > & operator=(SparseMatrix< Number, MemorySpace > &&other) noexcept
TpetraTypes::MatrixType< Number, MemorySpace > & trilinos_matrix()
void add(const size_type i, const size_type j, const Number value)
void set(const std::vector< size_type > &row_indices, const std::vector< size_type > &col_indices, const FullMatrix< Number > &full_matrix, const bool elide_zero_values=false)
SparseMatrix(SparseMatrix< Number, MemorySpace > &&other) noexcept
void clear_rows(const ArrayView< const size_type > &rows, const Number new_diag_value=0)
const_iterator end() const
void set(const std::vector< size_type > &indices, const FullMatrix< Number > &full_matrix, const bool elide_zero_values=false)
Number residual(Vector< Number, MemorySpace > &dst, const Vector< Number, MemorySpace > &x, const Vector< Number, MemorySpace > &b) const
Number linfty_norm() const
std::pair< size_type, size_type > local_range() const
void set(const size_type row, const std::vector< size_type > &col_indices, const std::vector< Number > &values, const bool elide_zero_values=false)
Number frobenius_norm() const
void Tvmult(InputVectorType &dst, const InputVectorType &src) const
Number matrix_scalar_product(const Vector< Number, MemorySpace > &u, const Vector< Number, MemorySpace > &v) const
Teuchos::RCP< const TpetraTypes::MatrixType< Number, MemorySpace > > trilinos_rcp() const
Teuchos::RCP< TpetraTypes::MatrixType< Number, MemorySpace > > matrix
void reinit(const SparsityPatternType &sparsity_pattern)
SparseMatrix & operator=(const double d)
Number element(const size_type i, const size_type j, const bool no_error) const
SparseMatrix(const size_type m, const size_type n, const unsigned int n_max_entries_per_row)
SparseMatrix & operator/=(const Number factor)
Teuchos::RCP< TpetraTypes::MapType< MemorySpace > > column_space_map
SparseMatrix & operator*=(const Number factor)
bool in_local_range(const size_type index) const
MPI_Comm get_mpi_communicator() const
void add(const size_type row, const size_type n_cols, const size_type *col_indices, const Number *values, const bool elide_zero_values=true, const bool col_indices_are_sorted=false)
const_iterator begin(const size_type r) const
const_iterator begin() const
void set(const size_type row, const size_type n_cols, const size_type *col_indices, const OtherNumber *values, const bool elide_zero_values=false)
SparseMatrix(const IndexSet &row_parallel_partitioning, const IndexSet &col_parallel_partitioning, const MPI_Comm communicator, const std::vector< unsigned int > &n_entries_per_row)
SparseMatrix(const IndexSet ¶llel_partitioning, const MPI_Comm communicator, const unsigned int n_max_entries_per_row)
SparseMatrix(const SparsityPattern< MemorySpace > &sparsity_pattern)
Number diag_element(const size_type i) const
const_iterator end(const size_type r) const
Number operator()(const size_type i, const size_type j) const
void vmult(InputVectorType &dst, const InputVectorType &src) const
SparseMatrix(const size_type m, const size_type n, const std::vector< unsigned int > &n_entries_per_row)
void set(const size_type i, const size_type j, const Number value)
void copy_from(const SparseMatrix< Number, MemorySpace > &source)
iterator end(const size_type r)
SparseMatrix(const IndexSet ¶llel_partitioning, const MPI_Comm communicator, const std::vector< unsigned int > &n_entries_per_row)
std::enable_if_t< !std::is_same_v< SparsityPatternType, ::SparseMatrix< double > > > reinit(const IndexSet &row_parallel_partitioning, const IndexSet &col_parallel_partitioning, const SparsityPatternType &sparsity_pattern, const MPI_Comm communicator=MPI_COMM_WORLD, const bool exchange_data=false)
SparseMatrix(const IndexSet &row_parallel_partitioning, const IndexSet &col_parallel_partitioning, const MPI_Comm communicator, const size_type n_max_entries_per_row)
size_type memory_consumption() const
SparseMatrix< Number, MemorySpace > & operator=(const SparseMatrix< Number, MemorySpace > &)=delete
IndexSet locally_owned_range_indices() const
Teuchos::RCP< TpetraTypes::MatrixType< Number, MemorySpace > > trilinos_rcp()
size_t n_nonzero_elements() const
void Tvmult_add(InputVectorType &dst, const InputVectorType &src) const
void reinit(const SparsityPattern< MemorySpace > &sparsity_pattern)
Number el(const size_type i, const size_type j) const
void vmult_add(InputVectorType &dst, const InputVectorType &src) const
const TpetraTypes::MatrixType< Number, MemorySpace > & trilinos_matrix() const
void compress(VectorOperation::values operation)
std::enable_if_t< !std::is_same_v< SparsityPatternType, ::SparseMatrix< double > > > reinit(const IndexSet ¶llel_partitioning, const SparsityPatternType &sparsity_pattern, const MPI_Comm communicator=MPI_COMM_WORLD, const bool exchange_data=false)
void add(const Number factor, const SparseMatrix< Number, MemorySpace > &matrix)
void reinit(const SparseMatrix< Number, MemorySpace > &matrix)
void reinit(const IndexSet &row_parallel_partitioning, const IndexSet &col_parallel_partitioning, const ::SparseMatrix< Number > &dealii_sparse_matrix, const MPI_Comm communicator=MPI_COMM_WORLD, const double drop_tolerance=1e-13, const bool copy_values=true, const ::SparsityPattern *use_this_sparsity=nullptr)
bool is_compressed() const
Number matrix_norm_square(const Vector< Number, MemorySpace > &v) const
unsigned int local_size() const
real_type l2_norm() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcDomainMapMismatch()
static ::ExceptionBase & ExcColMapMismatch()
#define DeclException0(Exception0)
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
#define Assert(cond, exc)
static ::ExceptionBase & ExcMatrixNotCompressed()
static ::ExceptionBase & ExcIteratorPastEnd()
static ::ExceptionBase & ExcAccessToNonlocalRow(std::size_t arg1)
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcBeyondEndOfMatrix()
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcInvalidIndex(size_type arg1, size_type arg2)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcInvalidIndexWithinRow(size_type arg1, size_type arg2)
static ::ExceptionBase & ExcSourceEqualsDestination()
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
unsigned int global_dof_index
static const bool zero_addition_can_be_elided
forward_iterator_tag iterator_category
typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::Iterator< Number, MemorySpace, Constness >::difference_type difference_type
typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::Iterator< Number, MemorySpace, Constness >::value_type value_type