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>
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) {
168 catch (InvalidParameterType&) {
174 if (! gotFillLevel) {
178 fillLevel = as<int> (params.
get<
double> (
"fact: iluk level-of-fill"));
181 catch (InvalidParameterType& e) {
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) {
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.");
267 catch (InvalidParameterType&) {
270 absThresh = as<magnitude_type> (params.
get<
double> (
"fact: absolute threshold"));
272 catch (InvalidParameterName&) {
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();
367 bool ReplaceValues = (L_->isStaticGraph() || L_->isLocallyIndexed());
372 L_->setAllToScalar(0.0);
373 U_->setAllToScalar(0.0);
384 for (global_ordinal_type i=rowMap->getMinGlobalIndex(); i<=rowMap->getMaxGlobalIndex(); ++i) {
385 global_ordinal_type global_row = i;
386 local_ordinal_type local_row = rowMap->getLocalElement(global_row);
388 OverlapA.getGlobalRowCopy(global_row, InI(), InV(), NumIn);
396 for (
size_t j=0; j< NumIn; j++) {
397 global_ordinal_type k = InI[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;
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>
477 "Ifpack2::RILUK::compute() ERROR: isInitialized() must be true.");
479 "Ifpack2::RILUK::compute() ERROR: Can't have already computed factors.");
487 size_t NumIn, NumL, NumU;
490 size_t MaxNumEntries = L_->getNodeMaxNumRowEntries() + U_->getNodeMaxNumRowEntries() + 1;
494 size_t num_cols = U_->getColMap()->getNodeNumElements();
499 size_t current_madds = 0;
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;
568 else DV[i] = MinDiagonalValue;
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;
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,
623 "Ifpack2::RILUK::apply() ERROR, compute() hasn't been called yet.");
626 alpha != STS::one (),
628 "Ifpack2::RILUK::apply() does not currently allow alpha != 1.");
630 beta != STS::zero (),
632 "Ifpack2::RILUK::apply() does not currently allow zero != 0.");
644 generateXY(mode, X, Y, X1, Y1);
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,
681 generateXY(mode, X, Y, X1, Y1);
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>
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);
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 {
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()) );
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
T & get(const std::string &name, T def_value)
void initialize()
Initialize by computing the symbolic incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:325
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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
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
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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
static magnitudeType rmin()
void compute()
Compute the (numeric) incomplete factorization.
Definition: Ifpack2_RILUK_def.hpp:471
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
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
TypeTo as(const TypeFrom &t)
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