43 #ifndef IFPACK2_CRSRILUK_DEF_HPP 44 #define IFPACK2_CRSRILUK_DEF_HPP 49 template<
class MatrixType>
51 : isOverlapped_(false),
59 isInitialized_(false),
68 OverlapMode_(Tpetra::REPLACE)
96 template<
class MatrixType>
101 template<
class MatrixType>
105 L_ = Teuchos::rcp(
new MatrixType(Graph_->getL_Graph()) );
106 U_ = Teuchos::rcp(
new MatrixType(Graph_->getU_Graph()) );
107 L_->setAllToScalar(0.0);
108 U_->setAllToScalar(0.0);
111 bool isLower = L_->isLowerTriangular();
112 bool isUpper = U_->isUpperTriangular();
113 if (!isLower || !isUpper) {
114 std::cout <<
"error in triangular detection" << std::endl;
117 D_ = Teuchos::rcp(
new Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(Graph_->getL_Graph()->getRowMap()) );
122 template<
class MatrixType>
128 using Teuchos::Exceptions::InvalidParameterName;
129 using Teuchos::Exceptions::InvalidParameterType;
130 typedef Teuchos::ScalarTraits<magnitude_type> STM;
134 int overlapLevel = 0;
147 bool gotFillLevel =
false;
149 fillLevel = params.get<
int> (
"fact: iluk level-of-fill");
152 catch (InvalidParameterType&) {
157 catch (InvalidParameterName&) {
161 if (! gotFillLevel) {
165 fillLevel = as<int> (params.get<
magnitude_type> (
"fact: iluk level-of-fill"));
168 catch (InvalidParameterType&) {
174 if (! gotFillLevel) {
178 fillLevel = as<int> (params.get<
double> (
"fact: iluk level-of-fill"));
181 catch (InvalidParameterType& e) {
191 TEUCHOS_TEST_FOR_EXCEPTION(
194 "Ifpack2::RILUK::setParameters: We should never get here! " 195 "The method should either have read the \"fact: iluk level-of-fill\" " 196 "parameter by this point, or have thrown an exception. " 197 "Please let the Ifpack2 developers know about this bug.");
207 bool gotOverlapLevel =
false;
209 overlapLevel = params.get<
int> (
"fact: iluk level-of-overlap");
210 gotOverlapLevel =
true;
212 catch (InvalidParameterType&) {
217 catch (InvalidParameterName&) {
218 gotOverlapLevel =
true;
221 if (! gotOverlapLevel) {
225 overlapLevel = as<int> (params.get<
magnitude_type> (
"fact: iluk level-of-overlap"));
226 gotOverlapLevel =
true;
228 catch (InvalidParameterType&) {
234 if (! gotOverlapLevel) {
238 overlapLevel = as<int> (params.get<
double> (
"fact: iluk level-of-overlap"));
239 gotOverlapLevel =
true;
241 catch (InvalidParameterType& e) {
251 TEUCHOS_TEST_FOR_EXCEPTION(
254 "Ifpack2::RILUK::setParameters: We should never get here! " 255 "The method should either have read the \"fact: iluk level-of-overlap\" " 256 "parameter by this point, or have thrown an exception. " 257 "Please let the Ifpack2 developers know about this bug.");
265 absThresh = params.get<
magnitude_type> (
"fact: absolute threshold");
267 catch (InvalidParameterType&) {
270 absThresh = as<magnitude_type> (params.get<
double> (
"fact: absolute threshold"));
272 catch (InvalidParameterName&) {
277 relThresh = params.get<
magnitude_type> (
"fact: relative threshold");
279 catch (InvalidParameterType&) {
282 relThresh = as<magnitude_type> (params.get<
double> (
"fact: relative threshold"));
284 catch (InvalidParameterName&) {
291 catch (InvalidParameterType&) {
294 relaxValue = as<magnitude_type> (params.get<
double> (
"fact: relax value"));
296 catch (InvalidParameterName&) {
304 LevelOfFill_ = fillLevel;
305 LevelOfOverlap_ = overlapLevel;
312 if (absThresh != -STM::one ()) {
313 Athresh_ = absThresh;
315 if (relThresh != -STM::one ()) {
316 Rthresh_ = relThresh;
318 if (relaxValue != -STM::one ()) {
319 RelaxValue_ = relaxValue;
324 template<
class MatrixType>
327 if (Graph_ != Teuchos::null)
return;
331 Graph_->constructFilledGraph();
333 setInitialized(
true);
335 if (!isAllocated()) allocate_L_and_U();
338 throw std::runtime_error(
"Error, Ifpack2::RILUK::initialize: overlapping not yet supported.");
351 template<
class MatrixType>
354 size_t NumIn = 0, NumL = 0, NumU = 0;
355 bool DiagFound =
false;
356 size_t NumNonzeroDiags = 0;
357 size_t MaxNumEntries = OverlapA.getGlobalMaxNumRowEntries();
360 Teuchos::Array<global_ordinal_type> InI(MaxNumEntries);
361 Teuchos::Array<global_ordinal_type> LI(MaxNumEntries);
362 Teuchos::Array<global_ordinal_type> UI(MaxNumEntries);
363 Teuchos::Array<scalar_type> InV(MaxNumEntries);
364 Teuchos::Array<scalar_type> LV(MaxNumEntries);
365 Teuchos::Array<scalar_type> UV(MaxNumEntries);
367 bool ReplaceValues = (L_->isStaticGraph() || L_->isLocallyIndexed());
372 L_->setAllToScalar(0.0);
373 U_->setAllToScalar(0.0);
377 Teuchos::ArrayRCP<scalar_type> DV = D_->get1dViewNonConst();
379 const Teuchos::RCP<const Tpetra::Map<local_ordinal_type,global_ordinal_type,node_type> >& rowMap =
384 for (
global_ordinal_type i=rowMap->getMinGlobalIndex(); i<=rowMap->getMaxGlobalIndex(); ++i) {
388 OverlapA.getGlobalRowCopy(global_row, InI(), InV(), NumIn);
396 for (
size_t j=0; j< NumIn; j++) {
401 DV[local_row] += Rthresh_ * InV[j] + IFPACK2_SGN(InV[j]) * Athresh_;
405 throw std::runtime_error(
"out of range (k<0) in Ifpack2::RILUK::initAllValues");
413 else if (k <= rowMap->getMaxGlobalIndex()) {
425 if (DiagFound) ++NumNonzeroDiags;
426 else DV[local_row] = Athresh_;
430 L_->replaceGlobalValues(global_row, LI(0, NumL), LV(0,NumL));
433 L_->insertGlobalValues(global_row, LI(0,NumL), LV(0,NumL));
439 U_->replaceGlobalValues(global_row, UI(0,NumU), UV(0,NumU));
442 U_->insertGlobalValues(global_row, UI(0,NumU), UV(0,NumU));
452 L_->fillComplete(L_->getColMap(), A_->getRangeMap());
453 U_->fillComplete(A_->getDomainMap(), U_->getRowMap());
457 setInitialized(
true);
460 size_t TotalNonzeroDiags = 0;
461 Teuchos::reduceAll(*L_->getRowMap()->getComm(),Teuchos::REDUCE_SUM,
462 1,&NumNonzeroDiags,&TotalNonzeroDiags);
463 NumMyDiagonals_ = NumNonzeroDiags;
464 if (NumNonzeroDiags != U_->getNodeNumRows()) {
465 throw std::runtime_error(
"Error in Ifpack2::RILUK::initAllValues, wrong number of diagonals.");
470 template<
class MatrixType>
476 TEUCHOS_TEST_FOR_EXCEPTION(!
isInitialized(), std::runtime_error,
477 "Ifpack2::RILUK::compute() ERROR: isInitialized() must be true.");
478 TEUCHOS_TEST_FOR_EXCEPTION(
isComputed() ==
true, std::runtime_error,
479 "Ifpack2::RILUK::compute() ERROR: Can't have already computed factors.");
484 scalar_type MinDiagonalValue = Teuchos::ScalarTraits<scalar_type>::rmin();
485 scalar_type MaxDiagonalValue = Teuchos::ScalarTraits<scalar_type>::one()/MinDiagonalValue;
487 size_t NumIn, NumL, NumU;
490 size_t MaxNumEntries = L_->getNodeMaxNumRowEntries() + U_->getNodeMaxNumRowEntries() + 1;
492 Teuchos::Array<local_ordinal_type> InI(MaxNumEntries);
493 Teuchos::Array<scalar_type> InV(MaxNumEntries);
494 size_t num_cols = U_->getColMap()->getNodeNumElements();
495 Teuchos::Array<int> colflag(num_cols);
497 Teuchos::ArrayRCP<scalar_type> DV = D_->get1dViewNonConst();
499 size_t current_madds = 0;
505 Teuchos::ArrayView<const local_ordinal_type> UUI;
506 Teuchos::ArrayView<const scalar_type> UUV;
507 for (
size_t j=0; j<num_cols; j++) colflag[j] = - 1;
509 for(
size_t i=0; i<L_->getNodeNumRows(); i++) {
514 NumIn = MaxNumEntries;
515 L_->getLocalRowCopy(local_row, InI(), InV(), NumL);
518 InI[NumL] = local_row;
520 U_->getLocalRowCopy(local_row, InI(NumL+1,MaxNumEntries-NumL-1), InV(NumL+1,MaxNumEntries-NumL-1), NumU);
524 for (
size_t j=0; j<NumIn; j++) colflag[InI[j]] = j;
528 for (
size_t jj=0; jj<NumL; jj++) {
534 U_->getLocalRowView(j, UUI, UUV);
537 if (RelaxValue_==0.0) {
538 for (
size_t k=0; k<NumUU; k++) {
539 int kk = colflag[UUI[k]];
541 InV[kk] -= multiplier*UUV[k];
547 for (
size_t k=0; k<NumUU; k++) {
548 int kk = colflag[UUI[k]];
549 if (kk>-1) InV[kk] -= multiplier*UUV[k];
550 else diagmod -= multiplier*UUV[k];
556 L_->replaceLocalValues(local_row, InI(0,NumL), InV(0,NumL));
561 if (RelaxValue_!=0.0) {
562 DV[i] += RelaxValue_*diagmod;
566 if (Teuchos::ScalarTraits<scalar_type>::magnitude(DV[i]) > Teuchos::ScalarTraits<scalar_type>::magnitude(MaxDiagonalValue)) {
567 if (Teuchos::ScalarTraits<scalar_type>::real(DV[i]) < 0) DV[i] = - MinDiagonalValue;
568 else DV[i] = MinDiagonalValue;
571 DV[i] = Teuchos::ScalarTraits<scalar_type>::one()/DV[i];
573 for (
size_t j=0; j<NumU; j++) InV[NumL+1+j] *= DV[i];
576 U_->replaceLocalValues(local_row, InI(NumL+1,NumU), InV(NumL+1,NumU));
580 for (
size_t j=0; j<NumIn; j++) colflag[InI[j]] = -1;
583 L_->fillComplete(L_->getColMap(), A_->getRangeMap());
584 U_->fillComplete(A_->getDomainMap(), U_->getRowMap());
588 if( !L_->isLowerTriangular() )
589 throw std::runtime_error(
"Ifpack2::RILUK::compute() ERROR, L isn't lower triangular.");
590 if( !U_->isUpperTriangular() )
591 throw std::runtime_error(
"Ifpack2::RILUK::compute() ERROR, U isn't lower triangular.");
595 double current_flops = 2 * current_madds;
596 double total_flops = 0;
599 Teuchos::reduceAll(*L_->getRowMap()->getComm(),Teuchos::REDUCE_SUM,
600 1,¤t_flops,&total_flops);
603 total_flops += (double) L_->getGlobalNumEntries();
604 total_flops += (double) D_->getGlobalLength();
605 if (RelaxValue_!=0.0) total_flops += 2 * (double)D_->getGlobalLength();
614 template<
class MatrixType>
616 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& X,
617 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
620 typedef Teuchos::ScalarTraits<scalar_type> STS;
622 TEUCHOS_TEST_FOR_EXCEPTION(!
isComputed(), std::runtime_error,
623 "Ifpack2::RILUK::apply() ERROR, compute() hasn't been called yet.");
625 TEUCHOS_TEST_FOR_EXCEPTION(
626 alpha != STS::one (),
628 "Ifpack2::RILUK::apply() does not currently allow alpha != 1.");
629 TEUCHOS_TEST_FOR_EXCEPTION(
630 beta != STS::zero (),
632 "Ifpack2::RILUK::apply() does not currently allow zero != 0.");
642 Teuchos::RCP<const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > X1;
643 Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > Y1;
644 generateXY(mode, X, Y, X1, Y1);
646 scalar_type one = Teuchos::ScalarTraits<scalar_type>::one();
647 scalar_type zero = Teuchos::ScalarTraits<scalar_type>::zero();
649 if (mode == Teuchos::NO_TRANS) {
651 L_->localSolve(*X1, *Y1,mode);
652 Y1->elementWiseMultiply(one, *D_, *Y1, zero);
653 U_->localSolve(*Y1, *Y1,mode);
656 Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);
660 U_->localSolve(*X1, *Y1,mode);
661 Y1->elementWiseMultiply(one, *D_, *Y1, zero);
662 L_->localSolve(*Y1, *Y1,mode);
663 if (isOverlapped_) {Y.doExport(*Y1,*U_->getGraph()->getImporter(), OverlapMode_);}
670 template<
class MatrixType>
672 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Y,
673 Teuchos::ETransp mode)
const {
679 Teuchos::RCP<const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > X1;
680 Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> > Y1;
681 generateXY(mode, X, Y, X1, Y1);
690 if (!mode == Teuchos::NO_TRANS) {
691 U_->apply(*X1, *Y1,mode);
692 Y1->update(1.0, *X1, 1.0);
693 Y1->elementWiseMultiply(1.0, *D_, *Y1, 0.0);
694 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Y1temp(*Y1);
695 L_->apply(Y1temp, *Y1,mode);
696 Y1->update(1.0, Y1temp, 1.0);
697 if (isOverlapped_) {Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);}
701 L_->apply(*X1, *Y1,mode);
702 Y1->update(1.0, *X1, 1.0);
703 Y1->elementWiseMultiply(1, *D_, *Y1, 0);
704 Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Y1temp(*Y1);
705 U_->apply(Y1temp, *Y1,mode);
706 Y1->update(1.0, Y1temp, 1.0);
707 if (isOverlapped_) {Y.doExport(*Y1,*L_->getGraph()->getExporter(), OverlapMode_);}
713 template<
class MatrixType>
714 typename Teuchos::ScalarTraits<typename MatrixType::scalar_type>::magnitudeType
721 Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> Ones(U_->getDomainMap());
722 Tpetra::Vector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> OnesResult(L_->getRangeMap());
725 apply(Ones, OnesResult,mode);
726 OnesResult.abs(OnesResult);
727 Teuchos::Array<magnitude_type> norms(1);
728 OnesResult.normInf(norms());
735 template<
class MatrixType>
737 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Xin,
738 const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>& Yin,
739 Teuchos::RCP<
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& Xout,
740 Teuchos::RCP<Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> >& Yout)
const {
744 TEUCHOS_TEST_FOR_EXCEPTION(Xin.getNumVectors()!=Yin.getNumVectors(), std::runtime_error,
745 "Ifpack2::RILUK::GenerateXY ERROR: X and Y not the same size");
748 Xout = Teuchos::rcp( (
const Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> *) &Xin,
false );
749 Yout = Teuchos::rcp( (Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type> *) &Yin,
false );
750 if (!isOverlapped_)
return;
754 if (OverlapX_!=Teuchos::null) {
755 if (OverlapX_->getNumVectors()!=Xin.getNumVectors()) {
756 OverlapX_ = Teuchos::null;
757 OverlapY_ = Teuchos::null;
760 if (OverlapX_==Teuchos::null) {
761 OverlapX_ = Teuchos::rcp(
new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(U_->getColMap(), Xout->getNumVectors()) );
762 OverlapY_ = Teuchos::rcp(
new Tpetra::MultiVector<scalar_type,local_ordinal_type,global_ordinal_type,node_type>(L_->getRowMap(), Yout->getNumVectors()) );
764 if (mode == Teuchos::NO_TRANS) {
765 OverlapX_->doImport(*Xout,*U_->getGraph()->getImporter(), Tpetra::INSERT);
768 OverlapX_->doImport(*Xout,*L_->getGraph()->getExporter(), Tpetra::INSERT);
magnitude_type computeCondEst(Teuchos::ETransp mode) const
Returns the maximum over all the condition number estimate for each local ILU set of factors...
Definition: Ifpack2_RILUK_def.hpp:715
MatrixType::global_ordinal_type global_ordinal_type
The type of global indices in the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:232
void initialize()
Initialize by computing the symbolic incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:325
void setParameters(const Teuchos::ParameterList ¶ms)
Definition: Ifpack2_RILUK_def.hpp:125
ILU(k) (incomplete LU with fill level k) factorization of a given Tpetra::RowMatrix.
Definition: Ifpack2_RILUK_decl.hpp:214
bool isComputed() const
Whether compute() has been called.
Definition: Ifpack2_RILUK_decl.hpp:312
int Multiply(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 incomplete factorization (as a product) to X, resulting in Y.
Definition: Ifpack2_RILUK_def.hpp:671
MatrixType::local_ordinal_type local_ordinal_type
The type of local indices in the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:225
virtual ~RILUK()
Ifpack2_RILUK Destructor.
Definition: Ifpack2_RILUK_def.hpp:97
void compute()
Compute the (numeric) incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:471
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 (inverse of the) incomplete factorization to X, resulting in Y.
Definition: Ifpack2_RILUK_def.hpp:615
Teuchos::ScalarTraits< scalar_type >::magnitudeType magnitude_type
The type of the magnitude (absolute value) of a matrix entry.
Definition: Ifpack2_RILUK_decl.hpp:246
A class for constructing level filled graphs for use with ILU(k) class preconditioners.
Definition: Ifpack2_IlukGraph.hpp:83
bool isInitialized() const
Whether initialize() has been called.
Definition: Ifpack2_RILUK_decl.hpp:296
Ifpack2::Container: a pure virtual class for creating and solving local linear problems.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:67
RILUK(const Teuchos::RCP< const MatrixType > &A_in)
RILUK constuctor with variable number of indices per row.
Definition: Ifpack2_RILUK_def.hpp:50
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_RILUK_decl.hpp:218