Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_DiagonalFilter_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_DIAGONALFILTER_DEF_HPP
11 #define IFPACK2_DIAGONALFILTER_DEF_HPP
12 #include "Ifpack2_DiagonalFilter_decl.hpp"
13 #include <vector>
14 
15 #include "Teuchos_Comm.hpp"
16 #include "Tpetra_Map.hpp"
17 #include "Tpetra_MultiVector.hpp"
18 #include "Tpetra_Vector.hpp"
19 
20 
21 namespace Ifpack2 {
22 
23 template<class MatrixType>
25 DiagonalFilter (const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& Matrix,
26  typename Teuchos::ScalarTraits<Scalar>::magnitudeType AbsoluteThreshold,
27  typename Teuchos::ScalarTraits<Scalar>::magnitudeType RelativeThreshold):
28  A_(Matrix),
29  AbsoluteThreshold_(AbsoluteThreshold),
30  RelativeThreshold_(RelativeThreshold)
31 {
32  pos_.resize(getLocalNumRows());
33  val_=Teuchos::rcp(new Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(A_->getRowMap()));
34 
35  nonconst_local_inds_host_view_type Indices("Indices",getLocalMaxNumRowEntries());
36  nonconst_values_host_view_type Values("Values",getLocalMaxNumRowEntries());
37  size_t NumEntries;
38  magnitudeType mysign;
39 
40 
41  for (size_t MyRow = 0 ; MyRow < getLocalNumRows() ; ++MyRow) {
42  pos_[MyRow] = -1;
43  A_->getLocalRowCopy(MyRow,Indices,Values,NumEntries);
44 
45  for (size_t i = 0 ; i < NumEntries ; ++i) {
46  if ((size_t)Indices[i] == MyRow) {
47  pos_[MyRow] = i;
48  if (Teuchos::ScalarTraits<Scalar>::real(Values[i]) < 0) {
50  }
51  else {
53  }
54  val_->replaceLocalValue (MyRow, Values[i] * (RelativeThreshold_ - 1) +
55  AbsoluteThreshold_ * mysign);
56  break;
57  }
58  }
59  }
60 }
61 
62 template<class MatrixType>
64 
65 template<class MatrixType>
67 {
68  return A_->getComm();
69 }
70 
71 
72 template<class MatrixType>
73 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
74  typename MatrixType::global_ordinal_type,
75  typename MatrixType::node_type> >
77 {
78  return A_->getRowMap();
79 }
80 
81 template<class MatrixType>
82 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
83  typename MatrixType::global_ordinal_type,
84  typename MatrixType::node_type> >
86 {
87  return A_->getColMap();
88 }
89 
90 template<class MatrixType>
91 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
92  typename MatrixType::global_ordinal_type,
93  typename MatrixType::node_type> >
95 {
96  return A_->getDomainMap();
97 }
98 
99 template<class MatrixType>
100 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
101  typename MatrixType::global_ordinal_type,
102  typename MatrixType::node_type> >
104 {
105  return A_->getRangeMap();
106 }
107 
108 template<class MatrixType>
109 Teuchos::RCP<const Tpetra::RowGraph<typename MatrixType::local_ordinal_type,
110  typename MatrixType::global_ordinal_type,
111  typename MatrixType::node_type> >
113 {
114  return A_->getGraph();
115 }
116 
117 template<class MatrixType>
119 {
120  return A_->getGlobalNumRows();
121 }
122 
123 template<class MatrixType>
125 {
126  return A_->getGlobalNumCols();
127 }
128 
129 template<class MatrixType>
131 {
132  return A_->getLocalNumRows();
133 }
134 
135 template<class MatrixType>
137 {
138  return A_->getLocalNumCols();
139 }
140 
141 template<class MatrixType>
142 typename MatrixType::global_ordinal_type DiagonalFilter<MatrixType>::getIndexBase() const
143 {
144  return A_->getIndexBase();
145 }
146 
147 template<class MatrixType>
149 {
150  return A_->getGlobalNumEntries();
151 }
152 
153 template<class MatrixType>
155 {
156  return A_->getLocalNumEntries();
157 }
158 
159 template<class MatrixType>
160 size_t DiagonalFilter<MatrixType>::getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
161 {
162  return A_->getNumEntriesInGlobalRow(globalRow);
163 }
164 
165 template<class MatrixType>
166 size_t DiagonalFilter<MatrixType>::getNumEntriesInLocalRow(LocalOrdinal localRow) const
167 {
168  return A_->getNumEntriesInLocalRow(localRow);
169 }
170 
171 template<class MatrixType>
173 {
174  return A_->getGlobalMaxNumRowEntries();
175 }
176 
177 template<class MatrixType>
179 {
180  return A_->getLocalMaxNumRowEntries();
181 }
182 
183 template<class MatrixType>
184 typename MatrixType::local_ordinal_type DiagonalFilter<MatrixType>::getBlockSize() const
185 {
186  return A_->getBlockSize();
187 }
188 
189 template<class MatrixType>
191 {
192  return A_->hasColMap();
193 }
194 
195 template<class MatrixType>
197 {
198  return A_->isLocallyIndexed();
199 }
200 
201 template<class MatrixType>
203 {
204  return A_->isGloballyIndexed();
205 }
206 
207 template<class MatrixType>
209 {
210  return A_->isFillComplete();
211 }
212 
213 template<class MatrixType>
215  getGlobalRowCopy (GlobalOrdinal GlobalRow,
216  nonconst_global_inds_host_view_type &Indices,
217  nonconst_values_host_view_type &Values,
218  size_t& NumEntries) const
219 
220 {
221  Teuchos::ArrayRCP< const Scalar > myvals=val_->get1dView();
222  LocalOrdinal LocalRow=getRowMap()->getLocalElement(GlobalRow);
223 
224  A_->getGlobalRowCopy(GlobalRow, Indices,Values,NumEntries);
225 
226  if (pos_[LocalRow] != -1)
227  Values[pos_[LocalRow]] += myvals[LocalRow];
228 }
229 
230 
231 template<class MatrixType>
233  getLocalRowCopy (LocalOrdinal LocalRow,
234  nonconst_local_inds_host_view_type &Indices,
235  nonconst_values_host_view_type &Values,
236  size_t& NumEntries) const
237 {
238  Teuchos::ArrayRCP< const Scalar > myvals=val_->get1dView();
239 
240  A_->getLocalRowCopy(LocalRow, Indices,Values,NumEntries);
241 
242  if (pos_[LocalRow] != -1)
243  Values[pos_[LocalRow]] += myvals[LocalRow];
244 }
245 
246 
247 template<class MatrixType>
248 void DiagonalFilter<MatrixType>::getGlobalRowView(GlobalOrdinal /* GlobalRow */,
249  global_inds_host_view_type &/*indices*/,
250  values_host_view_type &/*values*/) const
251 {
252  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getGlobalRowView.");
253 }
254 
255 
256 template<class MatrixType>
257 void DiagonalFilter<MatrixType>::getLocalRowView(LocalOrdinal /* LocalRow */,
258  local_inds_host_view_type & /*indices*/,
259  values_host_view_type & /*values*/) const
260 {
261  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getLocalRowView.");
262 }
263 
264 
265 template<class MatrixType>
266 void DiagonalFilter<MatrixType>::getLocalDiagCopy(Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &diag) const
267 {
268  // Returns the matrix's actual diagonal, rather than the "filtered" diagonal.
269  // This is dubious, but it duplicates the functionality of Old Ifpack.
270  return A_->getLocalDiagCopy(diag);
271 }
272 
273 template<class MatrixType>
274 void DiagonalFilter<MatrixType>::leftScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& /* x */)
275 {
276  throw std::runtime_error("Ifpack2::DiagonalFilter does not support leftScale.");
277 }
278 
279 template<class MatrixType>
280 void DiagonalFilter<MatrixType>::rightScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& /* x */)
281 {
282  throw std::runtime_error("Ifpack2::DiagonalFilter does not support rightScale.");
283 }
284 
285 template<class MatrixType>
287 apply (const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
288  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
289  Teuchos::ETransp mode,
290  Scalar alpha,
291  Scalar beta) const
292 {
293  // FIXME (mfh 27 Jul 2015) If this is a "diagonal filter," why do we
294  // need to apply the whole matrix???
295 
296  Scalar one = Teuchos::ScalarTraits<Scalar>::one();
297  A_->apply(X,Y,mode,alpha,beta);
298  Y.elementWiseMultiply(one,*val_,X,one);
299 }
300 
301 template<class MatrixType>
303 {
304  return A_->hasTransposeApply();
305 }
306 
307 template<class MatrixType>
309 {
310  return false;
311 }
312 
313 template<class MatrixType>
314 typename DiagonalFilter<MatrixType>::mag_type DiagonalFilter<MatrixType>::getFrobeniusNorm() const
315 {
316  throw std::runtime_error("Ifpack2::DiagonalFilter does not implement getFrobeniusNorm.");
317 }
318 
319 } // namespace Ifpack2
320 
321 #define IFPACK2_DIAGONALFILTER_INSTANT(S,LO,GO,N) \
322  template class Ifpack2::DiagonalFilter< Tpetra::RowMatrix<S, LO, GO, N> >;
323 
324 #endif
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:314
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_DiagonalFilter_def.hpp:66
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_DiagonalFilter_def.hpp:287
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:118
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:148
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_DiagonalFilter_def.hpp:215
virtual LocalOrdinal getBlockSize() const
The number of degrees of freedom per mesh point.
Definition: Ifpack2_DiagonalFilter_def.hpp:184
virtual ~DiagonalFilter()
Destructor.
Definition: Ifpack2_DiagonalFilter_def.hpp:63
virtual size_t getLocalNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:154
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_DiagonalFilter_def.hpp:136
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_DiagonalFilter_def.hpp:302
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_DiagonalFilter_def.hpp:172
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:142
virtual bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
Definition: Ifpack2_DiagonalFilter_def.hpp:202
virtual void leftScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the left with the Vector x.
Definition: Ifpack2_DiagonalFilter_def.hpp:274
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_DiagonalFilter_def.hpp:233
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:76
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_DiagonalFilter_def.hpp:190
virtual void rightScale(const Tpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x)
Scales the RowMatrix on the right with the Vector x.
Definition: Ifpack2_DiagonalFilter_def.hpp:280
virtual size_t getLocalNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_DiagonalFilter_def.hpp:130
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:112
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_DiagonalFilter_def.hpp:208
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_DiagonalFilter_def.hpp:266
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_DiagonalFilter_def.hpp:248
virtual bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
Definition: Ifpack2_DiagonalFilter_def.hpp:196
DiagonalFilter(const Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Matrix, magnitudeType AbsoluteThreshold, magnitudeType RelativeThreshold)
Constructor.
Definition: Ifpack2_DiagonalFilter_def.hpp:25
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Map that describes the range distribution in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:103
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:124
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map that describes the domain distribution in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:94
virtual size_t getLocalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_DiagonalFilter_def.hpp:178
virtual Teuchos::RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const
Returns the Map that describes the column distribution in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:85
virtual size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
Returns the current number of entries on this node in the specified global row.
Definition: Ifpack2_DiagonalFilter_def.hpp:160
virtual size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
Definition: Ifpack2_DiagonalFilter_def.hpp:166
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_DiagonalFilter_def.hpp:308
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_DiagonalFilter_def.hpp:257