Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_DiagonalFilter_def.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_DIAGONALFILTER_DEF_HPP
44 #define IFPACK2_DIAGONALFILTER_DEF_HPP
45 #include "Ifpack2_DiagonalFilter_decl.hpp"
46 #include <vector>
47 
48 #include "Teuchos_Comm.hpp"
49 #include "Tpetra_Map.hpp"
50 #include "Tpetra_MultiVector.hpp"
51 #include "Tpetra_Vector.hpp"
52 
53 
54 namespace Ifpack2 {
55 
56 template<class MatrixType>
58 DiagonalFilter (const Teuchos::RCP<const Tpetra::RowMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> >& Matrix,
59  typename Teuchos::ScalarTraits<Scalar>::magnitudeType AbsoluteThreshold,
60  typename Teuchos::ScalarTraits<Scalar>::magnitudeType RelativeThreshold):
61  A_(Matrix),
62  AbsoluteThreshold_(AbsoluteThreshold),
63  RelativeThreshold_(RelativeThreshold)
64 {
65  pos_.resize(getNodeNumRows());
66  val_=Teuchos::rcp(new Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(A_->getRowMap()));
67 
68  nonconst_local_inds_host_view_type Indices("Indices",getNodeMaxNumRowEntries());
69  nonconst_values_host_view_type Values("Values",getNodeMaxNumRowEntries());
70  size_t NumEntries;
71  magnitudeType mysign;
72 
73 
74  for (size_t MyRow = 0 ; MyRow < getNodeNumRows() ; ++MyRow) {
75  pos_[MyRow] = -1;
76  A_->getLocalRowCopy(MyRow,Indices,Values,NumEntries);
77 
78  for (size_t i = 0 ; i < NumEntries ; ++i) {
79  if ((size_t)Indices[i] == MyRow) {
80  pos_[MyRow] = i;
81  if (Teuchos::ScalarTraits<Scalar>::real(Values[i]) < 0) {
82  mysign=-Teuchos::ScalarTraits<magnitudeType>::one();
83  }
84  else {
85  mysign=Teuchos::ScalarTraits<magnitudeType>::one();
86  }
87  val_->replaceLocalValue (MyRow, Values[i] * (RelativeThreshold_ - 1) +
88  AbsoluteThreshold_ * mysign);
89  break;
90  }
91  }
92  }
93 }
94 
95 template<class MatrixType>
97 
98 template<class MatrixType>
99 Teuchos::RCP<const Teuchos::Comm<int> > DiagonalFilter<MatrixType>::getComm() const
100 {
101  return A_->getComm();
102 }
103 
104 
105 template<class MatrixType>
106 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
107  typename MatrixType::global_ordinal_type,
108  typename MatrixType::node_type> >
110 {
111  return A_->getRowMap();
112 }
113 
114 template<class MatrixType>
115 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
116  typename MatrixType::global_ordinal_type,
117  typename MatrixType::node_type> >
119 {
120  return A_->getColMap();
121 }
122 
123 template<class MatrixType>
124 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
125  typename MatrixType::global_ordinal_type,
126  typename MatrixType::node_type> >
128 {
129  return A_->getDomainMap();
130 }
131 
132 template<class MatrixType>
133 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,
134  typename MatrixType::global_ordinal_type,
135  typename MatrixType::node_type> >
137 {
138  return A_->getRangeMap();
139 }
140 
141 template<class MatrixType>
142 Teuchos::RCP<const Tpetra::RowGraph<typename MatrixType::local_ordinal_type,
143  typename MatrixType::global_ordinal_type,
144  typename MatrixType::node_type> >
146 {
147  return A_->getGraph();
148 }
149 
150 template<class MatrixType>
152 {
153  return A_->getGlobalNumRows();
154 }
155 
156 template<class MatrixType>
158 {
159  return A_->getGlobalNumCols();
160 }
161 
162 template<class MatrixType>
164 {
165  return A_->getNodeNumRows();
166 }
167 
168 template<class MatrixType>
170 {
171  return A_->getNodeNumCols();
172 }
173 
174 template<class MatrixType>
175 typename MatrixType::global_ordinal_type DiagonalFilter<MatrixType>::getIndexBase() const
176 {
177  return A_->getIndexBase();
178 }
179 
180 template<class MatrixType>
182 {
183  return A_->getGlobalNumEntries();
184 }
185 
186 template<class MatrixType>
188 {
189  return A_->getNodeNumEntries();
190 }
191 
192 template<class MatrixType>
193 size_t DiagonalFilter<MatrixType>::getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const
194 {
195  return A_->getNumEntriesInGlobalRow(globalRow);
196 }
197 
198 template<class MatrixType>
199 size_t DiagonalFilter<MatrixType>::getNumEntriesInLocalRow(LocalOrdinal localRow) const
200 {
201  return A_->getNumEntriesInLocalRow(localRow);
202 }
203 
204 template<class MatrixType>
206 {
207  return A_->getGlobalMaxNumRowEntries();
208 }
209 
210 template<class MatrixType>
212 {
213  return A_->getNodeMaxNumRowEntries();
214 }
215 
216 template<class MatrixType>
218 {
219  return A_->hasColMap();
220 }
221 
222 template<class MatrixType>
224 {
225  return A_->isLocallyIndexed();
226 }
227 
228 template<class MatrixType>
230 {
231  return A_->isGloballyIndexed();
232 }
233 
234 template<class MatrixType>
236 {
237  return A_->isFillComplete();
238 }
239 
240 template<class MatrixType>
242  getGlobalRowCopy (GlobalOrdinal GlobalRow,
243  nonconst_global_inds_host_view_type &Indices,
244  nonconst_values_host_view_type &Values,
245  size_t& NumEntries) const
246 
247 {
248  Teuchos::ArrayRCP< const Scalar > myvals=val_->get1dView();
249  LocalOrdinal LocalRow=getRowMap()->getLocalElement(GlobalRow);
250 
251  A_->getGlobalRowCopy(GlobalRow, Indices,Values,NumEntries);
252 
253  if (pos_[LocalRow] != -1)
254  Values[pos_[LocalRow]] += myvals[LocalRow];
255 }
256 
257 
258 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
259 template<class MatrixType>
261 getGlobalRowCopy (GlobalOrdinal GlobalRow,
262  const Teuchos::ArrayView<GlobalOrdinal> &Indices,
263  const Teuchos::ArrayView<Scalar> &Values,
264  size_t &NumEntries) const {
265  using IST = typename row_matrix_type::impl_scalar_type;
266  nonconst_global_inds_host_view_type ind_in(Indices.data(),Indices.size());
267  nonconst_values_host_view_type val_in(reinterpret_cast<IST*>(Values.data()),Values.size());
268  getGlobalRowCopy(GlobalRow,ind_in,val_in,NumEntries);
269 }
270 #endif
271 
272 template<class MatrixType>
274  getLocalRowCopy (LocalOrdinal LocalRow,
275  nonconst_local_inds_host_view_type &Indices,
276  nonconst_values_host_view_type &Values,
277  size_t& NumEntries) const
278 {
279  Teuchos::ArrayRCP< const Scalar > myvals=val_->get1dView();
280 
281  A_->getLocalRowCopy(LocalRow, Indices,Values,NumEntries);
282 
283  if (pos_[LocalRow] != -1)
284  Values[pos_[LocalRow]] += myvals[LocalRow];
285 }
286 
287 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
288 template<class MatrixType>
289 void
291 getLocalRowCopy (LocalOrdinal LocalRow,
292  const Teuchos::ArrayView<LocalOrdinal> &Indices,
293  const Teuchos::ArrayView<Scalar> &Values,
294  size_t &NumEntries) const
295 {
296  using IST = typename row_matrix_type::impl_scalar_type;
297  nonconst_local_inds_host_view_type ind_in(Indices.data(),Indices.size());
298  nonconst_values_host_view_type val_in(reinterpret_cast<IST*>(Values.data()),Values.size());
299  getLocalRowCopy(LocalRow,ind_in,val_in,NumEntries);
300 }
301 #endif
302 
303 
304 template<class MatrixType>
305 void DiagonalFilter<MatrixType>::getGlobalRowView(GlobalOrdinal /* GlobalRow */,
306  global_inds_host_view_type &/*indices*/,
307  values_host_view_type &/*values*/) const
308 {
309  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getGlobalRowView.");
310 }
311 
312 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
313 template<class MatrixType>
315 getGlobalRowView (GlobalOrdinal /* GlobalRow */,
316  Teuchos::ArrayView<const GlobalOrdinal> &/* indices */,
317  Teuchos::ArrayView<const Scalar> &/* values */) const
318 {
319  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getGlobalRowView.");
320 }
321 #endif
322 
323 template<class MatrixType>
324 void DiagonalFilter<MatrixType>::getLocalRowView(LocalOrdinal /* LocalRow */,
325  local_inds_host_view_type & /*indices*/,
326  values_host_view_type & /*values*/) const
327 {
328  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getLocalRowView.");
329 }
330 
331 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
332 template<class MatrixType>
334 getLocalRowView (LocalOrdinal /* LocalRow */,
335  Teuchos::ArrayView<const LocalOrdinal> &/* indices */,
336  Teuchos::ArrayView<const Scalar> &/* values */) const
337 {
338  throw std::runtime_error("Ifpack2::DiagonalFilter: does not support getLocalRowView.");
339 }
340 #endif
341 
342 template<class MatrixType>
343 void DiagonalFilter<MatrixType>::getLocalDiagCopy(Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &diag) const
344 {
345  // Returns the matrix's actual diagonal, rather than the "filtered" diagonal.
346  // This is dubious, but it duplicates the functionality of Old Ifpack.
347  return A_->getLocalDiagCopy(diag);
348 }
349 
350 template<class MatrixType>
351 void DiagonalFilter<MatrixType>::leftScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& /* x */)
352 {
353  throw std::runtime_error("Ifpack2::DiagonalFilter does not support leftScale.");
354 }
355 
356 template<class MatrixType>
357 void DiagonalFilter<MatrixType>::rightScale(const Tpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& /* x */)
358 {
359  throw std::runtime_error("Ifpack2::DiagonalFilter does not support rightScale.");
360 }
361 
362 template<class MatrixType>
364 apply (const Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
365  Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
366  Teuchos::ETransp mode,
367  Scalar alpha,
368  Scalar beta) const
369 {
370  // FIXME (mfh 27 Jul 2015) If this is a "diagonal filter," why do we
371  // need to apply the whole matrix???
372 
373  Scalar one = Teuchos::ScalarTraits<Scalar>::one();
374  A_->apply(X,Y,mode,alpha,beta);
375  Y.elementWiseMultiply(one,*val_,X,one);
376 }
377 
378 template<class MatrixType>
380 {
381  return A_->hasTransposeApply();
382 }
383 
384 template<class MatrixType>
386 {
387  return false;
388 }
389 
390 template<class MatrixType>
391 typename DiagonalFilter<MatrixType>::mag_type DiagonalFilter<MatrixType>::getFrobeniusNorm() const
392 {
393  throw std::runtime_error("Ifpack2::DiagonalFilter does not implement getFrobeniusNorm.");
394 }
395 
396 } // namespace Ifpack2
397 
398 #define IFPACK2_DIAGONALFILTER_INSTANT(S,LO,GO,N) \
399  template class Ifpack2::DiagonalFilter< Tpetra::RowMatrix<S, LO, GO, N> >;
400 
401 #endif
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:242
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:136
virtual global_size_t getGlobalNumRows() const
Returns the number of global rows in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:151
virtual global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:181
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:223
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:229
virtual GlobalOrdinal getIndexBase() const
Returns the index base for global indices for this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:175
virtual size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:187
virtual ~DiagonalFilter()
Destructor.
Definition: Ifpack2_DiagonalFilter_def.hpp:96
virtual bool hasColMap() const
Indicates whether this matrix has a well-defined column map.
Definition: Ifpack2_DiagonalFilter_def.hpp:217
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:324
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:109
virtual size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
Definition: Ifpack2_DiagonalFilter_def.hpp:205
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:127
virtual bool supportsRowViews() const
Returns true if RowViews are supported.
Definition: Ifpack2_DiagonalFilter_def.hpp:385
virtual mag_type getFrobeniusNorm() const
Returns the Frobenius norm of the matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:391
virtual Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns the communicator.
Definition: Ifpack2_DiagonalFilter_def.hpp:99
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:351
virtual size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
Definition: Ifpack2_DiagonalFilter_def.hpp:211
virtual bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Definition: Ifpack2_DiagonalFilter_def.hpp:379
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:357
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:364
virtual Teuchos::RCP< const Tpetra::RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const
Returns the RowGraph associated with this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:145
virtual global_size_t getGlobalNumCols() const
Returns the number of global columns in this matrix.
Definition: Ifpack2_DiagonalFilter_def.hpp:157
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:274
DiagonalFilter(const Teuchos::RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Matrix, magnitudeType AbsoluteThreshold, magnitudeType RelativeThreshold)
Constructor.
Definition: Ifpack2_DiagonalFilter_def.hpp:58
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:199
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:343
virtual size_t getNodeNumRows() const
Returns the number of rows owned on the calling node.
Definition: Ifpack2_DiagonalFilter_def.hpp:163
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:73
virtual bool isFillComplete() const
Returns true if fillComplete() has been called.
Definition: Ifpack2_DiagonalFilter_def.hpp:235
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:118
virtual size_t getNodeNumCols() 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:169
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:193
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:305