43 #include "Ifpack_ConfigDefs.h" 45 #include "Epetra_Operator.h" 46 #include "Epetra_RowMatrix.h" 47 #include "Epetra_Comm.h" 48 #include "Epetra_Map.h" 49 #include "Epetra_MultiVector.h" 50 #include "Epetra_Vector.h" 51 #include "Epetra_Time.h" 52 #include "Ifpack_Chebyshev.h" 54 #include "Ifpack_Condest.h" 55 #ifdef HAVE_IFPACK_AZTECOO 56 #include "Ifpack_DiagPreconditioner.h" 60 #ifdef HAVE_IFPACK_EPETRAEXT 61 #include "Epetra_CrsMatrix.h" 62 #include "EpetraExt_PointToBlockDiagPermute.h" 66 #define ABS(x) ((x)>0?(x):-(x)) 69 inline void Apply_Transpose(Teuchos::RCP<const Epetra_Operator> Operator_,
const Epetra_MultiVector &X,Epetra_MultiVector &Y){
70 Epetra_Operator * Operator=
const_cast<Epetra_Operator*
>(&*Operator_);
71 Operator->SetUseTranspose(
true);
73 Operator->SetUseTranspose(
false);
84 IsInitialized_(false),
91 ApplyInverseTime_(0.0),
93 ApplyInverseFlops_(0.0),
97 ComputeCondest_(false),
102 MinDiagonalValue_(0.0),
106 NumGlobalNonzeros_(0),
107 Operator_(
Teuchos::rcp(Operator,false)),
108 UseBlockMode_(false),
109 SolveNormalEquations_(false),
111 ZeroStartingSolution_(true)
126 IsInitialized_(false),
131 InitializeTime_(0.0),
133 ApplyInverseTime_(0.0),
135 ApplyInverseFlops_(0.0),
137 UseTranspose_(false),
139 ComputeCondest_(false),
145 MinDiagonalValue_(0.0),
149 NumGlobalNonzeros_(0),
150 Operator_(
Teuchos::rcp(Operator,false)),
151 Matrix_(
Teuchos::rcp(Operator,false)),
152 UseBlockMode_(false),
153 SolveNormalEquations_(false),
155 ZeroStartingSolution_(true)
163 EigRatio_ = List.get(
"chebyshev: ratio eigenvalue", EigRatio_);
164 LambdaMin_ = List.get(
"chebyshev: min eigenvalue", LambdaMin_);
165 LambdaMax_ = List.get(
"chebyshev: max eigenvalue", LambdaMax_);
166 PolyDegree_ = List.get(
"chebyshev: degree",PolyDegree_);
167 MinDiagonalValue_ = List.get(
"chebyshev: min diagonal value",
169 ZeroStartingSolution_ = List.get(
"chebyshev: zero starting solution",
170 ZeroStartingSolution_);
172 Epetra_Vector* ID = List.get(
"chebyshev: operator inv diagonal",
174 EigMaxIters_ = List.get(
"chebyshev: eigenvalue max iterations",EigMaxIters_);
176 #ifdef HAVE_IFPACK_EPETRAEXT 178 UseBlockMode_ = List.get(
"chebyshev: use block mode",UseBlockMode_);
179 if(!List.isParameter(
"chebyshev: block list")) UseBlockMode_=
false;
181 BlockList_ = List.get(
"chebyshev: block list",BlockList_);
185 Teuchos::ParameterList Blist;
186 Blist=BlockList_.get(
"blockdiagmatrix: list",Blist);
187 string dummy(
"invert");
188 string ApplyMode=Blist.get(
"apply mode",dummy);
189 if(ApplyMode==
string(
"multiply")){
190 Blist.set(
"apply mode",
"invert");
191 BlockList_.set(
"blockdiagmatrix: list",Blist);
196 SolveNormalEquations_ = List.get(
"chebyshev: solve normal equations",SolveNormalEquations_);
200 InvDiagonal_ = Teuchos::rcp(
new Epetra_Vector(*ID) );
211 return(Operator_->Comm());
217 return(Operator_->OperatorDomainMap());
223 return(Operator_->OperatorRangeMap());
228 Apply(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 233 if (X.NumVectors() != Y.NumVectors())
242 IFPACK_CHK_ERR(Operator_->Apply(X,Y));
251 IsInitialized_ =
false;
253 if (Operator_ == Teuchos::null)
256 if (Time_ == Teuchos::null)
257 Time_ = Teuchos::rcp(
new Epetra_Time(
Comm()) );
261 if (
Matrix().NumGlobalRows() !=
Matrix().NumGlobalCols())
264 NumMyRows_ = Matrix_->NumMyRows();
265 NumMyNonzeros_ = Matrix_->NumMyNonzeros();
266 NumGlobalRows_ = Matrix_->NumGlobalRows();
267 NumGlobalNonzeros_ = Matrix_->NumGlobalNonzeros();
271 if (Operator_->OperatorDomainMap().NumGlobalElements() !=
272 Operator_->OperatorRangeMap().NumGlobalElements())
277 InitializeTime_ += Time_->ElapsedTime();
278 IsInitialized_ =
true;
288 Time_->ResetStartTime();
294 if (PolyDegree_ <= 0)
297 #ifdef HAVE_IFPACK_EPETRAEXT 299 if(IsRowMatrix_ && InvDiagonal_ == Teuchos::null && UseBlockMode_){
300 const Epetra_CrsMatrix *CrsMatrix=
dynamic_cast<const Epetra_CrsMatrix*
>(&*Matrix_);
303 if(!CrsMatrix) UseBlockMode_=
false;
306 InvBlockDiagonal_=Teuchos::rcp(
new EpetraExt_PointToBlockDiagPermute(*CrsMatrix));
307 if(InvBlockDiagonal_==Teuchos::null) IFPACK_CHK_ERR(-6);
309 ierr=InvBlockDiagonal_->SetParameters(BlockList_);
310 if(ierr) IFPACK_CHK_ERR(-7);
312 ierr=InvBlockDiagonal_->Compute();
313 if(ierr) IFPACK_CHK_ERR(-8);
319 LambdaMax_=lambda_max;
321 if(ABS(LambdaMax_-1) < 1e-6) LambdaMax_=LambdaMin_=1.0;
322 else LambdaMin_=LambdaMax_/EigRatio_;
326 if (IsRowMatrix_ && InvDiagonal_ == Teuchos::null && !UseBlockMode_)
328 InvDiagonal_ = Teuchos::rcp(
new Epetra_Vector(
Matrix().Map()) );
330 if (InvDiagonal_ == Teuchos::null)
333 IFPACK_CHK_ERR(
Matrix().ExtractDiagonalCopy(*InvDiagonal_));
337 for (
int i = 0 ; i < NumMyRows_ ; ++i) {
338 double diag = (*InvDiagonal_)[i];
339 if (IFPACK_ABS(diag) < MinDiagonalValue_)
340 (*InvDiagonal_)[i] = MinDiagonalValue_;
342 (*InvDiagonal_)[i] = 1.0 / diag;
346 if (LambdaMax_ == -1) {
348 LambdaMax_=lambda_max;
350 if (ABS(LambdaMax_-1) < 1e-6) LambdaMax_=LambdaMin_=1.0;
351 else LambdaMin_=LambdaMax_/EigRatio_;
355 #ifdef IFPACK_FLOPCOUNTERS 356 ComputeFlops_ += NumMyRows_;
360 ComputeTime_ += Time_->ElapsedTime();
370 double MyMinVal, MyMaxVal;
371 double MinVal, MaxVal;
374 InvDiagonal_->MinValue(&MyMinVal);
375 InvDiagonal_->MaxValue(&MyMaxVal);
376 Comm().MinAll(&MyMinVal,&MinVal,1);
377 Comm().MinAll(&MyMaxVal,&MaxVal,1);
380 if (!
Comm().MyPID()) {
382 os <<
"================================================================================" << endl;
383 os <<
"Ifpack_Chebyshev" << endl;
384 os <<
"Degree of polynomial = " << PolyDegree_ << endl;
385 os <<
"Condition number estimate = " <<
Condest() << endl;
386 os <<
"Global number of rows = " << Operator_->OperatorRangeMap().NumGlobalElements() << endl;
388 os <<
"Minimum value on stored inverse diagonal = " << MinVal << endl;
389 os <<
"Maximum value on stored inverse diagonal = " << MaxVal << endl;
391 if (ZeroStartingSolution_)
392 os <<
"Using zero starting solution" << endl;
394 os <<
"Using input starting solution" << endl;
396 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
397 os <<
"----- ------- -------------- ------------ --------" << endl;
398 os <<
"Initialize() " << std::setw(5) << NumInitialize_
399 <<
" " << std::setw(15) << InitializeTime_
400 <<
" 0.0 0.0" << endl;
401 os <<
"Compute() " << std::setw(5) << NumCompute_
402 <<
" " << std::setw(15) << ComputeTime_
403 <<
" " << std::setw(15) << 1.0e-6 * ComputeFlops_;
404 if (ComputeTime_ != 0.0)
405 os <<
" " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
407 os <<
" " << std::setw(15) << 0.0 << endl;
408 os <<
"ApplyInverse() " << std::setw(5) << NumApplyInverse_
409 <<
" " << std::setw(15) << ApplyInverseTime_
410 <<
" " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
411 if (ApplyInverseTime_ != 0.0)
412 os <<
" " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
414 os <<
" " << std::setw(15) << 0.0 << endl;
415 os <<
"================================================================================" << endl;
425 const int MaxIters,
const double Tol,
426 Epetra_RowMatrix* Matrix_in)
433 Condest_ = Ifpack_Condest(*
this, CT, MaxIters, Tol, Matrix_in);
439 void Ifpack_Chebyshev::SetLabel()
441 Label_ =
"IFPACK (Chebyshev polynomial), degree=" +
Ifpack_toString(PolyDegree_);
452 if (PolyDegree_ == 0)
455 int nVec = X.NumVectors();
456 int len = X.MyLength();
457 if (nVec != Y.NumVectors())
460 Time_->ResetStartTime();
464 Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
465 if (X.Pointers()[0] == Y.Pointers()[0])
466 Xcopy = Teuchos::rcp(
new Epetra_MultiVector(X) );
468 Xcopy = Teuchos::rcp( &X,
false );
470 double **xPtr = 0, **yPtr = 0;
471 Xcopy->ExtractView(&xPtr);
472 Y.ExtractView(&yPtr);
474 #ifdef HAVE_IFPACK_EPETRAEXT 475 EpetraExt_PointToBlockDiagPermute* IBD=0;
476 if (UseBlockMode_) IBD=&*InvBlockDiagonal_;
482 if(!UseBlockMode_) invDiag=InvDiagonal_->Values();
483 if ((LambdaMin_ == 1.0) && (LambdaMax_ == LambdaMin_)) {
484 #ifdef HAVE_IFPACK_EPETRAEXT 485 if(UseBlockMode_) IBD->ApplyInverse(*Xcopy,Y);
489 double *yPointer = yPtr[0], *xPointer = xPtr[0];
490 for (
int i = 0; i < len; ++i)
491 yPointer[i] = xPointer[i]*invDiag[i];
495 for (i = 0; i < len; ++i) {
496 double coeff = invDiag[i];
497 for (k = 0; k < nVec; ++k)
498 yPtr[k][i] = xPtr[k][i] * coeff;
506 double alpha = LambdaMax_ / EigRatio_;
507 double beta = 1.1 * LambdaMax_;
508 double delta = 2.0 / (beta - alpha);
509 double theta = 0.5 * (beta + alpha);
510 double s1 = theta * delta;
514 Epetra_MultiVector V(X);
515 Epetra_MultiVector W(X);
516 #ifdef HAVE_IFPACK_EPETRAEXT 517 Epetra_MultiVector Temp(X);
520 double *vPointer = V.Values(), *wPointer = W.Values();
522 double oneOverTheta = 1.0/theta;
527 if(SolveNormalEquations_){
528 Apply_Transpose(Operator_,Y,V);
534 if (ZeroStartingSolution_ ==
false) {
535 Operator_->Apply(Y, V);
537 #ifdef HAVE_IFPACK_EPETRAEXT 539 Temp.Update(oneOverTheta,X,-oneOverTheta,V,0.0);
540 IBD->ApplyInverse(Temp,W);
544 if(SolveNormalEquations_){
545 IBD->ApplyInverse(W,Temp);
546 Apply_Transpose(Operator_,Temp,W);
552 double *xPointer = xPtr[0];
553 for (i = 0; i < len; ++i)
554 wPointer[i] = invDiag[i] * (xPointer[i] - vPointer[i]) * oneOverTheta;
557 for (i = 0; i < len; ++i) {
558 double coeff = invDiag[i]*oneOverTheta;
559 double *wi = wPointer + i, *vi = vPointer + i;
560 for (k = 0; k < nVec; ++k) {
561 *wi = (xPtr[k][i] - (*vi)) * coeff;
562 wi = wi + len; vi = vi + len;
567 Y.Update(1.0, W, 1.0);
571 #ifdef HAVE_IFPACK_EPETRAEXT 573 IBD->ApplyInverse(X,W);
577 if(SolveNormalEquations_){
578 IBD->ApplyInverse(W,Temp);
579 Apply_Transpose(Operator_,Temp,W);
582 W.Scale(oneOverTheta);
583 Y.Update(1.0, W, 0.0);
588 double *xPointer = xPtr[0];
589 for (i = 0; i < len; ++i){
590 wPointer[i] = invDiag[i] * xPointer[i] * oneOverTheta;
592 memcpy(yPtr[0], wPointer, len*
sizeof(
double));
595 for (i = 0; i < len; ++i) {
596 double coeff = invDiag[i]*oneOverTheta;
597 double *wi = wPointer + i;
598 for (k = 0; k < nVec; ++k) {
599 *wi = xPtr[k][i] * coeff;
603 for (k = 0; k < nVec; ++k)
604 memcpy(yPtr[k], wPointer + k*len, len*
sizeof(
double));
609 double rhok = 1.0/s1, rhokp1;
610 double dtemp1, dtemp2;
611 int degreeMinusOne = PolyDegree_ - 1;
613 double *xPointer = xPtr[0];
614 for (k = 0; k < degreeMinusOne; ++k) {
615 Operator_->Apply(Y, V);
616 rhokp1 = 1.0 / (2.0*s1 - rhok);
617 dtemp1 = rhokp1 * rhok;
618 dtemp2 = 2.0 * rhokp1 * delta;
623 #ifdef HAVE_IFPACK_EPETRAEXT 626 V.Update(dtemp2,X,-dtemp2);
627 IBD->ApplyInverse(V,Temp);
631 if(SolveNormalEquations_){
632 IBD->ApplyInverse(V,Temp);
633 Apply_Transpose(Operator_,Temp,V);
636 W.Update(1.0,Temp,1.0);
640 for (i = 0; i < len; ++i)
641 wPointer[i] += dtemp2* invDiag[i] * (xPointer[i] - vPointer[i]);
642 #ifdef HAVE_IFPACK_EPETRAEXT 647 Y.Update(1.0, W, 1.0);
651 for (k = 0; k < degreeMinusOne; ++k) {
652 Operator_->Apply(Y, V);
653 rhokp1 = 1.0 / (2.0*s1 - rhok);
654 dtemp1 = rhokp1 * rhok;
655 dtemp2 = 2.0 * rhokp1 * delta;
660 #ifdef HAVE_IFPACK_EPETRAEXT 663 V.Update(dtemp2,X,-dtemp2);
664 IBD->ApplyInverse(V,Temp);
668 if(SolveNormalEquations_){
669 IBD->ApplyInverse(V,Temp);
670 Apply_Transpose(Operator_,Temp,V);
674 W.Update(1.0,Temp,1.0);
678 for (i = 0; i < len; ++i) {
679 double coeff = invDiag[i]*dtemp2;
680 double *wi = wPointer + i, *vi = vPointer + i;
681 for (j = 0; j < nVec; ++j) {
682 *wi += (xPtr[j][i] - (*vi)) * coeff;
683 wi = wi + len; vi = vi + len;
686 #ifdef HAVE_IFPACK_EPETRAEXT 690 Y.Update(1.0, W, 1.0);
697 ApplyInverseTime_ += Time_->ElapsedTime();
704 const Epetra_Vector& InvPointDiagonal,
705 const int MaximumIterations,
710 double RQ_top, RQ_bottom, norm;
711 Epetra_Vector x(Operator.OperatorDomainMap());
712 Epetra_Vector y(Operator.OperatorRangeMap());
715 if (norm == 0.0) IFPACK_CHK_ERR(-1);
719 for (
int iter = 0; iter < MaximumIterations; ++iter)
721 Operator.Apply(x, y);
722 IFPACK_CHK_ERR(y.Multiply(1.0, InvPointDiagonal, y, 0.0));
723 IFPACK_CHK_ERR(y.Dot(x, &RQ_top));
724 IFPACK_CHK_ERR(x.Dot(x, &RQ_bottom));
725 lambda_max = RQ_top / RQ_bottom;
726 IFPACK_CHK_ERR(y.Norm2(&norm));
727 if (norm == 0.0) IFPACK_CHK_ERR(-1);
728 IFPACK_CHK_ERR(x.Update(1.0 / norm, y, 0.0));
736 CG(
const Epetra_Operator& Operator,
737 const Epetra_Vector& InvPointDiagonal,
738 const int MaximumIterations,
739 double& lambda_min,
double& lambda_max)
741 #ifdef HAVE_IFPACK_AZTECOO 742 Epetra_Vector x(Operator.OperatorDomainMap());
743 Epetra_Vector y(Operator.OperatorRangeMap());
747 Epetra_LinearProblem LP(const_cast<Epetra_Operator*>(&Operator), &x, &y);
749 solver.SetAztecOption(AZ_solver, AZ_cg_condnum);
750 solver.SetAztecOption(AZ_output, AZ_none);
753 Operator.OperatorRangeMap(),
755 solver.SetPrecOperator(&diag);
756 solver.Iterate(MaximumIterations, 1e-10);
758 const double* status = solver.GetAztecStatus();
760 lambda_min = status[AZ_lambda_min];
761 lambda_max = status[AZ_lambda_max];
765 cout <<
"You need to configure IFPACK with support for AztecOO" << endl;
766 cout <<
"to use the CG estimator. This may require --enable-aztecoo" << endl;
767 cout <<
"in your configure script." << endl;
773 #ifdef HAVE_IFPACK_EPETRAEXT 775 PowerMethod(
const int MaximumIterations,
double& lambda_max)
778 if(!UseBlockMode_) IFPACK_CHK_ERR(-1);
781 double RQ_top, RQ_bottom, norm;
782 Epetra_Vector x(Operator_->OperatorDomainMap());
783 Epetra_Vector y(Operator_->OperatorRangeMap());
784 Epetra_Vector z(Operator_->OperatorRangeMap());
787 if (norm == 0.0) IFPACK_CHK_ERR(-1);
791 for (
int iter = 0; iter < MaximumIterations; ++iter)
793 Operator_->Apply(x, z);
794 InvBlockDiagonal_->ApplyInverse(z,y);
795 if(SolveNormalEquations_){
796 InvBlockDiagonal_->ApplyInverse(y,z);
797 Apply_Transpose(Operator_,z, y);
800 IFPACK_CHK_ERR(y.Dot(x, &RQ_top));
801 IFPACK_CHK_ERR(x.Dot(x, &RQ_bottom));
802 lambda_max = RQ_top / RQ_bottom;
803 IFPACK_CHK_ERR(y.Norm2(&norm));
804 if (norm == 0.0) IFPACK_CHK_ERR(-1);
805 IFPACK_CHK_ERR(x.Update(1.0 / norm, y, 0.0));
813 #ifdef HAVE_IFPACK_EPETRAEXT 815 CG(
const int MaximumIterations,
816 double& lambda_min,
double& lambda_max)
821 if(!UseBlockMode_) IFPACK_CHK_ERR(-1);
823 #ifdef HAVE_IFPACK_AZTECOO 824 Epetra_Vector x(Operator_->OperatorDomainMap());
825 Epetra_Vector y(Operator_->OperatorRangeMap());
828 Epetra_LinearProblem LP(const_cast<Epetra_RowMatrix*>(&*Matrix_), &x, &y);
831 solver.SetAztecOption(AZ_solver, AZ_cg_condnum);
832 solver.SetAztecOption(AZ_output, AZ_none);
834 solver.SetPrecOperator(&*InvBlockDiagonal_);
835 solver.Iterate(MaximumIterations, 1e-10);
837 const double* status = solver.GetAztecStatus();
839 lambda_min = status[AZ_lambda_min];
840 lambda_max = status[AZ_lambda_max];
844 cout <<
"You need to configure IFPACK with support for AztecOO" << endl;
845 cout <<
"to use the CG estimator. This may require --enable-aztecoo" << endl;
846 cout <<
"in your configure script." << endl;
static int CG(const Epetra_Operator &Operator, const Epetra_Vector &InvPointDiagonal, const int MaximumIterations, double &lambda_min, double &lambda_max)
Uses AztecOO's CG to estimate lambda_min and lambda_max.
virtual int Compute()
Computes the preconditioners.
virtual ostream & Print(ostream &os) const
Prints object to an output stream.
virtual int SetParameters(Teuchos::ParameterList &List)
Sets all the parameters for the preconditioner.
virtual bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized, false otherwise.
Ifpack_DiagPreconditioner: a class for diagonal preconditioning.
string Ifpack_toString(const int &x)
Convertes an integer to string.
virtual bool IsComputed() const
Returns true if the preconditioner has been successfully computed.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the matrix to an Epetra_MultiVector.
static int PowerMethod(const Epetra_Operator &Operator, const Epetra_Vector &InvPointDiagonal, const int MaximumIterations, double &LambdaMax)
Simple power method to compute lambda_max.
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
virtual double Condest() const
Returns the condition number estimate, or -1.0 if not computed.
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
virtual const Epetra_RowMatrix & Matrix() const
Returns a pointer to the matrix to be preconditioned.
Ifpack_Chebyshev(const Epetra_Operator *Matrix)
Ifpack_Chebyshev constructor with given Epetra_Operator/Epetra_RowMatrix.
virtual int Initialize()
Computes all it is necessary to initialize the preconditioner.
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the preconditioner to X, returns the result in Y.