43 #ifndef IFPACK2_KRYLOV_DEF_HPP 44 #define IFPACK2_KRYLOV_DEF_HPP 46 #ifdef HAVE_IFPACK2_DEPRECATED_CODE 48 #include "Ifpack2_Chebyshev.hpp" 49 #include "Ifpack2_Heap.hpp" 50 #include "Ifpack2_ILUT.hpp" 51 #include "Ifpack2_Parameters.hpp" 52 #include "Ifpack2_Relaxation.hpp" 53 #include "Ifpack2_RILUK.hpp" 58 #include "Teuchos_Assert.hpp" 59 #include "Teuchos_Time.hpp" 69 template <
class MatrixType>
71 Krylov (
const Teuchos::RCP<const row_matrix_type>& A) :
74 iterationType_ (
"GMRES"),
78 ZeroStartingSolution_ (true),
79 PreconditionerType_ (1),
81 IsInitialized_ (false),
86 InitializeTime_ (0.0),
92 template <
class MatrixType>
93 Krylov<MatrixType>::~Krylov() {}
96 template <
class MatrixType>
97 void Krylov<MatrixType>::setMatrix (
const Teuchos::RCP<const row_matrix_type>& A)
100 TEUCHOS_TEST_FOR_EXCEPTION(
101 ! A.is_null () && A->getComm ()->getSize () == 1 &&
102 A->getNodeNumRows () != A->getNodeNumCols (),
103 std::runtime_error,
"Ifpack2::Krylov::setMatrix: If A's communicator only " 104 "contains one process, then A must be square. Instead, you provided a " 105 "matrix A with " << A->getNodeNumRows () <<
" rows and " 106 << A->getNodeNumCols () <<
" columns.");
112 IsInitialized_ =
false;
119 template <
class MatrixType>
120 void Krylov<MatrixType>::setParameters (
const Teuchos::ParameterList& plist)
123 using Teuchos::ParameterList;
124 using Teuchos::Exceptions::InvalidParameterName;
125 using Teuchos::Exceptions::InvalidParameterType;
130 ParameterList params = plist;
138 int numIters = numIters_;
139 std::string iterType = iterationType_;
140 int blockSize = BlockSize_;
141 bool zeroStartingSolution = ZeroStartingSolution_;
142 int precType = PreconditionerType_;
150 bool gotIterType =
false;
152 iterType = params.get<std::string> (
"krylov: iteration type");
155 catch (InvalidParameterName) {
158 catch (InvalidParameterType) {
164 const int iterTypeInt = params.get<
int> (
"krylov: iteration type");
167 if (iterTypeInt == 1) {
169 }
else if (iterTypeInt == 2) {
172 TEUCHOS_TEST_FOR_EXCEPTION(
173 true, std::invalid_argument,
"Ifpack2::Krylov::setParameters: Invalid " 174 "\"krylov: iteration type\" value " << iterTypeInt <<
". Valid int " 175 "values are 1 (GMRES) and 2 (CG). Please prefer setting this " 176 "parameter as a string (the name of the Krylov solver to use).");
180 resTol = params.get (
"krylov: residual tolerance", resTol);
181 numIters = params.get (
"krylov: number of iterations", numIters);
182 blockSize = params.get (
"krylov: block size", blockSize);
183 zeroStartingSolution = params.get (
"krylov: zero starting solution",
184 zeroStartingSolution);
185 precType = params.get (
"krylov: preconditioner type", precType);
191 if (PreconditionerType_ == 1) {
192 precParams_.set (
"relaxation: sweeps",
193 params.get (
"relaxation: sweeps", 1));
194 precParams_.set (
"relaxation: damping factor",
195 params.get (
"relaxation: damping factor", (
scalar_type) 1.0));
196 precParams_.set (
"relaxation: min diagonal value",
197 params.get (
"relaxation: min diagonal value", STS::one ()));
198 precParams_.set (
"relaxation: zero starting solution",
199 params.get (
"relaxation: zero starting solution",
true));
200 precParams_.set (
"relaxation: backward mode",
201 params.get (
"relaxation: backward mode",
false));
206 if (PreconditionerType_ == 2 || PreconditionerType_ == 3) {
210 precParams_.set (
"fact: ilut level-of-fill",
211 params.get (
"fact: ilut level-of-fill", (
double) 1.0));
212 precParams_.set (
"fact: iluk level-of-fill",
213 params.get (
"fact: iluk level-of-fill", (
double) 1.0));
216 precParams_.set (
"fact: absolute threshold",
217 params.get (
"fact: absolute threshold", (
double) 0.0));
220 precParams_.set (
"fact: relative threshold",
221 params.get(
"fact: relative threshold", (
double) 1.0));
224 precParams_.set (
"fact: relax value",
225 params.get (
"fact: relax value", (
double) 0.0));
229 iterationType_ = iterType;
230 numIters_ = numIters;
232 BlockSize_ = blockSize;
233 ZeroStartingSolution_ = zeroStartingSolution;
234 PreconditionerType_ = precType;
238 template <
class MatrixType>
239 Teuchos::RCP<const Teuchos::Comm<int> >
240 Krylov<MatrixType>::getComm ()
const {
241 TEUCHOS_TEST_FOR_EXCEPTION(
242 A_.is_null (), std::runtime_error,
"Ifpack2::Krylov::getComm: " 243 "The input matrix A is null. Please call setMatrix() with a nonnull " 244 "input matrix before calling this method.");
245 return A_->getComm ();
249 template <
class MatrixType>
250 Teuchos::RCP<const Tpetra::RowMatrix<typename MatrixType::scalar_type,typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
251 Krylov<MatrixType>::getMatrix ()
const {
256 template <
class MatrixType>
257 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
258 Krylov<MatrixType>::getDomainMap ()
const 260 TEUCHOS_TEST_FOR_EXCEPTION(
261 A_.is_null (), std::runtime_error,
"Ifpack2::Krylov::getDomainMap: " 262 "The input matrix A is null. Please call setMatrix() with a nonnull " 263 "input matrix before calling this method.");
264 return A_->getDomainMap ();
268 template <
class MatrixType>
269 Teuchos::RCP<const Tpetra::Map<typename MatrixType::local_ordinal_type,typename MatrixType::global_ordinal_type,typename MatrixType::node_type> >
270 Krylov<MatrixType>::getRangeMap ()
const 272 TEUCHOS_TEST_FOR_EXCEPTION(
273 A_.is_null (), std::runtime_error,
"Ifpack2::Krylov::getRangeMap: " 274 "The input matrix A is null. Please call setMatrix() with a nonnull " 275 "input matrix before calling this method.");
276 return A_->getRangeMap ();
280 template <
class MatrixType>
281 bool Krylov<MatrixType>::hasTransposeApply ()
const {
288 template <
class MatrixType>
289 int Krylov<MatrixType>::getNumInitialize ()
const {
290 return NumInitialize_;
294 template <
class MatrixType>
295 int Krylov<MatrixType>::getNumCompute ()
const {
300 template <
class MatrixType>
301 int Krylov<MatrixType>::getNumApply ()
const {
306 template <
class MatrixType>
307 double Krylov<MatrixType>::getInitializeTime ()
const {
308 return InitializeTime_;
312 template <
class MatrixType>
313 double Krylov<MatrixType>::getComputeTime ()
const {
318 template <
class MatrixType>
319 double Krylov<MatrixType>::getApplyTime ()
const {
324 template <
class MatrixType>
325 void Krylov<MatrixType>::initialize ()
327 using Teuchos::ParameterList;
335 TEUCHOS_TEST_FOR_EXCEPTION(
336 A_.is_null (), std::runtime_error,
"Ifpack2::Krylov::initialize: " 337 "The input matrix A is null. Please call setMatrix() with a nonnull " 338 "input matrix before calling this method.");
341 IsInitialized_ =
false;
344 Teuchos::Time timer (
"initialize");
346 Teuchos::TimeMonitor timeMon (timer);
349 RCP<ParameterList> belosList = rcp (
new ParameterList (
"GMRES"));
350 belosList->set (
"Maximum Iterations", numIters_);
351 belosList->set (
"Convergence Tolerance", resTol_);
358 if (PreconditionerType_ == 0) {
361 else if (PreconditionerType_==1) {
362 ifpack2_prec_=rcp (
new Relaxation<row_matrix_type> (A_));
364 else if (PreconditionerType_==2) {
365 ifpack2_prec_=rcp (
new ILUT<row_matrix_type> (A_));
367 else if (PreconditionerType_==3) {
368 ifpack2_prec_ = rcp (
new RILUK<row_matrix_type> (A_));
370 else if (PreconditionerType_==4) {
371 ifpack2_prec_ = rcp (
new Chebyshev<row_matrix_type> (A_));
373 if (PreconditionerType_>0) {
374 ifpack2_prec_->initialize();
375 ifpack2_prec_->setParameters(precParams_);
378 belosProblem_->setOperator (A_);
380 if (iterationType_ ==
"GMRES") {
390 IsInitialized_ =
true;
392 InitializeTime_ += timer.totalElapsedTime ();
396 template <
class MatrixType>
397 void Krylov<MatrixType>::compute ()
399 TEUCHOS_TEST_FOR_EXCEPTION(
400 A_.is_null (), std::runtime_error,
"Ifpack2::Krylov::compute: " 401 "The input matrix A is null. Please call setMatrix() with a nonnull " 402 "input matrix before calling this method.");
409 Teuchos::Time timer (
"compute");
411 Teuchos::TimeMonitor timeMon (timer);
412 if (PreconditionerType_ > 0) {
413 ifpack2_prec_->compute ();
414 belosProblem_->setLeftPrec (ifpack2_prec_);
419 ComputeTime_ += timer.totalElapsedTime ();
423 template <
class MatrixType>
424 void Krylov<MatrixType>::
425 apply (
const Tpetra::MultiVector<
typename MatrixType::scalar_type,
426 typename MatrixType::local_ordinal_type,
427 typename MatrixType::global_ordinal_type,
428 typename MatrixType::node_type>& X,
429 Tpetra::MultiVector<
typename MatrixType::scalar_type,
430 typename MatrixType::local_ordinal_type,
431 typename MatrixType::global_ordinal_type,
432 typename MatrixType::node_type>& Y,
433 Teuchos::ETransp mode,
434 typename MatrixType::scalar_type alpha,
435 typename MatrixType::scalar_type beta)
const 439 using Teuchos::rcpFromRef;
442 TEUCHOS_TEST_FOR_EXCEPTION(
444 "Ifpack2::Krylov::apply: You must call compute() before you may call apply().");
445 TEUCHOS_TEST_FOR_EXCEPTION(
446 X.getNumVectors () != Y.getNumVectors (), std::invalid_argument,
447 "Ifpack2::Krylov::apply: The MultiVector inputs X and Y do not have the " 448 "same number of columns. X.getNumVectors() = " << X.getNumVectors ()
449 <<
" != Y.getNumVectors() = " << Y.getNumVectors () <<
".");
452 TEUCHOS_TEST_FOR_EXCEPTION(
453 alpha != STS::one (), std::logic_error,
454 "Ifpack2::Krylov::apply: alpha != 1 has not been implemented.");
455 TEUCHOS_TEST_FOR_EXCEPTION(
456 beta != STS::zero (), std::logic_error,
457 "Ifpack2::Krylov::apply: zero != 0 has not been implemented.");
458 TEUCHOS_TEST_FOR_EXCEPTION(
459 mode != Teuchos::NO_TRANS, std::logic_error,
460 "Ifpack2::Krylov::apply: mode != Teuchos::NO_TRANS has not been implemented.");
462 Teuchos::Time timer (
"apply");
464 Teuchos::TimeMonitor timeMon (timer);
470 auto X_lcl_host = X.template getLocalView<Kokkos::HostSpace> ();
471 auto Y_lcl_host = Y.template getLocalView<Kokkos::HostSpace> ();
472 if (X_lcl_host.ptr_on_device () == Y_lcl_host.ptr_on_device ()) {
473 Xcopy = rcp (
new MV (X, Teuchos::Copy));
475 Xcopy = rcpFromRef (X);
479 RCP<MV> Ycopy = rcpFromRef (Y);
480 if (ZeroStartingSolution_) {
481 Ycopy->putScalar (STS::zero ());
485 belosProblem_->setProblem (Ycopy, Xcopy);
486 belosSolver_->solve ();
489 ApplyTime_ += timer.totalElapsedTime ();
493 template <
class MatrixType>
494 std::string Krylov<MatrixType>::description ()
const 496 std::ostringstream os;
501 os <<
"\"Ifpack2::Krylov\": {";
502 if (this->getObjectLabel () !=
"") {
503 os <<
"Label: \"" << this->getObjectLabel () <<
"\", ";
505 os <<
"Initialized: " << (
isInitialized () ?
"true" :
"false") <<
", " 506 <<
"Computed: " << (
isComputed () ?
"true" :
"false") <<
", ";
509 os <<
"Matrix: null";
512 os <<
"Matrix: not null" 513 <<
", Global matrix dimensions: [" 514 << A_->getGlobalNumRows () <<
", " << A_->getGlobalNumCols () <<
"]";
522 template <
class MatrixType>
523 void Krylov<MatrixType>::
524 describe (Teuchos::FancyOStream &out,
525 const Teuchos::EVerbosityLevel verbLevel)
const 529 using Teuchos::VERB_DEFAULT;
530 using Teuchos::VERB_NONE;
531 using Teuchos::VERB_LOW;
532 using Teuchos::VERB_MEDIUM;
533 using Teuchos::VERB_HIGH;
534 using Teuchos::VERB_EXTREME;
536 const Teuchos::EVerbosityLevel vl =
537 (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
539 if (vl != VERB_NONE) {
541 Teuchos::OSTab tab0 (out);
542 out <<
"\"Ifpack2::Krylov\":";
544 Teuchos::OSTab tab1 (out);
545 if (this->getObjectLabel () !=
"") {
546 out <<
"Label: " << this->getObjectLabel () << endl;
548 out <<
"Initialized: " << (
isInitialized () ?
"true" :
"false") << endl
549 <<
"Computed: " << (
isComputed () ?
"true" :
"false") << endl
550 <<
"Global number of rows: " << A_->getGlobalNumRows () << endl
551 <<
"Global number of columns: " << A_->getGlobalNumCols () << endl
554 out <<
" null" << endl;
556 A_->describe (out, vl);
573 #define IFPACK2_KRYLOV_INSTANT(S,LO,GO,N) \ 574 template class Ifpack2::DeprecatedAndMayDisappearAtAnyTime::Krylov< Tpetra::RowMatrix<S, LO, GO, N> >; 576 #endif // HAVE_IFPACK2_DEPRECATED_CODE MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:112
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_ILUT_decl.hpp:118
void initialize()
Clear any previously computed factors.
Definition: Ifpack2_ILUT_def.hpp:402
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:106
Ifpack2 features that have been DEPRECATED and may DISAPPEAR AT ANY TIME. USE AT YOUR OWN RISK...
bool isComputed() const
If compute() is completed, this query returns true, otherwise it returns false.
Definition: Ifpack2_ILUT_decl.hpp:215
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:72
bool isInitialized() const
Returns true if the preconditioner has been successfully initialized.
Definition: Ifpack2_ILUT_decl.hpp:200
MatrixType::node_type node_type
The Node type used by the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:115
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_ILUT_decl.hpp:109