/usr/include/trilinos/Ifpack2_Krylov_def.hpp is in libtrilinos-ifpack2-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*@HEADER
// ***********************************************************************
//
// Ifpack2: Tempated 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
*/
#ifndef IFPACK2_KRYLOV_DEF_HPP
#define IFPACK2_KRYLOV_DEF_HPP
#ifdef HAVE_IFPACK2_DEPRECATED_CODE
#include "Ifpack2_Chebyshev.hpp"
#include "Ifpack2_Heap.hpp"
#include "Ifpack2_ILUT.hpp"
#include "Ifpack2_Parameters.hpp"
#include "Ifpack2_Relaxation.hpp"
#include "Ifpack2_RILUK.hpp"
#include "BelosBlockGmresSolMgr.hpp"
#include "BelosBlockCGSolMgr.hpp"
#include "Teuchos_Assert.hpp"
#include "Teuchos_Time.hpp"
#include <iostream>
#include <sstream>
#include <cmath>
namespace Ifpack2 {
namespace DeprecatedAndMayDisappearAtAnyTime {
template <class MatrixType>
Krylov<MatrixType>::
Krylov (const Teuchos::RCP<const row_matrix_type>& A) :
A_ (A),
// Default values
iterationType_ ("GMRES"),
numIters_ (5),
resTol_ (0.001), // FIXME (mfh 17 Jan 2014) Make a function of STS::eps()
BlockSize_ (1),
ZeroStartingSolution_ (true),
PreconditionerType_ (1),
// General
IsInitialized_ (false),
IsComputed_ (false),
NumInitialize_ (0),
NumCompute_ (0),
NumApply_ (0),
InitializeTime_ (0.0),
ComputeTime_ (0.0),
ApplyTime_ (0.0)
{}
template <class MatrixType>
Krylov<MatrixType>::~Krylov() {}
template <class MatrixType>
void Krylov<MatrixType>::setMatrix (const Teuchos::RCP<const row_matrix_type>& A)
{
// Check in serial or one-process mode if the matrix is square.
TEUCHOS_TEST_FOR_EXCEPTION(
! A.is_null () && A->getComm ()->getSize () == 1 &&
A->getNodeNumRows () != A->getNodeNumCols (),
std::runtime_error, "Ifpack2::Krylov::setMatrix: If A's communicator only "
"contains one process, then A must be square. Instead, you provided a "
"matrix A with " << A->getNodeNumRows () << " rows and "
<< A->getNodeNumCols () << " columns.");
// It's legal for A to be null; in that case, you may not call
// initialize() until calling setMatrix() with a nonnull input.
// Regardless, setting the matrix invalidates any previous
// factorization.
IsInitialized_ = false;
IsComputed_ = false;
A_ = A;
}
template <class MatrixType>
void Krylov<MatrixType>::setParameters (const Teuchos::ParameterList& plist)
{
using Teuchos::as;
using Teuchos::ParameterList;
using Teuchos::Exceptions::InvalidParameterName;
using Teuchos::Exceptions::InvalidParameterType;
// FIXME (mfh 12 Sep 2014) Don't rewrite Belos::SolverFactory!!! Use
// that instead.
ParameterList params = plist;
// Get the current parameters' values. We don't assign to the
// instance data directly until we've gotten all the parameters.
// This ensures "transactional" semantics, so that if attempting to
// get some parameter throws an exception, the class' state doesn't
// change.
magnitude_type resTol = resTol_;
int numIters = numIters_;
std::string iterType = iterationType_;
int blockSize = BlockSize_;
bool zeroStartingSolution = ZeroStartingSolution_;
int precType = PreconditionerType_;
//
// Get the "krylov: iteration type" parameter.
//
// We prefer std::string (name of Belos solver), but allow int
// ("enum" value) for backwards compatibility.
//
bool gotIterType = false;
try {
iterType = params.get<std::string> ("krylov: iteration type");
gotIterType = true;
}
catch (InvalidParameterName) {
gotIterType = true; // the parameter is not there, so don't try to get it anymore
}
catch (InvalidParameterType) {
// Perhaps the user specified it as int.
}
// If it's not string, it has to be int.
// We've already checked whether the name exists.
if (! gotIterType) {
const int iterTypeInt = params.get<int> ("krylov: iteration type");
gotIterType = true;
if (iterTypeInt == 1) {
iterType = "GMRES";
} else if (iterTypeInt == 2) {
iterType = "CG";
} else {
TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument, "Ifpack2::Krylov::setParameters: Invalid "
"\"krylov: iteration type\" value " << iterTypeInt << ". Valid int "
"values are 1 (GMRES) and 2 (CG). Please prefer setting this "
"parameter as a string (the name of the Krylov solver to use).");
}
}
resTol = params.get ("krylov: residual tolerance", resTol);
numIters = params.get ("krylov: number of iterations", numIters);
blockSize = params.get ("krylov: block size", blockSize);
zeroStartingSolution = params.get ("krylov: zero starting solution",
zeroStartingSolution);
precType = params.get ("krylov: preconditioner type", precType);
// Separate preconditioner parameters into another list
//
// FIXME (mfh 17 Jan 2014) Inner preconditioner's parameters should
// be a sublist, not part of the main list!!!
if (PreconditionerType_ == 1) {
precParams_.set ("relaxation: sweeps",
params.get ("relaxation: sweeps", 1));
precParams_.set ("relaxation: damping factor",
params.get ("relaxation: damping factor", (scalar_type) 1.0));
precParams_.set ("relaxation: min diagonal value",
params.get ("relaxation: min diagonal value", STS::one ()));
precParams_.set ("relaxation: zero starting solution",
params.get ("relaxation: zero starting solution", true));
precParams_.set ("relaxation: backward mode",
params.get ("relaxation: backward mode", false));
}
// FIXME (mfh 17 Jan 2014) AdditiveSchwarz's ParameterList no longer
// takes parameters for its subdomain solver! You have to pass them
// into a sublist.
if (PreconditionerType_ == 2 || PreconditionerType_ == 3) {
// FIXME (mfh 17 Jan 2014) should be an integer, given how ILUT
// works! Furthermore, this parameter does not mean what you
// think it means.
precParams_.set ("fact: ilut level-of-fill",
params.get ("fact: ilut level-of-fill", (double) 1.0));
precParams_.set ("fact: iluk level-of-fill",
params.get ("fact: iluk level-of-fill", (double) 1.0));
// FIXME (mfh 17 Jan 2014) scalar_type or magnitude_type? not
// sure, but double is definitely wrong.
precParams_.set ("fact: absolute threshold",
params.get ("fact: absolute threshold", (double) 0.0));
// FIXME (mfh 17 Jan 2014) scalar_type or magnitude_type? not
// sure, but double is definitely wrong.
precParams_.set ("fact: relative threshold",
params.get("fact: relative threshold", (double) 1.0));
// FIXME (mfh 17 Jan 2014) scalar_type or magnitude_type? not
// sure, but double is definitely wrong.
precParams_.set ("fact: relax value",
params.get ("fact: relax value", (double) 0.0));
}
// "Commit" the new values to the instance data.
iterationType_ = iterType;
numIters_ = numIters;
resTol_ = resTol;
BlockSize_ = blockSize;
ZeroStartingSolution_ = zeroStartingSolution;
PreconditionerType_ = precType;
}
template <class MatrixType>
Teuchos::RCP<const Teuchos::Comm<int> >
Krylov<MatrixType>::getComm () const {
TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null (), std::runtime_error, "Ifpack2::Krylov::getComm: "
"The input matrix A is null. Please call setMatrix() with a nonnull "
"input matrix before calling this method.");
return A_->getComm ();
}
template <class MatrixType>
Teuchos::RCP<const Tpetra::RowMatrix<typename MatrixType::scalar_type,typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
Krylov<MatrixType>::getMatrix () const {
return A_;
}
template <class MatrixType>
Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
Krylov<MatrixType>::getDomainMap () const
{
TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null (), std::runtime_error, "Ifpack2::Krylov::getDomainMap: "
"The input matrix A is null. Please call setMatrix() with a nonnull "
"input matrix before calling this method.");
return A_->getDomainMap ();
}
template <class MatrixType>
Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
Krylov<MatrixType>::getRangeMap () const
{
TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null (), std::runtime_error, "Ifpack2::Krylov::getRangeMap: "
"The input matrix A is null. Please call setMatrix() with a nonnull "
"input matrix before calling this method.");
return A_->getRangeMap ();
}
template <class MatrixType>
bool Krylov<MatrixType>::hasTransposeApply () const {
// FIXME (mfh 17 Jan 2014) apply() does not currently work with mode
// != NO_TRANS, so it's correct to return false here.
return false;
}
template <class MatrixType>
int Krylov<MatrixType>::getNumInitialize () const {
return NumInitialize_;
}
template <class MatrixType>
int Krylov<MatrixType>::getNumCompute () const {
return NumCompute_;
}
template <class MatrixType>
int Krylov<MatrixType>::getNumApply () const {
return NumApply_;
}
template <class MatrixType>
double Krylov<MatrixType>::getInitializeTime () const {
return InitializeTime_;
}
template <class MatrixType>
double Krylov<MatrixType>::getComputeTime () const {
return ComputeTime_;
}
template <class MatrixType>
double Krylov<MatrixType>::getApplyTime () const {
return ApplyTime_;
}
template <class MatrixType>
void Krylov<MatrixType>::initialize ()
{
using Teuchos::ParameterList;
using Teuchos::RCP;
using Teuchos::rcp;
typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
global_ordinal_type, node_type> TMV;
typedef Tpetra::Operator<scalar_type, local_ordinal_type,
global_ordinal_type, node_type> TOP;
TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null (), std::runtime_error, "Ifpack2::Krylov::initialize: "
"The input matrix A is null. Please call setMatrix() with a nonnull "
"input matrix before calling this method.");
// clear any previous allocation
IsInitialized_ = false;
IsComputed_ = false;
Teuchos::Time timer ("initialize");
{ // The body of code to time
Teuchos::TimeMonitor timeMon (timer);
// Belos parameter list
RCP<ParameterList> belosList = rcp (new ParameterList ("GMRES"));
belosList->set ("Maximum Iterations", numIters_);
belosList->set ("Convergence Tolerance", resTol_);
// FIXME (17 Jan 2014) This whole "preconditioner type" thing is
// not how we want Krylov to initialize its inner preconditioner.
// Krylov should be initialized like AdditiveSchwarz: the Factory
// should create it, in order to avoid circular dependencies.
if (PreconditionerType_ == 0) {
// no preconditioner
}
else if (PreconditionerType_==1) {
ifpack2_prec_=rcp (new Relaxation<row_matrix_type> (A_));
}
else if (PreconditionerType_==2) {
ifpack2_prec_=rcp (new ILUT<row_matrix_type> (A_));
}
else if (PreconditionerType_==3) {
ifpack2_prec_ = rcp (new RILUK<row_matrix_type> (A_));
}
else if (PreconditionerType_==4) {
ifpack2_prec_ = rcp (new Chebyshev<row_matrix_type> (A_));
}
if (PreconditionerType_>0) {
ifpack2_prec_->initialize();
ifpack2_prec_->setParameters(precParams_);
}
belosProblem_ = rcp (new Belos::LinearProblem<belos_scalar_type,TMV,TOP> ());
belosProblem_->setOperator (A_);
if (iterationType_ == "GMRES") {
belosSolver_ =
rcp (new Belos::BlockGmresSolMgr<belos_scalar_type,TMV,TOP> (belosProblem_, belosList));
}
else {
belosSolver_ =
rcp (new Belos::BlockCGSolMgr<belos_scalar_type,TMV,TOP> (belosProblem_, belosList));
}
}
IsInitialized_ = true;
++NumInitialize_;
InitializeTime_ += timer.totalElapsedTime ();
}
template <class MatrixType>
void Krylov<MatrixType>::compute ()
{
TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null (), std::runtime_error, "Ifpack2::Krylov::compute: "
"The input matrix A is null. Please call setMatrix() with a nonnull "
"input matrix before calling this method.");
// Don't time the initialize(); that gets timed separately.
if (! isInitialized ()) {
initialize ();
}
Teuchos::Time timer ("compute");
{ // The body of code to time
Teuchos::TimeMonitor timeMon (timer);
if (PreconditionerType_ > 0) {
ifpack2_prec_->compute ();
belosProblem_->setLeftPrec (ifpack2_prec_);
}
}
IsComputed_ = true;
++NumCompute_;
ComputeTime_ += timer.totalElapsedTime ();
}
template <class MatrixType>
void Krylov<MatrixType>::
apply (const Tpetra::MultiVector<typename MatrixType::scalar_type,
typename MatrixType::local_ordinal_type,
typename MatrixType::global_ordinal_type,
typename MatrixType::node_type>& X,
Tpetra::MultiVector<typename MatrixType::scalar_type,
typename MatrixType::local_ordinal_type,
typename MatrixType::global_ordinal_type,
typename MatrixType::node_type>& Y,
Teuchos::ETransp mode,
typename MatrixType::scalar_type alpha,
typename MatrixType::scalar_type beta) const
{
using Teuchos::RCP;
using Teuchos::rcp;
using Teuchos::rcpFromRef;
typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
global_ordinal_type, node_type> MV;
TEUCHOS_TEST_FOR_EXCEPTION(
! isComputed (), std::runtime_error,
"Ifpack2::Krylov::apply: You must call compute() before you may call apply().");
TEUCHOS_TEST_FOR_EXCEPTION(
X.getNumVectors () != Y.getNumVectors (), std::invalid_argument,
"Ifpack2::Krylov::apply: The MultiVector inputs X and Y do not have the "
"same number of columns. X.getNumVectors() = " << X.getNumVectors ()
<< " != Y.getNumVectors() = " << Y.getNumVectors () << ".");
// Catch unimplemented cases: alpha != 1, beta != 0, mode != NO_TRANS.
TEUCHOS_TEST_FOR_EXCEPTION(
alpha != STS::one (), std::logic_error,
"Ifpack2::Krylov::apply: alpha != 1 has not been implemented.");
TEUCHOS_TEST_FOR_EXCEPTION(
beta != STS::zero (), std::logic_error,
"Ifpack2::Krylov::apply: zero != 0 has not been implemented.");
TEUCHOS_TEST_FOR_EXCEPTION(
mode != Teuchos::NO_TRANS, std::logic_error,
"Ifpack2::Krylov::apply: mode != Teuchos::NO_TRANS has not been implemented.");
Teuchos::Time timer ("apply");
{ // The body of code to time
Teuchos::TimeMonitor timeMon (timer);
// If X and Y are pointing to the same memory location,
// we need to create an auxiliary vector, Xcopy
RCP<const MV> Xcopy;
{
auto X_lcl_host = X.template getLocalView<Kokkos::HostSpace> ();
auto Y_lcl_host = Y.template getLocalView<Kokkos::HostSpace> ();
if (X_lcl_host.ptr_on_device () == Y_lcl_host.ptr_on_device ()) {
Xcopy = rcp (new MV (X, Teuchos::Copy));
} else {
Xcopy = rcpFromRef (X);
}
}
RCP<MV> Ycopy = rcpFromRef (Y);
if (ZeroStartingSolution_) {
Ycopy->putScalar (STS::zero ());
}
// Set left and right hand sides for Belos
belosProblem_->setProblem (Ycopy, Xcopy);
belosSolver_->solve (); // solve the linear system
}
++NumApply_;
ApplyTime_ += timer.totalElapsedTime ();
}
template <class MatrixType>
std::string Krylov<MatrixType>::description () const
{
std::ostringstream os;
// Output is a valid YAML dictionary in flow style. If you don't
// like everything on a single line, you should call describe()
// instead.
os << "\"Ifpack2::Krylov\": {";
if (this->getObjectLabel () != "") {
os << "Label: \"" << this->getObjectLabel () << "\", ";
}
os << "Initialized: " << (isInitialized () ? "true" : "false") << ", "
<< "Computed: " << (isComputed () ? "true" : "false") << ", ";
if (A_.is_null ()) {
os << "Matrix: null";
}
else {
os << "Matrix: not null"
<< ", Global matrix dimensions: ["
<< A_->getGlobalNumRows () << ", " << A_->getGlobalNumCols () << "]";
}
os << "}";
return os.str ();
}
template <class MatrixType>
void Krylov<MatrixType>::
describe (Teuchos::FancyOStream &out,
const Teuchos::EVerbosityLevel verbLevel) const
{
using std::endl;
using std::setw;
using Teuchos::VERB_DEFAULT;
using Teuchos::VERB_NONE;
using Teuchos::VERB_LOW;
using Teuchos::VERB_MEDIUM;
using Teuchos::VERB_HIGH;
using Teuchos::VERB_EXTREME;
const Teuchos::EVerbosityLevel vl =
(verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
if (vl != VERB_NONE) {
// describe() always starts with a tab by convention.
Teuchos::OSTab tab0 (out);
out << "\"Ifpack2::Krylov\":";
Teuchos::OSTab tab1 (out);
if (this->getObjectLabel () != "") {
out << "Label: " << this->getObjectLabel () << endl;
}
out << "Initialized: " << (isInitialized () ? "true" : "false") << endl
<< "Computed: " << (isComputed () ? "true" : "false") << endl
<< "Global number of rows: " << A_->getGlobalNumRows () << endl
<< "Global number of columns: " << A_->getGlobalNumCols () << endl
<< "Matrix:";
if (A_.is_null ()) {
out << " null" << endl;
} else {
A_->describe (out, vl);
}
}
}
} // namespace DeprecatedAndMayDisappearAtAnyTime
} // namespace Ifpack2
// There's no need to instantiate for CrsMatrix too. All Ifpack2
// preconditioners can and should do dynamic casts if they need a type
// more specific than RowMatrix.
//
// In fact, Krylov really doesn't _need_ the RowMatrix methods; it
// could very well just rely on the Operator interface. initialize()
// need only check whether the domain and range Maps have changed
// (which would necessitate reinitializing the Krylov solver).
#define IFPACK2_KRYLOV_INSTANT(S,LO,GO,N) \
template class Ifpack2::DeprecatedAndMayDisappearAtAnyTime::Krylov< Tpetra::RowMatrix<S, LO, GO, N> >;
#endif // HAVE_IFPACK2_DEPRECATED_CODE
#endif /* IFPACK2_KRYLOV_DEF_HPP */
|