43 #include "Ifpack_ConfigDefs.h" 44 #include "Ifpack_Preconditioner.h" 45 #include "Ifpack_ICT.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" 72 IsInitialized_(false),
81 ApplyInverseTime_(0.0),
83 ApplyInverseFlops_(0.0),
97 void Ifpack_ICT::Destroy()
99 IsInitialized_ =
false;
109 LevelOfFill_ = List.get(
"fact: ict level-of-fill",LevelOfFill_);
110 Athresh_ = List.get(
"fact: absolute threshold", Athresh_);
111 Rthresh_ = List.get(
"fact: relative threshold", Rthresh_);
112 Relax_ = List.get(
"fact: relax value", Relax_);
113 DropTolerance_ = List.get(
"fact: drop tolerance", DropTolerance_);
127 cerr <<
"Caught an exception while parsing the parameter list" << endl;
128 cerr <<
"This typically means that a parameter was set with the" << endl;
129 cerr <<
"wrong type (for example, int instead of double). " << endl;
130 cerr <<
"please check the documentation for the type required by each parameer." << endl;
141 Time_.ResetStartTime();
147 NumMyRows_ =
Matrix().NumMyRows();
150 IsInitialized_ =
true;
152 InitializeTime_ += Time_.ElapsedTime();
163 Time_.ResetStartTime();
166 NumMyRows_ = A_.NumMyRows();
167 int Length = A_.MaxNumEntries();
168 vector<int> RowIndices(Length);
169 vector<double> RowValues(Length);
171 bool distributed = (
Comm().NumProc() > 1)?
true:
false;
175 SerialComm_ = Teuchos::rcp(
new Epetra_SerialComm);
176 SerialMap_ = Teuchos::rcp(
new Epetra_Map(NumMyRows_, 0, *SerialComm_));
177 assert (SerialComm_.get() != 0);
178 assert (SerialMap_.get() != 0);
181 SerialMap_ = Teuchos::rcp(const_cast<Epetra_Map*>(&A_.RowMatrixRowMap()),
false);
184 #ifdef IFPACK_FLOPCOUNTERS 188 H_ = Teuchos::rcp(
new Epetra_CrsMatrix(Copy,*SerialMap_,0));
193 IFPACK_CHK_ERR(A_.ExtractMyRowCopy(0,Length,RowNnz,
194 &RowValues[0],&RowIndices[0]));
200 for (
int i = 0 ;i < RowNnz ; ++i)
202 if (RowIndices[i] < NumMyRows_){
203 RowIndices[count] = RowIndices[i];
204 RowValues[count] = RowValues[i];
214 double diag_val = 0.0;
215 for (
int i = 0 ;i < RowNnz ; ++i) {
216 if (RowIndices[i] == 0) {
217 double& v = RowValues[i];
224 diag_val = sqrt(diag_val);
226 EPETRA_CHK_ERR(H_->InsertGlobalValues(0,1,&diag_val, &diag_idx));
234 for (
int row_i = 1 ; row_i < NumMyRows_ ; ++row_i) {
237 IFPACK_CHK_ERR(A_.ExtractMyRowCopy(row_i,Length,RowNnz,
238 &RowValues[0],&RowIndices[0]));
244 for (
int i = 0 ;i < RowNnz ; ++i)
246 if (RowIndices[i] < NumMyRows_){
247 RowIndices[count] = RowIndices[i];
248 RowValues[count] = RowValues[i];
260 if (LOF == 0) LOF = 1;
266 for (
int i = 0 ; i < RowNnz ; ++i) {
267 if (RowIndices[i] == row_i) {
268 double& v = RowValues[i];
271 else if (RowIndices[i] < row_i)
273 Hash.set(RowIndices[i], RowValues[i],
true);
280 for (
int col_j = RowIndices[0] ; col_j < row_i ; ++col_j) {
282 double h_ij = 0.0, h_jj = 0.0;
284 h_ij = Hash.get(col_j);
290 H_->ExtractGlobalRowView(col_j, ColNnz, ColValues, ColIndices);
292 for (
int k = 0 ; k < ColNnz ; ++k) {
293 int col_k = ColIndices[k];
298 double xxx = Hash.get(col_k);
301 h_ij -= ColValues[k] * xxx;
302 #ifdef IFPACK_FLOPCOUNTERS 311 if (IFPACK_ABS(h_ij) > DropTolerance_)
313 Hash.set(col_j, h_ij);
316 #ifdef IFPACK_FLOPCOUNTERS 318 ComputeFlops_ += 2.0 * flops + 1.0;
322 int size = Hash.getNumEntries();
324 vector<double> AbsRow(size);
328 vector<int> keys(size + 1);
329 vector<double> values(size + 1);
331 Hash.arrayify(&keys[0], &values[0]);
333 for (
int i = 0 ; i < size ; ++i)
335 AbsRow[i] = IFPACK_ABS(values[i]);
341 nth_element(AbsRow.begin(), AbsRow.begin() + LOF, AbsRow.begin() + count,
343 std::greater<double>());
344 cutoff = AbsRow[LOF];
347 for (
int i = 0 ; i < size ; ++i)
349 h_ii -= values[i] * values[i];
352 if (h_ii < 0.0) h_ii = 1e-12;;
356 #ifdef IFPACK_FLOPCOUNTERS 358 ComputeFlops_ += 2 * size + 1;
361 double DiscardedElements = 0.0;
364 for (
int i = 0 ; i < size ; ++i)
366 if (IFPACK_ABS(values[i]) > cutoff)
368 values[count] = values[i];
369 keys[count] = keys[i];
373 DiscardedElements += values[i];
378 h_ii += DiscardedElements;
381 values[count] = h_ii;
385 H_->InsertGlobalValues(row_i, count, &(values[0]), (
int*)&(keys[0]));
388 IFPACK_CHK_ERR(H_->FillComplete());
392 Epetra_Vector LHS(
Matrix().RowMatrixRowMap());
393 Epetra_Vector RHS1(
Matrix().RowMatrixRowMap());
394 Epetra_Vector RHS2(
Matrix().RowMatrixRowMap());
395 Epetra_Vector RHS3(
Matrix().RowMatrixRowMap());
398 Matrix().Multiply(
false,LHS,RHS1);
399 H_->Multiply(
true,LHS,RHS2);
400 H_->Multiply(
false,RHS2,RHS3);
402 RHS1.Update(-1.0, RHS3, 1.0);
406 int MyNonzeros = H_->NumGlobalNonzeros();
407 Comm().SumAll(&MyNonzeros, &GlobalNonzeros_, 1);
410 #ifdef IFPACK_FLOPCOUNTERS 412 A_.Comm().SumAll(&flops, &TotalFlops, 1);
413 ComputeFlops_ += TotalFlops;
416 ComputeTime_ += Time_.ElapsedTime();
424 Epetra_MultiVector& Y)
const 430 if (X.NumVectors() != Y.NumVectors())
433 Time_.ResetStartTime();
437 Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
438 if (X.Pointers()[0] == Y.Pointers()[0])
439 Xcopy = Teuchos::rcp(
new Epetra_MultiVector(X) );
441 Xcopy = Teuchos::rcp( &X,
false );
447 EPETRA_CHK_ERR(H_->Solve(
false,
false,
false,*Xcopy,Y));
448 EPETRA_CHK_ERR(H_->Solve(
false,
true,
false,Y,Y));
450 #ifdef IFPACK_FLOPCOUNTERS 452 ApplyInverseFlops_ += 4.0 * GlobalNonzeros_;
456 ApplyInverseTime_ += Time_.ElapsedTime();
462 int Ifpack_ICT::Apply(
const Epetra_MultiVector& X,
463 Epetra_MultiVector& Y)
const 471 const int MaxIters,
const double Tol,
472 Epetra_RowMatrix* Matrix_in)
478 if (Condest_ == -1.0)
479 Condest_ = Ifpack_Condest(*
this, CT, MaxIters, Tol, Matrix_in);
488 if (!
Comm().MyPID()) {
490 os <<
"================================================================================" << endl;
491 os <<
"Ifpack_ICT: " << Label() << endl << endl;
495 os <<
"Relax value = " <<
RelaxValue() << endl;
496 os <<
"Condition number estimate = " <<
Condest() << endl;
497 os <<
"Global number of rows = " <<
Matrix().NumGlobalRows() << endl;
499 os <<
"Number of nonzeros of H = " << H_->NumGlobalNonzeros() << endl;
500 os <<
"nonzeros / rows = " 501 << 1.0 * H_->NumGlobalNonzeros() / H_->NumGlobalRows() << endl;
504 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
505 os <<
"----- ------- -------------- ------------ --------" << endl;
508 <<
" 0.0 0.0" << endl;
509 os <<
"Compute() " << std::setw(5) <<
NumCompute()
515 os <<
" " << std::setw(15) << 0.0 << endl;
522 os <<
" " << std::setw(15) << 0.0 << endl;
523 os <<
"================================================================================" << endl;
double RelaxValue() const
Returns the relaxation value.
virtual ostream & Print(std::ostream &os) const
Prints basic information on iostream. This function is used by operator<<.
double LevelOfFill() const
Returns the level-of-fill.
const Epetra_Comm & Comm() const
Returns the Epetra_BlockMap object associated with the range of this matrix operator.
int SetParameters(Teuchos::ParameterList ¶meterlis)
Set parameters using a Teuchos::ParameterList object.
bool IsInitialized() const
Returns true is the preconditioner has been successfully initialized.
int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Ifpack_ICT forward/back solve on a Epetra_MultiVector X in Y...
string Ifpack_toString(const int &x)
Convertes an integer to string.
int Initialize()
Initialize L and U with values from user matrix A.
double DropTolerance() const
Returns the drop threshold.
Ifpack_ICT(const Epetra_RowMatrix *A)
Ifpack_ICT constuctor with variable number of indices per row.
virtual int NumInitialize() const
Returns the number of calls to Initialize().
virtual int NumCompute() const
Returns the number of calls to Compute().
double Condest() const
Returns the computed condition number estimate, or -1.0 if not computed.
virtual double ComputeTime() const
Returns the time spent in Compute().
virtual double ApplyInverseTime() const
Returns the time spent in ApplyInverse().
virtual double ComputeFlops() const
Returns the number of flops in all applications of Compute().
bool IsComputed() const
If factor is completed, this query returns true, otherwise it returns false.
double RelativeThreshold() const
Returns the relative threshold.
const Epetra_RowMatrix & Matrix() const
Returns a reference to the matrix to be preconditioned.
virtual double ApplyInverseFlops() const
Returns the number of flops in all applications of ApplyInverse().
virtual double InitializeTime() const
Returns the time spent in Initialize().
int Compute()
Compute IC factor U using the specified graph, diagonal perturbation thresholds and relaxation parame...
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
double AbsoluteThreshold() const
Returns the absolute threshold.
virtual ~Ifpack_ICT()
Ifpack_ICT Destructor.