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