43 #ifndef IFPACK2_RELAXATION_DEF_HPP 44 #define IFPACK2_RELAXATION_DEF_HPP 46 #include "Ifpack2_Relaxation_decl.hpp" 47 #include "Teuchos_StandardParameterEntryValidators.hpp" 48 #include <Teuchos_TimeMonitor.hpp> 49 #include <Tpetra_ConfigDefs.hpp> 59 class NonnegativeIntValidator :
public Teuchos::ParameterEntryValidator {
62 NonnegativeIntValidator () {}
65 Teuchos::ParameterEntryValidator::ValidStringsList validStringValues ()
const {
71 validate (
const Teuchos::ParameterEntry& entry,
72 const std::string& paramName,
73 const std::string& sublistName)
const 76 Teuchos::any anyVal = entry.getAny (
true);
77 const std::string entryName = entry.getAny (
false).typeName ();
79 TEUCHOS_TEST_FOR_EXCEPTION(
80 anyVal.type () !=
typeid (int),
81 Teuchos::Exceptions::InvalidParameterType,
82 "Parameter \"" << paramName <<
"\" in sublist \"" << sublistName
83 <<
"\" has the wrong type." << endl <<
"Parameter: " << paramName
84 << endl <<
"Type specified: " << entryName << endl
85 <<
"Type required: int" << endl);
87 const int val = Teuchos::any_cast<
int> (anyVal);
88 TEUCHOS_TEST_FOR_EXCEPTION(
89 val < 0, Teuchos::Exceptions::InvalidParameterValue,
90 "Parameter \"" << paramName <<
"\" in sublist \"" << sublistName
91 <<
"\" is negative." << endl <<
"Parameter: " << paramName
92 << endl <<
"Value specified: " << val << endl
93 <<
"Required range: [0, INT_MAX]" << endl);
97 const std::string getXMLTypeName ()
const {
98 return "NonnegativeIntValidator";
103 printDoc (
const std::string& docString,
104 std::ostream &out)
const 106 Teuchos::StrUtils::printLines (out,
"# ", docString);
107 out <<
"#\tValidator Used: " << std::endl;
108 out <<
"#\t\tNonnegativeIntValidator" << std::endl;
115 template<class Scalar, const bool isOrdinal=Teuchos::ScalarTraits<Scalar>::isOrdinal>
119 static const Scalar eps ();
123 template<
class Scalar>
124 class SmallTraits<Scalar, true> {
126 static const Scalar eps () {
127 return Teuchos::ScalarTraits<Scalar>::one ();
132 template<
class Scalar>
133 class SmallTraits<Scalar, false> {
135 static const Scalar eps () {
136 return Teuchos::ScalarTraits<Scalar>::eps ();
144 template<
class MatrixType>
145 Relaxation<MatrixType>::
146 Relaxation (
const Teuchos::RCP<
const Tpetra::RowMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> >& A)
151 DampingFactor_ (STS::one ()),
152 IsParallel_ (A->getRowMap ()->getComm ()->getSize () > 1),
153 ZeroStartingSolution_ (true),
154 DoBackwardGS_ (false),
157 MinDiagonalValue_ (STS::zero ()),
158 fixTinyDiagEntries_ (false),
159 checkDiagEntries_ (false),
160 Condest_ (-STM::one ()),
161 IsInitialized_ (false),
166 InitializeTime_ (0.0),
171 globalMinMagDiagEntryMag_ (STM::zero ()),
172 globalMaxMagDiagEntryMag_ (STM::zero ()),
173 globalNumSmallDiagEntries_ (0),
174 globalNumZeroDiagEntries_ (0),
175 globalNumNegDiagEntries_ (0),
176 savedDiagOffsets_ (false)
178 this->setObjectLabel (
"Ifpack2::Relaxation");
179 TEUCHOS_TEST_FOR_EXCEPTION(
180 A_.is_null (), std::runtime_error,
181 "Ifpack2::Relaxation constructor: The input matrix A must be non-null.");
185 template<
class MatrixType>
189 template<
class MatrixType>
190 Teuchos::RCP<const Teuchos::ParameterList>
193 using Teuchos::Array;
194 using Teuchos::ParameterList;
195 using Teuchos::parameterList;
198 using Teuchos::rcp_const_cast;
199 using Teuchos::rcp_implicit_cast;
200 using Teuchos::setStringToIntegralParameter;
201 typedef Teuchos::ParameterEntryValidator PEV;
203 if (validParams_.is_null ()) {
204 RCP<ParameterList> pl = parameterList (
"Ifpack2::Relaxation");
208 Array<std::string> precTypes (3);
209 precTypes[0] =
"Jacobi";
210 precTypes[1] =
"Gauss-Seidel";
211 precTypes[2] =
"Symmetric Gauss-Seidel";
212 Array<RelaxationType> precTypeEnums (3);
213 precTypeEnums[0] = JACOBI;
214 precTypeEnums[1] = GS;
215 precTypeEnums[2] = SGS;
216 const std::string defaultPrecType (
"Jacobi");
217 setStringToIntegralParameter<RelaxationType> (
"relaxation: type",
218 defaultPrecType,
"Relaxation method", precTypes (), precTypeEnums (),
221 const int numSweeps = 1;
222 RCP<PEV> numSweepsValidator =
223 rcp_implicit_cast<PEV> (rcp (
new NonnegativeIntValidator));
224 pl->set (
"relaxation: sweeps", numSweeps,
"Number of relaxation sweeps",
225 rcp_const_cast<const PEV> (numSweepsValidator));
228 pl->set (
"relaxation: damping factor", dampingFactor);
230 const bool zeroStartingSolution =
true;
231 pl->set (
"relaxation: zero starting solution", zeroStartingSolution);
233 const bool doBackwardGS =
false;
234 pl->set (
"relaxation: backward mode", doBackwardGS);
236 const bool doL1Method =
false;
237 pl->set (
"relaxation: use l1", doL1Method);
239 const magnitude_type l1eta = (STM::one() + STM::one() + STM::one()) /
240 (STM::one() + STM::one());
241 pl->set (
"relaxation: l1 eta", l1eta);
244 pl->set (
"relaxation: min diagonal value", minDiagonalValue);
246 const bool fixTinyDiagEntries =
false;
247 pl->set (
"relaxation: fix tiny diagonal entries", fixTinyDiagEntries);
249 const bool checkDiagEntries =
false;
250 pl->set (
"relaxation: check diagonal entries", checkDiagEntries);
252 validParams_ = rcp_const_cast<
const ParameterList> (pl);
258 template<
class MatrixType>
261 using Teuchos::getIntegralValue;
262 using Teuchos::ParameterList;
268 const RelaxationType precType =
269 getIntegralValue<RelaxationType> (pl,
"relaxation: type");
270 const int numSweeps = pl.get<
int> (
"relaxation: sweeps");
271 const ST dampingFactor = pl.get<ST> (
"relaxation: damping factor");
272 const bool zeroStartSol = pl.get<
bool> (
"relaxation: zero starting solution");
273 const bool doBackwardGS = pl.get<
bool> (
"relaxation: backward mode");
274 const bool doL1Method = pl.get<
bool> (
"relaxation: use l1");
276 const ST minDiagonalValue = pl.get<ST> (
"relaxation: min diagonal value");
277 const bool fixTinyDiagEntries = pl.get<
bool> (
"relaxation: fix tiny diagonal entries");
278 const bool checkDiagEntries = pl.get<
bool> (
"relaxation: check diagonal entries");
281 PrecType_ = precType;
282 NumSweeps_ = numSweeps;
283 DampingFactor_ = dampingFactor;
284 ZeroStartingSolution_ = zeroStartSol;
285 DoBackwardGS_ = doBackwardGS;
286 DoL1Method_ = doL1Method;
288 MinDiagonalValue_ = minDiagonalValue;
289 fixTinyDiagEntries_ = fixTinyDiagEntries;
290 checkDiagEntries_ = checkDiagEntries;
294 template<
class MatrixType>
297 Teuchos::ParameterList plCopy (pl);
298 this->setParametersImpl (plCopy);
302 template<
class MatrixType>
303 const Teuchos::RCP<const Teuchos::Comm<int> > &
305 return A_->getRowMap ()->getComm ();
309 template<
class MatrixType>
310 Teuchos::RCP<
const Tpetra::RowMatrix<
typename MatrixType::scalar_type,
311 typename MatrixType::local_ordinal_type,
312 typename MatrixType::global_ordinal_type,
313 typename MatrixType::node_type> >
319 template<
class MatrixType>
320 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
321 typename MatrixType::global_ordinal_type,
322 typename MatrixType::node_type> >&
324 return A_->getDomainMap();
328 template<
class MatrixType>
329 const Teuchos::RCP<
const Tpetra::Map<
typename MatrixType::local_ordinal_type,
330 typename MatrixType::global_ordinal_type,
331 typename MatrixType::node_type> >&
333 return A_->getRangeMap();
337 template<
class MatrixType>
343 template<
class MatrixType>
345 return(NumInitialize_);
349 template<
class MatrixType>
355 template<
class MatrixType>
361 template<
class MatrixType>
363 return(InitializeTime_);
367 template<
class MatrixType>
369 return(ComputeTime_);
373 template<
class MatrixType>
379 template<
class MatrixType>
381 return(ComputeFlops_);
385 template<
class MatrixType>
391 template<
class MatrixType>
392 typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType
399 template<
class MatrixType>
400 typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType
403 typename MatrixType::local_ordinal_type MaxIters,
405 const Teuchos::Ptr<
const Tpetra::RowMatrix<
typename MatrixType::scalar_type,
406 typename MatrixType::local_ordinal_type,
407 typename MatrixType::global_ordinal_type,
408 typename MatrixType::node_type> > &matrix)
415 Condest_ = Ifpack2::Condest(*
this, CT, MaxIters, Tol, matrix);
421 template<
class MatrixType>
423 const Tpetra::MultiVector<
typename MatrixType::scalar_type,
424 typename MatrixType::local_ordinal_type,
425 typename MatrixType::global_ordinal_type,
426 typename MatrixType::node_type>& X,
427 Tpetra::MultiVector<
typename MatrixType::scalar_type,
428 typename MatrixType::local_ordinal_type,
429 typename MatrixType::global_ordinal_type,
430 typename MatrixType::node_type>& Y,
431 Teuchos::ETransp mode,
438 using Teuchos::rcpFromRef;
439 typedef Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> MV;
441 TEUCHOS_TEST_FOR_EXCEPTION(
444 "Ifpack2::Relaxation::apply: You must call compute() on this Ifpack2 " 445 "preconditioner instance before you may call apply(). You may call " 446 "isComputed() to find out if compute() has been called already.");
448 TEUCHOS_TEST_FOR_EXCEPTION(
449 X.getNumVectors() != Y.getNumVectors(),
451 "Ifpack2::Relaxation::apply: X and Y have different numbers of columns. " 452 "X has " << X.getNumVectors() <<
" columns, but Y has " 453 << Y.getNumVectors() <<
" columns.");
455 TEUCHOS_TEST_FOR_EXCEPTION(
456 beta != STS::zero (), std::logic_error,
457 "Ifpack2::Relaxation::apply: beta = " << beta <<
" != 0 case not " 462 Teuchos::TimeMonitor timeMon (*Time_,
true);
465 if (alpha == STS::zero ()) {
467 Y.putScalar (STS::zero ());
473 if (X.getLocalMV().getValues() == Y.getLocalMV().getValues()) {
474 Xcopy = rcp (
new MV (X));
477 Xcopy = rcpFromRef (X);
484 case Ifpack2::JACOBI:
485 ApplyInverseJacobi(*Xcopy,Y);
488 ApplyInverseGS(*Xcopy,Y);
491 ApplyInverseSGS(*Xcopy,Y);
494 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
495 "Ifpack2::Relaxation::apply: Invalid preconditioner type enum value " 496 << PrecType_ <<
". Please report this bug to the Ifpack2 developers.");
498 if (alpha != STS::one ()) {
500 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
501 const double numVectors = as<double> (Y.getNumVectors ());
502 ApplyFlops_ += numGlobalRows * numVectors;
506 ApplyTime_ += Time_->totalElapsedTime ();
511 template<
class MatrixType>
513 const Tpetra::MultiVector<
typename MatrixType::scalar_type,
514 typename MatrixType::local_ordinal_type,
515 typename MatrixType::global_ordinal_type,
516 typename MatrixType::node_type>& X,
517 Tpetra::MultiVector<
typename MatrixType::scalar_type,
518 typename MatrixType::local_ordinal_type,
519 typename MatrixType::global_ordinal_type,
520 typename MatrixType::node_type>& Y,
521 Teuchos::ETransp mode)
const 523 TEUCHOS_TEST_FOR_EXCEPTION(
isComputed() ==
false, std::runtime_error,
524 "Ifpack2::Relaxation::applyMat() ERROR: isComputed() must be true prior to calling applyMat().");
525 TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
526 "Ifpack2::Relaxation::applyMat() ERROR: X.getNumVectors() != Y.getNumVectors().");
527 A_->apply(X, Y, mode);
531 template<
class MatrixType>
536 IsInitialized_ =
true;
540 template<
class MatrixType>
543 using Teuchos::Array;
544 using Teuchos::ArrayRCP;
545 using Teuchos::ArrayView;
550 using Teuchos::REDUCE_MAX;
551 using Teuchos::REDUCE_MIN;
552 using Teuchos::REDUCE_SUM;
553 using Teuchos::rcp_dynamic_cast;
554 using Teuchos::reduceAll;
555 typedef Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> vector_type;
567 Teuchos::TimeMonitor timeMon (*Time_,
true);
571 Condest_ = -STM::one ();
573 TEUCHOS_TEST_FOR_EXCEPTION(
574 NumSweeps_ < 0, std::logic_error,
575 "Ifpack2::Relaxation::compute: NumSweeps_ = " << NumSweeps_ <<
" < 0. " 576 "Please report this bug to the Ifpack2 developers.");
578 Diagonal_ = rcp (
new vector_type (A_->getRowMap ()));
580 TEUCHOS_TEST_FOR_EXCEPTION(
581 Diagonal_.is_null (), std::logic_error,
582 "Ifpack2::Relaxation::compute: Vector of diagonal entries has not been " 583 "created yet. Please report this bug to the Ifpack2 developers.");
596 RCP<const MatrixType> crsMat = rcp_dynamic_cast<
const MatrixType> (A_);
597 if (crsMat.is_null () || ! crsMat->isStaticGraph ()) {
598 A_->getLocalDiagCopy (*Diagonal_);
600 if (! savedDiagOffsets_) {
601 crsMat->getLocalDiagOffsets (diagOffsets_);
602 savedDiagOffsets_ =
true;
604 crsMat->getLocalDiagCopy (*Diagonal_, diagOffsets_ ());
605 #ifdef HAVE_TPETRA_DEBUG 607 vector_type D_copy (A_->getRowMap ());
608 A_->getLocalDiagCopy (D_copy);
609 D_copy.update (STS::one (), *Diagonal_, -STS::one ());
613 TEUCHOS_TEST_FOR_EXCEPTION(
614 err != STM::zero(), std::logic_error,
"Ifpack2::Relaxation::compute: " 615 "\"fast-path\" diagonal computation failed. \\|D1 - D2\\|_inf = " 617 #endif // HAVE_TPETRA_DEBUG 623 RCP<vector_type> origDiag;
624 if (checkDiagEntries_) {
625 origDiag = rcp (
new vector_type (A_->getRowMap ()));
626 *origDiag = *Diagonal_;
632 ArrayRCP<scalar_type> diagHostView = Diagonal_->get1dViewNonConst ();
638 #ifdef HAVE_IFPACK2_DEBUG 639 ArrayView<scalar_type> diag = diagHostView ();
641 scalar_type* KOKKOSCLASSIC_RESTRICT
const diag = diagHostView.getRawPtr ();
642 #endif // HAVE_IFPACK2_DEBUG 644 const size_t numMyRows = A_->getNodeNumRows ();
653 if (DoL1Method_ && IsParallel_) {
655 const size_t maxLength = A_->getNodeMaxNumRowEntries ();
656 Array<local_ordinal_type> indices (maxLength);
657 Array<scalar_type> values (maxLength);
660 for (
size_t i = 0; i < numMyRows; ++i) {
661 A_->getLocalRowCopy (i, indices (), values (), numEntries);
663 for (
size_t k = 0 ; k < numEntries ; ++k) {
664 if (as<size_t> (indices[k]) > numMyRows) {
665 diagonal_boost += STS::magnitude (values[k] / two);
668 if (STS::magnitude (diag[i]) < L1Eta_ * diagonal_boost) {
669 diag[i] += diagonal_boost;
685 const scalar_type oneOverMinDiagVal = (MinDiagonalValue_ == zero) ?
686 one / SmallTraits<scalar_type>::eps () :
687 one / MinDiagonalValue_;
689 const magnitude_type minDiagValMag = STS::magnitude (MinDiagonalValue_);
691 if (checkDiagEntries_) {
697 size_t numSmallDiagEntries = 0;
698 size_t numZeroDiagEntries = 0;
699 size_t numNegDiagEntries = 0;
713 minMagDiagEntry = d_0;
714 maxMagDiagEntry = d_0;
715 minMagDiagEntryMag = d_0_mag;
716 maxMagDiagEntryMag = d_0_mag;
724 for (
size_t i = 0 ; i < numMyRows; ++i) {
731 if (d_i_real < STM::zero ()) {
734 if (d_i_mag < minMagDiagEntryMag) {
735 minMagDiagEntry = d_i;
736 minMagDiagEntryMag = d_i_mag;
738 if (d_i_mag > maxMagDiagEntryMag) {
739 maxMagDiagEntry = d_i;
740 maxMagDiagEntryMag = d_i_mag;
743 if (fixTinyDiagEntries_) {
745 if (d_i_mag <= minDiagValMag) {
746 ++numSmallDiagEntries;
747 if (d_i_mag == STM::zero ()) {
748 ++numZeroDiagEntries;
750 diag[i] = oneOverMinDiagVal;
757 if (d_i_mag <= minDiagValMag) {
758 ++numSmallDiagEntries;
759 if (d_i_mag == STM::zero ()) {
760 ++numZeroDiagEntries;
770 diagHostView = Teuchos::null;
775 if (STS::isComplex) {
776 ComputeFlops_ += 4.0 * numMyRows;
778 ComputeFlops_ += numMyRows;
795 const Comm<int>& comm = * (A_->getRowMap ()->getComm ());
798 Array<magnitude_type> localVals (2);
799 localVals[0] = minMagDiagEntryMag;
801 localVals[1] = -maxMagDiagEntryMag;
802 Array<magnitude_type> globalVals (2);
803 reduceAll<int, magnitude_type> (comm, REDUCE_MIN, 2,
804 localVals.getRawPtr (),
805 globalVals.getRawPtr ());
806 globalMinMagDiagEntryMag_ = globalVals[0];
807 globalMaxMagDiagEntryMag_ = -globalVals[1];
809 Array<size_t> localCounts (3);
810 localCounts[0] = numSmallDiagEntries;
811 localCounts[1] = numZeroDiagEntries;
812 localCounts[2] = numNegDiagEntries;
813 Array<size_t> globalCounts (3);
814 reduceAll<int, size_t> (comm, REDUCE_SUM, 3,
815 localCounts.getRawPtr (),
816 globalCounts.getRawPtr ());
817 globalNumSmallDiagEntries_ = globalCounts[0];
818 globalNumZeroDiagEntries_ = globalCounts[1];
819 globalNumNegDiagEntries_ = globalCounts[2];
822 (void) minMagDiagEntry;
823 (void) maxMagDiagEntry;
827 RCP<vector_type> diff = rcp (
new vector_type (A_->getRowMap ()));
828 diff->reciprocal (*origDiag);
829 diff->update (-one, *Diagonal_, one);
830 globalDiagNormDiff_ = diff->norm2 ();
833 if (fixTinyDiagEntries_) {
837 for (
size_t i = 0 ; i < numMyRows; ++i) {
842 if (d_i_mag <= minDiagValMag) {
843 diag[i] = oneOverMinDiagVal;
850 for (
size_t i = 0 ; i < numMyRows; ++i) {
851 diag[i] = one / diag[i];
854 if (STS::isComplex) {
855 ComputeFlops_ += 4.0 * numMyRows;
857 ComputeFlops_ += numMyRows;
861 if (IsParallel_ && ((PrecType_ == Ifpack2::GS) || (PrecType_ == Ifpack2::SGS))) {
862 Importer_ = A_->getGraph ()->getImporter ();
869 ComputeTime_ += Time_->totalElapsedTime ();
875 template<
class MatrixType>
877 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
878 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 881 typedef Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> MV;
883 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
884 const double numVectors = as<double> (X.getNumVectors ());
885 if (ZeroStartingSolution_) {
890 Y.elementWiseMultiply (DampingFactor_, *Diagonal_, X, STS::zero ());
896 double flopUpdate = 0.0;
897 if (DampingFactor_ == STS::one ()) {
899 flopUpdate = numGlobalRows * numVectors;
903 flopUpdate = 2.0 * numGlobalRows * numVectors;
905 ApplyFlops_ += flopUpdate;
906 if (NumSweeps_ == 1) {
912 const int startSweep = ZeroStartingSolution_ ? 1 : 0;
915 MV A_times_Y (Y.getMap (), as<size_t>(numVectors),
false);
916 for (
int j = startSweep; j < NumSweeps_; ++j) {
919 A_times_Y.update (STS::one (), X, -STS::one ());
920 Y.elementWiseMultiply (DampingFactor_, *Diagonal_, A_times_Y, STS::one ());
934 const double numGlobalNonzeros = as<double> (A_->getGlobalNumEntries ());
935 const double dampingFlops = (DampingFactor_ == STS::one ()) ? 0.0 : 1.0;
936 ApplyFlops_ += as<double> (NumSweeps_ - startSweep) * numVectors *
937 (2.0 * numGlobalRows + 2.0 * numGlobalNonzeros + dampingFlops);
941 template<
class MatrixType>
943 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
944 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 947 using Teuchos::rcp_dynamic_cast;
951 RCP<const MatrixType> crsMat = rcp_dynamic_cast<
const MatrixType> (A_);
957 if (! crsMat.is_null ()) {
958 ApplyInverseGS_CrsMatrix (*crsMat, X, Y);
961 ApplyInverseGS_RowMatrix (X, Y);
966 template<
class MatrixType>
968 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
969 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 971 using Teuchos::Array;
972 using Teuchos::ArrayRCP;
976 using Teuchos::rcpFromRef;
977 typedef Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> MV;
982 if (ZeroStartingSolution_) {
983 Y.putScalar (STS::zero ());
986 const size_t NumVectors = X.getNumVectors ();
987 const size_t maxLength = A_->getNodeMaxNumRowEntries ();
988 Array<local_ordinal_type> Indices (maxLength);
989 Array<scalar_type> Values (maxLength);
993 if (Importer_.is_null ()) {
995 Y2 = rcp (
new MV (Y.getMap (), NumVectors,
false));
1000 Y2 = rcp (
new MV (Importer_->getTargetMap (), NumVectors));
1004 Y2 = rcpFromRef (Y);
1008 ArrayRCP<ArrayRCP<scalar_type> > y_ptr = Y.get2dViewNonConst();
1009 ArrayRCP<ArrayRCP<scalar_type> > y2_ptr = Y2->get2dViewNonConst();
1010 ArrayRCP<ArrayRCP<const scalar_type> > x_ptr = X.get2dView();
1011 ArrayRCP<const scalar_type> d_ptr = Diagonal_->get1dView();
1013 const size_t numMyRows = A_->getNodeNumRows ();
1014 for (
int j = 0; j < NumSweeps_; j++) {
1017 if (Importer_.is_null ()) {
1020 Y2->doImport (Y, *Importer_, Tpetra::INSERT);
1024 if (! DoBackwardGS_) {
1025 for (
size_t i = 0; i < numMyRows; ++i) {
1027 A_->getLocalRowCopy (as<local_ordinal_type> (i), Indices (), Values (), NumEntries);
1029 for (
size_t m = 0; m < NumVectors; ++m) {
1031 for (
size_t k = 0; k < NumEntries; ++k) {
1033 dtemp += Values[k] * y2_ptr[m][col];
1035 y2_ptr[m][i] += DampingFactor_ * d_ptr[i] * (x_ptr[m][i] - dtemp);
1042 for (ptrdiff_t i = as<ptrdiff_t> (numMyRows) - 1; i >= 0; --i) {
1044 A_->getLocalRowCopy (as<local_ordinal_type> (i), Indices (), Values (), NumEntries);
1046 for (
size_t m = 0; m < NumVectors; ++m) {
1048 for (
size_t k = 0; k < NumEntries; ++k) {
1050 dtemp += Values[k] * y2_ptr[m][col];
1052 y2_ptr[m][i] += DampingFactor_ * d_ptr[i] * (x_ptr[m][i] - dtemp);
1064 const double dampingFlops = (DampingFactor_ == STS::one()) ? 0.0 : 1.0;
1065 const double numVectors = as<double> (X.getNumVectors ());
1066 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
1067 const double numGlobalNonzeros = as<double> (A_->getGlobalNumEntries ());
1068 ApplyFlops_ += 2.0 * NumSweeps_ * numVectors *
1069 (2.0 * numGlobalRows + 2.0 * numGlobalNonzeros + dampingFlops);
1073 template<
class MatrixType>
1077 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
1078 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 1082 const Tpetra::ESweepDirection direction =
1083 DoBackwardGS_ ? Tpetra::Backward : Tpetra::Forward;
1084 A.gaussSeidelCopy (Y, X, *Diagonal_, DampingFactor_, direction,
1085 NumSweeps_, ZeroStartingSolution_);
1098 const double dampingFlops = (DampingFactor_ == STS::one()) ? 0.0 : 1.0;
1099 const double numVectors = as<double> (X.getNumVectors ());
1100 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
1101 const double numGlobalNonzeros = as<double> (A_->getGlobalNumEntries ());
1102 ApplyFlops_ += NumSweeps_ * numVectors *
1103 (2.0 * numGlobalRows + 2.0 * numGlobalNonzeros + dampingFlops);
1107 template<
class MatrixType>
1109 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
1110 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 1113 using Teuchos::rcp_dynamic_cast;
1128 RCP<const MatrixType> crsMat = rcp_dynamic_cast<
const MatrixType> (A_);
1134 if (! crsMat.is_null ()) {
1135 ApplyInverseSGS_CrsMatrix (*crsMat, X, Y);
1138 ApplyInverseSGS_RowMatrix (X, Y);
1143 template<
class MatrixType>
1145 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
1146 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 1148 using Teuchos::Array;
1149 using Teuchos::ArrayRCP;
1153 using Teuchos::rcpFromRef;
1154 typedef Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> MV;
1159 if (ZeroStartingSolution_) {
1160 Y.putScalar (STS::zero ());
1163 const size_t NumVectors = X.getNumVectors ();
1164 const size_t maxLength = A_->getNodeMaxNumRowEntries ();
1165 Array<local_ordinal_type> Indices (maxLength);
1166 Array<scalar_type> Values (maxLength);
1170 if (Importer_.is_null ()) {
1172 Y2 = rcp (
new MV (Y.getMap (), NumVectors,
false));
1177 Y2 = rcp (
new MV (Importer_->getTargetMap (), NumVectors));
1181 Y2 = rcpFromRef (Y);
1184 ArrayRCP<ArrayRCP<scalar_type> > y_ptr = Y.get2dViewNonConst ();
1185 ArrayRCP<ArrayRCP<scalar_type> > y2_ptr = Y2->get2dViewNonConst ();
1186 ArrayRCP<ArrayRCP<const scalar_type> > x_ptr = X.get2dView ();
1187 ArrayRCP<const scalar_type> d_ptr = Diagonal_->get1dView ();
1189 const size_t numMyRows = A_->getNodeNumRows ();
1191 for (
int iter = 0; iter < NumSweeps_; ++iter) {
1194 if (Importer_.is_null ()) {
1197 Y2->doImport (Y, *Importer_, Tpetra::INSERT);
1201 for (
size_t i = 0; i < numMyRows; ++i) {
1204 A_->getLocalRowCopy (as<local_ordinal_type> (i), Indices (), Values (), NumEntries);
1206 for (
size_t m = 0; m < NumVectors; ++m) {
1208 for (
size_t k = 0; k < NumEntries; ++k) {
1210 dtemp += Values[k] * y2_ptr[m][col];
1212 y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
1218 for (ptrdiff_t i = as<ptrdiff_t> (numMyRows) - 1; i >= 0; --i) {
1221 A_->getLocalRowCopy (as<local_ordinal_type> (i), Indices (), Values (), NumEntries);
1223 for (
size_t m = 0; m < NumVectors; ++m) {
1225 for (
size_t k = 0; k < NumEntries; ++k) {
1227 dtemp += Values[k] * y2_ptr[m][col];
1229 y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
1240 const double dampingFlops = (DampingFactor_ == STS::one()) ? 0.0 : 1.0;
1241 const double numVectors = as<double> (X.getNumVectors ());
1242 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
1243 const double numGlobalNonzeros = as<double> (A_->getGlobalNumEntries ());
1244 ApplyFlops_ += 2.0 * NumSweeps_ * numVectors *
1245 (2.0 * numGlobalRows + 2.0 * numGlobalNonzeros + dampingFlops);
1249 template<
class MatrixType>
1253 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
1254 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y)
const 1258 const Tpetra::ESweepDirection direction = Tpetra::Symmetric;
1259 A.gaussSeidelCopy (Y, X, *Diagonal_, DampingFactor_, direction,
1260 NumSweeps_, ZeroStartingSolution_);
1276 const double dampingFlops = (DampingFactor_ == STS::one()) ? 0.0 : 1.0;
1277 const double numVectors = as<double> (X.getNumVectors ());
1278 const double numGlobalRows = as<double> (A_->getGlobalNumRows ());
1279 const double numGlobalNonzeros = as<double> (A_->getGlobalNumEntries ());
1280 ApplyFlops_ += 2.0 * NumSweeps_ * numVectors *
1281 (2.0 * numGlobalRows + 2.0 * numGlobalNonzeros + dampingFlops);
1285 template<
class MatrixType>
1287 using Teuchos::TypeNameTraits;
1288 std::ostringstream os;
1292 status =
"initialized";
1294 status +=
", computed";
1296 status +=
", not computed";
1299 status =
"not initialized";
1303 if (PrecType_ == Ifpack2::JACOBI) {
1305 }
else if (PrecType_ == Ifpack2::GS) {
1306 type =
"Gauss-Seidel";
1307 }
else if (PrecType_ == Ifpack2::SGS) {
1308 type =
"Symmetric Gauss-Seidel";
1316 os <<
"\"Ifpack2::Relaxation\": { " 1317 <<
"MatrixType: \"" << TypeNameTraits<MatrixType>::name () <<
"\", " 1318 <<
"Status: " << status <<
", " 1319 <<
"\"relaxation: type\": " << type <<
", " 1320 <<
"\"relaxation: sweeps\": " << NumSweeps_ <<
", " 1321 <<
"\"relaxation: damping factor\": " << DampingFactor_ <<
", ";
1323 os <<
"\"relaxation: use l1\": " << DoL1Method_ <<
", " 1324 <<
"\"relaxation: l1 eta\": " << L1Eta_ <<
", ";
1326 os <<
"\"Global number of rows\": " << A_->getGlobalNumRows () <<
", " 1327 <<
"\"Global number of columns\": " << A_->getGlobalNumCols ()
1333 template<
class MatrixType>
1337 const Teuchos::EVerbosityLevel verbLevel)
const 1339 using Teuchos::OSTab;
1340 using Teuchos::TypeNameTraits;
1341 using Teuchos::VERB_DEFAULT;
1342 using Teuchos::VERB_NONE;
1343 using Teuchos::VERB_LOW;
1344 using Teuchos::VERB_MEDIUM;
1345 using Teuchos::VERB_HIGH;
1346 using Teuchos::VERB_EXTREME;
1349 const Teuchos::EVerbosityLevel vl =
1350 (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
1352 const int myRank = this->
getComm ()->getRank ();
1360 if (vl != VERB_NONE && myRank == 0) {
1364 out <<
"\"Ifpack2::Relaxation\":" << endl;
1366 out <<
"MatrixType: \"" << TypeNameTraits<MatrixType>::name () <<
"\"" << endl
1367 <<
"Label: " << this->getObjectLabel () << endl
1368 <<
"Parameters: " << endl;
1371 out <<
"\"relaxation: type\": ";
1372 if (PrecType_ == Ifpack2::JACOBI) {
1374 }
else if (PrecType_ == Ifpack2::GS) {
1375 out <<
"Gauss-Seidel";
1376 }
else if (PrecType_ == Ifpack2::SGS) {
1377 out <<
"Symmetric Gauss-Seidel";
1384 <<
"\"relaxation: sweeps\": " << NumSweeps_ << endl
1385 <<
"\"relaxation: damping factor\": " << DampingFactor_ << endl
1386 <<
"\"relaxation: min diagonal value\": " << MinDiagonalValue_ << endl
1387 <<
"\"relaxation: zero starting solution\": " << ZeroStartingSolution_ << endl
1388 <<
"\"relaxation: backward mode\": " << DoBackwardGS_ << endl
1389 <<
"\"relaxation: use l1\": " << DoL1Method_ << endl
1390 <<
"\"relaxation: l1 eta\": " << L1Eta_ << endl;
1392 out <<
"Computed quantities: " << endl;
1395 out <<
"initialized: " << (
isInitialized () ?
"true" :
"false") << endl
1396 <<
"computed: " << (
isComputed () ?
"true" :
"false") << endl
1397 <<
"Condition number estimate: " << Condest_ << endl
1398 <<
"Global number of rows: " << A_->getGlobalNumRows () << endl
1399 <<
"Global number of columns: " << A_->getGlobalNumCols () << endl;
1402 out <<
"Properties of input diagonal entries:" << endl;
1405 out <<
"Magnitude of minimum-magnitude diagonal entry: " 1406 << globalMinMagDiagEntryMag_ << endl
1407 <<
"Magnitude of maximum-magnitude diagonal entry: " 1408 << globalMaxMagDiagEntryMag_ << endl
1409 <<
"Number of diagonal entries with small magnitude: " 1410 << globalNumSmallDiagEntries_ << endl
1411 <<
"Number of zero diagonal entries: " 1412 << globalNumZeroDiagEntries_ << endl
1413 <<
"Number of diagonal entries with negative real part: " 1414 << globalNumNegDiagEntries_ << endl
1415 <<
"Abs 2-norm diff between computed and actual inverse " 1416 <<
"diagonal: " << globalDiagNormDiff_ << endl;
1420 out <<
"Saved diagonal offsets: " 1421 << (savedDiagOffsets_ ?
"true" :
"false") << endl;
1423 out <<
"Call counts and total times (in seconds): " << endl;
1426 out <<
"initialize: " << endl;
1429 out <<
"Call count: " << NumInitialize_ << endl;
1430 out <<
"Total time: " << InitializeTime_ << endl;
1432 out <<
"compute: " << endl;
1435 out <<
"Call count: " << NumCompute_ << endl;
1436 out <<
"Total time: " << ComputeTime_ << endl;
1438 out <<
"apply: " << endl;
1441 out <<
"Call count: " << NumApply_ << endl;
1442 out <<
"Total time: " << ApplyTime_ << endl;
1450 #endif // IFPACK2_RELAXATION_DEF_HPP bool hasTransposeApply() const
Whether apply() and applyMat() let you apply the transpose or conjugate transpose.
Definition: Ifpack2_Relaxation_def.hpp:338
CondestType
Ifpack2::CondestType: enum to define the type of condition number estimate.
Definition: Ifpack2_CondestType.hpp:50
double getComputeFlops() const
Total number of floating-point operations over all calls to compute().
Definition: Ifpack2_Relaxation_def.hpp:380
double getApplyFlops() const
Total number of floating-point operations over all calls to apply().
Definition: Ifpack2_Relaxation_def.hpp:386
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object's attributes to the given output stream.
Definition: Ifpack2_Relaxation_def.hpp:1336
void compute()
Compute the preconditioner for the specified matrix, diagonal perturbation thresholds and relaxation ...
Definition: Ifpack2_Relaxation_def.hpp:541
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
The communicator over which the matrix and vectors are distributed.
Definition: Ifpack2_Relaxation_def.hpp:304
Teuchos::RCP< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > getMatrix() const
The matrix to be preconditioned.
Definition: Ifpack2_Relaxation_def.hpp:314
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getRangeMap() const
Returns the Tpetra::Map object associated with the range of this operator.
Definition: Ifpack2_Relaxation_def.hpp:332
int getNumApply() const
Total number of calls to apply().
Definition: Ifpack2_Relaxation_def.hpp:356
const Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > & getDomainMap() const
Returns the Tpetra::Map object associated with the domain of this operator.
Definition: Ifpack2_Relaxation_def.hpp:323
double getComputeTime() const
Total time in seconds spent in all calls to compute().
Definition: Ifpack2_Relaxation_def.hpp:368
magnitude_type getCondEst() const
The computed estimated condition number, or -1 if not previously computed.
Definition: Ifpack2_Relaxation_def.hpp:393
void setParameters(const Teuchos::ParameterList ¶ms)
Set the relaxation / preconditioner parameters.
Definition: Ifpack2_Relaxation_def.hpp:295
std::string description() const
A simple one-line description of this object.
Definition: Ifpack2_Relaxation_def.hpp:1286
int getNumInitialize() const
Total number of calls to initialize().
Definition: Ifpack2_Relaxation_def.hpp:344
bool isComputed() const
Return true if compute() has been called.
Definition: Ifpack2_Relaxation_decl.hpp:416
magnitude_type computeCondEst(CondestType CT=Cheap, local_ordinal_type MaxIters=1550, magnitude_type Tol=1e-9, const Teuchos::Ptr< const Tpetra::RowMatrix< scalar_type, local_ordinal_type, global_ordinal_type, node_type > > &matrix=Teuchos::null)
Computes and returns the estimated condition number.
Definition: Ifpack2_Relaxation_def.hpp:401
double getInitializeTime() const
Total time in seconds spent in all calls to initialize().
Definition: Ifpack2_Relaxation_def.hpp:362
void applyMat(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS) const
Apply the preconditioner to X, returning the result in Y.
Definition: Ifpack2_Relaxation_def.hpp:512
Definition: Ifpack2_BlockRelaxation_decl.hpp:64
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_Relaxation_decl.hpp:249
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_Relaxation_decl.hpp:242
virtual ~Relaxation()
Destructor.
Definition: Ifpack2_Relaxation_def.hpp:186
int getNumCompute() const
Total number of calls to compute().
Definition: Ifpack2_Relaxation_def.hpp:350
bool isInitialized() const
Returns true if the preconditioner has been successfully initialized.
Definition: Ifpack2_Relaxation_decl.hpp:408
Relaxation preconditioners for Tpetra::RowMatrix and Tpetra::CrsMatrix sparse matrices.
Definition: Ifpack2_Relaxation_decl.hpp:231
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
double getApplyTime() const
Total time in seconds spent in all calls to apply().
Definition: Ifpack2_Relaxation_def.hpp:374
void initialize()
Initialize the preconditioner.
Definition: Ifpack2_Relaxation_def.hpp:532
void apply(const Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &X, Tpetra::MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, scalar_type alpha=Teuchos::ScalarTraits< scalar_type >::one(), scalar_type beta=Teuchos::ScalarTraits< scalar_type >::zero()) const
Apply the preconditioner to X, returning the result in Y.
Definition: Ifpack2_Relaxation_def.hpp:422
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a list of all the parameters that this class accepts.
Definition: Ifpack2_Relaxation_def.hpp:191
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_Relaxation_decl.hpp:270