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"
67 namespace DeprecatedAndMayDisappearAtAnyTime {
69 template <
class MatrixType>
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>
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>
130 ParameterList params = plist;
137 magnitude_type resTol = resTol_;
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) {
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>
240 Krylov<MatrixType>::getComm ()
const {
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>
251 Krylov<MatrixType>::getMatrix ()
const {
256 template <
class MatrixType>
258 Krylov<MatrixType>::getDomainMap ()
const
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>
270 Krylov<MatrixType>::getRangeMap ()
const
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 ()
330 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
331 global_ordinal_type, node_type> TMV;
332 typedef Tpetra::Operator<scalar_type, local_ordinal_type,
333 global_ordinal_type, node_type> TOP;
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;
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 ()
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.");
405 if (! isInitialized ()) {
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,
434 typename MatrixType::scalar_type alpha,
435 typename MatrixType::scalar_type beta)
const
439 using Teuchos::rcpFromRef;
440 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type,
441 global_ordinal_type, node_type> MV;
443 ! isComputed (), std::runtime_error,
444 "Ifpack2::Krylov::apply: You must call compute() before you may call apply().");
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 () <<
".");
453 alpha != STS::one (), std::logic_error,
454 "Ifpack2::Krylov::apply: alpha != 1 has not been implemented.");
456 beta != STS::zero (), std::logic_error,
457 "Ifpack2::Krylov::apply: zero != 0 has not been implemented.");
460 "Ifpack2::Krylov::apply: mode != Teuchos::NO_TRANS has not been implemented.");
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.data () == Y_lcl_host.data ()) {
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>::
542 out <<
"\"Ifpack2::Krylov\":";
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
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TypeTo as(const TypeFrom &t)