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