IFPACK  Development
Ifpack_Chebyshev.cpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "Ifpack_ConfigDefs.h"
44 #include <iomanip>
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"
53 #include "Ifpack_Utils.h"
54 #include "Ifpack_Condest.h"
55 #ifdef HAVE_IFPACK_AZTECOO
56 #include "Ifpack_DiagPreconditioner.h"
57 #include "AztecOO.h"
58 #endif
59 
60 #ifdef HAVE_IFPACK_EPETRAEXT
61 #include "Epetra_CrsMatrix.h"
62 #include "EpetraExt_PointToBlockDiagPermute.h"
63 #endif
64 
65 
66 #define ABS(x) ((x)>0?(x):-(x))
67 
68 // Helper function for normal equations
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);
72  Operator->Apply(X,Y);
73  Operator->SetUseTranspose(false);
74 }
75 
76 
77 
78 
79 //==============================================================================
80 // NOTE: any change to the default values should be committed to the other
81 // constructor as well.
83 Ifpack_Chebyshev(const Epetra_Operator* Operator) :
84  IsInitialized_(false),
85  IsComputed_(false),
86  NumInitialize_(0),
87  NumCompute_(0),
88  NumApplyInverse_(0),
89  InitializeTime_(0.0),
90  ComputeTime_(0.0),
91  ApplyInverseTime_(0.0),
92  ComputeFlops_(0.0),
93  ApplyInverseFlops_(0.0),
94  PolyDegree_(1),
95  UseTranspose_(false),
96  Condest_(-1.0),
97  ComputeCondest_(false),
98  EigRatio_(30.0),
99  Label_(),
100  LambdaMin_(0.0),
101  LambdaMax_(-1.0),
102  MinDiagonalValue_(0.0),
103  NumMyRows_(0),
104  NumMyNonzeros_(0),
105  NumGlobalRows_(0),
106  NumGlobalNonzeros_(0),
107  Operator_(Teuchos::rcp(Operator,false)),
108  UseBlockMode_(false),
109  SolveNormalEquations_(false),
110  IsRowMatrix_(false),
111  ZeroStartingSolution_(true)
112 {
113 }
114 
115 //==============================================================================
116 // NOTE: This constructor has been introduced because SWIG does not appear
117 // to appreciate dynamic_cast. An instruction of type
118 // Matrix_ = dynamic_cast<const Epetra_RowMatrix*> in the
119 // other construction does not work in PyTrilinos -- of course
120 // it does in any C++ code (for an Epetra_Operator that is also
121 // an Epetra_RowMatrix).
122 //
123 // FIXME: move declarations into a separate method?
125 Ifpack_Chebyshev(const Epetra_RowMatrix* Operator) :
126  IsInitialized_(false),
127  IsComputed_(false),
128  NumInitialize_(0),
129  NumCompute_(0),
130  NumApplyInverse_(0),
131  InitializeTime_(0.0),
132  ComputeTime_(0.0),
133  ApplyInverseTime_(0.0),
134  ComputeFlops_(0.0),
135  ApplyInverseFlops_(0.0),
136  PolyDegree_(1),
137  UseTranspose_(false),
138  Condest_(-1.0),
139  ComputeCondest_(false),
140  EigRatio_(30.0),
141  EigMaxIters_(10),
142  Label_(),
143  LambdaMin_(0.0),
144  LambdaMax_(-1.0),
145  MinDiagonalValue_(0.0),
146  NumMyRows_(0),
147  NumMyNonzeros_(0),
148  NumGlobalRows_(0),
149  NumGlobalNonzeros_(0),
150  Operator_(Teuchos::rcp(Operator,false)),
151  Matrix_(Teuchos::rcp(Operator,false)),
152  UseBlockMode_(false),
153  SolveNormalEquations_(false),
154  IsRowMatrix_(true),
155  ZeroStartingSolution_(true)
156 {
157 }
158 
159 //==============================================================================
160 int Ifpack_Chebyshev::SetParameters(Teuchos::ParameterList& List)
161 {
162 
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",
168  MinDiagonalValue_);
169  ZeroStartingSolution_ = List.get("chebyshev: zero starting solution",
170  ZeroStartingSolution_);
171 
172  Epetra_Vector* ID = List.get("chebyshev: operator inv diagonal",
173  (Epetra_Vector*)0);
174  EigMaxIters_ = List.get("chebyshev: eigenvalue max iterations",EigMaxIters_);
175 
176 #ifdef HAVE_IFPACK_EPETRAEXT
177  // This is *always* false if EpetraExt isn't enabled
178  UseBlockMode_ = List.get("chebyshev: use block mode",UseBlockMode_);
179  if(!List.isParameter("chebyshev: block list")) UseBlockMode_=false;
180  else{
181  BlockList_ = List.get("chebyshev: block list",BlockList_);
182 
183  // Since we know we're doing a matrix inverse, clobber the block list
184  // w/"invert" if it's set to multiply
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);
192  }
193  }
194 #endif
195 
196  SolveNormalEquations_ = List.get("chebyshev: solve normal equations",SolveNormalEquations_);
197 
198  if (ID != 0)
199  {
200  InvDiagonal_ = Teuchos::rcp( new Epetra_Vector(*ID) );
201  }
202 
203  SetLabel();
204 
205  return(0);
206 }
207 
208 //==============================================================================
209 const Epetra_Comm& Ifpack_Chebyshev::Comm() const
210 {
211  return(Operator_->Comm());
212 }
213 
214 //==============================================================================
215 const Epetra_Map& Ifpack_Chebyshev::OperatorDomainMap() const
216 {
217  return(Operator_->OperatorDomainMap());
218 }
219 
220 //==============================================================================
221 const Epetra_Map& Ifpack_Chebyshev::OperatorRangeMap() const
222 {
223  return(Operator_->OperatorRangeMap());
224 }
225 
226 //==============================================================================
228 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
229 {
230  if (IsComputed() == false)
231  IFPACK_CHK_ERR(-3);
232 
233  if (X.NumVectors() != Y.NumVectors())
234  IFPACK_CHK_ERR(-2);
235 
236  if (IsRowMatrix_)
237  {
238  IFPACK_CHK_ERR(Matrix_->Multiply(UseTranspose(),X,Y));
239  }
240  else
241  {
242  IFPACK_CHK_ERR(Operator_->Apply(X,Y));
243  }
244 
245  return(0);
246 }
247 
248 //==============================================================================
250 {
251  IsInitialized_ = false;
252 
253  if (Operator_ == Teuchos::null)
254  IFPACK_CHK_ERR(-2);
255 
256  if (Time_ == Teuchos::null)
257  Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
258 
259  if (IsRowMatrix_)
260  {
261  if (Matrix().NumGlobalRows() != Matrix().NumGlobalCols())
262  IFPACK_CHK_ERR(-2); // only square matrices
263 
264  NumMyRows_ = Matrix_->NumMyRows();
265  NumMyNonzeros_ = Matrix_->NumMyNonzeros();
266  NumGlobalRows_ = Matrix_->NumGlobalRows();
267  NumGlobalNonzeros_ = Matrix_->NumGlobalNonzeros();
268  }
269  else
270  {
271  if (Operator_->OperatorDomainMap().NumGlobalElements() !=
272  Operator_->OperatorRangeMap().NumGlobalElements())
273  IFPACK_CHK_ERR(-2); // only square operators
274  }
275 
276  ++NumInitialize_;
277  InitializeTime_ += Time_->ElapsedTime();
278  IsInitialized_ = true;
279  return(0);
280 }
281 
282 //==============================================================================
284 {
285  if (!IsInitialized())
286  IFPACK_CHK_ERR(Initialize());
287 
288  Time_->ResetStartTime();
289 
290  // reset values
291  IsComputed_ = false;
292  Condest_ = -1.0;
293 
294  if (PolyDegree_ <= 0)
295  IFPACK_CHK_ERR(-2); // at least one application
296 
297 #ifdef HAVE_IFPACK_EPETRAEXT
298  // Check to see if we can run in block mode
299  if(IsRowMatrix_ && InvDiagonal_ == Teuchos::null && UseBlockMode_){
300  const Epetra_CrsMatrix *CrsMatrix=dynamic_cast<const Epetra_CrsMatrix*>(&*Matrix_);
301 
302  // If we don't have CrsMatrix, we can't use the block preconditioner
303  if(!CrsMatrix) UseBlockMode_=false;
304  else{
305  int ierr;
306  InvBlockDiagonal_=Teuchos::rcp(new EpetraExt_PointToBlockDiagPermute(*CrsMatrix));
307  if(InvBlockDiagonal_==Teuchos::null) IFPACK_CHK_ERR(-6);
308 
309  ierr=InvBlockDiagonal_->SetParameters(BlockList_);
310  if(ierr) IFPACK_CHK_ERR(-7);
311 
312  ierr=InvBlockDiagonal_->Compute();
313  if(ierr) IFPACK_CHK_ERR(-8);
314  }
315 
316  // Automatically Compute Eigenvalues
317  double lambda_max=0;
318  PowerMethod(EigMaxIters_,lambda_max);
319  LambdaMax_=lambda_max;
320  // Test for Exact Preconditioned case
321  if(ABS(LambdaMax_-1) < 1e-6) LambdaMax_=LambdaMin_=1.0;
322  else LambdaMin_=LambdaMax_/EigRatio_;
323  }
324 #endif
325 
326  if (IsRowMatrix_ && InvDiagonal_ == Teuchos::null && !UseBlockMode_)
327  {
328  InvDiagonal_ = Teuchos::rcp( new Epetra_Vector(Matrix().Map()) );
329 
330  if (InvDiagonal_ == Teuchos::null)
331  IFPACK_CHK_ERR(-5);
332 
333  IFPACK_CHK_ERR(Matrix().ExtractDiagonalCopy(*InvDiagonal_));
334 
335  // Inverse diagonal elements
336  // Replace zeros with 1.0
337  for (int i = 0 ; i < NumMyRows_ ; ++i) {
338  double diag = (*InvDiagonal_)[i];
339  if (IFPACK_ABS(diag) < MinDiagonalValue_)
340  (*InvDiagonal_)[i] = MinDiagonalValue_;
341  else
342  (*InvDiagonal_)[i] = 1.0 / diag;
343  }
344  // Automatically compute maximum eigenvalue estimate of D^{-1}A if user hasn't provided one
345  double lambda_max=0;
346  if (LambdaMax_ == -1) {
347  PowerMethod(Matrix(), *InvDiagonal_, EigMaxIters_, lambda_max);
348  LambdaMax_=lambda_max;
349  // Test for Exact Preconditioned case
350  if (ABS(LambdaMax_-1) < 1e-6) LambdaMax_=LambdaMin_=1.0;
351  else LambdaMin_=LambdaMax_/EigRatio_;
352  }
353  // otherwise the inverse of the diagonal has been given by the user
354  }
355 #ifdef IFPACK_FLOPCOUNTERS
356  ComputeFlops_ += NumMyRows_;
357 #endif
358 
359  ++NumCompute_;
360  ComputeTime_ += Time_->ElapsedTime();
361  IsComputed_ = true;
362 
363  return(0);
364 }
365 
366 //==============================================================================
367 ostream& Ifpack_Chebyshev::Print(ostream & os) const
368 {
369 
370  double MyMinVal, MyMaxVal;
371  double MinVal, MaxVal;
372 
373  if (IsComputed_) {
374  InvDiagonal_->MinValue(&MyMinVal);
375  InvDiagonal_->MaxValue(&MyMaxVal);
376  Comm().MinAll(&MyMinVal,&MinVal,1);
377  Comm().MinAll(&MyMaxVal,&MaxVal,1);
378  }
379 
380  if (!Comm().MyPID()) {
381  os << endl;
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;
387  if (IsComputed_) {
388  os << "Minimum value on stored inverse diagonal = " << MinVal << endl;
389  os << "Maximum value on stored inverse diagonal = " << MaxVal << endl;
390  }
391  if (ZeroStartingSolution_)
392  os << "Using zero starting solution" << endl;
393  else
394  os << "Using input starting solution" << endl;
395  os << 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;
406  else
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;
413  else
414  os << " " << std::setw(15) << 0.0 << endl;
415  os << "================================================================================" << endl;
416  os << endl;
417  }
418 
419  return(os);
420 }
421 
422 //==============================================================================
423 double Ifpack_Chebyshev::
424 Condest(const Ifpack_CondestType CT,
425  const int MaxIters, const double Tol,
426  Epetra_RowMatrix* Matrix_in)
427 {
428  if (!IsComputed()) // cannot compute right now
429  return(-1.0);
430 
431  // always computes it. Call Condest() with no parameters to get
432  // the previous estimate.
433  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
434 
435  return(Condest_);
436 }
437 
438 //==============================================================================
439 void Ifpack_Chebyshev::SetLabel()
440 {
441  Label_ = "IFPACK (Chebyshev polynomial), degree=" + Ifpack_toString(PolyDegree_);
442 }
443 
444 //==============================================================================
446 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
447 {
448 
449  if (!IsComputed())
450  IFPACK_CHK_ERR(-3);
451 
452  if (PolyDegree_ == 0)
453  return 0;
454 
455  int nVec = X.NumVectors();
456  int len = X.MyLength();
457  if (nVec != Y.NumVectors())
458  IFPACK_CHK_ERR(-2);
459 
460  Time_->ResetStartTime();
461 
462  // AztecOO gives X and Y pointing to the same memory location,
463  // need to create an auxiliary vector, Xcopy
464  Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
465  if (X.Pointers()[0] == Y.Pointers()[0])
466  Xcopy = Teuchos::rcp( new Epetra_MultiVector(X) );
467  else
468  Xcopy = Teuchos::rcp( &X, false );
469 
470  double **xPtr = 0, **yPtr = 0;
471  Xcopy->ExtractView(&xPtr);
472  Y.ExtractView(&yPtr);
473 
474 #ifdef HAVE_IFPACK_EPETRAEXT
475  EpetraExt_PointToBlockDiagPermute* IBD=0;
476  if (UseBlockMode_) IBD=&*InvBlockDiagonal_;
477 #endif
478 
479 
480  //--- Do a quick solve when the matrix is identity
481  double *invDiag=0;
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);
486  else
487 #endif
488  if (nVec == 1) {
489  double *yPointer = yPtr[0], *xPointer = xPtr[0];
490  for (int i = 0; i < len; ++i)
491  yPointer[i] = xPointer[i]*invDiag[i];
492  }
493  else {
494  int i, k;
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;
499  }
500  } // if (nVec == 1)
501  return 0;
502  } // if ((LambdaMin_ == 1.0) && (LambdaMax_ == LambdaMin_))
503 
504  //--- Initialize coefficients
505  // Note that delta stores the inverse of ML_Cheby::delta
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;
511 
512  //--- Define vectors
513  // In ML_Cheby, V corresponds to pAux and W to dk
514  Epetra_MultiVector V(X);
515  Epetra_MultiVector W(X);
516 #ifdef HAVE_IFPACK_EPETRAEXT
517  Epetra_MultiVector Temp(X);
518 #endif
519 
520  double *vPointer = V.Values(), *wPointer = W.Values();
521 
522  double oneOverTheta = 1.0/theta;
523  int i, j, k;
524 
525 
526  //--- If solving normal equations, multiply RHS by A^T
527  if(SolveNormalEquations_){
528  Apply_Transpose(Operator_,Y,V);
529  Y=V;
530  }
531 
532  // Do the smoothing when block scaling is turned OFF
533  // --- Treat the initial guess
534  if (ZeroStartingSolution_ == false) {
535  Operator_->Apply(Y, V);
536  // Compute W = invDiag * ( X - V )/ Theta
537 #ifdef HAVE_IFPACK_EPETRAEXT
538  if(UseBlockMode_) {
539  Temp.Update(oneOverTheta,X,-oneOverTheta,V,0.0);
540  IBD->ApplyInverse(Temp,W);
541 
542  // Perform additional matvecs for normal equations
543  // CMS: Testing this only in block mode FOR NOW
544  if(SolveNormalEquations_){
545  IBD->ApplyInverse(W,Temp);
546  Apply_Transpose(Operator_,Temp,W);
547  }
548  }
549  else
550 #endif
551  if (nVec == 1) {
552  double *xPointer = xPtr[0];
553  for (i = 0; i < len; ++i)
554  wPointer[i] = invDiag[i] * (xPointer[i] - vPointer[i]) * oneOverTheta;
555  }
556  else {
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;
563  }
564  }
565  } // if (nVec == 1)
566  // Update the vector Y
567  Y.Update(1.0, W, 1.0);
568  }
569  else {
570  // Compute W = invDiag * X / Theta
571 #ifdef HAVE_IFPACK_EPETRAEXT
572  if(UseBlockMode_) {
573  IBD->ApplyInverse(X,W);
574 
575  // Perform additional matvecs for normal equations
576  // CMS: Testing this only in block mode FOR NOW
577  if(SolveNormalEquations_){
578  IBD->ApplyInverse(W,Temp);
579  Apply_Transpose(Operator_,Temp,W);
580  }
581 
582  W.Scale(oneOverTheta);
583  Y.Update(1.0, W, 0.0);
584  }
585  else
586 #endif
587  if (nVec == 1) {
588  double *xPointer = xPtr[0];
589  for (i = 0; i < len; ++i){
590  wPointer[i] = invDiag[i] * xPointer[i] * oneOverTheta;
591  }
592  memcpy(yPtr[0], wPointer, len*sizeof(double));
593  }
594  else {
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;
600  wi = wi + len;
601  }
602  }
603  for (k = 0; k < nVec; ++k)
604  memcpy(yPtr[k], wPointer + k*len, len*sizeof(double));
605  } // if (nVec == 1)
606  } // if (ZeroStartingSolution_ == false)
607 
608  //--- Apply the polynomial
609  double rhok = 1.0/s1, rhokp1;
610  double dtemp1, dtemp2;
611  int degreeMinusOne = PolyDegree_ - 1;
612  if (nVec == 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;
619  rhok = rhokp1;
620  // Compute W = dtemp1 * W
621  W.Scale(dtemp1);
622  // Compute W = W + dtemp2 * invDiag * ( X - V )
623 #ifdef HAVE_IFPACK_EPETRAEXT
624  if(UseBlockMode_) {
625  //NTS: We can clobber V since it will be reset in the Apply
626  V.Update(dtemp2,X,-dtemp2);
627  IBD->ApplyInverse(V,Temp);
628 
629  // Perform additional matvecs for normal equations
630  // CMS: Testing this only in block mode FOR NOW
631  if(SolveNormalEquations_){
632  IBD->ApplyInverse(V,Temp);
633  Apply_Transpose(Operator_,Temp,V);
634  }
635 
636  W.Update(1.0,Temp,1.0);
637  }
638  else{
639 #endif
640  for (i = 0; i < len; ++i)
641  wPointer[i] += dtemp2* invDiag[i] * (xPointer[i] - vPointer[i]);
642 #ifdef HAVE_IFPACK_EPETRAEXT
643  }
644 #endif
645 
646  // Update the vector Y
647  Y.Update(1.0, W, 1.0);
648  } // for (k = 0; k < degreeMinusOne; ++k)
649  }
650  else {
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;
656  rhok = rhokp1;
657  // Compute W = dtemp1 * W
658  W.Scale(dtemp1);
659  // Compute W = W + dtemp2 * invDiag * ( X - V )
660 #ifdef HAVE_IFPACK_EPETRAEXT
661  if(UseBlockMode_) {
662  //We can clobber V since it will be reset in the Apply
663  V.Update(dtemp2,X,-dtemp2);
664  IBD->ApplyInverse(V,Temp);
665 
666  // Perform additional matvecs for normal equations
667  // CMS: Testing this only in block mode FOR NOW
668  if(SolveNormalEquations_){
669  IBD->ApplyInverse(V,Temp);
670  Apply_Transpose(Operator_,Temp,V);
671  }
672 
673 
674  W.Update(1.0,Temp,1.0);
675  }
676  else{
677 #endif
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;
684  }
685  }
686 #ifdef HAVE_IFPACK_EPETRAEXT
687  }
688 #endif
689  // Update the vector Y
690  Y.Update(1.0, W, 1.0);
691  } // for (k = 0; k < degreeMinusOne; ++k)
692  } // if (nVec == 1)
693 
694 
695  // Flops are updated in each of the following.
696  ++NumApplyInverse_;
697  ApplyInverseTime_ += Time_->ElapsedTime();
698  return(0);
699 }
700 
701 //==============================================================================
703 PowerMethod(const Epetra_Operator& Operator,
704  const Epetra_Vector& InvPointDiagonal,
705  const int MaximumIterations,
706  double& lambda_max)
707 {
708  // this is a simple power method
709  lambda_max = 0.0;
710  double RQ_top, RQ_bottom, norm;
711  Epetra_Vector x(Operator.OperatorDomainMap());
712  Epetra_Vector y(Operator.OperatorRangeMap());
713  x.Random();
714  x.Norm2(&norm);
715  if (norm == 0.0) IFPACK_CHK_ERR(-1);
716 
717  x.Scale(1.0 / norm);
718 
719  for (int iter = 0; iter < MaximumIterations; ++iter)
720  {
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));
729  }
730 
731  return(0);
732 }
733 
734 //==============================================================================
736 CG(const Epetra_Operator& Operator,
737  const Epetra_Vector& InvPointDiagonal,
738  const int MaximumIterations,
739  double& lambda_min, double& lambda_max)
740 {
741 #ifdef HAVE_IFPACK_AZTECOO
742  Epetra_Vector x(Operator.OperatorDomainMap());
743  Epetra_Vector y(Operator.OperatorRangeMap());
744  x.Random();
745  y.PutScalar(0.0);
746 
747  Epetra_LinearProblem LP(const_cast<Epetra_Operator*>(&Operator), &x, &y);
748  AztecOO solver(LP);
749  solver.SetAztecOption(AZ_solver, AZ_cg_condnum);
750  solver.SetAztecOption(AZ_output, AZ_none);
751 
752  Ifpack_DiagPreconditioner diag(Operator.OperatorDomainMap(),
753  Operator.OperatorRangeMap(),
754  InvPointDiagonal);
755  solver.SetPrecOperator(&diag);
756  solver.Iterate(MaximumIterations, 1e-10);
757 
758  const double* status = solver.GetAztecStatus();
759 
760  lambda_min = status[AZ_lambda_min];
761  lambda_max = status[AZ_lambda_max];
762 
763  return(0);
764 #else
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;
768  IFPACK_CHK_ERR(-1);
769 #endif
770 }
771 
772 //==============================================================================
773 #ifdef HAVE_IFPACK_EPETRAEXT
775 PowerMethod(const int MaximumIterations, double& lambda_max)
776 {
777 
778  if(!UseBlockMode_) IFPACK_CHK_ERR(-1);
779  // this is a simple power method
780  lambda_max = 0.0;
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());
785  x.Random();
786  x.Norm2(&norm);
787  if (norm == 0.0) IFPACK_CHK_ERR(-1);
788 
789  x.Scale(1.0 / norm);
790 
791  for (int iter = 0; iter < MaximumIterations; ++iter)
792  {
793  Operator_->Apply(x, z);
794  InvBlockDiagonal_->ApplyInverse(z,y);
795  if(SolveNormalEquations_){
796  InvBlockDiagonal_->ApplyInverse(y,z);
797  Apply_Transpose(Operator_,z, y);
798  }
799 
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));
806  }
807 
808  return(0);
809 }
810 #endif
811 
812 //==============================================================================
813 #ifdef HAVE_IFPACK_EPETRAEXT
815 CG(const int MaximumIterations,
816  double& lambda_min, double& lambda_max)
817 {
818  IFPACK_CHK_ERR(-1);// NTS: This always seems to yield errors in AztecOO, ergo,
819  // I turned it off.
820 
821  if(!UseBlockMode_) IFPACK_CHK_ERR(-1);
822 
823 #ifdef HAVE_IFPACK_AZTECOO
824  Epetra_Vector x(Operator_->OperatorDomainMap());
825  Epetra_Vector y(Operator_->OperatorRangeMap());
826  x.Random();
827  y.PutScalar(0.0);
828  Epetra_LinearProblem LP(const_cast<Epetra_RowMatrix*>(&*Matrix_), &x, &y);
829 
830  AztecOO solver(LP);
831  solver.SetAztecOption(AZ_solver, AZ_cg_condnum);
832  solver.SetAztecOption(AZ_output, AZ_none);
833 
834  solver.SetPrecOperator(&*InvBlockDiagonal_);
835  solver.Iterate(MaximumIterations, 1e-10);
836 
837  const double* status = solver.GetAztecStatus();
838 
839  lambda_min = status[AZ_lambda_min];
840  lambda_max = status[AZ_lambda_max];
841 
842  return(0);
843 #else
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;
847  IFPACK_CHK_ERR(-1);
848 #endif
849 }
850 #endif
static int CG(const Epetra_Operator &Operator, const Epetra_Vector &InvPointDiagonal, const int MaximumIterations, double &lambda_min, double &lambda_max)
Uses AztecOO&#39;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.