1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
|
/*
//@HEADER
// ***********************************************************************
//
// Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
// Copyright (2009) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
//@HEADER
*/
/*! \file Ifpack2_UnitTestChebyshev2.cpp
\brief A convergence test for Ifpack2::Chebyshev.
\author Mark Hoemmen
This test compares Ifpack2's implementation of Chebyshev iteration
(which as of 25 Jan 2013 is a direct imitation of Ifpack's
implementation) against the following implementations:
1. A textbook version of the algorithm
2. A textbook implementation of CG
All three implementations use left diagonal scaling. "Textbook" means
"Templates for the Solution of Linear Systems," 2nd edition. We
include CG just to give us a rough measure of how fast the methods
"should" converge.
The test exercises all three algorithms with a 1-D Poisson equation.
We know the eigenvalues of the matrix exactly as a function of its
dimensions (see Chapter 6 of "Applied Numerical Linear Algebra," James
Demmel, SIAM), so we can give perfect eigenvalue bounds. We also
experiment with variations of max and min eigenvalue estimates and the
eigenvalue ratio parameter.
This test has the following command-line arguments:
- numIters: The number of iterations of Chebyshev or CG.
- localNumRows: The number of rows of the matrix on each process.
- numEigIters: The number of iterations of eigenvalue analysis (the
power method) to find the max eigenvalue of the matrix.
The textbook implementation of Chebyshev converges faster if the
eigenvalue bounds are good, but it is much more sensitive than
Ifpack2::Chebyshev to an incorrect upper bound on the eigenvalues.
This gives me confidence that Ifpack2's version is correct.
There is also dead code for imitating ML's Chebyshev implementation
(ML_Cheby(), in packages/ml/src/Smoother/ml_smoother.c) in
Ifpack2::Details::Chebyshev. I couldn't get it to converge, and
didn't want to waste time. ML uses Ifpack::Chebyshev for the
top-level smoother if you give it an Epetra matrix, so Ifpack's
implementation (which Ifpack2 imitates) been tested in the field.
*/
#include <Ifpack2_ConfigDefs.hpp>
#include <Ifpack2_Chebyshev.hpp>
#include <Ifpack2_UnitTestHelpers.hpp>
#include <Teuchos_DefaultComm.hpp>
#include <Teuchos_oblackholestream.hpp>
#include <Teuchos_ScalarTraits.hpp>
#include <Teuchos_UnitTestHarness.hpp>
#include <Teuchos_UnitTestRepository.hpp>
// mfh 24 Jan 2013: Hack to make the linker stop complaining that it
// can't find the Ifpack2::Details::Chebyshev methods.
#ifdef HAVE_IFPACK2_EXPLICIT_INSTANTIATION
#include "Ifpack2_Details_Chebyshev_def.hpp"
#endif // HAVE_IFPACK2_EXPLICIT_INSTANTIATION
namespace {
/// \class CG
/// \brief Method of conjugate gradients, with left-scaling preconditioning.
/// \tparam ScalarType The type of entries in the matrix and vectors.
/// \tparam MV Specialization of Tpetra::MultiVector.
/// \tparam MAT Corresponding specialization of Tpetra::CrsMatrix.
///
/// This class requires that the matrix A be symmetric
/// (resp. Hermitian) positive definite.
template<class ScalarType, class MV, class MAT>
class CG {
public:
typedef ScalarType ST;
typedef Teuchos::ScalarTraits<ScalarType> STS;
typedef typename STS::magnitudeType MT;
typedef Tpetra::Vector<typename MV::scalar_type,
typename MV::local_ordinal_type,
typename MV::global_ordinal_type,
typename MV::node_type> V;
/// Constructor.
///
/// \param A [in] The matrix A in the linear system to solve.
/// A must be symmetric (resp. Hermitian) positive definite.
CG (Teuchos::RCP<const MAT> A) :
A_ (A),
D_ (getDiagonal (*A)),
numIters_ (1)
{}
/// Constructor with parameters.
///
/// \param A [in] The matrix A in the linear system to solve.
/// A must be symmetric (resp. Hermitian) positive definite.
/// \param params [in/out] On input: the parameters. On output:
/// filled with the current parameter settings.
CG (Teuchos::RCP<const MAT> A, Teuchos::ParameterList& params) :
A_ (A),
D_ (getDiagonal (*A)),
numIters_ (1)
{
setParameters (params);
}
/// \brief Set (or reset) parameters.
///
/// This method accepts the following parameters:
/// - "relaxation: sweeps" (\c int): numIters, the number of iterations.
///
/// \pre numIters >= 0
void setParameters (Teuchos::ParameterList& plist) {
int numIters = numIters_;
if (plist.isParameter ("chebyshev: degree")) {
numIters = plist.get<int> ("chebyshev: degree");
} else if (plist.isParameter ("CG: iterations")) {
numIters = plist.get<int> ("CG: iterations");
} else {
numIters = plist.get ("relaxation: sweeps", numIters);
}
numIters_ = numIters;
}
/// Solve Ax=b for x with Chebyshev iteration, using diagonal left preconditioning.
///
/// \param b [in] Right-hand side(s) in the linear system to solve.
/// \param x [in] Initial guess(es) for the linear system to solve.
///
/// \return Max (over all columns) absolute residual 2-norm after iterating.
MT apply (const MV& b, MV& x) {
return leftScaledCG (*A_, b, x, numIters_, *D_);
}
private:
Teuchos::RCP<const MAT> A_;
Teuchos::RCP<const V> D_;
int numIters_;
//! r = b - A * x
static void
computeResidual (MV& r, const MV& b, const MAT& A, const MV& x,
const Teuchos::ETransp mode = Teuchos::NO_TRANS)
{
r = b;
A.apply (x, r, mode, -STS::one(), STS::one());
}
//! z = D_inv * r, = D \ r.
static void solve (MV& z, const V& D_inv, const MV& r) {
z.elementWiseMultiply (STS::one(), D_inv, r, STS::zero());
}
//! Get a copy of the diagonal of the matrix, as a row Map vector.
static Teuchos::RCP<V> getDiagonal (const MAT& A) {
Teuchos::RCP<V> D (new V (A.getGraph ()->getRowMap ()));
A.getLocalDiagCopy (*D);
typedef typename V::scalar_type scalar_type;
typedef typename V::mag_type mag_type;
typedef Teuchos::ScalarTraits<scalar_type> STS;
const scalar_type ONE = STS::one ();
const mag_type min_val_abs = STS::magnitude (STS::eps ());
Teuchos::ArrayRCP<scalar_type> D_0 = D->getDataNonConst (0);
scalar_type* const D_0_raw = D_0.getRawPtr ();
const size_t lclNumRows = D->getLocalLength ();
for (size_t i = 0; i < lclNumRows; ++i) {
const scalar_type D_0i = D_0_raw[i];
if (STS::magnitude (D_0i) < min_val_abs) {
D_0_raw[i] = STS::eps ();
} else {
D_0_raw[i] = ONE / D_0i;
}
}
return D;
}
/// Solve Ax=b for x with CG, using diagonal left preconditioning.
///
/// \pre A must be real-valued and symmetric positive definite.
/// \pre iterNum >= 0.
/// \pre All entries of D_inv are positive.
///
/// \param A [in] The matrix A in the linear system to solve.
/// \param b [in] Right-hand side(s) in the linear system to solve.
/// \param x [in] Initial guess(es) for the linear system to solve.
/// \param numIters [in] Number of iterations.
/// \param D_inv [in] Vector of diagonal entries of A. It must have
/// the same distribution as b.
///
/// \return Max (over all columns) absolute residual 2-norm after iterating.
static MT
leftScaledCG (const MAT& A,
const MV& B,
MV& X,
const int numIters,
const V& D_inv)
{
Teuchos::Array<MT> norms (B.getNumVectors ());
for (size_t j = 0; j < B.getNumVectors (); ++j) {
Teuchos::RCP<const V> b_j = B.getVector (j);
Teuchos::RCP<V> x_j = X.getVectorNonConst (j);
norms[j] = oneVecLeftScaledCG (A, *b_j, *x_j, numIters, D_inv);
}
return *std::max_element (norms.begin (), norms.end ());
}
/// Solve Ax=b for x with CG, using diagonal left preconditioning.
///
/// \pre A must be real-valued and symmetric positive definite.
/// \pre iterNum >= 0.
/// \pre All entries of D_inv are positive.
///
/// \param A [in] The matrix A in the linear system to solve.
/// \param b [in] Right-hand side(s) in the linear system to solve.
/// \param x [in] Initial guess(es) for the linear system to solve.
/// \param numIters [in] Number of iterations.
/// \param D_inv [in] Vector of diagonal entries of A. It must have
/// the same distribution as b.
///
/// \return Max (over all columns) absolute residual 2-norm after iterating.
static MT
oneVecLeftScaledCG (const MAT& A,
const V& b,
V& x,
const int numIters,
const V& D_inv)
{
const ST one = STS::one ();
V r (b.getMap ());
V p (b.getMap ());
V q (b.getMap ());
V z (b.getMap ());
ST alpha, beta, rho;
// We don't actually need to assign to rho_prev here,
// but doing so silences a GCC 4.4.6 compiler warning.
ST rho_prev = STS::zero ();
computeResidual (r, b, A, x); // r = b - A*x
for (int i = 0; i < numIters; ++i) {
solve (z, D_inv, r); // z = D_inv * r, that is, D \ r.
rho = r.dot (z); // rho = r^T z; not sure if the order is right for complex arithmetic.
if (i == 0) {
p = z;
} else {
beta = rho / rho_prev;
p.update (one, z, beta); // p = z + beta*p
}
A.apply (p, q);
const ST p_dot_q = p.dot (q); // p_dot_q = p^T q; not sure if the order is right for complex arithmetic.
alpha = rho / p_dot_q;
x.update (+alpha, p, one); // x = x + alpha*p
r.update (-alpha, q, one); // r = r - alpha*q
rho_prev = rho;
}
const bool computeResidualNorm = true;
if (computeResidualNorm) {
computeResidual (r, b, A, x);
return r.norm2 ();
}
}
};
//////////////////////////////////////////////////////////////////////
// Command-line arguments
//////////////////////////////////////////////////////////////////////
// They have long names so I don't confuse them with the shorter-named
// actual options in the body of the test.
int numberOfIterations = 50;
int numberOfEigenanalysisIterations = 15;
int localNumberOfRows = 10000;
} // namespace (anonymous)
TEUCHOS_STATIC_SETUP()
{
Teuchos::CommandLineProcessor &clp = Teuchos::UnitTestRepository::getCLP ();
clp.setOption ("numIters", &numberOfIterations,
"Number of Chebyshev iterations");
clp.setOption ("numEigIters", &numberOfEigenanalysisIterations,
"Number of iterations of eigenvalue analysis (e.g., power method)");
clp.setOption ("localNumRows", &localNumberOfRows,
"Number of rows per process in the sparse matrix.");
}
TEUCHOS_UNIT_TEST(Ifpack2Chebyshev, Convergence)
{
// We are now in a class method declared by the above macro, and
// that method has these input arguments:
// Teuchos::FancyOStream& out, bool& success
using Tpetra::global_size_t;
using Teuchos::Array;
using Teuchos::ArrayView;
using Teuchos::as;
using Teuchos::Comm;
using Teuchos::ParameterList;
using Teuchos::RCP;
using Teuchos::rcp;
using Teuchos::tuple;
using std::cout;
using std::endl;
// Typedefs for basic Tpetra template parameters.
typedef Tpetra::MultiVector<>::scalar_type ST;
typedef Tpetra::MultiVector<>::local_ordinal_type LO;
typedef Tpetra::MultiVector<>::global_ordinal_type GO;
typedef Tpetra::MultiVector<>::node_type NT;
// Convenience typedefs.
typedef Tpetra::Map<LO, GO, NT> map_type;
typedef Tpetra::CrsMatrix<ST, LO, GO, NT> crs_matrix_type;
typedef Tpetra::RowMatrix<ST, LO, GO, NT> row_matrix_type;
typedef Tpetra::MultiVector<ST, LO, GO, NT> MV;
typedef Tpetra::Vector<ST, LO, GO, NT> V;
typedef Ifpack2::Chebyshev<row_matrix_type> prec_type;
typedef Teuchos::ScalarTraits<ST> STS;
typedef STS::magnitudeType MT;
const ST zero = STS::zero ();
const ST one = STS::one ();
const ST two = one + one;
// Prepare arguments for creating the Map.
RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm ();
const size_t localNumRows = as<size_t> (localNumberOfRows);
const global_size_t globalNumRows = localNumRows * comm->getSize ();
const GO indexBase = 0;
const Tpetra::LocalGlobal lg = Tpetra::GloballyDistributed;
// Create the row Map of the matrix, and the matrix's other Maps.
RCP<const map_type> rowMap (new map_type (globalNumRows, indexBase, comm, lg));
RCP<const map_type> rangeMap = rowMap;
RCP<const map_type> domainMap = rowMap;
// Create the matrix, with static profile.
RCP<crs_matrix_type> A (new crs_matrix_type (rowMap, 3, Tpetra::StaticProfile));
// Fill the matrix.
Array<GO> cols (3);
Array<ST> vals (3);
for (GO globalRow = rowMap->getMinGlobalIndex ();
globalRow <= rowMap->getMaxGlobalIndex (); ++globalRow) {
size_t numEntries = 3;
if (globalRow == rowMap->getMinAllGlobalIndex ()) {
numEntries = 2;
cols[0] = globalRow;
cols[1] = globalRow+1;
vals[0] = two;
vals[1] = -one;
}
else if (globalRow == rowMap->getMaxAllGlobalIndex ()) {
numEntries = 2;
cols[0] = globalRow-1;
cols[1] = globalRow;
vals[0] = -one;
vals[1] = two;
}
else {
numEntries = 3;
cols[0] = globalRow-1;
cols[1] = globalRow;
cols[2] = globalRow+1;
vals[0] = -one;
vals[1] = two;
vals[2] = -one;
}
ArrayView<const GO> colsView = cols.view (0, numEntries);
ArrayView<const ST> valsView = vals.view (0, numEntries);
A->insertGlobalValues (globalRow, colsView, valsView);
}
A->fillComplete (domainMap, rangeMap);
// See James Demmel, "Applied Numerical Linear Algebra," SIAM,
// pp. 267-8. The eigenvalue approximations apply to the N x N
// matrix with typical row (-1, 2, -1), representing the
// discretization of the 1-D Poisson equation with Dirichlet
// boundary conditions.
const ST pi = acos (-1.0);
const ST N = as<ST> (globalNumRows);
const ST lambdaMax = two * (one - cos ((pi*N) / (N+one)));
const ST lambdaMin = (pi / N) * (pi / N);
ST eigRatio = lambdaMax / lambdaMin;
const int numIters = numberOfIterations;
int numEigIters = numberOfEigenanalysisIterations;
// Set up the linear system to solve.
V x_exact (domainMap), x (domainMap), b (rangeMap);
x_exact.randomize ();
A->apply (x_exact, b);
x.putScalar (zero);
V r (rangeMap); // for storing residual vector(s).
Array<MT> norms (b.getNumVectors ());
// Compute max initial absolute residual 2-norm.
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
const MT maxInitResNorm = *std::max_element (norms.begin (), norms.end ());
// Make an output stream that only prints on Proc 0.
// The usual 'out' stream in unit tests only prints if the test failed.
Teuchos::oblackholestream blackHole;
std::ostream& os2 = (comm->getRank () == 0) ? cout : blackHole;
os2 << std::scientific;
os2 << endl
<< "numIters: " << numIters << endl
<< "localNumRows: " << localNumRows << endl
<< "globalNumRows: " << globalNumRows << endl
<< "lambdaMin: " << lambdaMin << endl
<< "lambdaMax: " << lambdaMax << endl
<< "eigRatio: " << eigRatio << endl
<< "Initial residual norm: " << maxInitResNorm << endl
<< endl;
Teuchos::ParameterList params;
// Set parameters for the various Chebyshev implementations. The
// above Chebyshev class understands many of the same parameters as
// Ifpack2, Ifpack, and ML. For this first pass, we only set the
// max eigenvalue. Below, we'll experiment with also setting the
// min eigenvalue and the min / max eigenvalue ratio.
params.set ("chebyshev: eigenvalue max iterations", numEigIters);
params.set ("chebyshev: degree", numIters);
params.set ("chebyshev: max eigenvalue", lambdaMax);
// Create the operators: Ifpack2, textbook Chebyshev, and custom CG.
prec_type ifpack2Cheby (A);
Ifpack2::Details::Chebyshev<ST, MV> myCheby (A);
CG<ST, MV, crs_matrix_type> cg (A);
// Residual 2-norms for comparison.
MT maxResNormIfpack2, maxResNormTextbook, maxResNormCg;
////////////////////////////////////////////////////////////////////
// Test 1: set lambdaMax exactly, use default values of eigRatio and
// lambdaMin. Run each version of Chebyshev and compare results.
////////////////////////////////////////////////////////////////////
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with lambdaMax = " << lambdaMax
<< ", default lambdaMin and eigRatio:" << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
////////////////////////////////////////////////////////////////////
// Test 2: set lambdaMax and lambdaMin exactly, and set eigRatio =
// lambdaMax / lambdaMin.
////////////////////////////////////////////////////////////////////
// Reset parameters.
params.set ("chebyshev: textbook algorithm", false);
params.set ("chebyshev: min eigenvalue", lambdaMin);
params.set ("chebyshev: ratio eigenvalue", eigRatio);
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with lambdaMax = " << lambdaMax
<< ", lambdaMin = " << lambdaMin << ", eigRatio = " << eigRatio << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
// Reset parameters.
params.set ("chebyshev: textbook algorithm", false);
// ParameterList is NOT a delta. That is, if we remove these
// parameters from the list, setParameters() will use default
// values, rather than letting the current settings remain.
params.remove ("chebyshev: min eigenvalue", false);
params.remove ("chebyshev: ratio eigenvalue", false);
////////////////////////////////////////////////////////////////////
// Test 3: set lambdaMax exactly, and set eigRatio = 20 (ML's
// default).
////////////////////////////////////////////////////////////////////
// Set new parameter values.
eigRatio = Teuchos::as<ST> (20);
params.set ("chebyshev: ratio eigenvalue", eigRatio);
//
// Run each version of Chebyshev and compare their results.
//
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with lambdaMax = " << lambdaMax
<< ", default lambdaMin, eigRatio = " << eigRatio << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
// Reset parameters.
params.set ("chebyshev: textbook algorithm", false);
////////////////////////////////////////////////////////////////////
// Test 4: set lambdaMax exactly, and set eigRatio = 30 (Ifpack's
// default).
////////////////////////////////////////////////////////////////////
eigRatio = Teuchos::as<ST> (30);
params.set ("chebyshev: ratio eigenvalue", eigRatio);
//
// Run each version of Chebyshev and compare their results.
//
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with lambdaMax = " << lambdaMax
<< ", default lambdaMin, eigRatio = " << eigRatio << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
// Reset parameters to their original values.
params.set ("chebyshev: textbook algorithm", false);
params.remove ("chebyshev: ratio eigenvalue", false);
eigRatio = lambdaMax / lambdaMin;
////////////////////////////////////////////////////////////////////
// Test 5: Clear lambdaMax, lambdaMin, and eigRatio. Let the
// smoother do eigenanalysis to estimate lambdaMax.
////////////////////////////////////////////////////////////////////
params.remove ("chebyshev: min eigenvalue", false);
params.remove ("chebyshev: max eigenvalue", false);
params.remove ("chebyshev: ratio eigenvalue", false);
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with default lambdaMax, lambdaMin, and eigRatio, "
"with numEigIters = " << numEigIters << ":" << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
// Print the computed max and min eigenvalues, and other details.
os2 << endl;
myCheby.print (os2);
// Reset parameters.
params.set ("chebyshev: textbook algorithm", false);
////////////////////////////////////////////////////////////////////
// Test 6: Clear lambdaMax, lambdaMin, and eigRatio. Let the
// smoother do eigenanalysis to estimate lambdaMax, with more
// iterations.
////////////////////////////////////////////////////////////////////
params.remove ("chebyshev: min eigenvalue", false);
params.remove ("chebyshev: max eigenvalue", false);
params.remove ("chebyshev: ratio eigenvalue", false);
numEigIters = 2 * numEigIters;
params.set ("chebyshev: eigenvalue max iterations", numEigIters);
// Run Ifpack2's version of Chebyshev.
ifpack2Cheby.setParameters (params);
ifpack2Cheby.initialize ();
ifpack2Cheby.compute ();
ifpack2Cheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormIfpack2 = *std::max_element (norms.begin (), norms.end ());
// Run our custom version of Chebyshev.
x.putScalar (zero); // Reset the initial guess(es).
params.set ("chebyshev: textbook algorithm", true);
myCheby.setParameters (params);
myCheby.compute ();
(void) myCheby.apply (b, x);
r = b;
A->apply (x, r, Teuchos::NO_TRANS, -one, one);
r.norm2 (norms ());
maxResNormTextbook = *std::max_element (norms.begin (), norms.end ());
// Run CG, just to compare.
x.putScalar (zero); // Reset the initial guess(es).
cg.setParameters (params);
maxResNormCg = cg.apply (b, x);
os2 << "Results with default lambdaMax, lambdaMin, and eigRatio, "
"with numEigIters = " << numEigIters << ":" << endl
<< "- Ifpack2::Chebyshev: " << maxResNormIfpack2 / maxInitResNorm << endl
<< "- Textbook Chebyshev: " << maxResNormTextbook / maxInitResNorm << endl
<< "- CG: " << maxResNormCg / maxInitResNorm << endl;
// For this case, if there are enough eigenanalysis iterations,
// Ifpack2 should do quite a bit better than the textbook version of
// the algorithm. We'll be generous and say that it does "no worse"
// than the textbook version. We give "wiggle room" of four digits
// for defining "no worse than."
if (numEigIters >= 15) {
const MT tol = Teuchos::as<MT> (1.0e-4);
// Avoid division by zero when computing relative accuracy.
const MT relDiff = maxResNormTextbook == zero ?
STS::magnitude (maxResNormIfpack2 - maxResNormTextbook) :
STS::magnitude (maxResNormIfpack2 - maxResNormTextbook) / maxResNormTextbook;
TEUCHOS_TEST_FOR_EXCEPTION(
maxResNormIfpack2 > maxResNormTextbook && relDiff > tol,
std::runtime_error,
"After " << numIters << " iterations of Chebyshev, with lambdaMax = "
<< lambdaMax << " and default lambdaMin and eigRatio, Ifpack2::Chebyshev "
"does quite a bit worse than the textbook version of the algorithm. The "
"former has a max relative residual norm of " << maxResNormIfpack2 << ", "
"and the latter of " << maxResNormTextbook << ".");
}
// Print the computed max and min eigenvalues, and other details.
os2 << endl;
myCheby.print (os2);
}
|