deal.II version GIT relicensing-7058-gb35ee29762 2026-11-11 09:30: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
840 template <typename InputVectorType>
841 void
842 vmult(InputVectorType &dst, const InputVectorType &src) const;
843
851 template <typename InputVectorType>
852 void
853 Tvmult(InputVectorType &dst, const InputVectorType &src) const;
854
861 template <typename InputVectorType>
862 void
863 vmult_add(InputVectorType &dst, const InputVectorType &src) const;
864
872 template <typename InputVectorType>
873 void
874 Tvmult_add(InputVectorType &dst, const InputVectorType &src) const;
875
897 Number
899
919 Number
921 const Vector<Number, MemorySpace> &v) const;
922
939 Number
942 const Vector<Number, MemorySpace> &b) const;
943
955 Number
957
965 Number
966 l1_norm() const;
967
976 Number
977 linfty_norm() const;
978
992 void
993 print(std::ostream &out,
994 const bool print_detailed_trilinos_information = false) const;
995
1027 void
1029
1036 void
1038
1047
1056
1066 Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
1068
1078 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
1091 IndexSet
1093
1099 IndexSet
1101
1128 begin() const;
1129
1133 iterator
1135
1141 end() const;
1142
1146 iterator
1148
1178 begin(const size_type r) const;
1179
1183 iterator
1185
1196 end(const size_type r) const;
1197
1201 iterator
1202 end(const size_type r);
1203
1214
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.");
1223
1224 /*
1225 * Exception
1226 */
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?");
1232
1233 /*
1234 * Exception
1235 */
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 "
1242 "ghost elements?");
1243
1248 size_type,
1249 size_type,
1250 << "The entry with index <" << arg1 << ',' << arg2
1251 << "> does not exist.");
1252
1257 size_type,
1258 size_type,
1259 size_type,
1260 size_type,
1261 << "You tried to access element (" << arg1 << '/' << arg2
1262 << ')'
1263 << " of a distributed matrix, but only rows in range ["
1264 << arg3 << ',' << arg4
1265 << "] are stored locally and can be accessed.");
1266
1269 private:
1273 Number
1274 element(const size_type i, const size_type j, const bool no_error) const;
1275
1285 Teuchos::RCP<TpetraTypes::MapType<MemorySpace>> column_space_map;
1286
1292 Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>> matrix;
1293
1299
1312 void
1314
1322 void
1324
1325 // To allow calling protected prepare_add() and prepare_set().
1326 friend class BlockMatrixBase<SparseMatrix<Number, MemorySpace>>;
1327 }; // class SparseMatrix
1328
1333 {
1338
1346 template <typename Number, typename MemorySpace>
1348 {
1349 public:
1354
1359 const size_type row,
1360 const size_type index);
1361
1365 size_type
1366 row() const;
1367
1371 size_type
1372 index() const;
1373
1377 size_type
1378 column() const;
1379
1380 protected:
1392
1397
1403 void
1405
1418 std::shared_ptr<std::vector<::types::signed_global_dof_index>>
1420
1424 std::shared_ptr<std::vector<Number>> value_cache;
1425
1426 private:
1427 friend class Iterator<Number, MemorySpace, false>;
1428 friend class Iterator<Number, MemorySpace, true>;
1429 };
1430
1441 template <typename Number, typename MemorySpace, bool Constness>
1442 class Accessor : public AccessorBase<Number, MemorySpace>
1443 {
1447 Number
1448 value() const;
1449
1453 Number &
1455 };
1456
1460 template <typename Number, typename MemorySpace>
1461 class Accessor<Number, MemorySpace, true>
1462 : public AccessorBase<Number, MemorySpace>
1463 {
1464 public:
1470
1475
1481 const size_type row,
1482 const size_type index);
1483
1488 template <bool Other>
1490
1494 Number
1495 value() const;
1496 };
1497
1501 template <typename Number, typename MemorySpace>
1502 class Accessor<Number, MemorySpace, false>
1503 : public AccessorBase<Number, MemorySpace>
1504 {
1505 class Reference
1506 {
1507 public:
1511 Reference(const Accessor<Number, MemorySpace, false> &accessor);
1512
1516 operator Number() 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
1545 const Reference &
1546 operator/=(const Number n) const;
1547
1548 private:
1554 };
1555
1556 public:
1562
1567
1573 const size_type row,
1574 const size_type index);
1575
1579 Reference
1580 value() const;
1581
1582 private:
1583 // Make Reference object a friend.
1584 friend class Reference;
1585 };
1586
1600 template <typename Number, typename MemorySpace, bool Constness>
1602 {
1603 public:
1608
1614
1619 using value_type = Number;
1620
1627
1632 Iterator(MatrixType *matrix,
1633 const size_type row,
1634 const size_type index);
1635
1639 template <bool Other>
1641
1646 operator++();
1647
1652 operator++(int);
1653
1658 operator*() const;
1659
1664 operator->() const;
1665
1670 template <bool OtherConstness>
1671 bool
1673
1677 template <bool OtherConstness>
1678 bool
1680
1686 template <bool OtherConstness>
1687 bool
1689
1693 template <bool OtherConstness>
1694 bool
1696
1701 size_type,
1702 size_type,
1703 << "Attempt to access element " << arg2 << " of row "
1704 << arg1 << " which doesn't have that many elements.");
1705
1706 private:
1711
1712 friend class Iterator<Number, MemorySpace, true>;
1713 friend class Iterator<Number, MemorySpace, false>;
1714 };
1715 } // namespace SparseMatrixIterators
1716
1717 } // namespace TpetraWrappers
1718
1719} // namespace LinearAlgebra
1720
1721
1722DEAL_II_NAMESPACE_CLOSE // Do not convert for module purposes
1723
1724 namespace std
1725{
1726 template <typename Number, typename MemorySpace, bool Constness>
1729 Iterator<Number, MemorySpace, Constness>>
1730 {
1731 using iterator_category = forward_iterator_tag;
1733 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1734 Iterator<Number, MemorySpace, Constness>::value_type;
1736 typename ::LinearAlgebra::TpetraWrappers::SparseMatrixIterators::
1737 Iterator<Number, MemorySpace, Constness>::difference_type;
1738 };
1739} // namespace std
1740
1741/* ------------------------- Inline functions ---------------------- */
1742
1743
1744DEAL_II_NAMESPACE_OPEN // Do not convert for module purposes
1745
1746 namespace LinearAlgebra
1747{
1748 namespace TpetraWrappers
1749 {
1750 template <typename Number, typename MemorySpace>
1751 inline void
1753 const size_type j,
1754 const Number value)
1755 {
1756 set(i, 1, &j, &value, false);
1757 }
1758
1759
1760
1761 template <typename Number, typename MemorySpace>
1762 inline void
1764 const size_type j,
1765 const Number value)
1766 {
1767 add(i, 1, &j, &value, false);
1768 }
1769
1770
1771
1772 template <typename Number, typename MemorySpace>
1773 inline Number
1777 const Vector<Number, MemorySpace> &b) const
1778 {
1779 vmult(dst, x);
1780 dst -= b;
1781 dst *= -1.;
1782
1783 return dst.l2_norm();
1784 }
1785
1786
1787
1788 template <typename Number, typename MemorySpace>
1791 {
1792 return matrix->getRowMap()->getGlobalNumElements();
1793 }
1794
1795
1796
1797 template <typename Number, typename MemorySpace>
1800 {
1801 // If the matrix structure has not been fixed (i.e., we did not have a
1802 // sparsity pattern), it does not know about the number of columns, so we
1803 // must always take this from the additional column space map
1804 Assert(column_space_map.get() != nullptr, ExcInternalError());
1805 return column_space_map->getGlobalNumElements();
1806 }
1807
1808
1809
1810 template <typename Number, typename MemorySpace>
1813 {
1814 size_type static_memory =
1815 sizeof(*this) + sizeof(*matrix) + sizeof(matrix->getGraph().get());
1816 return ((sizeof(Number) + sizeof(TrilinosWrappers::types::int_type)) *
1817# if DEAL_II_TRILINOS_VERSION_GTE(13, 4, 0)
1818 matrix->getLocalNumEntries() +
1819# else
1820 matrix->getNodeNumEntries() +
1821# endif
1822 sizeof(int) * local_size() + static_memory);
1823 }
1824
1825
1826
1827 template <typename Number, typename MemorySpace>
1828 inline bool
1830 {
1831 return compressed;
1832 }
1833
1834
1835
1836 template <typename Number, typename MemorySpace>
1839 {
1840 return begin(0);
1841 }
1842
1843
1844
1845 template <typename Number, typename MemorySpace>
1848 {
1849 return const_iterator(this, m(), 0);
1850 }
1851
1852
1853
1854 template <typename Number, typename MemorySpace>
1857 {
1858 AssertIndexRange(r, m());
1859 if (in_local_range(r) && (row_length(r) > 0))
1860 return const_iterator(this, r, 0);
1861 else
1862 return end(r);
1863 }
1864
1865
1866 template <typename Number, typename MemorySpace>
1869 {
1870 AssertIndexRange(r, m());
1871
1872 // place the iterator on the first entry
1873 // past this line, or at the end of the
1874 // matrix
1875 for (size_type i = r + 1; i < m(); ++i)
1876 if (in_local_range(i) && (row_length(i) > 0))
1877 return const_iterator(this, i, 0);
1878
1879 // if there is no such line, then take the
1880 // end iterator of the matrix
1881 return end();
1882 }
1883
1884
1885
1886 template <typename Number, typename MemorySpace>
1889 {
1890 return begin(0);
1891 }
1892
1893
1894
1895 template <typename Number, typename MemorySpace>
1898 {
1899 return iterator(this, m(), 0);
1900 }
1901
1902
1903
1904 template <typename Number, typename MemorySpace>
1907 {
1908 AssertIndexRange(r, m());
1909 if (in_local_range(r) && (row_length(r) > 0))
1910 return iterator(this, r, 0);
1911 else
1912 return end(r);
1913 }
1914
1915
1916
1917 template <typename Number, typename MemorySpace>
1920 {
1921 AssertIndexRange(r, m());
1922
1923 // place the iterator on the first entry
1924 // past this line, or at the end of the
1925 // matrix
1926 for (size_type i = r + 1; i < m(); ++i)
1927 if (in_local_range(i) && (row_length(i) > 0))
1928 return iterator(this, i, 0);
1929
1930 // if there is no such line, then take the
1931 // end iterator of the matrix
1932 return end();
1933 }
1934
1935
1936
1937 template <typename Number, typename MemorySpace>
1938 inline bool
1940 const size_type index) const
1941 {
1942 const size_type begin = matrix->getRowMap()->getMinGlobalIndex();
1943 const size_type end = matrix->getRowMap()->getMaxGlobalIndex() + 1;
1944
1945 return ((index >= begin) && (index < end));
1946 }
1947
1948
1949
1950 template <typename Number, typename MemorySpace>
1951 unsigned int
1953 {
1954 auto n_entries = matrix->getNumEntriesInGlobalRow(row);
1955 Assert(n_entries !=
1956 Teuchos::OrdinalTraits<decltype(n_entries)>::invalid(),
1957 ExcAccessToNonlocalRow(row));
1958
1959 return n_entries;
1960 }
1961
1962
1963
1964 template <typename Number, typename MemorySpace>
1965 inline void
1967 {
1968 // nothing to do here
1969 }
1970
1971
1972
1973 template <typename Number, typename MemorySpace>
1974 inline void
1976 {
1977 // nothing to do here
1978 }
1979
1980
1981
1982 template <typename Number, typename MemorySpace>
1985 {
1986 return *matrix;
1987 }
1988
1989
1990
1991 template <typename Number, typename MemorySpace>
1994 {
1995 return *matrix;
1996 }
1997
1998
1999
2000 template <typename Number, typename MemorySpace>
2001 inline Teuchos::RCP<const TpetraTypes::MatrixType<Number, MemorySpace>>
2003 {
2004 return matrix.getConst();
2005 }
2006
2007
2008
2009 template <typename Number, typename MemorySpace>
2010 inline Teuchos::RCP<TpetraTypes::MatrixType<Number, MemorySpace>>
2012 {
2013 return matrix;
2014 }
2015
2016
2017
2018 template <typename Number, typename MemorySpace>
2019 inline IndexSet
2021 {
2022 return IndexSet(matrix->getDomainMap());
2023 }
2024
2025
2026
2027 template <typename Number, typename MemorySpace>
2028 inline IndexSet
2030 {
2031 return IndexSet(matrix->getRangeMap());
2032 }
2033
2034
2035 namespace SparseMatrixIterators
2036 {
2037 template <typename Number, typename MemorySpace>
2040 size_type row,
2041 size_type index)
2042 : matrix(matrix)
2043 , a_row(row)
2044 , a_index(index)
2045 {
2047 }
2048
2049
2050 template <typename Number, typename MemorySpace>
2053 {
2054 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2055 return a_row;
2056 }
2057
2058
2059 template <typename Number, typename MemorySpace>
2062 {
2063 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2064 return (*colnum_cache)[a_index];
2065 }
2066
2067
2068 template <typename Number, typename MemorySpace>
2071 {
2072 Assert(a_row < matrix->m(), ExcBeyondEndOfMatrix());
2073 return a_index;
2074 }
2075
2076
2077
2078 template <typename Number, typename MemorySpace>
2079 void
2081 {
2082 // if we are asked to visit the past-the-end line, then simply
2083 // release all our caches and go on with life.
2084 //
2085 // do the same if the row we're supposed to visit is not locally
2086 // owned. this is simply going to make non-locally owned rows
2087 // look like they're empty
2088 if ((this->a_row == matrix->m()) ||
2089 (matrix->in_local_range(this->a_row) == false))
2090 {
2091 colnum_cache.reset();
2092 value_cache.reset();
2093
2094 return;
2095 }
2096
2097 // get a representation of the present row
2098 size_t ncols;
2100 matrix->row_length(this->a_row);
2101 if (value_cache.get() == nullptr)
2102 {
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);
2106 }
2107 else
2108 {
2109 value_cache->resize(colnums);
2110 colnum_cache->resize(colnums);
2111 }
2112
2114 nonconst_global_inds_host_view_type col_indices(colnum_cache->data(),
2115 colnums);
2117 nonconst_values_host_view_type values(value_cache->data(), colnums);
2118
2119 matrix->trilinos_matrix().getGlobalRowCopy(this->a_row,
2120 col_indices,
2121 values,
2122 ncols);
2123
2124 AssertDimension(ncols, colnums);
2125
2126 // copy it into our caches if the
2127 // line isn't empty. if it is, then
2128 // we've done something wrong, since
2129 // we shouldn't have initialized an
2130 // iterator for an empty line (what
2131 // would it point to?)
2132 }
2133
2134
2135
2136 template <typename Number, typename MemorySpace>
2138 MatrixType *matrix,
2139 const size_type row,
2140 const size_type index)
2141 : AccessorBase<Number, MemorySpace>(
2142 const_cast<SparseMatrix<Number, MemorySpace> *>(matrix),
2143 row,
2144 index)
2145 {}
2146
2147
2148
2149 template <typename Number, typename MemorySpace>
2150 template <bool Other>
2155
2156
2157
2158 template <typename Number, typename MemorySpace>
2159 inline Number
2168
2169
2170
2171 template <typename Number, typename MemorySpace>
2174 : accessor(const_cast<Accessor<Number, MemorySpace, false> &>(acc))
2175 {}
2176
2177
2178
2179 template <typename Number, typename MemorySpace>
2181 const
2182 {
2183 return (*accessor.value_cache)[accessor.a_index];
2184 }
2185
2186
2187
2188 template <typename Number, typename MemorySpace>
2191 const Number n) const
2192 {
2193 (*accessor.value_cache)[accessor.a_index] = n;
2194 accessor.matrix->set(accessor.row(),
2195 accessor.column(),
2196 static_cast<Number>(*this));
2197 return *this;
2198 }
2199
2200
2201
2202 template <typename Number, typename MemorySpace>
2205 const Number n) const
2206 {
2207 (*accessor.value_cache)[accessor.a_index] += n;
2208 accessor.matrix->set(accessor.row(),
2209 accessor.column(),
2210 static_cast<Number>(*this));
2211 return *this;
2212 }
2213
2214
2215
2216 template <typename Number, typename MemorySpace>
2219 const Number n) const
2220 {
2221 (*accessor.value_cache)[accessor.a_index] -= n;
2222 accessor.matrix->set(accessor.row(),
2223 accessor.column(),
2224 static_cast<Number>(*this));
2225 return *this;
2226 }
2227
2228
2229 template <typename Number, typename MemorySpace>
2232 const Number n) const
2233 {
2234 (*accessor.value_cache)[accessor.a_index] *= n;
2235 accessor.matrix->set(accessor.row(),
2236 accessor.column(),
2237 static_cast<Number>(*this));
2238 return *this;
2239 }
2240
2241
2242 template <typename Number, typename MemorySpace>
2245 const Number n) const
2246 {
2247 (*accessor.value_cache)[accessor.a_index] /= n;
2248 accessor.matrix->set(accessor.row(),
2249 accessor.column(),
2250 static_cast<Number>(*this));
2251 return *this;
2252 }
2253
2254
2255 template <typename Number, typename MemorySpace>
2262
2263
2264 template <typename Number, typename MemorySpace>
2273
2274
2275
2276 template <typename Number, typename MemorySpace, bool Constness>
2278 MatrixType *matrix,
2279 const size_type row,
2280 const size_type index)
2281 : accessor(matrix, row, index)
2282 {}
2283
2284
2285 template <typename Number, typename MemorySpace, bool Constness>
2286 template <bool Other>
2289 : accessor(other.accessor)
2290 {}
2291
2292
2293
2294 template <typename Number, typename MemorySpace, bool Constness>
2297 {
2298 Assert(accessor.a_row < accessor.matrix->m(), ExcIteratorPastEnd());
2299
2300 ++accessor.a_index;
2301
2302 // If at end of line: do one
2303 // step, then cycle until we
2304 // find a row with a nonzero
2305 // number of entries.
2306 if (accessor.a_index >= accessor.colnum_cache->size())
2307 {
2308 accessor.a_index = 0;
2309 ++accessor.a_row;
2310
2311 while (
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)))
2315 ++accessor.a_row;
2316
2317 accessor.visit_present_row();
2318 }
2319 return *this;
2320 }
2321
2322
2323 template <typename Number, typename MemorySpace, bool Constness>
2326 {
2327 const Iterator<Number, MemorySpace, Constness> old_state = *this;
2328 ++(*this);
2329 return old_state;
2330 }
2331
2332
2333
2334 template <typename Number, typename MemorySpace, bool Constness>
2337 {
2338 return accessor;
2339 }
2340
2341
2342
2343 template <typename Number, typename MemorySpace, bool Constness>
2346 {
2347 return &accessor;
2348 }
2349
2350
2351
2352 template <typename Number, typename MemorySpace, bool Constness>
2353 template <bool OtherConstness>
2354 inline bool
2357 {
2358 return (accessor.a_row == other.accessor.a_row &&
2359 accessor.a_index == other.accessor.a_index);
2360 }
2361
2362
2363
2364 template <typename Number, typename MemorySpace, bool Constness>
2365 template <bool OtherConstness>
2366 inline bool
2369 {
2370 return !(*this == other);
2371 }
2372
2373
2374
2375 template <typename Number, typename MemorySpace, bool Constness>
2376 template <bool OtherConstness>
2377 inline bool
2380 {
2381 return (accessor.row() < other.accessor.row() ||
2382 (accessor.row() == other.accessor.row() &&
2383 accessor.index() < other.accessor.index()));
2384 }
2385
2386
2387 template <typename Number, typename MemorySpace, bool Constness>
2388 template <bool OtherConstness>
2389 inline bool
2392 {
2393 return (other < *this);
2394 }
2395
2396 } // namespace SparseMatrixIterators
2397 } // namespace TpetraWrappers
2398
2399} // namespace LinearAlgebra
2400
2401#endif // DEAL_II_TRILINOS_WITH_TPETRA
2402
2404
2405#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