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);
840 template <
typename InputVectorType>
842 vmult(InputVectorType &dst,
const InputVectorType &src)
const;
851 template <
typename InputVectorType>
853 Tvmult(InputVectorType &dst,
const InputVectorType &src)
const;
861 template <
typename InputVectorType>
863 vmult_add(InputVectorType &dst,
const InputVectorType &src)
const;
872 template <
typename InputVectorType>
874 Tvmult_add(InputVectorType &dst,
const InputVectorType &src)
const;
994 const bool print_detailed_trilinos_information =
false)
const;
1066 Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1078 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
1220 "You are attempting an operation on two vectors that "
1221 "are the same object, but the operation requires that the "
1222 "two objects are in fact different.");
1228 "The column partitioning of a matrix does not match "
1229 "the partitioning of a vector you are trying to "
1230 "multiply it with. Are you multiplying the "
1231 "matrix with a vector that has ghost elements?");
1237 "The row partitioning of a matrix does not match "
1238 "the partitioning of a vector you are trying to "
1239 "put the result of a matrix-vector product in. "
1240 "Are you trying to put the product of the "
1241 "matrix with a vector into a vector that has "
1250 <<
"The entry with index <" << arg1 <<
',' << arg2
1251 <<
"> does not exist.");
1261 <<
"You tried to access element (" << arg1 <<
'/' << arg2
1263 <<
" of a distributed matrix, but only rows in range ["
1264 << arg3 <<
',' << arg4
1265 <<
"] are stored locally and can be accessed.");
1292 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
matrix;
1346 template <
typename Number,
typename MemorySpace>
1418 std::shared_ptr<std::vector<::types::signed_global_dof_index>>
1441 template <
typename Number,
typename MemorySpace,
bool Constness>
1460 template <
typename Number,
typename MemorySpace>
1488 template <
bool Other>
1501 template <
typename Number,
typename MemorySpace>
1516 operator Number()
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;
1546 operator/=(
const Number n)
const;
1584 friend class Reference;
1600 template <
typename Number,
typename MemorySpace,
bool Constness>
1639 template <
bool Other>
1670 template <
bool OtherConstness>
1677 template <
bool OtherConstness>
1686 template <
bool OtherConstness>
1693 template <
bool OtherConstness>
1703 <<
"Attempt to access element " << arg2 <<
" of row "
1704 << arg1 <<
" which doesn't have that many elements.");
1726 template <
typename Number,
typename MemorySpace,
bool Constness>
1729 Iterator<Number, MemorySpace, Constness>>
1733 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1734 Iterator<Number, MemorySpace, Constness>::value_type;
1736 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1737 Iterator<Number, MemorySpace, Constness>::difference_type;
1748 namespace TpetraWrappers
1750 template <
typename Number,
typename MemorySpace>
1756 set(i, 1, &j, &value,
false);
1761 template <
typename Number,
typename MemorySpace>
1767 add(i, 1, &j, &value,
false);
1772 template <
typename Number,
typename MemorySpace>
1788 template <
typename Number,
typename MemorySpace>
1792 return matrix->getRowMap()->getGlobalNumElements();
1797 template <
typename Number,
typename MemorySpace>
1805 return column_space_map->getGlobalNumElements();
1810 template <
typename Number,
typename MemorySpace>
1815 sizeof(*this) +
sizeof(*matrix) +
sizeof(matrix->getGraph().get());
1817# if DEAL_II_TRILINOS_VERSION_GTE(13, 4, 0)
1818 matrix->getLocalNumEntries() +
1820 matrix->getNodeNumEntries() +
1822 sizeof(
int) * local_size() + static_memory);
1827 template <
typename Number,
typename MemorySpace>
1836 template <
typename Number,
typename MemorySpace>
1845 template <
typename Number,
typename MemorySpace>
1854 template <
typename Number,
typename MemorySpace>
1859 if (in_local_range(r) && (row_length(r) > 0))
1866 template <
typename Number,
typename MemorySpace>
1876 if (in_local_range(i) && (row_length(i) > 0))
1886 template <
typename Number,
typename MemorySpace>
1895 template <
typename Number,
typename MemorySpace>
1904 template <
typename Number,
typename MemorySpace>
1909 if (in_local_range(r) && (row_length(r) > 0))
1917 template <
typename Number,
typename MemorySpace>
1927 if (in_local_range(i) && (row_length(i) > 0))
1937 template <
typename Number,
typename MemorySpace>
1942 const size_type begin = matrix->getRowMap()->getMinGlobalIndex();
1943 const size_type end = matrix->getRowMap()->getMaxGlobalIndex() + 1;
1945 return ((index >=
begin) && (index <
end));
1950 template <
typename Number,
typename MemorySpace>
1954 auto n_entries = matrix->getNumEntriesInGlobalRow(row);
1956 Teuchos::OrdinalTraits<
decltype(n_entries)>::invalid(),
1957 ExcAccessToNonlocalRow(row));
1964 template <
typename Number,
typename MemorySpace>
1973 template <
typename Number,
typename MemorySpace>
1982 template <
typename Number,
typename MemorySpace>
1991 template <
typename Number,
typename MemorySpace>
2000 template <
typename Number,
typename MemorySpace>
2001 inline Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
2004 return matrix.getConst();
2009 template <
typename Number,
typename MemorySpace>
2010 inline Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
2018 template <
typename Number,
typename MemorySpace>
2022 return IndexSet(matrix->getDomainMap());
2027 template <
typename Number,
typename MemorySpace>
2031 return IndexSet(matrix->getRangeMap());
2037 template <
typename Number,
typename MemorySpace>
2050 template <
typename Number,
typename MemorySpace>
2059 template <
typename Number,
typename MemorySpace>
2064 return (*colnum_cache)[a_index];
2068 template <
typename Number,
typename MemorySpace>
2078 template <
typename Number,
typename MemorySpace>
2088 if ((this->a_row == matrix->m()) ||
2089 (matrix->in_local_range(this->a_row) ==
false))
2091 colnum_cache.reset();
2092 value_cache.reset();
2100 matrix->row_length(this->a_row);
2101 if (value_cache.get() ==
nullptr)
2103 value_cache = std::make_shared<std::vector<Number>>(colnums);
2104 colnum_cache = std::make_shared<
2105 std::vector<::types::signed_global_dof_index>>(colnums);
2109 value_cache->resize(colnums);
2110 colnum_cache->resize(colnums);
2114 nonconst_global_inds_host_view_type col_indices(colnum_cache->data(),
2117 nonconst_values_host_view_type values(value_cache->data(), colnums);
2119 matrix->trilinos_matrix().getGlobalRowCopy(this->a_row,
2136 template <
typename Number,
typename MemorySpace>
2149 template <
typename Number,
typename MemorySpace>
2150 template <
bool Other>
2158 template <
typename Number,
typename MemorySpace>
2171 template <
typename Number,
typename MemorySpace>
2179 template <
typename Number,
typename MemorySpace>
2183 return (*accessor.value_cache)[accessor.a_index];
2188 template <
typename Number,
typename MemorySpace>
2191 const Number n)
const
2193 (*accessor.value_cache)[accessor.a_index] = n;
2194 accessor.matrix->set(accessor.row(),
2196 static_cast<Number
>(*
this));
2202 template <
typename Number,
typename MemorySpace>
2205 const Number n)
const
2207 (*accessor.value_cache)[accessor.a_index] += n;
2208 accessor.matrix->set(accessor.row(),
2210 static_cast<Number
>(*
this));
2216 template <
typename Number,
typename MemorySpace>
2219 const Number n)
const
2221 (*accessor.value_cache)[accessor.a_index] -= n;
2222 accessor.matrix->set(accessor.row(),
2224 static_cast<Number
>(*
this));
2229 template <
typename Number,
typename MemorySpace>
2232 const Number n)
const
2234 (*accessor.value_cache)[accessor.a_index] *= n;
2235 accessor.matrix->set(accessor.row(),
2237 static_cast<Number
>(*
this));
2242 template <
typename Number,
typename MemorySpace>
2245 const Number n)
const
2247 (*accessor.value_cache)[accessor.a_index] /= n;
2248 accessor.matrix->set(accessor.row(),
2250 static_cast<Number
>(*
this));
2255 template <
typename Number,
typename MemorySpace>
2264 template <
typename Number,
typename MemorySpace>
2276 template <
typename Number,
typename MemorySpace,
bool Constness>
2281 : accessor(matrix, row, index)
2285 template <
typename Number,
typename MemorySpace,
bool Constness>
2286 template <
bool Other>
2289 : accessor(other.accessor)
2294 template <
typename Number,
typename MemorySpace,
bool Constness>
2306 if (accessor.a_index >= accessor.colnum_cache->size())
2308 accessor.a_index = 0;
2312 (accessor.a_row < accessor.matrix->m()) &&
2313 ((accessor.matrix->in_local_range(accessor.a_row) ==
false) ||
2314 (accessor.matrix->row_length(accessor.a_row) == 0)))
2317 accessor.visit_present_row();
2323 template <
typename Number,
typename MemorySpace,
bool Constness>
2334 template <
typename Number,
typename MemorySpace,
bool Constness>
2343 template <
typename Number,
typename MemorySpace,
bool Constness>
2352 template <
typename Number,
typename MemorySpace,
bool Constness>
2353 template <
bool OtherConstness>
2358 return (accessor.a_row == other.
accessor.a_row &&
2359 accessor.a_index == other.
accessor.a_index);
2364 template <
typename Number,
typename MemorySpace,
bool Constness>
2365 template <
bool OtherConstness>
2370 return !(*
this == other);
2375 template <
typename Number,
typename MemorySpace,
bool Constness>
2376 template <
bool OtherConstness>
2381 return (accessor.row() < other.
accessor.row() ||
2382 (accessor.row() == other.
accessor.row() &&
2383 accessor.index() < other.
accessor.index()));
2387 template <
typename Number,
typename MemorySpace,
bool Constness>
2388 template <
bool OtherConstness>
2393 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