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