Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_Chebyshev_def.hpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 // ***********************************************************************
45 //
46 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
47 // Copyright (2004) Sandia Corporation
48 //
49 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
50 // license for use of this work by or on behalf of the U.S. Government.
51 //
52 // This library is free software; you can redistribute it and/or modify
53 // it under the terms of the GNU Lesser General Public License as
54 // published by the Free Software Foundation; either version 2.1 of the
55 // License, or (at your option) any later version.
56 //
57 // This library is distributed in the hope that it will be useful, but
58 // WITHOUT ANY WARRANTY; without even the implied warranty of
59 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60 // Lesser General Public License for more details.
61 //
62 // You should have received a copy of the GNU Lesser General Public
63 // License along with this library; if not, write to the Free Software
64 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
65 // USA
66 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
67 //
68 // ***********************************************************************
69 
70 #ifndef IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP
71 #define IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP
72 
79 
81 #include <cmath>
82 
83 // Uncommit the #define line below if you want Chebyshev to do extra
84 // debug checking and generate a lot of debugging output to stderr (on
85 // all processes in the communicator). Even if you uncomment this
86 // line, the debugging code will only be enabled if the CMake option
87 // Teuchos_ENABLE_DEBUG was set to ON when configuring Trilinos.
88 //#define IFPACK_DETAILS_CHEBYSHEV_DEBUG 1
89 
90 namespace Ifpack2 {
91 namespace Details {
92 
93 namespace {
94  // We use this text a lot in error messages.
95  const char computeBeforeApplyReminder[] =
96  "This means one of the following:\n"
97  " - you have not yet called compute() on this instance, or \n"
98  " - you didn't call compute() after calling setParameters().\n\n"
99  "After creating an Ifpack2::Chebyshev instance,\n"
100  "you must _always_ call compute() at least once before calling apply().\n"
101  "After calling compute() once, you do not need to call it again,\n"
102  "unless the matrix has changed or you have changed parameters\n"
103  "(by calling setParameters()).";
104 }
105 
106 template<class ScalarType, class MV, class MAT>
107 void
108 Chebyshev<ScalarType, MV, MAT>::
109 checkConstructorInput () const
110 {
111  TEUCHOS_TEST_FOR_EXCEPTION(STS::isComplex, std::logic_error,
112  "Ifpack2::Details::Chebyshev: This class' implementation of Chebyshev "
113  "iteration only works for real-valued, symmetric positive definite "
114  "matrices. However, you instantiated this class for ScalarType="
115  << Teuchos::TypeNameTraits<ScalarType>::name () << ", which is a complex-"
116  "valued type. While this may be algorithmically correct if all of the "
117  "complex numbers in the matrix have zero imaginary part, we forbid using "
118  "complex ScalarType altogether in order to remind you of the limitations "
119  "of our implementation (and of the algorithm itself).");
120  TEUCHOS_TEST_FOR_EXCEPTION(A_.is_null (), std::invalid_argument,
121  "Ifpack2::Chebyshev: Input matrix to constructor is null.");
123  A_->getGlobalNumRows() != A_->getGlobalNumCols(),
124  std::invalid_argument,
125  "Ifpack2::Chebyshev: The input matrix A must be square. "
126  "A has " << A_->getGlobalNumRows() << " rows and "
127  << A_->getGlobalNumCols() << " columns.");
128 
129  // In a debug build, test that the domain and range Maps of the
130  // matrix are the same.
131 #ifdef HAVE_TEUCHOS_DEBUG
132  Teuchos::RCP<const map_type> domainMap = A_->getDomainMap ();
133  Teuchos::RCP<const map_type> rangeMap = A_->getRangeMap ();
134 
135  // isSameAs is a collective, but if the two pointers are the same,
136  // isSameAs will assume that they are the same on all processes, and
137  // return true without an all-reduce.
139  ! domainMap->isSameAs (*rangeMap), std::invalid_argument,
140  "Ifpack2::Chebyshev: The domain Map and range Map of the matrix must be "
141  "the same (in the sense of isSameAs())." << std::endl << "We only check "
142  "for this if Trilinos was built with the CMake configuration option "
143  "Teuchos_ENABLE_DEBUG set to ON.");
144 #endif // HAVE_TEUCHOS_DEBUG
145 }
146 
147 template<class ScalarType, class MV, class MAT>
150  A_ (A),
151  savedDiagOffsets_ (false),
152  computedLambdaMax_ (STS::nan ()),
153  computedLambdaMin_ (STS::nan ()),
154  lambdaMaxForApply_ (STS::nan ()),
155  lambdaMinForApply_ (STS::nan ()),
156  eigRatioForApply_ (STS::nan ()),
157  userLambdaMax_ (STS::nan ()),
158  userLambdaMin_ (STS::nan ()),
159  userEigRatio_ (Teuchos::as<ST> (30)),
160  minDiagVal_ (STS::eps ()),
161  numIters_ (1),
162  eigMaxIters_ (10),
163  zeroStartingSolution_ (true),
164  assumeMatrixUnchanged_ (false),
165  textbookAlgorithm_ (false),
166  computeMaxResNorm_ (false)
167 {
168  checkConstructorInput ();
169 }
170 
171 template<class ScalarType, class MV, class MAT>
174  A_ (A),
175  savedDiagOffsets_ (false),
176  computedLambdaMax_ (STS::nan ()),
177  computedLambdaMin_ (STS::nan ()),
178  lambdaMaxForApply_ (STS::nan ()),
179  lambdaMinForApply_ (STS::nan ()),
180  eigRatioForApply_ (STS::nan ()),
181  userLambdaMax_ (STS::nan ()),
182  userLambdaMin_ (STS::nan ()),
183  userEigRatio_ (Teuchos::as<ST> (30)),
184  minDiagVal_ (STS::eps ()),
185  numIters_ (1),
186  eigMaxIters_ (10),
187  zeroStartingSolution_ (true),
188  assumeMatrixUnchanged_ (false),
189  textbookAlgorithm_ (false),
190  computeMaxResNorm_ (false)
191 {
192  checkConstructorInput ();
193  setParameters (params);
194 }
195 
196 template<class ScalarType, class MV, class MAT>
197 void
200  using Teuchos::RCP;
201  using Teuchos::rcp;
202  using Teuchos::rcp_const_cast;
203  using Teuchos::rcpFromRef;
204 
205  // Note to developers: The logic for this method is complicated,
206  // because we want to accept Ifpack and ML parameters whenever
207  // possible, but we don't want to add their default values to the
208  // user's ParameterList. That's why we do all the isParameter()
209  // checks, instead of using the two-argument version of get()
210  // everywhere. The min and max eigenvalue parameters are also a
211  // special case, because we decide whether or not to do eigenvalue
212  // analysis based on whether the user supplied the max eigenvalue.
213 
214  // Default values of all the parameters.
215  const ST defaultLambdaMax = STS::nan ();
216  const ST defaultLambdaMin = STS::nan ();
217  // 30 is Ifpack::Chebyshev's default. ML has a different default
218  // eigRatio for smoothers and the coarse-grid solve (if using
219  // Chebyshev for that). The former uses 20; the latter uses 30.
220  // We're testing smoothers here, so use 20. (However, if you give
221  // ML an Epetra matrix, it will use Ifpack for Chebyshev, in which
222  // case it would defer to Ifpack's default settings.)
223  const ST defaultEigRatio = Teuchos::as<ST> (30);
224  const ST defaultMinDiagVal = STS::eps ();
225  const int defaultNumIters = 1;
226  const int defaultEigMaxIters = 10;
227  const bool defaultZeroStartingSolution = true; // Ifpack::Chebyshev default
228  const bool defaultAssumeMatrixUnchanged = false;
229  const bool defaultTextbookAlgorithm = false;
230  const bool defaultComputeMaxResNorm = false;
231 
232  // We'll set the instance data transactionally, after all reads
233  // from the ParameterList. That way, if any of the ParameterList
234  // reads fail (e.g., due to the wrong parameter type), we will not
235  // have left the instance data in a half-changed state.
236  RCP<const V> userInvDiag;
237  ST lambdaMax = defaultLambdaMax;
238  ST lambdaMin = defaultLambdaMin;
239  ST eigRatio = defaultEigRatio;
240  ST minDiagVal = defaultMinDiagVal;
241  int numIters = defaultNumIters;
242  int eigMaxIters = defaultEigMaxIters;
243  bool zeroStartingSolution = defaultZeroStartingSolution;
244  bool assumeMatrixUnchanged = defaultAssumeMatrixUnchanged;
245  bool textbookAlgorithm = defaultTextbookAlgorithm;
246  bool computeMaxResNorm = defaultComputeMaxResNorm;
247 
248  // Fetch the parameters from the ParameterList. Defer all
249  // externally visible side effects until we have finished all
250  // ParameterList interaction. This makes the method satisfy the
251  // strong exception guarantee.
252 
253  // Get the user-supplied inverse diagonal.
254  //
255  // Check for a raw pointer (const V* or V*), for Ifpack
256  // compatibility, as well as for RCP<const V>, RCP<V>, const V, or
257  // V. We'll copy the vector anyway, so it doesn't matter whether
258  // it's const or nonconst.
259  if (plist.isParameter ("chebyshev: operator inv diagonal")) {
260  try { // Could the type be const V*?
261  const V* rawUserInvDiag = plist.get<const V*> ("chebyshev: operator inv diagonal");
262  // It's OK to have a nonowning reference; we'll copy the vector anyway.
263  userInvDiag = rcp (rawUserInvDiag, false);
265  }
266  if (userInvDiag.is_null ()) {
267  try { // Could the type be V*?
268  V* rawUserInvDiag = plist.get<V*> ("chebyshev: operator inv diagonal");
269  // It's OK to have a nonowning reference; we'll copy the vector anyway.
270  userInvDiag = rcp (const_cast<const V*> (rawUserInvDiag), false);
272  }
273  }
274  if (userInvDiag.is_null ()) {
275  try { // Could the type be RCP<const V>?
276  userInvDiag = plist.get<RCP<const V> > ("chebyshev: operator inv diagonal");
278  }
279  }
280  if (userInvDiag.is_null ()) {
281  try { // Could the type be RCP<V>?
282  RCP<V> userInvDiagNonconst = plist.get<RCP<V> > ("chebyshev: operator inv diagonal");
283  userInvDiag = rcp_const_cast<const V> (userInvDiagNonconst);
285  }
286  }
287  if (userInvDiag.is_null ()) {
288  try { // Could the type be const V?
289  // The line below does a deep copy (V::operator=).
290  userInvDiag = rcp (new V (plist.get<const V> ("chebyshev: operator inv diagonal")));
292  }
293  }
294  if (userInvDiag.is_null ()) {
295  try { // Could the type be V?
296  V userInvDiagNonconst = plist.get<V> ("chebyshev: operator inv diagonal");
297  userInvDiag = rcp (new V (userInvDiagNonconst)); // deep copy
299  }
300  }
301  // We've tried all the possible types. If we got something, then
302  // make a range Map copy of it.
303  if (! userInvDiag.is_null ()) {
304  userInvDiag = makeRangeMapVector (*userInvDiag);
305  }
306  }
307 
308  // Don't fill in defaults for the max or min eigenvalue, because
309  // this class uses the existence of those parameters to determine
310  // whether it should do eigenanalysis.
311  if (plist.isParameter ("chebyshev: max eigenvalue")) {
312  lambdaMax = plist.get<ST> ("chebyshev: max eigenvalue");
314  STS::isnaninf (lambdaMax), std::invalid_argument,
315  "Ifpack2::Chebyshev::setParameters: \"chebyshev: max eigenvalue\" "
316  "parameter is NaN or Inf. This parameter is optional, but if you "
317  "choose to supply it, it must have a finite value.");
318  }
319  if (plist.isParameter ("chebyshev: min eigenvalue")) {
320  lambdaMin = plist.get<ST> ("chebyshev: min eigenvalue");
322  STS::isnaninf (lambdaMin), std::invalid_argument,
323  "Ifpack2::Chebyshev::setParameters: \"chebyshev: min eigenvalue\" "
324  "parameter is NaN or Inf. This parameter is optional, but if you "
325  "choose to supply it, it must have a finite value.");
326  }
327 
328  // Only fill in Ifpack2's name for the default parameter, not ML's.
329  if (plist.isParameter ("smoother: Chebyshev alpha")) { // ML compatibility
330  eigRatio = plist.get<ST> ("smoother: Chebyshev alpha");
331  }
332  // Ifpack2's name overrides ML's name.
333  eigRatio = plist.get ("chebyshev: ratio eigenvalue", eigRatio);
335  STS::isnaninf (eigRatio), std::invalid_argument,
336  "Ifpack2::Chebyshev::setParameters: \"chebyshev: ratio eigenvalue\" "
337  "parameter (also called \"smoother: Chebyshev alpha\") is NaN or Inf. "
338  "This parameter is optional, but if you choose to supply it, it must have "
339  "a finite value.");
340  // mfh 11 Feb 2013: This class is currently only correct for real
341  // Scalar types, but we still want it to build for complex Scalar
342  // type so that users of Ifpack2::Factory can build their
343  // executables for real or complex Scalar type. Thus, we take the
344  // real parts here, which are always less-than comparable.
346  STS::real (eigRatio) < STS::real (STS::one ()),
347  std::invalid_argument,
348  "Ifpack2::Chebyshev::setParameters: \"chebyshev: ratio eigenvalue\""
349  "parameter (also called \"smoother: Chebyshev alpha\") must be >= 1, "
350  "but you supplied the value " << eigRatio << ".");
351 
352  // Same name in Ifpack2 and Ifpack.
353  minDiagVal = plist.get ("chebyshev: min diagonal value", minDiagVal);
355  STS::isnaninf (minDiagVal), std::invalid_argument,
356  "Ifpack2::Chebyshev::setParameters: \"chebyshev: min diagonal value\" "
357  "parameter is NaN or Inf. This parameter is optional, but if you choose "
358  "to supply it, it must have a finite value.");
359 
360  // Only fill in Ifpack2's name, not ML's or Ifpack's.
361  if (plist.isParameter ("smoother: sweeps")) { // ML compatibility
362  numIters = plist.get<int> ("smoother: sweeps");
363  } // Ifpack's name overrides ML's name.
364  if (plist.isParameter ("relaxation: sweeps")) { // Ifpack compatibility
365  numIters = plist.get<int> ("relaxation: sweeps");
366  } // Ifpack2's name overrides Ifpack's name.
367  numIters = plist.get ("chebyshev: degree", numIters);
369  numIters < 0, std::invalid_argument,
370  "Ifpack2::Chebyshev::setParameters: \"chebyshev: degree\" parameter (also "
371  "called \"smoother: sweeps\" or \"relaxation: sweeps\") must be a "
372  "nonnegative integer. You gave a value of " << numIters << ".");
373 
374  // The last parameter name overrides the first.
375  if (plist.isParameter ("eigen-analysis: iterations")) { // ML compatibility
376  eigMaxIters = plist.get<int> ("eigen-analysis: iterations");
377  } // Ifpack2's name overrides ML's name.
378  eigMaxIters = plist.get ("chebyshev: eigenvalue max iterations", eigMaxIters);
380  eigMaxIters < 0, std::invalid_argument,
381  "Ifpack2::Chebyshev::setParameters: \"chebyshev: eigenvalue max iterations"
382  "\" parameter (also called \"eigen-analysis: iterations\") must be a "
383  "nonnegative integer. You gave a value of " << eigMaxIters << ".");
384 
385  zeroStartingSolution = plist.get ("chebyshev: zero starting solution",
386  zeroStartingSolution);
387  assumeMatrixUnchanged = plist.get ("chebyshev: assume matrix does not change",
388  assumeMatrixUnchanged);
389 
390  // We don't want to fill these parameters in, because they shouldn't
391  // be visible to Ifpack2::Chebyshev users.
392  if (plist.isParameter ("chebyshev: textbook algorithm")) {
393  textbookAlgorithm = plist.get<bool> ("chebyshev: textbook algorithm");
394  }
395  if (plist.isParameter ("chebyshev: compute max residual norm")) {
396  computeMaxResNorm = plist.get<bool> ("chebyshev: compute max residual norm");
397  }
398 
399  // Test for Ifpack parameters that we won't ever implement here.
400  // Be careful to use the one-argument version of get(), since the
401  // two-argment version adds the parameter if it's not there.
403  plist.isParameter ("chebyshev: use block mode") &&
404  ! plist.get<bool> ("chebyshev: use block mode"),
405  std::invalid_argument,
406  "Ifpack2::Chebyshev requires that if you supply the Ifpack parameter "
407  "\"chebyshev: use block mode\", it must be set to false. Ifpack2's "
408  "Chebyshev does not implement Ifpack's block mode.");
410  plist.isParameter ("chebyshev: solve normal equations") &&
411  ! plist.get<bool> ("chebyshev: solve normal equations"),
412  std::invalid_argument,
413  "Ifpack2::Chebyshev does not and will never implement the Ifpack "
414  "parameter \"chebyshev: solve normal equations\". If you want to solve "
415  "the normal equations, construct a Tpetra::Operator that implements "
416  "A^* A, and use Chebyshev to solve A^* A x = A^* b.");
417 
418  // Test for Ifpack parameters that we haven't implemented yet.
419  //
420  // For now, we only check that this ML parameter, if provided, has
421  // the one value that we support. We consider other values "invalid
422  // arguments" rather than "logic errors," because Ifpack does not
423  // implement eigenanalyses other than the power method.
424  std::string eigenAnalysisType ("power-method");
425  if (plist.isParameter ("eigen-analysis: type")) {
426  eigenAnalysisType = plist.get<std::string> ("eigen-analysis: type");
428  eigenAnalysisType != "power-method" &&
429  eigenAnalysisType != "power method",
430  std::invalid_argument,
431  "Ifpack2::Chebyshev: This class supports the ML parameter \"eigen-"
432  "analysis: type\" for backwards compatibility. However, Ifpack2 "
433  "currently only supports the \"power-method\" option for this "
434  "parameter. This imitates Ifpack, which only implements the power "
435  "method for eigenanalysis.");
436  }
437 
438  // We've validated all the parameters, so it's safe now to "commit" them.
439  userInvDiag_ = userInvDiag;
440  userLambdaMax_ = lambdaMax;
441  userLambdaMin_ = lambdaMin;
442  userEigRatio_ = eigRatio;
443  minDiagVal_ = minDiagVal;
444  numIters_ = numIters;
445  eigMaxIters_ = eigMaxIters;
446  zeroStartingSolution_ = zeroStartingSolution;
447  assumeMatrixUnchanged_ = assumeMatrixUnchanged;
448  textbookAlgorithm_ = textbookAlgorithm;
449  computeMaxResNorm_ = computeMaxResNorm;
450 }
451 
452 template<class ScalarType, class MV, class MAT>
453 void
455 compute () {
456  using std::endl;
457 
458  // Some of the optimizations below only work if A_ is a
459  // Tpetra::CrsMatrix. We'll make our best guess about its type
460  // here, since we have no way to get back the original fifth
461  // template parameter.
462  typedef Tpetra::CrsMatrix<typename MV::scalar_type,
463  typename MV::local_ordinal_type,
464  typename MV::global_ordinal_type,
465  typename MV::node_type> crs_matrix_type;
466 
467  // If A_ is a CrsMatrix and its graph is constant, we presume that
468  // the user plans to reuse the structure of A_, but possibly change
469  // A_'s values before each compute() call. This is the intended use
470  // case for caching the offsets of the diagonal entries of A_, to
471  // speed up extraction of diagonal entries on subsequent compute()
472  // calls.
473 
474  // FIXME (mfh 22 Jan 2013, 10 Feb 2013) In all cases when we use
475  // isnaninf() in this method, we really only want to check if the
476  // number is NaN. Inf means something different. However,
477  // Teuchos::ScalarTraits doesn't distinguish the two cases.
478 
479  if (userInvDiag_.is_null ()) {
481  Teuchos::rcp_dynamic_cast<const crs_matrix_type> (A_);
482 
483  if (D_.is_null ()) { // We haven't computed D_ before
484  if (! A_crsMat.is_null () && A_crsMat->isStaticGraph ()) {
485  // It's a CrsMatrix with a const graph; cache diagonal offsets.
486  A_crsMat->getLocalDiagOffsets (diagOffsets_);
487  savedDiagOffsets_ = true;
488  D_ = makeInverseDiagonal (*A_, true);
489  }
490  else { // either A_ is not a CrsMatrix, or its graph is nonconst
491  D_ = makeInverseDiagonal (*A_);
492  }
493  }
494  else if (! assumeMatrixUnchanged_) { // D_ exists but A_ may have changed
495  if (! A_crsMat.is_null () && A_crsMat->isStaticGraph ()) {
496  // It's a CrsMatrix with a const graph; cache diagonal offsets
497  // if we haven't already.
498  if (! savedDiagOffsets_) {
499  A_crsMat->getLocalDiagOffsets (diagOffsets_);
500  savedDiagOffsets_ = true;
501  }
502  // Now we're guaranteed to have cached diagonal offsets.
503  D_ = makeInverseDiagonal (*A_, true);
504  }
505  else { // either A_ is not a CrsMatrix, or its graph is nonconst
506  D_ = makeInverseDiagonal (*A_);
507  }
508  }
509  }
510  else { // the user provided an inverse diagonal
511  D_ = userInvDiag_;
512  }
513 
514  // Have we estimated eigenvalues before?
515  const bool computedEigenvalueEstimates =
516  STS::isnaninf (computedLambdaMax_) || STS::isnaninf (computedLambdaMin_);
517 
518  // Only recompute the eigenvalue estimates if
519  // - we are supposed to assume that the matrix may have changed, or
520  // - they haven't been computed before, and the user hasn't given
521  // us at least an estimate of the max eigenvalue.
522  //
523  // We at least need an estimate of the max eigenvalue. This is the
524  // most important one if using Chebyshev as a smoother.
525  if (! assumeMatrixUnchanged_ ||
526  (! computedEigenvalueEstimates && STS::isnaninf (userLambdaMax_))) {
527  const ST computedLambdaMax = powerMethod (*A_, *D_, eigMaxIters_);
529  STS::isnaninf (computedLambdaMax),
530  std::runtime_error,
531  "Ifpack2::Chebyshev::compute: Estimation of the max eigenvalue "
532  "of D^{-1} A failed, by producing Inf or NaN. This probably means that "
533  "the matrix contains Inf or NaN values, or that it is badly scaled.");
535  STS::isnaninf (userEigRatio_),
536  std::logic_error,
537  "Ifpack2::Chebyshev::compute: userEigRatio_ is Inf or NaN."
538  << endl << "This should be impossible." << endl <<
539  "Please report this bug to the Ifpack2 developers.");
540 
541  // The power method doesn't estimate the min eigenvalue, so we
542  // do our best to provide an estimate. userEigRatio_ has a
543  // reasonable default value, and if the user provided it, we
544  // have already checked that its value is finite and >= 1.
545  const ST computedLambdaMin = computedLambdaMax / userEigRatio_;
546 
547  // Defer "committing" results until all computations succeeded.
548  computedLambdaMax_ = computedLambdaMax;
549  computedLambdaMin_ = computedLambdaMin;
550  } else {
552  STS::isnaninf (userLambdaMax_) && STS::isnaninf (computedLambdaMax_),
553  std::logic_error,
554  "Ifpack2::Chebyshev::compute: " << endl <<
555  "Both userLambdaMax_ and computedLambdaMax_ are Inf or NaN."
556  << endl << "This should be impossible." << endl <<
557  "Please report this bug to the Ifpack2 developers.");
558  }
559 
561  // Figure out the eigenvalue estimates that apply() will use.
563 
564  // Always favor the user's max eigenvalue estimate, if provided.
565  if (STS::isnaninf (userLambdaMax_)) {
566  lambdaMaxForApply_ = computedLambdaMax_;
567  } else {
568  lambdaMaxForApply_ = userLambdaMax_;
569  }
570  // mfh 11 Feb 2013: For now, we imitate Ifpack by ignoring the
571  // user's min eigenvalue estimate, and using the given eigenvalue
572  // ratio to estimate the min eigenvalue. We could instead do this:
573  // favor the user's eigenvalue ratio estimate, but if it's not
574  // provided, use lambdaMax / lambdaMin. However, we want Chebyshev
575  // to have sensible smoother behavior if the user did not provide
576  // eigenvalue estimates. Ifpack's behavior attempts to push down
577  // the error terms associated with the largest eigenvalues, while
578  // expecting that users will only want a small number of iterations,
579  // so that error terms associated with the smallest eigenvalues
580  // won't grow too much. This is sensible behavior for a smoother.
581  lambdaMinForApply_ = lambdaMaxForApply_ / userEigRatio_;
582  eigRatioForApply_ = userEigRatio_;
583 
584  if (! textbookAlgorithm_) {
585  // Ifpack has a special-case modification of the eigenvalue bounds
586  // for the case where the max eigenvalue estimate is close to one.
587  const ST one = Teuchos::as<ST> (1);
588  if (STS::magnitude (lambdaMaxForApply_ - one) < Teuchos::as<MT> (1.0e-6)) {
589  lambdaMinForApply_ = one;
590  lambdaMaxForApply_ = lambdaMinForApply_;
591  eigRatioForApply_ = one; // Ifpack doesn't include this line.
592  }
593  }
594 } //compute()
595 
596 template<class ScalarType, class MV, class MAT>
597 ScalarType
598 Chebyshev<ScalarType, MV, MAT>::
599 getLambdaMaxForApply() const {
600  return lambdaMaxForApply_;
601 }
602 
603 template<class ScalarType, class MV, class MAT>
604 typename Chebyshev<ScalarType, MV, MAT>::MT
606 apply (const MV& B, MV& X) {
608  STS::isnaninf (lambdaMaxForApply_), std::runtime_error,
609  "Ifpack2::Chebyshev::apply: There is no estimate for the max eigenvalue."
610  << std::endl << computeBeforeApplyReminder);
612  STS::isnaninf (lambdaMinForApply_), std::runtime_error,
613  "Ifpack2::Chebyshev::apply: There is no estimate for the min eigenvalue."
614  << std::endl << computeBeforeApplyReminder);
616  STS::isnaninf (eigRatioForApply_), std::runtime_error,
617  "Ifpack2::Chebyshev::apply: There is no estimate for the ratio of the max "
618  "eigenvalue to the min eigenvalue."
619  << std::endl << computeBeforeApplyReminder);
621  D_.is_null (), std::runtime_error,
622  "Ifpack2::Chebyshev::apply: "
623  "The vector of inverse diagonal entries of the matrix has not yet been "
624  "computed." << std::endl << computeBeforeApplyReminder);
625 
626  if (textbookAlgorithm_) {
627  textbookApplyImpl (*A_, B, X, numIters_, lambdaMaxForApply_,
628  lambdaMinForApply_, eigRatioForApply_, *D_);
629  } else {
630  ifpackApplyImpl (*A_, B, X, numIters_, lambdaMaxForApply_,
631  lambdaMinForApply_, eigRatioForApply_, *D_);
632  }
633 
634  if (computeMaxResNorm_ && B.getNumVectors () > 0) {
635  MV R (B.getMap (), B.getNumVectors ());
636  computeResidual (R, B, *A_, X);
637  Teuchos::Array<MT> norms (B.getNumVectors ());
638  R.norm2 (norms ());
639  return *std::max_element (norms.begin (), norms.end ());
640  } else {
642  }
643 }
644 
645 template<class ScalarType, class MV, class MAT>
646 void
648 print (std::ostream& out) {
649  this->describe (* (Teuchos::getFancyOStream (Teuchos::rcpFromRef (out))),
651 }
652 
653 template<class ScalarType, class MV, class MAT>
654 void
656 computeResidual (MV& R, const MV& B, const MAT& A, const MV& X,
657  const Teuchos::ETransp mode)
658 {
659  R = B;
660  A.apply (X, R, mode, -STS::one(), STS::one());
661 }
662 
663 template<class ScalarType, class MV, class MAT>
664 void
665 Chebyshev<ScalarType, MV, MAT>::
666 solve (MV& Z, const V& D_inv, const MV& R) {
667  Z.elementWiseMultiply (STS::one(), D_inv, R, STS::zero());
668 }
669 
670 template<class ScalarType, class MV, class MAT>
671 void
672 Chebyshev<ScalarType, MV, MAT>::
673 solve (MV& Z, const ST alpha, const V& D_inv, const MV& R) {
674  Z.elementWiseMultiply (alpha, D_inv, R, STS::zero());
675 }
676 
677 template<class ScalarType, class MV, class MAT>
679 Chebyshev<ScalarType, MV, MAT>::
680 makeInverseDiagonal (const MAT& A, const bool useDiagOffsets) const {
681  using Teuchos::RCP;
682  using Teuchos::rcpFromRef;
683  using Teuchos::rcp_dynamic_cast;
684 
685  RCP<V> D_rowMap (new V (A.getGraph ()->getRowMap ()));
686  if (useDiagOffsets) {
687  // The optimizations below only work if A_ is a Tpetra::CrsMatrix.
688  // We'll make our best guess about its type here, since we have no
689  // way to get back the original fifth template parameter.
690  typedef Tpetra::CrsMatrix<typename MV::scalar_type,
691  typename MV::local_ordinal_type,
692  typename MV::global_ordinal_type,
693  typename MV::node_type> crs_matrix_type;
694  RCP<const crs_matrix_type> A_crsMat =
695  rcp_dynamic_cast<const crs_matrix_type> (rcpFromRef (A));
696  if (! A_crsMat.is_null ()) {
698  ! savedDiagOffsets_, std::logic_error,
699  "Ifpack2::Details::Chebyshev::makeInverseDiagonal: "
700  "It is not allowed to call this method with useDiagOffsets=true, "
701  "if you have not previously saved offsets of diagonal entries. "
702  "This situation should never arise if this class is used properly. "
703  "Please report this bug to the Ifpack2 developers.");
704  A_crsMat->getLocalDiagCopy (*D_rowMap, diagOffsets_ ());
705  }
706  }
707  else {
708  A.getLocalDiagCopy (*D_rowMap);
709  }
710  RCP<V> D_rangeMap = makeRangeMapVector (*D_rowMap);
711 
712  // Invert the diagonal entries, replacing entries less (in
713  // magnitude) than the user-specified value with that value.
714  typedef Kokkos::MultiVector<ST, typename MAT::node_type> KMV;
715  KMV& localDiag = D_rangeMap->getLocalMVNonConst ();
716  typedef Kokkos::DefaultArithmetic<KMV> KMVT;
717  KMVT::ReciprocalThreshold (localDiag, minDiagVal_);
718  return D_rangeMap;
719 }
720 
721 template<class ScalarType, class MV, class MAT>
723 Chebyshev<ScalarType, MV, MAT>::
724 makeRangeMapVector (const V& D) const {
725  using Teuchos::RCP;
726  using Teuchos::rcp;
727  typedef Tpetra::Export<typename MV::local_ordinal_type,
728  typename MV::global_ordinal_type,
729  typename MV::node_type> export_type;
730  RCP<const map_type> sourceMap = D.getMap ();
731  RCP<const map_type> rangeMap = A_->getRangeMap ();
732  RCP<const map_type> rowMap = A_->getRowMap ();
733  RCP<V> D_out;
734 
735  if (rangeMap.getRawPtr () == sourceMap.getRawPtr ()) {
736  // The given vector's Map is identical to the matrix's range Map.
737  // That means we don't need to Export. (We don't call isSameAs()
738  // here, because that takes at least one global reduction. This
739  // is the fast path. We may call isSameAs() in the slow path
740  // below, to avoid an even slower path.
741  D_out = rcp (new V (D));
742  }
743  else { // We need to Export.
744  RCP<const export_type> exporter;
745  // Making an Export object from scratch is expensive enough that
746  // it's worth the O(1) global reductions to call isSameAs(), to
747  // see if we can avoid that cost.
748  if (sourceMap.getRawPtr () == rowMap.getRawPtr () ||
749  sourceMap->isSameAs (*rowMap)) {
750  // We can reuse the matrix's Export object, if there is one.
751  exporter = A_->getGraph ()->getExporter ();
752  }
753  else { // We have to make a new Export object.
754  exporter = rcp (new export_type (sourceMap, rangeMap));
755  }
756 
757  D_out = rcp (new V (D));
758  if (exporter.is_null ()) {
759  // Row Map and range Map are the same; no need to Export.
760  *D_out = D;
761  }
762  else {
763  D_out->doExport (D, *exporter, Tpetra::ADD);
764  }
765  } // if we don't need to Export, or if we do
766 
767  return D_out;
768 }
769 
770 
771 template<class ScalarType, class MV, class MAT>
772 void
773 Chebyshev<ScalarType, MV, MAT>::
774 textbookApplyImpl (const MAT& A,
775  const MV& B,
776  MV& X,
777  const int numIters,
778  const ST lambdaMax,
779  const ST lambdaMin,
780  const ST eigRatio,
781  const V& D_inv) const
782 {
783  (void) lambdaMin; // Forestall compiler warning.
784  const ST myLambdaMin = lambdaMax / eigRatio;
785 
786  const ST zero = Teuchos::as<ST> (0);
787  const ST one = Teuchos::as<ST> (1);
788  const ST two = Teuchos::as<ST> (2);
789  const ST d = (lambdaMax + myLambdaMin) / two; // Ifpack2 calls this theta
790  const ST c = (lambdaMax - myLambdaMin) / two; // Ifpack2 calls this 1/delta
791 
792  if (zeroStartingSolution_ && numIters > 0) {
793  // If zero iterations, then input X is output X.
794  X.putScalar (zero);
795  }
796  MV R (B.getMap (), B.getNumVectors (), false);
797  MV P (B.getMap (), B.getNumVectors (), false);
798  MV Z (B.getMap (), B.getNumVectors (), false);
799  ST alpha, beta;
800  for (int i = 0; i < numIters; ++i) {
801  computeResidual (R, B, A, X); // R = B - A*X
802  solve (Z, D_inv, R); // z = D_inv * R, that is, D \ R.
803  if (i == 0) {
804  P = Z;
805  alpha = two / d;
806  } else {
807  //beta = (c * alpha / two)^2;
808  //const ST sqrtBeta = c * alpha / two;
809  //beta = sqrtBeta * sqrtBeta;
810  beta = alpha * (c/two) * (c/two);
811  alpha = one / (d - beta);
812  P.update (one, Z, beta); // P = Z + beta*P
813  }
814  X.update (alpha, P, one); // X = X + alpha*P
815  // If we compute the residual here, we could either do R = B -
816  // A*X, or R = R - alpha*A*P. Since we choose the former, we
817  // can move the computeResidual call to the top of the loop.
818  }
819 }
820 
821 template<class ScalarType, class MV, class MAT>
822 typename Chebyshev<ScalarType, MV, MAT>::MT
823 Chebyshev<ScalarType, MV, MAT>::maxNormInf (const MV& X) {
824  std::vector<MT> norms (X.getNumVectors ());
825  X.normInf (norms);
826  return *std::max_element (norms.begin (), norms.end ());
827 }
828 
829 template<class ScalarType, class MV, class MAT>
830 void
831 Chebyshev<ScalarType, MV, MAT>::
832 ifpackApplyImpl (const MAT& A,
833  const MV& B,
834  MV& X,
835  const int numIters,
836  const ST lambdaMax,
837  const ST lambdaMin,
838  const ST eigRatio,
839  const V& D_inv)
840 {
841 #ifdef HAVE_TEUCHOS_DEBUG
842 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
843  using std::cerr;
844  using std::endl;
845  cerr << "\\|B\\|_{\\infty} = " << maxNormInf (B) << endl;
846  cerr << "\\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
847 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
848 #endif // HAVE_TEUCHOS_DEBUG
849 
850  if (numIters <= 0) {
851  return;
852  }
853  const ST one = Teuchos::as<ST> (1);
854  const ST two = Teuchos::as<ST> (2);
855 
856  // Quick solve when the matrix A is the identity.
857  if (lambdaMin == one && lambdaMax == lambdaMin) {
858  solve (X, D_inv, B);
859  return;
860  }
861 
862  // Initialize coefficients
863  const ST alpha = lambdaMax / eigRatio;
864  const ST beta = Teuchos::as<ST> (1.1) * lambdaMax;
865  const ST delta = two / (beta - alpha);
866  const ST theta = (beta + alpha) / two;
867  const ST s1 = theta * delta;
868 
869 #ifdef HAVE_TEUCHOS_DEBUG
870 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
871  cerr << "alpha = " << alpha << endl
872  << "beta = " << beta << endl
873  << "delta = " << delta << endl
874  << "theta = " << theta << endl
875  << "s1 = " << s1 << endl;
876 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
877 #endif // HAVE_TEUCHOS_DEBUG
878 
879  // Fetch cached temporary vectors.
880  Teuchos::RCP<MV> V_ptr, W_ptr;
881  makeTempMultiVectors (V_ptr, W_ptr, B);
882 
883  // mfh 28 Jan 2013: We write V1 instead of V, so as not to confuse
884  // the multivector V with the typedef V (for Tpetra::Vector).
885  //MV V1 (B.getMap (), B.getNumVectors (), false);
886  //MV W (B.getMap (), B.getNumVectors (), false);
887  MV& V1 = *V_ptr;
888  MV& W = *W_ptr;
889 
890 #ifdef HAVE_TEUCHOS_DEBUG
891 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
892  cerr << "Iteration " << 1 << ":" << endl
893  << "- \\|D\\|_{\\infty} = " << D_->normInf () << endl;
894 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
895 #endif // HAVE_TEUCHOS_DEBUG
896 
897  // Special case for the first iteration.
898  if (! zeroStartingSolution_) {
899  computeResidual (V1, B, A, X); // V1 = B - A*X
900 
901 #ifdef HAVE_TEUCHOS_DEBUG
902 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
903  cerr << "- \\|B - A*X\\|_{\\infty} = " << maxNormInf (V1) << endl;
904 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
905 #endif // HAVE_TEUCHOS_DEBUG
906 
907  solve (W, one/theta, D_inv, V1); // W = (1/theta)*D_inv*(B-A*X)
908 
909 #ifdef HAVE_TEUCHOS_DEBUG
910 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
911  cerr << "- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
912 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
913 #endif // HAVE_TEUCHOS_DEBUG
914 
915  X.update (one, W, one); // X = X + W
916  } else {
917  solve (W, one/theta, D_inv, B); // W = (1/theta)*D_inv*B
918 
919 #ifdef HAVE_TEUCHOS_DEBUG
920 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
921  cerr << "- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
922 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
923 #endif // HAVE_TEUCHOS_DEBUG
924 
925  X = W; // X = 0 + W
926  }
927 #ifdef HAVE_TEUCHOS_DEBUG
928 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
929  cerr << "- \\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
930 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
931 #endif // HAVE_TEUCHOS_DEBUG
932 
933  // The rest of the iterations.
934  ST rhok = one / s1;
935  ST rhokp1, dtemp1, dtemp2;
936  for (int deg = 1; deg < numIters; ++deg) {
937 
938 #ifdef HAVE_TEUCHOS_DEBUG
939 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
940  cerr << "Iteration " << deg+1 << ":" << endl;
941  cerr << "- \\|D\\|_{\\infty} = " << D_->normInf () << endl;
942  cerr << "- \\|B\\|_{\\infty} = " << maxNormInf (B) << endl;
943  cerr << "- \\|A\\|_{\\text{frob}} = " << A_->getFrobeniusNorm () << endl;
944  cerr << "- rhok = " << rhok << endl;
945  V1.putScalar (STS::zero ());
946 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
947 #endif // HAVE_TEUCHOS_DEBUG
948 
949  computeResidual (V1, B, A, X); // V1 = B - A*X
950 
951 #ifdef HAVE_TEUCHOS_DEBUG
952 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
953  cerr << "- \\|B - A*X\\|_{\\infty} = " << maxNormInf (V1) << endl;
954 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
955 #endif // HAVE_TEUCHOS_DEBUG
956 
957  rhokp1 = one / (two * s1 - rhok);
958  dtemp1 = rhokp1 * rhok;
959  dtemp2 = two * rhokp1 * delta;
960  rhok = rhokp1;
961 
962 #ifdef HAVE_TEUCHOS_DEBUG
963 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
964  cerr << "- dtemp1 = " << dtemp1 << endl
965  << "- dtemp2 = " << dtemp2 << endl;
966 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
967 #endif // HAVE_TEUCHOS_DEBUG
968 
969  W.scale (dtemp1);
970  W.elementWiseMultiply (dtemp2, D_inv, V1, one);
971  X.update (one, W, one);
972 
973 #ifdef HAVE_TEUCHOS_DEBUG
974 #ifdef IFPACK_DETAILS_CHEBYSHEV_DEBUG
975  cerr << "- \\|W\\|_{\\infty} = " << maxNormInf (W) << endl;
976  cerr << "- \\|X\\|_{\\infty} = " << maxNormInf (X) << endl;
977 #endif // IFPACK_DETAILS_CHEBYSHEV_DEBUG
978 #endif // HAVE_TEUCHOS_DEBUG
979  }
980 }
981 
982 template<class ScalarType, class MV, class MAT>
983 typename Chebyshev<ScalarType, MV, MAT>::ST
984 Chebyshev<ScalarType, MV, MAT>::
985 powerMethod (const MAT& A, const V& D_inv, const int numIters) {
986  const ST zero = Teuchos::as<ST> (0);
987  const ST one = Teuchos::as<ST> (1);
988  ST lambdaMax = zero;
989  ST RQ_top, RQ_bottom, norm;
990 
991  V x (A.getDomainMap ());
992  V y (A.getRangeMap ());
993  x.randomize ();
994  norm = x.norm2 ();
995  TEUCHOS_TEST_FOR_EXCEPTION(norm == zero, std::runtime_error,
996  "Ifpack2::Chebyshev::powerMethod: "
997  "Tpetra::Vector's randomize() method filled the vector "
998  "with zeros. This is not impossible, but is unlikely. "
999  "It's far more likely that there is a bug in Tpetra.");
1000 
1001  x.scale (one / norm);
1002  for (int iter = 0; iter < numIters; ++iter) {
1003  A.apply (x, y);
1004  solve (y, D_inv, y);
1005  RQ_top = y.dot (x);
1006  RQ_bottom = x.dot (x);
1007  lambdaMax = RQ_top / RQ_bottom;
1008  norm = y.norm2 ();
1009  if (norm == zero) { // Return something reasonable.
1010  return zero;
1011  }
1012  x.update (one / norm, y, zero);
1013  }
1014  return lambdaMax;
1015 }
1016 
1017 template<class ScalarType, class MV, class MAT>
1020 getMatrix() const {
1021  return A_;
1022 }
1023 
1024 template<class ScalarType, class MV, class MAT>
1025 bool
1027 hasTransposeApply() const {
1028  // Technically, this is true, because the matrix must be symmetric.
1029  return true;
1030 }
1031 
1032 template<class ScalarType, class MV, class MAT>
1033 void
1034 Chebyshev<ScalarType, MV, MAT>::
1035 makeTempMultiVectors (Teuchos::RCP<MV>& V1,
1036  Teuchos::RCP<MV>& W,
1037  const MV& X)
1038 {
1039  if (V_.is_null ()) {
1040  V_ = Teuchos::rcp (new MV (X.getMap (), X.getNumVectors (), false));
1041  }
1042  //W must be initialized to zero when it is used as a multigrid smoother.
1043  if (W_.is_null ()) {
1044  W_ = Teuchos::rcp (new MV (X.getMap (), X.getNumVectors (), true));
1045  }
1046  V1 = V_;
1047  W = W_;
1048 }
1049 
1050 template<class ScalarType, class MV, class MAT>
1051 std::string
1053 description() const {
1054  std::ostringstream oss;
1055  oss << "Ifpack2::Details::Chebyshev : "
1056  << "degree = " << numIters_
1057  << ", lambdaMax = " << lambdaMaxForApply_
1058  << ", alpha = " << eigRatioForApply_
1059  << ", lambdaMin = " << lambdaMinForApply_;
1060 
1061  return oss.str();
1062 }
1063 
1064 template<class ScalarType, class MV, class MAT>
1065 void
1068  const Teuchos::EVerbosityLevel verbLevel) const
1069 {
1071  using std::endl;
1072 
1073  const Teuchos::EVerbosityLevel vl =
1074  (verbLevel == Teuchos::VERB_DEFAULT) ? Teuchos::VERB_LOW : verbLevel;
1075  if (vl > Teuchos::VERB_NONE) {
1076  if (vl == Teuchos::VERB_LOW) {
1077  out << "Ifpack2::Details::Chebyshev" << endl;
1078  } else { // vl > Teuchos::VERB_LOW
1079  // YAML flow mapping style (with curly braces) lets the key (the
1080  // above "description") be arbitrarily long. Simple indenting
1081  // limits the key length to 1024 characters. It's not an issue
1082  // right now, but it's better to be more general.
1083  out << "Ifpack2::Details::Chebyshev {" << endl;
1084  Teuchos::OSTab tab1 (Teuchos::rcpFromRef (out));
1085  out << "Template parameters: {" << endl;
1086  {
1087  Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1088  out << "ScalarType: \"" << TypeNameTraits<ScalarType>::name () << "\"" << endl
1089  << "MV: \"" << TypeNameTraits<MV>::name () << "\"" << endl
1090  << "MAT: \"" << TypeNameTraits<MAT>::name () << "\"" << endl;
1091  }
1092  // "Computed parameters" literally means "parameters whose
1093  // values were computed by compute()."
1094  out << "}" << endl << "Computed parameters: {" << endl;
1095  {
1096  Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1097  // Users might want to see the values in the computed inverse
1098  // diagonal, so we print them out at the highest verbosity.
1099  out << "D_: ";
1100  if (D_.is_null ()) {
1101  out << "unset" << endl;
1102  } else if (vl <= Teuchos::VERB_HIGH) {
1103  out << "set" << endl;
1104  } else { // D_ not null and vl > Teuchos::VERB_HIGH
1105  out << "{" << endl;
1106  {
1107  Teuchos::OSTab tab3 (Teuchos::rcpFromRef (out));
1108  D_->describe (out, vl);
1109  }
1110  out << "}" << endl;
1111  }
1112  // V_ and W_ are scratch space; their values are irrelevant.
1113  // All that matters is whether or not they have been set.
1114  out << "V_: " << (V_.is_null () ? "unset" : "set") << endl
1115  << "W_: " << (W_.is_null () ? "unset" : "set") << endl
1116  << "computedLambdaMax_: " << computedLambdaMax_ << endl
1117  << "computedLambdaMin_: " << computedLambdaMin_ << endl
1118  << "lambdaMaxForApply_: " << lambdaMaxForApply_ << endl
1119  << "lambdaMinForApply_: " << lambdaMinForApply_ << endl
1120  << "eigRatioForApply_: " << eigRatioForApply_ << endl;
1121  }
1122  out << "}" << endl << "User parameters: {" << endl;
1123  {
1124  Teuchos::OSTab tab2 (Teuchos::rcpFromRef (out));
1125  out << "userInvDiag_: ";
1126  if (userInvDiag_.is_null ()) {
1127  out << "unset" << endl;
1128  } else if (vl <= Teuchos::VERB_HIGH) {
1129  out << "set" << endl;
1130  } else { // userInvDiag_ not null and vl > Teuchos::VERB_HIGH
1131  out << "{" << endl;
1132  {
1133  Teuchos::OSTab tab3 (Teuchos::rcpFromRef (out));
1134  userInvDiag_->describe (out, vl);
1135  }
1136  out << "}" << endl;
1137  }
1138  out << "userLambdaMax_: " << userLambdaMax_ << endl
1139  << "userLambdaMin_: " << userLambdaMin_ << endl
1140  << "userEigRatio_: " << userEigRatio_ << endl
1141  << "numIters_: " << numIters_ << endl
1142  << "eigMaxIters_: " << eigMaxIters_ << endl
1143  << "zeroStartingSolution_: " << zeroStartingSolution_ << endl
1144  << "assumeMatrixUnchanged_: " << assumeMatrixUnchanged_ << endl
1145  << "textbookAlgorithm_: " << textbookAlgorithm_ << endl
1146  << "computeMaxResNorm_: " << computeMaxResNorm_ << endl;
1147  }
1148  out << "}" << endl;
1149  }
1150  }
1151 }
1152 
1153 } // namespace Details
1154 } // namespace Ifpack2
1155 
1156 #endif // IFPACK2_DETAILS_CHEBYSHEV_DEF_HPP
MT apply(const MV &B, MV &X)
Solve Ax=b for x with Chebyshev iteration with left diagonal scaling.
Definition: Ifpack2_Chebyshev_def.hpp:199
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print a description of the Chebyshev solver to out.
Definition: Ifpack2_Chebyshev_def.hpp:364
Chebyshev(Teuchos::RCP< const MAT > A)
Definition: Ifpack2_Details_Chebyshev_def.hpp:149
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void print(std::ostream &out)
Print instance data to the given output stream.
Definition: Ifpack2_Details_Chebyshev_def.hpp:648
Left-scaled Chebyshev iteration.
Definition: Ifpack2_Details_Chebyshev_decl.hpp:128
void setParameters(Teuchos::ParameterList &plist)
Set (or reset) parameters.
Definition: Ifpack2_Chebyshev_def.hpp:75
Declaration of Chebyshev implementation.
void compute()
(Re)compute the left scaling D_inv, and estimate min and max eigenvalues of D_inv * A...
Definition: Ifpack2_Chebyshev_def.hpp:270
bool hasTransposeApply() const
Whether it&#39;s possible to apply the transpose of this operator.
Definition: Ifpack2_Chebyshev_def.hpp:115
TypeTo as(const TypeFrom &t)
std::string description() const
A single-line description of the Chebyshev solver.
Definition: Ifpack2_Chebyshev_def.hpp:339
Teuchos::RCP< const MAT > getMatrix() const
Get the matrix given to the constructor.
Definition: Ifpack2_Chebyshev_def.hpp:93
bool is_null() const