Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_SparsityFilter_def.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_SPARSITYFILTER_DEF_HPP
11 #define IFPACK2_SPARSITYFILTER_DEF_HPP
12 
13 #include "Tpetra_Map.hpp"
14 #include "Tpetra_MultiVector.hpp"
15 #include "Tpetra_Vector.hpp"
16 
17 #include <algorithm>
18 #include <vector>
19 
20 namespace Ifpack2 {
21 
22 //==========================================================================
23 template <class MatrixType>
24 SparsityFilter<MatrixType>::SparsityFilter(const Teuchos::RCP<const Tpetra::RowMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > &Matrix,
25  size_t AllowedNumEntries,
26  LocalOrdinal AllowedBandwidth)
27  : A_(Matrix)
28  , AllowedNumEntries_(AllowedNumEntries)
29  , AllowedBandwidth_(AllowedBandwidth)
30  , NumRows_(0)
31  , NumNonzeros_(0)
32  , MaxNumEntries_(0)
33  , MaxNumEntriesA_(0) {
34  // use this filter only on serial matrices
36  A_->getComm()->getSize() != 1 || A_->getLocalNumRows() != A_->getGlobalNumRows(),
37  std::runtime_error,
38  "Ifpack2::SparsityFilter: "
39  "This class may only be used when A.getComm()->getSize() == 1.");
40 
41  // localized matrix has all the local rows of Matrix
42  NumRows_ = A_->getLocalNumRows();
43 
44  // NodeNumEntries_ will contain the actual number of nonzeros
45  // for each localized row (that is, without external nodes,
46  // and always with the diagonal entry)
47  NumEntries_.resize(NumRows_);
48 
49  // tentative value for MaxNumEntries. This is the number of
50  // nonzeros in the local matrix
51  MaxNumEntries_ = A_->getLocalMaxNumRowEntries();
52  MaxNumEntriesA_ = A_->getLocalMaxNumRowEntries();
53 
54  // ExtractMyRowCopy() will use these vectors
55  Kokkos::resize(Indices_, MaxNumEntries_);
56  Kokkos::resize(Values_, MaxNumEntries_);
57 
58  size_t ActualMaxNumEntries = 0;
59  for (size_t i = 0; i < NumRows_; ++i) {
60  NumEntries_[i] = MaxNumEntriesA_;
61  size_t Nnz;
62  A_->getLocalRowCopy(i, Indices_, Values_, Nnz);
63 
64  NumNonzeros_ += Nnz;
65  NumEntries_[i] = Nnz;
66  if (Nnz > ActualMaxNumEntries)
67  ActualMaxNumEntries = Nnz;
68  }
69 
70  MaxNumEntries_ = ActualMaxNumEntries;
71 }
72 
73 //=========================================================================
74 template <class MatrixType>
76 
77 //==========================================================================
78 template <class MatrixType>
80  return A_->getComm();
81 }
82 
83 //==========================================================================
84 template <class MatrixType>
85 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
86  typename MatrixType::global_ordinal_type,
87  typename MatrixType::node_type> >
89  return A_->getRowMap();
90 }
91 
92 //==========================================================================
93 template <class MatrixType>
94 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
95  typename MatrixType::global_ordinal_type,
96  typename MatrixType::node_type> >
98  return A_->getColMap();
99 }
100 
101 //==========================================================================
102 template <class MatrixType>
103 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
104  typename MatrixType::global_ordinal_type,
105  typename MatrixType::node_type> >
107  return A_->getDomainMap();
108 }
109 
110 //==========================================================================
111 template <class MatrixType>
112 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
113  typename MatrixType::global_ordinal_type,
114  typename MatrixType::node_type> >
116  return A_->getRangeMap();
117 }
118 
119 //==========================================================================
120 template <class MatrixType>
121 Teuchos::RCP<const Tpetra::RowGraph<typename MatrixType::local_ordinal_type,
122  typename MatrixType::global_ordinal_type,
123  typename MatrixType::node_type> >
125  throw std::runtime_error("Ifpack2::SparsityFilter: does not support getGraph.");
126 }
127 
128 //==========================================================================
129 template <class MatrixType>
131  return NumRows_;
132 }
133 
134 //==========================================================================
135 template <class MatrixType>
137  return NumRows_;
138 }
139 
140 //==========================================================================
141 template <class MatrixType>
143  return NumRows_;
144 }
145 
146 //==========================================================================
147 
148 template <class MatrixType>
150  return NumRows_;
151 }
152 
153 //==========================================================================
154 template <class MatrixType>
155 typename MatrixType::global_ordinal_type SparsityFilter<MatrixType>::getIndexBase() const {
156  return A_->getIndexBase();
157 }
158 
159 //==========================================================================
160 template <class MatrixType>
162  return NumNonzeros_;
163 }
164 
165 //==========================================================================
166 template <class MatrixType>
168  return NumNonzeros_;
169 }
170 
171 //==========================================================================
172 template <class MatrixType>
173 size_t SparsityFilter<MatrixType>::getNumEntriesInGlobalRow(GlobalOrdinal /* globalRow */) const {
174  throw std::runtime_error("Ifpack2::SparsityFilter does not implement getNumEntriesInGlobalRow.");
175 }
176 
177 //==========================================================================
178 template <class MatrixType>
179 size_t SparsityFilter<MatrixType>::getNumEntriesInLocalRow(LocalOrdinal localRow) const {
180  return NumEntries_[localRow];
181 }
182 
183 //==========================================================================
184 template <class MatrixType>
186  return MaxNumEntries_;
187 }
188 
189 //==========================================================================
190 template <class MatrixType>
192  return MaxNumEntries_;
193 }
194 
195 //==========================================================================
196 template <class MatrixType>
197 typename MatrixType::local_ordinal_type SparsityFilter<MatrixType>::getBlockSize() const {
198  return A_->getBlockSize();
199 }
200 
201 //==========================================================================
202 template <class MatrixType>
204  return true;
205 }
206 
207 //==========================================================================
208 template <class MatrixType>
210  return A_->isLocallyIndexed();
211 }
212 
213 //==========================================================================
214 template <class MatrixType>
216  return A_->isGloballyIndexed();
217 }
218 
219 //==========================================================================
220 template <class MatrixType>
222  return A_->isFillComplete();
223 }
224 
225 //==========================================================================
226 template <class MatrixType>
228  getGlobalRowCopy(GlobalOrdinal /*GlobalRow*/,
229  nonconst_global_inds_host_view_type & /*Indices*/,
230  nonconst_values_host_view_type & /*Values*/,
231  size_t & /*NumEntries*/) const {
232  throw std::runtime_error("Ifpack2::SparsityFilter does not implement getGlobalRowCopy.");
233 }
234 
235 //==========================================================================
236 template <class MatrixType>
238  getLocalRowCopy(LocalOrdinal LocalRow,
239  nonconst_local_inds_host_view_type &Indices,
240  nonconst_values_host_view_type &Values,
241  size_t &NumEntries) const {
242  TEUCHOS_TEST_FOR_EXCEPTION((LocalRow < 0 || (size_t)LocalRow >= NumRows_ || (size_t)Indices.size() < NumEntries_[LocalRow]), std::runtime_error, "Ifpack2::SparsityFilter::getLocalRowCopy invalid row or array size.");
243 
244  // Note: This function will work correctly if called by apply, say, with Indices_ and Values_ as
245  // parameters. The structure of the loop below should make that obvious.
246 
247  // always extract using the object Values_ and Indices_.
248  // This is because I need more space than that given by
249  // the user (for the external nodes)
250  size_t A_NumEntries = 0;
251  A_->getLocalRowCopy(LocalRow, Indices_, Values_, A_NumEntries);
252  magnitudeType Threshold = Teuchos::ScalarTraits<magnitudeType>::zero();
253  std::vector<magnitudeType> Values2(A_NumEntries, Teuchos::ScalarTraits<magnitudeType>::zero());
254 
255  if (A_NumEntries > AllowedNumEntries_) {
256  size_t count = 0;
257  for (size_t i = 0; i < A_NumEntries; ++i) {
258  // skip diagonal entry (which is always inserted)
259  if (Indices_[i] == LocalRow)
260  continue;
261  // put magnitude
262  Values2[count] = Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]);
263  count++;
264  }
265 
266  // sort in descending order
267  std::sort(Values2.rbegin(), Values2.rend());
268  // get the cut-off value
269  Threshold = Values2[AllowedNumEntries_ - 1];
270  }
271 
272  // loop over all nonzero elements of row MyRow,
273  // and drop elements below specified threshold.
274  // Also, add value to zero diagonal
275  NumEntries = 0;
276  for (size_t i = 0; i < A_NumEntries; ++i) {
277  if (std::abs(Indices_[i] - LocalRow) > AllowedBandwidth_)
278  continue;
279 
280  if ((Indices_[i] != LocalRow) && (Teuchos::ScalarTraits<Scalar>::magnitude(Values_[i]) < Threshold))
281  continue;
282 
283  Values[NumEntries] = Values_[i];
284  Indices[NumEntries] = Indices_[i];
285 
286  NumEntries++;
287  if (NumEntries > AllowedNumEntries_)
288  break;
289  }
290 }
291 
292 //==========================================================================
293 template <class MatrixType>
294 void SparsityFilter<MatrixType>::getGlobalRowView(GlobalOrdinal /* GlobalRow */,
295  global_inds_host_view_type & /*indices*/,
296  values_host_view_type & /*values*/) const {
297  throw std::runtime_error("Ifpack2::SparsityFilter: does not support getGlobalRowView.");
298 }
299 
300 //==========================================================================
301 template <class MatrixType>
302 void SparsityFilter<MatrixType>::getLocalRowView(LocalOrdinal /* LocalRow */,
303  local_inds_host_view_type & /*indices*/,
304  values_host_view_type & /*values*/) const {
305  throw std::runtime_error("Ifpack2::SparsityFilter: does not support getLocalRowView.");
306 }
307 
308 //==========================================================================
309 template <class MatrixType>
310 void SparsityFilter<MatrixType>::getLocalDiagCopy(Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &diag) const {
311  // This is somewhat dubious as to how the maps match.
312  return A_->getLocalDiagCopy(diag);
313 }
314 
315 //==========================================================================
316 template <class MatrixType>
317 void SparsityFilter<MatrixType>::leftScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & /* x */) {
318  throw std::runtime_error("Ifpack2::SparsityFilter does not support leftScale.");
319 }
320 
321 //==========================================================================
322 template <class MatrixType>
323 void SparsityFilter<MatrixType>::rightScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & /* x */) {
324  throw std::runtime_error("Ifpack2::SparsityFilter does not support rightScale.");
325 }
326 
327 //==========================================================================
328 template <class MatrixType>
329 void SparsityFilter<MatrixType>::apply(const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &X,
330  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &Y,
331  Teuchos::ETransp mode,
332  Scalar /* alpha */,
333  Scalar /* beta */) const {
334  // Note: This isn't AztecOO compliant. But neither was Ifpack's version.
335  // Note: The localized maps mean the matvec is trivial (and has no import)
336  TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
337  "Ifpack2::SparsityFilter::apply ERROR: X.getNumVectors() != Y.getNumVectors().");
338 
339  Scalar zero = Teuchos::ScalarTraits<Scalar>::zero();
341  Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > y_ptr = Y.get2dViewNonConst();
342 
343  Y.putScalar(zero);
344  size_t NumVectors = Y.getNumVectors();
345 
346  for (size_t i = 0; i < NumRows_; ++i) {
347  size_t Nnz;
348  // Use this class's getrow to make the below code simpler
349  getLocalRowCopy(i, Indices_, Values_, Nnz);
350  Scalar *Values = reinterpret_cast<Scalar *>(Values_.data());
351  if (mode == Teuchos::NO_TRANS) {
352  for (size_t j = 0; j < Nnz; ++j)
353  for (size_t k = 0; k < NumVectors; ++k)
354  y_ptr[k][i] += Values[j] * x_ptr[k][Indices_[j]];
355  } else if (mode == Teuchos::TRANS) {
356  for (size_t j = 0; j < Nnz; ++j)
357  for (size_t k = 0; k < NumVectors; ++k)
358  y_ptr[k][Indices_[j]] += Values[j] * x_ptr[k][i];
359  } else { // mode==Teuchos::CONJ_TRANS
360  for (size_t j = 0; j < Nnz; ++j)
361  for (size_t k = 0; k < NumVectors; ++k)
362  y_ptr[k][Indices_[j]] += Teuchos::ScalarTraits<Scalar>::conjugate(Values[j]) * x_ptr[k][i];
363  }
364  }
365 }
366 
367 //==========================================================================
368 template <class MatrixType>
370  return true;
371 }
372 
373 //==========================================================================
374 template <class MatrixType>
376  return false;
377 }
378 
379 //==========================================================================
380 template <class MatrixType>
381 typename SparsityFilter<MatrixType>::mag_type SparsityFilter<MatrixType>::getFrobeniusNorm() const {
382  throw std::runtime_error("Ifpack2::SparsityFilter does not implement getFrobeniusNorm.");
383 }
384 
385 } // namespace Ifpack2
386 
387 #define IFPACK2_SPARSITYFILTER_INSTANT(S, LO, GO, N) \
388  template class Ifpack2::SparsityFilter<Tpetra::RowMatrix<S, LO, GO, N> >;
389 
390 #endif
virtual void apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Computes the operator-multivector application.
Definition: Ifpack2_SparsityFilter_def.hpp:329
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
Definition: Ifpack2_SparsityFilter_def.hpp:179
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_SparsityFilter_def.hpp:369
virtual size_t getLocalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_SparsityFilter_def.hpp:191
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:88
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:106
virtual void getLocalRowCopy(LocalOrdinal LocalRow, nonconst_local_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const
Extract a list of entries in a specified local row of the graph. Put into storage allocated by callin...
Definition: Ifpack2_SparsityFilter_def.hpp:238
virtual size_t getLocalNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:167
virtual void getGlobalRowView(GlobalOrdinal GlobalRow, global_inds_host_view_type &indices, values_host_view_type &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:294
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:155
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:97
virtual void getLocalDiagCopy(Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &diag) const
Get a copy of the diagonal entries owned by this node, with local row indices.
Definition: Ifpack2_SparsityFilter_def.hpp:310
virtual LocalOrdinal getBlockSize() const
The number of degrees of freedom per mesh point.
Definition: Ifpack2_SparsityFilter_def.hpp:197
virtual bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
Definition: Ifpack2_SparsityFilter_def.hpp:215
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:124
virtual bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
Definition: Ifpack2_SparsityFilter_def.hpp:209
virtual ~SparsityFilter()
Destructor.
Definition: Ifpack2_SparsityFilter_def.hpp:75
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:130
virtual size_t getLocalNumCols() const
Returns the number of columns needed to apply the forward operator on this node, i.e., the number of elements listed in the column map.
Definition: Ifpack2_SparsityFilter_def.hpp:149
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_SparsityFilter_def.hpp:79
SparsityFilter(const Teuchos::RCP< const row_matrix_type > &Matrix, size_t AllowedNumEntries, LocalOrdinal AllowedBandwidth=-Teuchos::ScalarTraits< LocalOrdinal >::one())
Constructor.
Definition: Ifpack2_SparsityFilter_def.hpp:24
virtual size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
Returns the current number of entries on this node in the specified global row.
Definition: Ifpack2_SparsityFilter_def.hpp:173
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:136
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_SparsityFilter_def.hpp:203
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:381
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_SparsityFilter_def.hpp:185
virtual void getLocalRowView(LocalOrdinal LocalRow, local_inds_host_view_type &indices, values_host_view_type &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:302
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:115
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_SparsityFilter_def.hpp:161
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_SparsityFilter_def.hpp:221
static magnitudeType magnitude(T a)
virtual void getGlobalRowCopy(GlobalOrdinal GlobalRow, nonconst_global_inds_host_view_type &Indices, nonconst_values_host_view_type &Values, size_t &NumEntries) const
Extract a list of entries in a specified global row of this matrix. Put into pre-allocated storage...
Definition: Ifpack2_SparsityFilter_def.hpp:228
virtual void rightScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the right with the Vector x.
Definition: Ifpack2_SparsityFilter_def.hpp:323
virtual void leftScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_SparsityFilter_def.hpp:317
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_SparsityFilter_def.hpp:375
virtual size_t getLocalNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_SparsityFilter_def.hpp:142