43 #include "Ifpack_ConfigDefs.h"
44 #include "Ifpack_CondestType.h"
45 #include "Ifpack_ILU.h"
46 #include "Epetra_ConfigDefs.h"
47 #include "Epetra_Comm.h"
48 #include "Epetra_Map.h"
49 #include "Epetra_RowMatrix.h"
50 #include "Epetra_Vector.h"
51 #include "Epetra_MultiVector.h"
52 #include "Epetra_CrsGraph.h"
53 #include "Epetra_CrsMatrix.h"
54 #include "Teuchos_ParameterList.hpp"
55 #include "Teuchos_RefCountPtr.hpp"
57 using Teuchos::RefCountPtr;
60 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
61 # include "Teuchos_TimeMonitor.hpp"
66 A_(rcp(Matrix_in,false)),
67 Comm_(Matrix_in->Comm()),
75 IsInitialized_(false),
82 ApplyInverseTime_(0.0),
84 ApplyInverseFlops_(0.0),
87 Teuchos::ParameterList List;
92 void Ifpack_ILU::Destroy()
102 RelaxValue_ = List.get(
"fact: relax value", RelaxValue_);
103 Athresh_ = List.get(
"fact: absolute threshold", Athresh_);
104 Rthresh_ = List.get(
"fact: relative threshold", Rthresh_);
105 LevelOfFill_ = List.get(
"fact: level-of-fill", LevelOfFill_);
108 sprintf(Label_,
"IFPACK ILU (fill=%d, relax=%f, athr=%f, rthr=%f)",
109 LevelOfFill(), RelaxValue(), AbsoluteThreshold(),
110 RelativeThreshold());
115 int Ifpack_ILU::ComputeSetup()
118 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
119 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::ComputeSetup");
122 L_ = rcp(
new Epetra_CrsMatrix(Copy, Graph().L_Graph()));
123 U_ = rcp(
new Epetra_CrsMatrix(Copy, Graph().U_Graph()));
124 D_ = rcp(
new Epetra_Vector(Graph().L_Graph().RowMap()));
125 if ((L_.get() == 0) || (U_.get() == 0) || (D_.get() == 0))
129 int MaxNumEntries =
Matrix().MaxNumEntries();
132 U_DomainMap_ = &(
Matrix().OperatorDomainMap());
133 L_RangeMap_ = &(
Matrix().OperatorRangeMap());
138 int NumIn, NumL, NumU;
140 int NumNonzeroDiags = 0;
142 vector<int> InI(MaxNumEntries);
143 vector<int> LI(MaxNumEntries);
144 vector<int> UI(MaxNumEntries);
145 vector<double> InV(MaxNumEntries);
146 vector<double> LV(MaxNumEntries);
147 vector<double> UV(MaxNumEntries);
149 bool ReplaceValues = (L_->StaticGraph() || L_->IndicesAreLocal());
158 IFPACK_CHK_ERR(D_->ExtractView(&DV));
162 for (i=0; i< NumMyRows(); i++) {
164 IFPACK_CHK_ERR(
Matrix().ExtractMyRowCopy(i, MaxNumEntries, NumIn, &InV[0], &InI[0]));
172 for (j=0; j< NumIn; j++) {
177 DV[i] += Rthresh_ * InV[j] + EPETRA_SGN(InV[j]) * Athresh_;
180 else if (k < 0) {IFPACK_CHK_ERR(-4);}
187 else if (k<NumMyRows()) {
196 if (DiagFound) NumNonzeroDiags++;
197 else DV[i] = Athresh_;
201 (L_->ReplaceMyValues(i, NumL, &LV[0], &LI[0]));
204 IFPACK_CHK_ERR(L_->InsertMyValues(i, NumL, &LV[0], &LI[0]));
210 (U_->ReplaceMyValues(i, NumU, &UV[0], &UI[0]));
213 IFPACK_CHK_ERR(U_->InsertMyValues(i, NumU, &UV[0], &UI[0]));
219 if (!ReplaceValues) {
224 IFPACK_CHK_ERR(L_->FillComplete((L_->RowMatrixColMap()), *L_RangeMap_));
225 IFPACK_CHK_ERR(U_->FillComplete(*U_DomainMap_, U_->RowMatrixRowMap()));
229 int TotalNonzeroDiags = 0;
230 IFPACK_CHK_ERR(Graph().L_Graph().RowMap().
Comm().SumAll(&NumNonzeroDiags, &TotalNonzeroDiags, 1));
231 NumMyDiagonals_ = NumNonzeroDiags;
232 if (NumNonzeroDiags != NumMyRows()) ierr = 1;
234 IFPACK_CHK_ERR(ierr);
242 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
243 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::Initialize");
246 Time_.ResetStartTime();
247 IsInitialized_ =
false;
252 Epetra_CrsMatrix* CrsMatrix;
253 CrsMatrix =
dynamic_cast<Epetra_CrsMatrix*
>(&*A_);
254 if (CrsMatrix == 0) {
259 int size = A_->MaxNumEntries();
260 CrsGraph_ = rcp(
new Epetra_CrsGraph(Copy,A_->RowMatrixRowMap(), size));
261 if (CrsGraph_.get() == 0)
264 vector<int> Indices(size);
265 vector<double> Values(size);
269 for (
int i = 0 ; i < A_->NumMyRows() ; ++i) {
271 int GlobalRow = A_->RowMatrixRowMap().GID(i);
272 IFPACK_CHK_ERR(A_->ExtractMyRowCopy(i, size, NumEntries,
273 &Values[0], &Indices[0]));
275 for (
int j = 0 ; j < NumEntries ; ++j) {
276 Indices[j] = A_->RowMatrixColMap().GID(Indices[j]);
278 IFPACK_CHK_ERR(CrsGraph_->InsertGlobalIndices(GlobalRow,NumEntries,
282 IFPACK_CHK_ERR(CrsGraph_->FillComplete(A_->RowMatrixRowMap(),
283 A_->RowMatrixRowMap()));
295 if (Graph_.get() == 0)
297 IFPACK_CHK_ERR(Graph_->ConstructFilledGraph());
299 IsInitialized_ =
true;
301 InitializeTime_ += Time_.ElapsedTime();
310 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
311 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::Compute");
317 Time_.ResetStartTime();
321 IFPACK_CHK_ERR(ComputeSetup());
326 double MinDiagonalValue = Epetra_MinDouble;
327 double MaxDiagonalValue = 1.0/MinDiagonalValue;
333 int NumIn, NumL, NumU;
336 int MaxNumEntries = L_->MaxNumEntries() + U_->MaxNumEntries() + 1;
338 vector<int> InI(MaxNumEntries+1);
339 vector<double> InV(MaxNumEntries+1);
340 vector<int> colflag(NumMyCols());
343 ierr = D_->ExtractView(&DV);
345 #ifdef IFPACK_FLOPCOUNTERS
346 int current_madds = 0;
357 for (j = 0; j < NumMyCols(); ++j) colflag[j] = - 1;
359 for (i = 0; i < NumMyRows(); ++i) {
363 NumIn = MaxNumEntries;
364 IFPACK_CHK_ERR(L_->ExtractMyRowCopy(i, NumIn, NumL, &InV[0], &InI[0]));
371 IFPACK_CHK_ERR(U_->ExtractMyRowCopy(i, NumIn-NumL-1, NumU, &InV[NumL+1], &InI[NumL+1]));
377 for (j=0; j<NumIn; j++) colflag[InI[j]] = j;
379 double diagmod = 0.0;
381 for (
int jj=0; jj<NumL; jj++) {
383 double multiplier = InV[jj];
387 IFPACK_CHK_ERR(U_->ExtractMyRowView(j, NumUU, UUV, UUI));
389 if (RelaxValue_==0.0) {
390 for (k=0; k<NumUU; k++) {
391 int kk = colflag[UUI[k]];
393 InV[kk] -= multiplier*UUV[k];
394 #ifdef IFPACK_FLOPCOUNTERS
401 for (k=0; k<NumUU; k++) {
402 int kk = colflag[UUI[k]];
403 if (kk>-1) InV[kk] -= multiplier*UUV[k];
404 else diagmod -= multiplier*UUV[k];
405 #ifdef IFPACK_FLOPCOUNTERS
412 IFPACK_CHK_ERR(L_->ReplaceMyValues(i, NumL, LV, LI));
417 if (RelaxValue_!=0.0) {
418 DV[i] += RelaxValue_*diagmod;
422 if (fabs(DV[i]) > MaxDiagonalValue) {
423 if (DV[i] < 0) DV[i] = - MinDiagonalValue;
424 else DV[i] = MinDiagonalValue;
429 for (j=0; j<NumU; j++) UV[j] *= DV[i];
432 IFPACK_CHK_ERR(U_->ReplaceMyValues(i, NumU, UV, UI));
436 for (j=0; j<NumIn; j++) colflag[InI[j]] = -1;
441 if (!L_->LowerTriangular())
443 if (!U_->UpperTriangular())
446 #ifdef IFPACK_FLOPCOUNTERS
449 double current_flops = 2 * current_madds;
450 double total_flops = 0;
452 IFPACK_CHK_ERR(Graph().L_Graph().RowMap().
Comm().SumAll(¤t_flops, &total_flops, 1));
455 total_flops += (double) L_->NumGlobalNonzeros();
456 total_flops += (double) D_->GlobalLength();
457 if (RelaxValue_!=0.0) total_flops += 2 * (double)D_->GlobalLength();
460 ComputeFlops_ += total_flops;
465 ComputeTime_ += Time_.ElapsedTime();
473 int Ifpack_ILU::Solve(
bool Trans,
const Epetra_MultiVector& X,
474 Epetra_MultiVector& Y)
const
477 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
478 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::ApplyInverse - Solve");
484 bool UnitDiagonal =
true;
488 IFPACK_CHK_ERR(L_->Solve(Lower, Trans, UnitDiagonal, X, Y));
490 IFPACK_CHK_ERR(Y.Multiply(1.0, *D_, Y, 0.0));
492 IFPACK_CHK_ERR(U_->Solve(Upper, Trans, UnitDiagonal, Y, Y));
496 IFPACK_CHK_ERR(U_->Solve(Upper, Trans, UnitDiagonal, X, Y));
498 IFPACK_CHK_ERR(Y.Multiply(1.0, *D_, Y, 0.0));
499 IFPACK_CHK_ERR(L_->Solve(Lower, Trans, UnitDiagonal, Y, Y));
508 int Ifpack_ILU::Multiply(
bool Trans,
const Epetra_MultiVector& X,
509 Epetra_MultiVector& Y)
const
512 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
513 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::Multiply");
520 IFPACK_CHK_ERR(U_->Multiply(Trans, X, Y));
522 IFPACK_CHK_ERR(Y.Update(1.0, X, 1.0));
524 IFPACK_CHK_ERR(Y.ReciprocalMultiply(1.0, *D_, Y, 0.0));
525 Epetra_MultiVector Y1temp(Y);
526 IFPACK_CHK_ERR(L_->Multiply(Trans, Y1temp, Y));
528 IFPACK_CHK_ERR(Y.Update(1.0, Y1temp, 1.0));
532 IFPACK_CHK_ERR(L_->Multiply(Trans, X, Y));
534 IFPACK_CHK_ERR(Y.Update(1.0, X, 1.0));
536 IFPACK_CHK_ERR(Y.ReciprocalMultiply(1.0, *D_, Y, 0.0));
537 Epetra_MultiVector Y1temp(Y);
538 IFPACK_CHK_ERR(U_->Multiply(Trans, Y1temp, Y));
540 IFPACK_CHK_ERR(Y.Update(1.0, Y1temp, 1.0));
549 Epetra_MultiVector& Y)
const
552 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
553 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::ApplyInverse");
559 if (X.NumVectors() != Y.NumVectors())
562 Time_.ResetStartTime();
566 Teuchos::RefCountPtr< const Epetra_MultiVector > Xcopy;
567 if (X.Pointers()[0] == Y.Pointers()[0])
568 Xcopy = Teuchos::rcp(
new Epetra_MultiVector(X) );
570 Xcopy = Teuchos::rcp( &X,
false );
575 #ifdef IFPACK_FLOPCOUNTERS
576 ApplyInverseFlops_ += X.NumVectors() * 4 *
577 (L_->NumGlobalNonzeros() + U_->NumGlobalNonzeros());
581 ApplyInverseTime_ += Time_.ElapsedTime();
589 const int MaxIters,
const double Tol,
590 Epetra_RowMatrix* Matrix_in)
593 #ifdef IFPACK_TEUCHOS_TIME_MONITOR
594 TEUCHOS_FUNC_TIME_MONITOR(
"Ifpack_ILU::Condest");
600 Condest_ = Ifpack_Condest(*
this, CT, MaxIters, Tol, Matrix_in);
609 if (!
Comm().MyPID()) {
611 os <<
"================================================================================" << endl;
612 os <<
"Ifpack_ILU: " <<
Label() << endl << endl;
613 os <<
"Level-of-fill = " << LevelOfFill() << endl;
614 os <<
"Absolute threshold = " << AbsoluteThreshold() << endl;
615 os <<
"Relative threshold = " << RelativeThreshold() << endl;
616 os <<
"Relax value = " << RelaxValue() << endl;
617 os <<
"Condition number estimate = " <<
Condest() << endl;
618 os <<
"Global number of rows = " << A_->NumGlobalRows() << endl;
620 os <<
"Number of rows of L, D, U = " << L_->NumGlobalRows() << endl;
621 os <<
"Number of nonzeros of L + U = " << NumGlobalNonzeros() << endl;
622 os <<
"nonzeros / rows = "
623 << 1.0 * NumGlobalNonzeros() / U_->NumGlobalRows() << endl;
626 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
627 os <<
"----- ------- -------------- ------------ --------" << endl;
630 <<
" 0.0 0.0" << endl;
631 os <<
"Compute() " << std::setw(5) <<
NumCompute()
637 os <<
" " << std::setw(15) << 0.0 << endl;
644 os <<
" " << std::setw(15) << 0.0 << endl;
645 os <<
"================================================================================" << endl;
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_Operator inverse applied to an Epetra_MultiVector X in Y...
virtual int NumCompute() const
Returns the number of calls to Compute().
virtual ostream & Print(ostream &os) const
Prints on stream basic information about this object.
virtual double ComputeFlops() const
Returns the number of flops in the computation phase.
bool UseTranspose() const
Returns the current UseTranspose setting.
Ifpack_IlukGraph: A class for constructing level filled graphs for use with ILU(k) class precondition...
const Epetra_RowMatrix & Matrix() const
Returns a reference to the matrix to be preconditioned.
const Epetra_Comm & Comm() const
Returns the Epetra_BlockMap object associated with the range of this matrix operator.
virtual double ComputeTime() const
Returns the time spent in Compute().
bool IsComputed() const
If factor is completed, this query returns true, otherwise it returns false.
int Compute()
Compute ILU factors L and U using the specified graph, diagonal perturbation thresholds and relaxatio...
virtual double ApplyInverseTime() const
Returns the time spent in ApplyInverse().
int SetParameters(Teuchos::ParameterList ¶meterlist)
Set parameters using a Teuchos::ParameterList object.
int Initialize()
Initialize the preconditioner, does not touch matrix values.
virtual double InitializeTime() const
Returns the time spent in Initialize().
virtual double ApplyInverseFlops() const
Returns the number of flops in the application of the preconditioner.
bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized.
double Condest() const
Returns the computed estimated condition number, or -1.0 if not computed.
Ifpack_ILU(Epetra_RowMatrix *A)
Constructor.
const char * Label() const
Returns a character string describing the operator.
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
virtual int NumInitialize() const
Returns the number of calls to Initialize().