43 #include "Ifpack_ConfigDefs.h" 44 #include "Ifpack_Preconditioner.h" 45 #include "Ifpack_ILUT.h" 46 #include "Ifpack_Condest.h" 48 #include "Ifpack_HashTable.h" 49 #include "Epetra_SerialComm.h" 50 #include "Epetra_Comm.h" 51 #include "Epetra_Map.h" 52 #include "Epetra_RowMatrix.h" 53 #include "Epetra_CrsMatrix.h" 54 #include "Epetra_Vector.h" 55 #include "Epetra_MultiVector.h" 56 #include "Epetra_Util.h" 57 #include "Teuchos_ParameterList.hpp" 58 #include "Teuchos_RefCountPtr.hpp" 73 DropTolerance_(1e-12),
74 IsInitialized_(false),
83 ApplyInverseTime_(0.0),
85 ApplyInverseFlops_(0.0),
99 void Ifpack_ILUT::Destroy()
101 IsInitialized_ =
false;
110 LevelOfFill_ = List.get<
double>(
"fact: ilut level-of-fill", LevelOfFill());
111 if (LevelOfFill_ <= 0.0)
114 Athresh_ = List.get<
double>(
"fact: absolute threshold", Athresh_);
115 Rthresh_ = List.get<
double>(
"fact: relative threshold", Rthresh_);
116 Relax_ = List.get<
double>(
"fact: relax value", Relax_);
117 DropTolerance_ = List.get<
double>(
"fact: drop tolerance", DropTolerance_);
129 cerr <<
"Caught an exception while parsing the parameter list" << endl;
130 cerr <<
"This typically means that a parameter was set with the" << endl;
131 cerr <<
"wrong type (for example, int instead of double). " << endl;
132 cerr <<
"please check the documentation for the type required by each parameer." << endl;
145 Time_.ResetStartTime();
151 NumMyRows_ =
Matrix().NumMyRows();
154 IsInitialized_ =
true;
156 InitializeTime_ += Time_.ElapsedTime();
165 inline bool operator()(
const double& x,
const double& y)
167 return(IFPACK_ABS(x) > IFPACK_ABS(y));
180 Time_.ResetStartTime();
183 NumMyRows_ = A_.NumMyRows();
184 int Length = A_.MaxNumEntries();
185 vector<int> RowIndicesL(Length);
186 vector<double> RowValuesL(Length);
187 vector<int> RowIndicesU(Length);
188 vector<double> RowValuesU(Length);
189 bool distributed = (
Comm().NumProc() > 1)?
true:
false;
193 SerialComm_ = rcp(
new Epetra_SerialComm);
194 SerialMap_ = rcp(
new Epetra_Map(NumMyRows_, 0, *SerialComm_));
195 assert (SerialComm_.get() != 0);
196 assert (SerialMap_.get() != 0);
199 SerialMap_ = rcp(const_cast<Epetra_Map*>(&A_.RowMatrixRowMap()),
false);
203 L_ = rcp(
new Epetra_CrsMatrix(Copy, *SerialMap_, 0));
204 U_ = rcp(
new Epetra_CrsMatrix(Copy, *SerialMap_, 0));
206 if ((L_.get() == 0) || (U_.get() == 0))
210 IFPACK_CHK_ERR(A_.ExtractMyRowCopy(0,Length,RowNnzU,
211 &RowValuesU[0],&RowIndicesU[0]));
216 for (
int i = 0 ;i < RowNnzU ; ++i)
218 if (RowIndicesU[i] < NumMyRows_){
219 RowIndicesU[count] = RowIndicesU[i];
220 RowValuesU[count] = RowValuesU[i];
230 for (
int i = 0 ;i < RowNnzU ; ++i) {
231 if (RowIndicesU[i] == 0) {
232 double& v = RowValuesU[i];
238 IFPACK_CHK_ERR(U_->InsertGlobalValues(0,RowNnzU,&(RowValuesU[0]),
243 IFPACK_CHK_ERR(L_->InsertGlobalValues(0,1,&(RowValuesU[0]),
246 int max_keys = (int) 10 * A_.MaxNumEntries() * LevelOfFill() ;
250 int hash_size = SingleRowU.getRecommendedHashSize(max_keys) ;
251 vector<int> keys; keys.reserve(hash_size * 10);
252 vector<double> values; values.reserve(hash_size * 10);
253 vector<double> AbsRow; AbsRow.reserve(hash_size * 10);
259 #ifdef IFPACK_FLOPCOUNTERS 260 double this_proc_flops = 0.0;
263 for (
int row_i = 1 ; row_i < NumMyRows_ ; ++row_i)
266 IFPACK_CHK_ERR(A_.ExtractMyRowCopy(row_i,Length,RowNnzU,
267 &RowValuesU[0],&RowIndicesU[0]));
272 for (
int i = 0 ;i < RowNnzU ; ++i)
274 if (RowIndicesU[i] < NumMyRows_){
275 RowIndicesU[count] = RowIndicesU[i];
276 RowValuesU[count] = RowValuesU[i];
288 for (
int i = 0 ;i < RowNnzU ; ++i) {
289 if (RowIndicesU[i] < row_i)
291 else if (RowIndicesU[i] == row_i) {
294 double& v = RowValuesU[i];
301 int FillL = (int)(LevelOfFill() * NnzLower);
302 int FillU = (int)(LevelOfFill() * NnzUpper);
303 if (FillL == 0) FillL = 1;
304 if (FillU == 0) FillU = 1;
309 for (
int i = 0 ; i < RowNnzU ; ++i) {
310 SingleRowU.
set(RowIndicesU[i], RowValuesU[i]);
316 int start_col = NumMyRows_;
317 for (
int i = 0 ; i < RowNnzU ; ++i)
318 start_col = EPETRA_MIN(start_col, RowIndicesU[i]);
320 #ifdef IFPACK_FLOPCOUNTERS 324 for (
int col_k = start_col ; col_k < row_i ; ++col_k) {
330 double xxx = SingleRowU.
get(col_k);
334 IFPACK_CHK_ERR(U_->ExtractGlobalRowView(col_k, ColNnzK, ColValuesK,
338 double DiagonalValueK = 0.0;
339 for (
int i = 0 ; i < ColNnzK ; ++i) {
340 if (ColIndicesK[i] == col_k) {
341 DiagonalValueK = ColValuesK[i];
347 if (DiagonalValueK == 0.0)
350 double yyy = xxx / DiagonalValueK ;
351 SingleRowL.
set(col_k, yyy);
352 #ifdef IFPACK_FLOPCOUNTERS 356 for (
int j = 0 ; yyy != 0.0 && j < ColNnzK ; ++j)
358 int col_j = ColIndicesK[j];
360 if (col_j < col_k)
continue;
362 SingleRowU.
set(col_j, -yyy * ColValuesK[j],
true);
363 #ifdef IFPACK_FLOPCOUNTERS 370 #ifdef IFPACK_FLOPCOUNTERS 371 this_proc_flops += 1.0 * flops;
375 double DiscardedElements = 0.0;
382 values.resize(sizeL);
384 AbsRow.resize(sizeL);
387 keys.size() ? &keys[0] : 0,
388 values.size() ? &values[0] : 0
390 for (
int i = 0; i < sizeL; ++i)
392 AbsRow[count++] = IFPACK_ABS(values[i]);
396 nth_element(AbsRow.begin(), AbsRow.begin() + FillL, AbsRow.begin() + count,
398 cutoff = AbsRow[FillL];
401 for (
int i = 0; i < sizeL; ++i) {
402 if (IFPACK_ABS(values[i]) >= cutoff) {
403 IFPACK_CHK_ERR(L_->InsertGlobalValues(row_i,1, &values[i], (
int*)&keys[i]));
406 DiscardedElements += values[i];
412 IFPACK_CHK_ERR(L_->InsertGlobalValues(row_i,1, &dtmp, &row_i));
417 AbsRow.resize(sizeU + 1);
418 keys.resize(sizeU + 1);
419 values.resize(sizeU + 1);
421 SingleRowU.
arrayify(&keys[0], &values[0]);
423 for (
int i = 0; i < sizeU; ++i)
424 if (keys[i] >= row_i && IFPACK_ABS(values[i]) >
DropTolerance())
426 AbsRow[count++] = IFPACK_ABS(values[i]);
430 nth_element(AbsRow.begin(), AbsRow.begin() + FillU, AbsRow.begin() + count,
432 cutoff = AbsRow[FillU];
436 for (
int i = 0; i < sizeU; ++i)
439 double val = values[i];
442 if (IFPACK_ABS(val) >= cutoff || row_i == col) {
443 IFPACK_CHK_ERR(U_->InsertGlobalValues(row_i,1, &val, &col));
446 DiscardedElements += val;
453 IFPACK_CHK_ERR(U_->InsertGlobalValues(row_i,1, &DiscardedElements,
458 #ifdef IFPACK_FLOPCOUNTERS 460 Comm().SumAll(&this_proc_flops, &tf, 1);
464 IFPACK_CHK_ERR(L_->FillComplete());
465 IFPACK_CHK_ERR(U_->FillComplete());
469 Epetra_Vector LHS(A_.RowMatrixRowMap());
470 Epetra_Vector RHS1(A_.RowMatrixRowMap());
471 Epetra_Vector RHS2(A_.RowMatrixRowMap());
472 Epetra_Vector RHS3(A_.RowMatrixRowMap());
475 cout <<
"A = " << A_.NumGlobalNonzeros() << endl;
476 cout <<
"L = " << L_->NumGlobalNonzeros() << endl;
477 cout <<
"U = " << U_->NumGlobalNonzeros() << endl;
479 A_.Multiply(
false,LHS,RHS1);
480 U_->Multiply(
false,LHS,RHS2);
481 L_->Multiply(
false,RHS2,RHS3);
483 RHS1.Update(-1.0, RHS3, 1.0);
488 int MyNonzeros = L_->NumGlobalNonzeros() + U_->NumGlobalNonzeros();
489 Comm().SumAll(&MyNonzeros, &GlobalNonzeros_, 1);
494 ComputeTime_ += Time_.ElapsedTime();
502 Epetra_MultiVector& Y)
const 507 if (X.NumVectors() != Y.NumVectors())
510 Time_.ResetStartTime();
518 Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
519 if (X.Pointers()[0] == Y.Pointers()[0])
520 Xcopy = Teuchos::rcp(
new Epetra_MultiVector(X) );
522 Xcopy = Teuchos::rcp( &X,
false );
527 IFPACK_CHK_ERR(L_->Solve(
false,
false,
false,*Xcopy,Y));
528 IFPACK_CHK_ERR(U_->Solve(
true,
false,
false,Y,Y));
533 IFPACK_CHK_ERR(U_->Solve(
true,
true,
false,*Xcopy,Y));
534 IFPACK_CHK_ERR(L_->Solve(
false,
true,
false,Y,Y));
538 #ifdef IFPACK_FLOPCOUNTERS 539 ApplyInverseFlops_ += X.NumVectors() * 2 * GlobalNonzeros_;
541 ApplyInverseTime_ += Time_.ElapsedTime();
548 int Ifpack_ILUT::Apply(
const Epetra_MultiVector& X,
549 Epetra_MultiVector& Y)
const 556 const int MaxIters,
const double Tol,
557 Epetra_RowMatrix* Matrix_in)
563 if (Condest_ == -1.0)
564 Condest_ = Ifpack_Condest(*
this, CT, MaxIters, Tol, Matrix_in);
573 if (!
Comm().MyPID()) {
575 os <<
"================================================================================" << endl;
576 os <<
"Ifpack_ILUT: " <<
Label() << endl << endl;
577 os <<
"Level-of-fill = " << LevelOfFill() << endl;
580 os <<
"Relax value = " <<
RelaxValue() << endl;
581 os <<
"Condition number estimate = " <<
Condest() << endl;
582 os <<
"Global number of rows = " << A_.NumGlobalRows() << endl;
584 os <<
"Number of nonzeros in A = " << A_.NumGlobalNonzeros() << endl;
587 <<
" % of A)" << endl;
588 os <<
"nonzeros / rows = " 592 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
593 os <<
"----- ------- -------------- ------------ --------" << endl;
596 <<
" 0.0 0.0" << endl;
597 os <<
"Compute() " << std::setw(5) <<
NumCompute()
603 os <<
" " << std::setw(15) << 0.0 << endl;
610 os <<
" " << std::setw(15) << 0.0 << endl;
611 os <<
"================================================================================" << endl;
virtual ostream & Print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator<<.
const Epetra_RowMatrix & Matrix() const
Returns a reference to the matrix to be preconditioned.
double Condest() const
Returns the computed estimated condition number, or -1.0 if no computed.
bool IsComputed() const
If factor is completed, this query returns true, otherwise it returns false.
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
Ifpack_ILUT(const Epetra_RowMatrix *A)
Ifpack_ILUT constuctor with variable number of indices per row.
int SetParameters(Teuchos::ParameterList ¶meterlis)
Set parameters using a Teuchos::ParameterList object.
virtual int NumInitialize() const
Returns the number of calls to Initialize().
string Ifpack_toString(const int &x)
Convertes an integer to string.
int Initialize()
Initialize L and U with values from user matrix A.
virtual ~Ifpack_ILUT()
Ifpack_ILUT Destructor.
double RelativeThreshold() const
Get relative threshold value.
void arrayify(int *key_array, double *val_array)
Converts the contents in array format for both keys and values.
const Epetra_Comm & Comm() const
Returns the Epetra_BlockMap object associated with the range of this matrix operator.
virtual int NumCompute() const
Returns the number of calls to Compute().
int Compute()
Compute IC factor U using the specified graph, diagonal perturbation thresholds and relaxation parame...
void set(const int key, const double value, const bool addToValue=false)
Sets an element in the hash table.
virtual double ComputeFlops() const
Returns the number of flops in the computation phase.
virtual double ApplyInverseTime() const
Returns the time spent in ApplyInverse().
int getNumEntries() const
Returns the number of stored entries.
bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized.
virtual double ApplyInverseFlops() const
Returns the number of flops in the application of the preconditioner.
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Ifpack_ILUT forward/back solve on a Epetra_MultiVector X in Y...
double DropTolerance() const
Gets the dropping tolerance.
virtual double ComputeTime() const
Returns the time spent in Compute().
double AbsoluteThreshold() const
Get absolute threshold value.
double get(const int key)
Returns an element from the hash table, or 0.0 if not found.
int NumGlobalNonzeros() const
Returns the number of nonzero entries in the global graph.
const char * Label() const
Returns the label of this object.
virtual double InitializeTime() const
Returns the time spent in Initialize().
void reset()
Resets the entries of the already allocated memory. This method can be used to clean an array...
double RelaxValue() const
Set relative threshold value.