deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
trilinos_tpetra_sparse_matrix.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2024 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_trilinos_tpetra_sparse_matrix_h
14#define dealii_trilinos_tpetra_sparse_matrix_h
15
16#include <deal.II/base/config.h>
17
18#ifdef DEAL_II_TRILINOS_WITH_TPETRA
19
23
28# include <deal.II/lac/vector.h>
29
30// Tpetra includes
31# include <Tpetra_Core.hpp>
32# include <Tpetra_CrsMatrix.hpp>
33
34# include <type_traits>
35
36#endif // DEAL_II_TRILINOS_WITH_TPETRA
37
39
40#ifdef DEAL_II_TRILINOS_WITH_TPETRA
41// forward declarations
42
43template <typename Number>
44class SparseMatrix;
45
46# ifndef DOXYGEN
47template <typename MatrixType>
48class BlockMatrixBase;
49
50namespace LinearAlgebra
51{
52 namespace TpetraWrappers
53 {
54 template <typename MemorySpace>
55 class SparsityPattern;
56
57 namespace SparseMatrixIterators
58 {
59 template <typename Number, typename MemorySpace, bool Constness>
60 class Iterator;
61 }
62 } // namespace TpetraWrappers
63} // namespace LinearAlgebra
64# endif
65
66namespace LinearAlgebra
67{
68
69 namespace TpetraWrappers
70 {
109 template <typename Number, typename MemorySpace = ::MemorySpace::Host>
111 {
112 public:
117
122 std::size_t,
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.");
127
135 struct Traits
136 {
141 static const bool zero_addition_can_be_elided = true;
142 };
143
147 using iterator =
149
155
160 using value_type = Number;
161
170
175
184 const size_type n,
185 const unsigned int n_max_entries_per_row);
186
195 const size_type n,
196 const std::vector<unsigned int> &n_entries_per_row);
197
203
208
214
220
225 virtual ~SparseMatrix() override = default;
226
242 template <typename SparsityPatternType>
243 void
244 reinit(const SparsityPatternType &sparsity_pattern);
245
255 void
256 reinit(const SparsityPattern<MemorySpace> &sparsity_pattern);
257
268 void
288 SparseMatrix(const IndexSet &parallel_partitioning,
289 const MPI_Comm communicator,
290 const unsigned int n_max_entries_per_row);
291
300 SparseMatrix(const IndexSet &parallel_partitioning,
301 const MPI_Comm communicator,
302 const std::vector<unsigned int> &n_entries_per_row);
303
318 SparseMatrix(const IndexSet &row_parallel_partitioning,
319 const IndexSet &col_parallel_partitioning,
320 const MPI_Comm communicator,
321 const size_type n_max_entries_per_row);
322
331 SparseMatrix(const IndexSet &row_parallel_partitioning,
332 const IndexSet &col_parallel_partitioning,
333 const MPI_Comm communicator,
334 const std::vector<unsigned int> &n_entries_per_row);
335
355 template <typename SparsityPatternType>
356 std::enable_if_t<
357 !std::is_same_v<SparsityPatternType, ::SparseMatrix<double>>>
358 reinit(const IndexSet &parallel_partitioning,
359 const SparsityPatternType &sparsity_pattern,
360 const MPI_Comm communicator = MPI_COMM_WORLD,
361 const bool exchange_data = false);
362
375 template <typename SparsityPatternType>
376 std::enable_if_t<
377 !std::is_same_v<SparsityPatternType, ::SparseMatrix<double>>>
378 reinit(const IndexSet &row_parallel_partitioning,
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);
383
400 void
401 reinit(const IndexSet &row_parallel_partitioning,
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);
408
419 m() const;
420
425 n() const;
426
427
436 unsigned int
437 local_size() const;
438
447 std::pair<size_type, size_type>
448 local_range() const;
449
454 bool
455 in_local_range(const size_type index) const;
456
461 size_t
463
467 unsigned int
468 row_length(const size_type row) const;
469
476 bool
478
486
508 operator=(const double d);
509
514 operator*=(const Number factor);
515
520 operator/=(const Number factor);
521
525 void
527
537 void
538 add(const size_type i, const size_type j, const Number value);
539
553 void
554 add(const size_type row,
555 const size_type n_cols,
556 const size_type *col_indices,
557 const Number *values,
558 const bool elide_zero_values = true,
559 const bool col_indices_are_sorted = false);
560
568 void
569 add(const Number factor, const SparseMatrix<Number, MemorySpace> &matrix);
570
592 void
593 set(const size_type i, const size_type j, const Number value);
594
627 void
628 set(const std::vector<size_type> &indices,
629 const FullMatrix<Number> &full_matrix,
630 const bool elide_zero_values = false);
631
637 void
638 set(const std::vector<size_type> &row_indices,
639 const std::vector<size_type> &col_indices,
640 const FullMatrix<Number> &full_matrix,
641 const bool elide_zero_values = false);
642
670 void
671 set(const size_type row,
672 const std::vector<size_type> &col_indices,
673 const std::vector<Number> &values,
674 const bool elide_zero_values = false);
675
703 template <typename OtherNumber>
704 void
705 set(const size_type row,
706 const size_type n_cols,
707 const size_type *col_indices,
708 const OtherNumber *values,
709 const bool elide_zero_values = false);
710
737 void
738 clear_row(const size_type row, const Number new_diag_value = 0);
739
760 void
762 const Number new_diag_value = 0);
763
771 void
773
788 Number
789 operator()(const size_type i, const size_type j) const;
790
807 Number
808 el(const size_type i, const size_type j) const;
809
815 Number
816 diag_element(const size_type i) const;
817
823 /*
824 * Matrix-vector multiplication: let <i>dst = M*src</i> with <i>M</i>
825 * being this matrix.
826 *
827 * Source and destination must not be the same vector.
828 *
829 * The vector @p dst has to be initialized with the same IndexSet that was
830 * used for the row indices of the matrix and the vector @p src has to be
831 * initialized with the same IndexSet that was used for the column indices
832 * of the matrix.
833 */
834 template <typename InputVectorType>
835 void
836 vmult(InputVectorType &dst, const InputVectorType &src) const;
837
838 /*
839 * Matrix-vector multiplication: let <i>dst = M<sup>T</sup>*src</i> with
840 * <i>M</i> being this matrix. This function does the same as vmult() but
841 * takes the transposed matrix.
842 *
843 * Source and destination must not be the same vector.
844 */
845 template <typename InputVectorType>
846 void
847 Tvmult(InputVectorType &dst, const InputVectorType &src) const;
848
855 template <typename InputVectorType>
856 void
857 vmult_add(InputVectorType &dst, const InputVectorType &src) const;
858
866 template <typename InputVectorType>
867 void
868 Tvmult_add(InputVectorType &dst, const InputVectorType &src) const;
869
891 Number
893
913 Number
915 const Vector<Number, MemorySpace> &v) const;
916
933 Number
936 const Vector<Number, MemorySpace> &b) const;
937
949 Number
951
959 Number
960 l1_norm() const;
961
970 Number
971 linfty_norm() const;
972
986 void
987 print(std::ostream &out,
988 const bool print_detailed_trilinos_information = false) const;
989
1021 void
1023
1030 void
1032
1041
1050
1060 Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1062
1072 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
1085 IndexSet
1087
1093 IndexSet
1095
1122 begin() const;
1123
1127 iterator
1129
1135 end() const;
1136
1140 iterator
1142
1172 begin(const size_type r) const;
1173
1177 iterator
1179
1190 end(const size_type r) const;
1191
1195 iterator
1196 end(const size_type r);
1197
1208
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.");
1217
1218 /*
1219 * Exception
1220 */
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?");
1226
1227 /*
1228 * Exception
1229 */
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 "
1236 "ghost elements?");
1237
1242 size_type,
1243 size_type,
1244 << "The entry with index <" << arg1 << ',' << arg2
1245 << "> does not exist.");
1246
1251 size_type,
1252 size_type,
1253 size_type,
1254 size_type,
1255 << "You tried to access element (" << arg1 << '/' << arg2
1256 << ')'
1257 << " of a distributed matrix, but only rows in range ["
1258 << arg3 << ',' << arg4
1259 << "] are stored locally and can be accessed.");
1260
1263 private:
1267 Number
1268 element(const size_type i, const size_type j, const bool no_error) const;
1269
1279 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> column_space_map;
1280
1286 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>> matrix;
1287
1293
1306 void
1308
1316 void
1318
1319 // To allow calling protected prepare_add() and prepare_set().
1320 friend class BlockMatrixBase<SparseMatrix<Number, MemorySpace>>;
1321 }; // class SparseMatrix
1322
1327 {
1332
1340 template <typename Number, typename MemorySpace>
1342 {
1343 public:
1348
1353 const size_type row,
1354 const size_type index);
1355
1359 size_type
1360 row() const;
1361
1365 size_type
1366 index() const;
1367
1371 size_type
1372 column() const;
1373
1374 protected:
1386
1391
1397 void
1399
1412 std::shared_ptr<std::vector<::types::signed_global_dof_index>>
1414
1418 std::shared_ptr<std::vector<Number>> value_cache;
1419
1420 private:
1421 friend class Iterator<Number, MemorySpace, false>;
1422 friend class Iterator<Number, MemorySpace, true>;
1423 };
1424
1435 template <typename Number, typename MemorySpace, bool Constness>
1436 class Accessor : public AccessorBase<Number, MemorySpace>
1437 {
1441 Number
1442 value() const;
1443
1447 Number &
1449 };
1450
1454 template <typename Number, typename MemorySpace>
1455 class Accessor<Number, MemorySpace, true>
1456 : public AccessorBase<Number, MemorySpace>
1457 {
1458 public:
1464
1469
1475 const size_type row,
1476 const size_type index);
1477
1482 template <bool Other>
1484
1488 Number
1489 value() const;
1490 };
1491
1495 template <typename Number, typename MemorySpace>
1496 class Accessor<Number, MemorySpace, false>
1497 : public AccessorBase<Number, MemorySpace>
1498 {
1499 class Reference
1500 {
1501 public:
1505 Reference(const Accessor<Number, MemorySpace, false> &accessor);
1506
1510 operator Number() const;
1511
1515 const Reference &
1516 operator=(const Number n) const;
1517
1521 const Reference &
1522 operator+=(const Number n) const;
1523
1527 const Reference &
1528 operator-=(const Number n) const;
1529
1533 const Reference &
1534 operator*=(const Number n) const;
1535
1539 const Reference &
1540 operator/=(const Number n) const;
1541
1542 private:
1548 };
1549
1550 public:
1556
1561
1567 const size_type row,
1568 const size_type index);
1569
1573 Reference
1574 value() const;
1575
1576 private:
1577 // Make Reference object a friend.
1578 friend class Reference;
1579 };
1580
1594 template <typename Number, typename MemorySpace, bool Constness>
1596 {
1597 public:
1602
1608
1613 using value_type = Number;
1614
1621
1626 Iterator(MatrixType *matrix,
1627 const size_type row,
1628 const size_type index);
1629
1633 template <bool Other>
1635
1640 operator++();
1641
1646 operator++(int);
1647
1652 operator*() const;
1653
1658 operator->() const;
1659
1664 template <bool OtherConstness>
1665 bool
1667
1671 template <bool OtherConstness>
1672 bool
1674
1680 template <bool OtherConstness>
1681 bool
1683
1687 template <bool OtherConstness>
1688 bool
1690
1695 size_type,
1696 size_type,
1697 << "Attempt to access element " << arg2 << " of row "
1698 << arg1 << " which doesn't have that many elements.");
1699
1700 private:
1705
1706 friend class Iterator<Number, MemorySpace, true>;
1707 friend class Iterator<Number, MemorySpace, false>;
1708 };
1709 } // namespace SparseMatrixIterators
1710
1711 } // namespace TpetraWrappers
1712
1713} // namespace LinearAlgebra
1714
1715
1716DEAL_II_NAMESPACE_CLOSE // Do not convert for module purposes
1717
1718 namespace std
1719{
1720 template <typename Number, typename MemorySpace, bool Constness>
1723 Iterator<Number, MemorySpace, Constness>>
1724 {
1725 using iterator_category = forward_iterator_tag;
1727 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1728 Iterator<Number, MemorySpace, Constness>::value_type;
1730 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1731 Iterator<Number, MemorySpace, Constness>::difference_type;
1732 };
1733} // namespace std
1734
1735/* ------------------------- Inline functions ---------------------- */
1736
1737
1738DEAL_II_NAMESPACE_OPEN // Do not convert for module purposes
1739
1740 namespace LinearAlgebra
1741{
1742 namespace TpetraWrappers
1743 {
1744 template <typename Number, typename MemorySpace>
1745 inline void
1747 const size_type j,
1748 const Number value)
1749 {
1750 set(i, 1, &j, &value, false);
1751 }
1752
1753
1754
1755 template <typename Number, typename MemorySpace>
1756 inline void
1758 const size_type j,
1759 const Number value)
1760 {
1761 add(i, 1, &j, &value, false);
1762 }
1763
1764
1765
1766 template <typename Number, typename MemorySpace>
1767 inline Number
1771 const Vector<Number, MemorySpace> &b) const
1772 {
1773 vmult(dst, x);
1774 dst -= b;
1775 dst *= -1.;
1776
1777 return dst.l2_norm();
1778 }
1779
1780
1781
1782 template <typename Number, typename MemorySpace>
1785 {
1786 return matrix->getRowMap()->getGlobalNumElements();
1787 }
1788
1789
1790
1791 template <typename Number, typename MemorySpace>
1794 {
1795 // If the matrix structure has not been fixed (i.e., we did not have a
1796 // sparsity pattern), it does not know about the number of columns, so we
1797 // must always take this from the additional column space map
1798 Assert(column_space_map.get() != nullptr, ExcInternalError());
1799 return column_space_map->getGlobalNumElements();
1800 }
1801
1802
1803
1804 template <typename Number, typename MemorySpace>
1807 {
1808 size_type static_memory =
1809 sizeof(*this) + sizeof(*matrix) + sizeof(matrix->getGraph().get());
1810 return ((sizeof(Number) + sizeof(TrilinosWrappers::types::int_type)) *
1811# if DEAL_II_TRILINOS_VERSION_GTE(13, 4, 0)
1812 matrix->getLocalNumEntries() +
1813# else
1814 matrix->getNodeNumEntries() +
1815# endif
1816 sizeof(int) * local_size() + static_memory);
1817 }
1818
1819
1820
1821 template <typename Number, typename MemorySpace>
1822 inline bool
1824 {
1825 return compressed;
1826 }
1827
1828
1829
1830 template <typename Number, typename MemorySpace>
1833 {
1834 return begin(0);
1835 }
1836
1837
1838
1839 template <typename Number, typename MemorySpace>
1842 {
1843 return const_iterator(this, m(), 0);
1844 }
1845
1846
1847
1848 template <typename Number, typename MemorySpace>
1851 {
1852 AssertIndexRange(r, m());
1853 if (in_local_range(r) && (row_length(r) > 0))
1854 return const_iterator(this, r, 0);
1855 else
1856 return end(r);
1857 }
1858
1859
1860 template <typename Number, typename MemorySpace>
1863 {
1864 AssertIndexRange(r, m());
1865
1866 // place the iterator on the first entry
1867 // past this line, or at the end of the
1868 // matrix
1869 for (size_type i = r + 1; i < m(); ++i)
1870 if (in_local_range(i) && (row_length(i) > 0))
1871 return const_iterator(this, i, 0);
1872
1873 // if there is no such line, then take the
1874 // end iterator of the matrix
1875 return end();
1876 }
1877
1878
1879
1880 template <typename Number, typename MemorySpace>
1883 {
1884 return begin(0);
1885 }
1886
1887
1888
1889 template <typename Number, typename MemorySpace>
1892 {
1893 return iterator(this, m(), 0);
1894 }
1895
1896
1897
1898 template <typename Number, typename MemorySpace>
1901 {
1902 AssertIndexRange(r, m());
1903 if (in_local_range(r) && (row_length(r) > 0))
1904 return iterator(this, r, 0);
1905 else
1906 return end(r);
1907 }
1908
1909
1910
1911 template <typename Number, typename MemorySpace>
1914 {
1915 AssertIndexRange(r, m());
1916
1917 // place the iterator on the first entry
1918 // past this line, or at the end of the
1919 // matrix
1920 for (size_type i = r + 1; i < m(); ++i)
1921 if (in_local_range(i) && (row_length(i) > 0))
1922 return iterator(this, i, 0);
1923
1924 // if there is no such line, then take the
1925 // end iterator of the matrix
1926 return end();
1927 }
1928
1929
1930
1931 template <typename Number, typename MemorySpace>
1932 inline bool
1934 const size_type index) const
1935 {
1936 const size_type begin = matrix->getRowMap()->getMinGlobalIndex();
1937 const size_type end = matrix->getRowMap()->getMaxGlobalIndex() + 1;
1938
1939 return ((index >= begin) && (index < end));
1940 }
1941
1942
1943
1944 template <typename Number, typename MemorySpace>
1945 unsigned int
1947 {
1948 auto n_entries = matrix->getNumEntriesInGlobalRow(row);
1949 Assert(n_entries !=
1950 Teuchos::OrdinalTraits<decltype(n_entries)>::invalid(),
1951 ExcAccessToNonlocalRow(row));
1952
1953 return n_entries;
1954 }
1955
1956
1957
1958 template <typename Number, typename MemorySpace>
1959 inline void
1961 {
1962 // nothing to do here
1963 }
1964
1965
1966
1967 template <typename Number, typename MemorySpace>
1968 inline void
1970 {
1971 // nothing to do here
1972 }
1973
1974
1975
1976 template <typename Number, typename MemorySpace>
1979 {
1980 return *matrix;
1981 }
1982
1983
1984
1985 template <typename Number, typename MemorySpace>
1988 {
1989 return *matrix;
1990 }
1991
1992
1993
1994 template <typename Number, typename MemorySpace>
1995 inline Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1997 {
1998 return matrix.getConst();
1999 }
2000
2001
2002
2003 template <typename Number, typename MemorySpace>
2004 inline Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
2006 {
2007 return matrix;
2008 }
2009
2010
2011
2012 template <typename Number, typename MemorySpace>
2013 inline IndexSet
2015 {
2016 return IndexSet(matrix->getDomainMap());
2017 }
2018
2019
2020
2021 template <typename Number, typename MemorySpace>
2022 inline IndexSet
2024 {
2025 return IndexSet(matrix->getRangeMap());
2026 }
2027
2028
2029 namespace SparseMatrixIterators
2030 {
2031 template <typename Number, typename MemorySpace>
2034 size_type row,
2035 size_type index)
2036 : matrix(matrix)
2037 , a_row(row)
2038 , a_index(index)
2039 {
2041 }
2042
2043
2044 template <typename Number, typename MemorySpace>
2047 {
2048 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2049 return a_row;
2050 }
2051
2052
2053 template <typename Number, typename MemorySpace>
2056 {
2057 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2058 return (*colnum_cache)[a_index];
2059 }
2060
2061
2062 template <typename Number, typename MemorySpace>
2065 {
2066 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2067 return a_index;
2068 }
2069
2070
2071
2072 template <typename Number, typename MemorySpace>
2073 void
2075 {
2076 // if we are asked to visit the past-the-end line, then simply
2077 // release all our caches and go on with life.
2078 //
2079 // do the same if the row we're supposed to visit is not locally
2080 // owned. this is simply going to make non-locally owned rows
2081 // look like they're empty
2082 if ((this->a_row == matrix->m()) ||
2083 (matrix->in_local_range(this->a_row) == false))
2084 {
2085 colnum_cache.reset();
2086 value_cache.reset();
2087
2088 return;
2089 }
2090
2091 // get a representation of the present row
2092 size_t ncols;
2094 matrix->row_length(this->a_row);
2095 if (value_cache.get() == nullptr)
2096 {
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);
2100 }
2101 else
2102 {
2103 value_cache->resize(colnums);
2104 colnum_cache->resize(colnums);
2105 }
2106
2108 nonconst_global_inds_host_view_type col_indices(colnum_cache->data(),
2109 colnums);
2111 nonconst_values_host_view_type values(value_cache->data(), colnums);
2112
2113 matrix->trilinos_matrix().getGlobalRowCopy(this->a_row,
2114 col_indices,
2115 values,
2116 ncols);
2117
2118 AssertDimension(ncols, colnums);
2119
2120 // copy it into our caches if the
2121 // line isn't empty. if it is, then
2122 // we've done something wrong, since
2123 // we shouldn't have initialized an
2124 // iterator for an empty line (what
2125 // would it point to?)
2126 }
2127
2128
2129
2130 template <typename Number, typename MemorySpace>
2132 MatrixType *matrix,
2133 const size_type row,
2134 const size_type index)
2135 : AccessorBase<Number, MemorySpace>(
2136 const_cast<SparseMatrix<Number, MemorySpace> *>(matrix),
2137 row,
2138 index)
2139 {}
2140
2141
2142
2143 template <typename Number, typename MemorySpace>
2144 template <bool Other>
2149
2150
2151
2152 template <typename Number, typename MemorySpace>
2153 inline Number
2162
2163
2164
2165 template <typename Number, typename MemorySpace>
2168 : accessor(const_cast<Accessor<Number, MemorySpace, false> &>(acc))
2169 {}
2170
2171
2172
2173 template <typename Number, typename MemorySpace>
2175 const
2176 {
2177 return (*accessor.value_cache)[accessor.a_index];
2178 }
2179
2180
2181
2182 template <typename Number, typename MemorySpace>
2185 const Number n) const
2186 {
2187 (*accessor.value_cache)[accessor.a_index] = n;
2188 accessor.matrix->set(accessor.row(),
2189 accessor.column(),
2190 static_cast<Number>(*this));
2191 return *this;
2192 }
2193
2194
2195
2196 template <typename Number, typename MemorySpace>
2199 const Number n) const
2200 {
2201 (*accessor.value_cache)[accessor.a_index] += n;
2202 accessor.matrix->set(accessor.row(),
2203 accessor.column(),
2204 static_cast<Number>(*this));
2205 return *this;
2206 }
2207
2208
2209
2210 template <typename Number, typename MemorySpace>
2213 const Number n) const
2214 {
2215 (*accessor.value_cache)[accessor.a_index] -= n;
2216 accessor.matrix->set(accessor.row(),
2217 accessor.column(),
2218 static_cast<Number>(*this));
2219 return *this;
2220 }
2221
2222
2223 template <typename Number, typename MemorySpace>
2226 const Number n) const
2227 {
2228 (*accessor.value_cache)[accessor.a_index] *= n;
2229 accessor.matrix->set(accessor.row(),
2230 accessor.column(),
2231 static_cast<Number>(*this));
2232 return *this;
2233 }
2234
2235
2236 template <typename Number, typename MemorySpace>
2239 const Number n) const
2240 {
2241 (*accessor.value_cache)[accessor.a_index] /= n;
2242 accessor.matrix->set(accessor.row(),
2243 accessor.column(),
2244 static_cast<Number>(*this));
2245 return *this;
2246 }
2247
2248
2249 template <typename Number, typename MemorySpace>
2256
2257
2258 template <typename Number, typename MemorySpace>
2267
2268
2269
2270 template <typename Number, typename MemorySpace, bool Constness>
2272 MatrixType *matrix,
2273 const size_type row,
2274 const size_type index)
2275 : accessor(matrix, row, index)
2276 {}
2277
2278
2279 template <typename Number, typename MemorySpace, bool Constness>
2280 template <bool Other>
2283 : accessor(other.accessor)
2284 {}
2285
2286
2287
2288 template <typename Number, typename MemorySpace, bool Constness>
2291 {
2292 Assert(accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd());
2293
2294 ++accessor.a_index;
2295
2296 // If at end of line: do one
2297 // step, then cycle until we
2298 // find a row with a nonzero
2299 // number of entries.
2300 if (accessor.a_index >= accessor.colnum_cache->size())
2301 {
2302 accessor.a_index = 0;
2303 ++accessor.a_row;
2304
2305 while (
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)))
2309 ++accessor.a_row;
2310
2311 accessor.visit_present_row();
2312 }
2313 return *this;
2314 }
2315
2316
2317 template <typename Number, typename MemorySpace, bool Constness>
2320 {
2321 const Iterator<Number, MemorySpace, Constness> old_state = *this;
2322 ++(*this);
2323 return old_state;
2324 }
2325
2326
2327
2328 template <typename Number, typename MemorySpace, bool Constness>
2331 {
2332 return accessor;
2333 }
2334
2335
2336
2337 template <typename Number, typename MemorySpace, bool Constness>
2340 {
2341 return &accessor;
2342 }
2343
2344
2345
2346 template <typename Number, typename MemorySpace, bool Constness>
2347 template <bool OtherConstness>
2348 inline bool
2351 {
2352 return (accessor.a_row == other.accessor.a_row &&
2353 accessor.a_index == other.accessor.a_index);
2354 }
2355
2356
2357
2358 template <typename Number, typename MemorySpace, bool Constness>
2359 template <bool OtherConstness>
2360 inline bool
2363 {
2364 return !(*this == other);
2365 }
2366
2367
2368
2369 template <typename Number, typename MemorySpace, bool Constness>
2370 template <bool OtherConstness>
2371 inline bool
2374 {
2375 return (accessor.row() < other.accessor.row() ||
2376 (accessor.row() == other.accessor.row() &&
2377 accessor.index() < other.accessor.index()));
2378 }
2379
2380
2381 template <typename Number, typename MemorySpace, bool Constness>
2382 template <bool OtherConstness>
2383 inline bool
2386 {
2387 return (other < *this);
2388 }
2389
2390 } // namespace SparseMatrixIterators
2391 } // namespace TpetraWrappers
2392
2393} // namespace LinearAlgebra
2394
2395#endif // DEAL_II_TRILINOS_WITH_TPETRA
2396
2398
2399#endif // dealii_trilinos_tpetra_sparse_matrix_h
*  iterator end()
*  *  iterator begin()
*  x_component_mask set(0, true)
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)
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)
const Accessor< Number, MemorySpace, Constness > * operator->() const
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
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)
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
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)
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)
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)
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 &parallel_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
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)
SparseMatrix(const IndexSet &parallel_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)
SparseMatrix< Number, MemorySpace > & operator=(const SparseMatrix< Number, MemorySpace > &)=delete
Teuchos::RCP< TpetraTypes::MatrixType< Number, MemorySpace > > trilinos_rcp()
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 &parallel_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)
Number matrix_norm_square(const Vector< Number, MemorySpace > &v) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
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)
#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
STL namespace.
unsigned int global_dof_index
Definition types.h:92
typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::Iterator< Number, MemorySpace, Constness >::difference_type difference_type
typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::Iterator< Number, MemorySpace, Constness >::value_type value_type