IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_PointRelaxation.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 <cmath>
46 #include "Epetra_Operator.h"
47 #include "Epetra_CrsMatrix.h"
48 #include "Epetra_Comm.h"
49 #include "Epetra_Map.h"
50 #include "Epetra_MultiVector.h"
51 #include "Ifpack_Preconditioner.h"
52 #include "Ifpack_PointRelaxation.h"
53 #include "Ifpack_Utils.h"
54 #include "Ifpack_Condest.h"
55 
56 static const int IFPACK_JACOBI = 0;
57 static const int IFPACK_GS = 1;
58 static const int IFPACK_SGS = 2;
59 
60 //==============================================================================
62 Ifpack_PointRelaxation(const Epetra_RowMatrix* Matrix_in) :
63  IsInitialized_(false),
64  IsComputed_(false),
65  NumInitialize_(0),
66  NumCompute_(0),
67  NumApplyInverse_(0),
68  InitializeTime_(0.0),
69  ComputeTime_(0.0),
70  ApplyInverseTime_(0.0),
71  ComputeFlops_(0.0),
72  ApplyInverseFlops_(0.0),
73  NumSweeps_(1),
74  DampingFactor_(1.0),
75  UseTranspose_(false),
76  Condest_(-1.0),
77  ComputeCondest_(false),
78  PrecType_(IFPACK_JACOBI),
79  MinDiagonalValue_(0.0),
80  NumMyRows_(0),
81  NumMyNonzeros_(0),
82  NumGlobalRows_(0),
83  NumGlobalNonzeros_(0),
84  Matrix_(Teuchos::rcp(Matrix_in,false)),
85  IsParallel_(false),
86  ZeroStartingSolution_(true),
87  DoBackwardGS_(false),
88  DoL1Method_(false),
89  L1Eta_(1.5)
90 {
91 }
92 
93 //==============================================================================
94 int Ifpack_PointRelaxation::SetParameters(Teuchos::ParameterList& List)
95 {
96 
97  string PT;
98  if (PrecType_ == IFPACK_JACOBI)
99  PT = "Jacobi";
100  else if (PrecType_ == IFPACK_GS)
101  PT = "Gauss-Seidel";
102  else if (PrecType_ == IFPACK_SGS)
103  PT = "symmetric Gauss-Seidel";
104 
105  PT = List.get("relaxation: type", PT);
106 
107  if (PT == "Jacobi")
108  PrecType_ = IFPACK_JACOBI;
109  else if (PT == "Gauss-Seidel")
110  PrecType_ = IFPACK_GS;
111  else if (PT == "symmetric Gauss-Seidel")
112  PrecType_ = IFPACK_SGS;
113  else {
114  IFPACK_CHK_ERR(-2);
115  }
116 
117  NumSweeps_ = List.get("relaxation: sweeps",NumSweeps_);
118  DampingFactor_ = List.get("relaxation: damping factor",
119  DampingFactor_);
120  MinDiagonalValue_ = List.get("relaxation: min diagonal value",
121  MinDiagonalValue_);
122  ZeroStartingSolution_ = List.get("relaxation: zero starting solution",
123  ZeroStartingSolution_);
124 
125  DoBackwardGS_ = List.get("relaxation: backward mode",DoBackwardGS_);
126 
127  DoL1Method_ = List.get("relaxation: use l1",DoL1Method_);
128 
129  L1Eta_ = List.get("relaxation: l1 eta",L1Eta_);
130 
131  SetLabel();
132 
133  return(0);
134 }
135 
136 //==============================================================================
137 const Epetra_Comm& Ifpack_PointRelaxation::Comm() const
138 {
139  return(Matrix_->Comm());
140 }
141 
142 //==============================================================================
144 {
145  return(Matrix_->OperatorDomainMap());
146 }
147 
148 //==============================================================================
150 {
151  return(Matrix_->OperatorRangeMap());
152 }
153 
154 //==============================================================================
156 {
157  IsInitialized_ = false;
158 
159  if (Matrix_ == Teuchos::null)
160  IFPACK_CHK_ERR(-2);
161 
162  if (Time_ == Teuchos::null)
163  Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
164 
165  if (Matrix().NumGlobalRows() != Matrix().NumGlobalCols())
166  IFPACK_CHK_ERR(-2); // only square matrices
167 
168  NumMyRows_ = Matrix_->NumMyRows();
169  NumMyNonzeros_ = Matrix_->NumMyNonzeros();
170  NumGlobalRows_ = Matrix_->NumGlobalRows();
171  NumGlobalNonzeros_ = Matrix_->NumGlobalNonzeros();
172 
173  if (Comm().NumProc() != 1)
174  IsParallel_ = true;
175  else
176  IsParallel_ = false;
177 
178  ++NumInitialize_;
179  InitializeTime_ += Time_->ElapsedTime();
180  IsInitialized_ = true;
181  return(0);
182 }
183 
184 //==============================================================================
186 {
187  int ierr = 0;
188  if (!IsInitialized())
189  IFPACK_CHK_ERR(Initialize());
190 
191  Time_->ResetStartTime();
192 
193  // reset values
194  IsComputed_ = false;
195  Condest_ = -1.0;
196 
197  if (NumSweeps_ == 0) ierr = 1; // Warning: no sweeps performed.
198  if (NumSweeps_ < 0)
199  IFPACK_CHK_ERR(-2); // at least one application
200 
201  Diagonal_ = Teuchos::rcp( new Epetra_Vector(Matrix().RowMatrixRowMap()) );
202 
203  if (Diagonal_ == Teuchos::null)
204  IFPACK_CHK_ERR(-5);
205 
206  IFPACK_CHK_ERR(Matrix().ExtractDiagonalCopy(*Diagonal_));
207 
208  // Setup for L1 Methods.
209  // Here we add half the value of the off-processor entries in the row,
210  // but only if diagonal isn't sufficiently large.
211  //
212  // Note: This is only done in the slower-but-more-general "RowMatrix" mode.
213  //
214  // This follows from Equation (6.5) in:
215  // Baker, Falgout, Kolev and Yang. Multigrid Smoothers for Ultraparallel Computing.
216  // SIAM J. Sci. Comput., Vol. 33, No. 5. (2011), pp. 2864--2887.
217  if(DoL1Method_ && IsParallel_) {
218  int maxLength = Matrix().MaxNumEntries();
219  std::vector<int> Indices(maxLength);
220  std::vector<double> Values(maxLength);
221  int NumEntries;
222 
223  for (int i = 0 ; i < NumMyRows_ ; ++i) {
224  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, maxLength,NumEntries,
225  &Values[0], &Indices[0]));
226  double diagonal_boost=0.0;
227  for (int k = 0 ; k < NumEntries ; ++k)
228  if(Indices[k] > NumMyRows_)
229  diagonal_boost+=std::abs(Values[k]/2.0);
230  if ((*Diagonal_)[i] < L1Eta_*diagonal_boost)
231  (*Diagonal_)[i]+=diagonal_boost;
232  }
233  }
234 
235  // check diagonal elements, store the inverses, and verify that
236  // no zeros are around. If an element is zero, then by default
237  // its inverse is zero as well (that is, the row is ignored).
238  for (int i = 0 ; i < NumMyRows_ ; ++i) {
239  double& diag = (*Diagonal_)[i];
240  if (IFPACK_ABS(diag) < MinDiagonalValue_)
241  diag = MinDiagonalValue_;
242  if (diag != 0.0)
243  diag = 1.0 / diag;
244  }
245 #ifdef IFPACK_FLOPCOUNTERS
246  ComputeFlops_ += NumMyRows_;
247 #endif
248 
249 #if 0
250  // some methods require the inverse of the diagonal, compute it
251  // now and store it in `Diagonal_'
252  if ((PrecType_ == IFPACK_JACOBI) || (PrecType_ == IFPACK_GS)) {
253  Diagonal_->Reciprocal(*Diagonal_);
254  // update flops
255  ComputeFlops_ += NumMyRows_;
256  }
257 #endif
258 
259  // We need to import data from external processors. Here I create an
260  // Epetra_Import object because I cannot assume that Matrix_ has one.
261  // This is a bit of waste of resources (but the code is more robust).
262  // Note that I am doing some strange stuff to set the components of Y
263  // from Y2 (to save some time).
264  //
265  if (IsParallel_ && ((PrecType_ == IFPACK_GS) || (PrecType_ == IFPACK_SGS))) {
266  Importer_ = Teuchos::rcp( new Epetra_Import(Matrix().RowMatrixColMap(),
267  Matrix().RowMatrixRowMap()) );
268  if (Importer_ == Teuchos::null) IFPACK_CHK_ERR(-5);
269  }
270 
271  ++NumCompute_;
272  ComputeTime_ += Time_->ElapsedTime();
273  IsComputed_ = true;
274 
275  IFPACK_CHK_ERR(ierr);
276  return(0);
277 }
278 
279 //==============================================================================
280 ostream& Ifpack_PointRelaxation::Print(ostream & os) const
281 {
282 
283  double MyMinVal, MyMaxVal;
284  double MinVal, MaxVal;
285 
286  if (IsComputed_) {
287  Diagonal_->MinValue(&MyMinVal);
288  Diagonal_->MaxValue(&MyMaxVal);
289  Comm().MinAll(&MyMinVal,&MinVal,1);
290  Comm().MinAll(&MyMaxVal,&MaxVal,1);
291  }
292 
293  if (!Comm().MyPID()) {
294  os << endl;
295  os << "================================================================================" << endl;
296  os << "Ifpack_PointRelaxation" << endl;
297  os << "Sweeps = " << NumSweeps_ << endl;
298  os << "damping factor = " << DampingFactor_ << endl;
299  if (PrecType_ == IFPACK_JACOBI)
300  os << "Type = Jacobi" << endl;
301  else if (PrecType_ == IFPACK_GS)
302  os << "Type = Gauss-Seidel" << endl;
303  else if (PrecType_ == IFPACK_SGS)
304  os << "Type = symmetric Gauss-Seidel" << endl;
305  if (DoBackwardGS_)
306  os << "Using backward mode (GS only)" << endl;
307  if (ZeroStartingSolution_)
308  os << "Using zero starting solution" << endl;
309  else
310  os << "Using input starting solution" << endl;
311  os << "Condition number estimate = " << Condest() << endl;
312  os << "Global number of rows = " << Matrix_->NumGlobalRows() << endl;
313  if (IsComputed_) {
314  os << "Minimum value on stored diagonal = " << MinVal << endl;
315  os << "Maximum value on stored diagonal = " << MaxVal << endl;
316  }
317  os << endl;
318  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
319  os << "----- ------- -------------- ------------ --------" << endl;
320  os << "Initialize() " << std::setw(5) << NumInitialize_
321  << " " << std::setw(15) << InitializeTime_
322  << " 0.0 0.0" << endl;
323  os << "Compute() " << std::setw(5) << NumCompute_
324  << " " << std::setw(15) << ComputeTime_
325  << " " << std::setw(15) << 1.0e-6 * ComputeFlops_;
326  if (ComputeTime_ != 0.0)
327  os << " " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
328  else
329  os << " " << std::setw(15) << 0.0 << endl;
330  os << "ApplyInverse() " << std::setw(5) << NumApplyInverse_
331  << " " << std::setw(15) << ApplyInverseTime_
332  << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
333  if (ApplyInverseTime_ != 0.0)
334  os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
335  else
336  os << " " << std::setw(15) << 0.0 << endl;
337  os << "================================================================================" << endl;
338  os << endl;
339  }
340 
341  return(os);
342 }
343 
344 //==============================================================================
346 Condest(const Ifpack_CondestType CT,
347  const int MaxIters, const double Tol,
348  Epetra_RowMatrix* Matrix_in)
349 {
350  if (!IsComputed()) // cannot compute right now
351  return(-1.0);
352 
353  // always computes it. Call Condest() with no parameters to get
354  // the previous estimate.
355  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
356 
357  return(Condest_);
358 }
359 
360 //==============================================================================
361 void Ifpack_PointRelaxation::SetLabel()
362 {
363  string PT;
364  if (PrecType_ == IFPACK_JACOBI)
365  PT = "Jacobi";
366  else if (PrecType_ == IFPACK_GS){
367  PT = "GS";
368  if(DoBackwardGS_)
369  PT = "Backward " + PT;
370  }
371  else if (PrecType_ == IFPACK_SGS)
372  PT = "SGS";
373 
374  Label_ = "IFPACK (" + PT + ", sweeps=" + Ifpack_toString(NumSweeps_)
375  + ", damping=" + Ifpack_toString(DampingFactor_) + ")";
376 }
377 
378 //==============================================================================
379 // Note that Ifpack_PointRelaxation and Jacobi is much faster than
380 // Ifpack_AdditiveSchwarz<Ifpack_PointRelaxation> (because of the
381 // way the matrix-vector produce is performed).
382 //
383 // Another ML-related observation is that the starting solution (in Y)
384 // is NOT supposed to be zero. This may slow down the application of just
385 // one sweep of Jacobi.
386 //
388 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
389 {
390  if (!IsComputed())
391  IFPACK_CHK_ERR(-3);
392 
393  if (X.NumVectors() != Y.NumVectors())
394  IFPACK_CHK_ERR(-2);
395 
396  Time_->ResetStartTime();
397 
398  // AztecOO gives X and Y pointing to the same memory location,
399  // need to create an auxiliary vector, Xcopy
400  Teuchos::RefCountPtr< const Epetra_MultiVector > Xcopy;
401  if (X.Pointers()[0] == Y.Pointers()[0])
402  Xcopy = Teuchos::rcp( new Epetra_MultiVector(X) );
403  else
404  Xcopy = Teuchos::rcp( &X, false );
405 
406  if (ZeroStartingSolution_)
407  Y.PutScalar(0.0);
408 
409  // Flops are updated in each of the following.
410  switch (PrecType_) {
411  case IFPACK_JACOBI:
412  IFPACK_CHK_ERR(ApplyInverseJacobi(*Xcopy,Y));
413  break;
414  case IFPACK_GS:
415  IFPACK_CHK_ERR(ApplyInverseGS(*Xcopy,Y));
416  break;
417  case IFPACK_SGS:
418  IFPACK_CHK_ERR(ApplyInverseSGS(*Xcopy,Y));
419  break;
420  default:
421  IFPACK_CHK_ERR(-1); // something wrong
422  }
423 
424  ++NumApplyInverse_;
425  ApplyInverseTime_ += Time_->ElapsedTime();
426  return(0);
427 }
428 
429 //==============================================================================
430 // This preconditioner can be much slower than AztecOO and ML versions
431 // if the matrix-vector product requires all ExtractMyRowCopy()
432 // (as done through Ifpack_AdditiveSchwarz).
433 int Ifpack_PointRelaxation::
434 ApplyInverseJacobi(const Epetra_MultiVector& RHS, Epetra_MultiVector& LHS) const
435 {
436 
437  int NumVectors = LHS.NumVectors();
438  Epetra_MultiVector A_times_LHS( LHS.Map(),NumVectors );
439 
440  for (int j = 0; j < NumSweeps_ ; j++) {
441 
442  IFPACK_CHK_ERR(Apply(LHS,A_times_LHS));
443  IFPACK_CHK_ERR(A_times_LHS.Update(1.0,RHS,-1.0));
444  for (int v = 0 ; v < NumVectors ; ++v)
445  IFPACK_CHK_ERR(LHS(v)->Multiply(DampingFactor_, *(A_times_LHS(v)),
446  *Diagonal_, 1.0));
447 
448  }
449 
450  // Flops:
451  // - matrix vector (2 * NumGlobalNonzeros_)
452  // - update (2 * NumGlobalRows_)
453  // - Multiply:
454  // - DampingFactor (NumGlobalRows_)
455  // - Diagonal (NumGlobalRows_)
456  // - A + B (NumGlobalRows_)
457  // - 1.0 (NumGlobalRows_)
458 #ifdef IFPACK_FLOPCOUNTERS
459  ApplyInverseFlops_ += NumVectors * (6 * NumGlobalRows_ + 2 * NumGlobalNonzeros_);
460 #endif
461 
462  return(0);
463 }
464 
465 //==============================================================================
466 int Ifpack_PointRelaxation::
467 ApplyInverseGS(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
468 {
469  const Epetra_CrsMatrix* CrsMatrix = dynamic_cast<const Epetra_CrsMatrix*>(&*Matrix_);
470  // try to pick the best option; performances may be improved
471  // if several sweeps are used.
472  if (CrsMatrix != 0)
473  {
474  if (CrsMatrix->StorageOptimized())
475  return(ApplyInverseGS_FastCrsMatrix(CrsMatrix, X, Y));
476  else
477  return(ApplyInverseGS_CrsMatrix(CrsMatrix, X, Y));
478  }
479  else
480  return(ApplyInverseGS_RowMatrix(X, Y));
481 } //ApplyInverseGS()
482 
483 //==============================================================================
484 int Ifpack_PointRelaxation::
485 ApplyInverseGS_RowMatrix(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
486 {
487  int NumVectors = X.NumVectors();
488 
489  int Length = Matrix().MaxNumEntries();
490  vector<int> Indices(Length);
491  vector<double> Values(Length);
492 
493  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
494  if (IsParallel_)
495  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
496  else
497  Y2 = Teuchos::rcp( &Y, false );
498 
499  // extract views (for nicer and faster code)
500  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
501  X.ExtractView(&x_ptr);
502  Y.ExtractView(&y_ptr);
503  Y2->ExtractView(&y2_ptr);
504  Diagonal_->ExtractView(&d_ptr);
505 
506  for (int j = 0; j < NumSweeps_ ; j++) {
507 
508  // data exchange is here, once per sweep
509  if (IsParallel_)
510  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
511 
512  // FIXME: do I really need this code below?
513  if (NumVectors == 1) {
514 
515  double* y0_ptr = y_ptr[0];
516  double* y20_ptr = y2_ptr[0];
517  double* x0_ptr = x_ptr[0];
518 
519  if(!DoBackwardGS_){
520  /* Forward Mode */
521  for (int i = 0 ; i < NumMyRows_ ; ++i) {
522 
523  int NumEntries;
524  int col;
525  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
526  &Values[0], &Indices[0]));
527 
528  double dtemp = 0.0;
529  for (int k = 0 ; k < NumEntries ; ++k) {
530 
531  col = Indices[k];
532  dtemp += Values[k] * y20_ptr[col];
533  }
534 
535  y20_ptr[i] += DampingFactor_ * d_ptr[i] * (x0_ptr[i] - dtemp);
536  }
537  }
538  else {
539  /* Backward Mode */
540  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
541 
542  int NumEntries;
543  int col;
544  (void) col; // Forestall compiler warning for unused variable.
545  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
546  &Values[0], &Indices[0]));
547  double dtemp = 0.0;
548  for (int k = 0 ; k < NumEntries ; ++k) {
549 
550  col = Indices[k];
551  dtemp += Values[k] * y20_ptr[i];
552  }
553 
554  y20_ptr[i] += DampingFactor_ * d_ptr[i] * (x0_ptr[i] - dtemp);
555  }
556  }
557 
558  // using Export() sounded quite expensive
559  if (IsParallel_)
560  for (int i = 0 ; i < NumMyRows_ ; ++i)
561  y0_ptr[i] = y20_ptr[i];
562 
563  }
564  else {
565  if(!DoBackwardGS_){
566  /* Forward Mode */
567  for (int i = 0 ; i < NumMyRows_ ; ++i) {
568 
569  int NumEntries;
570  int col;
571  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
572  &Values[0], &Indices[0]));
573 
574  for (int m = 0 ; m < NumVectors ; ++m) {
575 
576  double dtemp = 0.0;
577  for (int k = 0 ; k < NumEntries ; ++k) {
578 
579  col = Indices[k];
580  dtemp += Values[k] * y2_ptr[m][col];
581  }
582 
583  y2_ptr[m][i] += DampingFactor_ * d_ptr[i] * (x_ptr[m][i] - dtemp);
584  }
585  }
586  }
587  else {
588  /* Backward Mode */
589  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
590  int NumEntries;
591  int col;
592  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
593  &Values[0], &Indices[0]));
594 
595  for (int m = 0 ; m < NumVectors ; ++m) {
596 
597  double dtemp = 0.0;
598  for (int k = 0 ; k < NumEntries ; ++k) {
599 
600  col = Indices[k];
601  dtemp += Values[k] * y2_ptr[m][col];
602  }
603 
604  y2_ptr[m][i] += DampingFactor_ * d_ptr[i] * (x_ptr[m][i] - dtemp);
605 
606  }
607  }
608  }
609 
610  // using Export() sounded quite expensive
611  if (IsParallel_)
612  for (int m = 0 ; m < NumVectors ; ++m)
613  for (int i = 0 ; i < NumMyRows_ ; ++i)
614  y_ptr[m][i] = y2_ptr[m][i];
615 
616  }
617  }
618 
619 #ifdef IFPACK_FLOPCOUNTERS
620  ApplyInverseFlops_ += NumVectors * (4 * NumGlobalRows_ + 2 * NumGlobalNonzeros_);
621 #endif
622 
623  return(0);
624 } //ApplyInverseGS_RowMatrix()
625 
626 //==============================================================================
627 int Ifpack_PointRelaxation::
628 ApplyInverseGS_CrsMatrix(const Epetra_CrsMatrix* A, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
629 {
630  int NumVectors = X.NumVectors();
631 
632  int* Indices;
633  double* Values;
634 
635  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
636  if (IsParallel_) {
637  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
638  }
639  else
640  Y2 = Teuchos::rcp( &Y, false );
641 
642  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
643  X.ExtractView(&x_ptr);
644  Y.ExtractView(&y_ptr);
645  Y2->ExtractView(&y2_ptr);
646  Diagonal_->ExtractView(&d_ptr);
647 
648  for (int iter = 0 ; iter < NumSweeps_ ; ++iter) {
649 
650  // only one data exchange per sweep
651  if (IsParallel_)
652  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
653 
654  if(!DoBackwardGS_){
655  /* Forward Mode */
656  for (int i = 0 ; i < NumMyRows_ ; ++i) {
657 
658  int NumEntries;
659  int col;
660  double diag = d_ptr[i];
661 
662  IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
663 
664  for (int m = 0 ; m < NumVectors ; ++m) {
665 
666  double dtemp = 0.0;
667 
668  for (int k = 0; k < NumEntries; ++k) {
669 
670  col = Indices[k];
671  dtemp += Values[k] * y2_ptr[m][col];
672  }
673 
674  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
675  }
676  }
677  }
678  else {
679  /* Backward Mode */
680  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
681 
682  int NumEntries;
683  int col;
684  double diag = d_ptr[i];
685 
686  IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
687 
688  for (int m = 0 ; m < NumVectors ; ++m) {
689 
690  double dtemp = 0.0;
691  for (int k = 0; k < NumEntries; ++k) {
692 
693  col = Indices[k];
694  dtemp += Values[k] * y2_ptr[m][col];
695  }
696 
697  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
698 
699  }
700  }
701  }
702 
703  if (IsParallel_)
704  for (int m = 0 ; m < NumVectors ; ++m)
705  for (int i = 0 ; i < NumMyRows_ ; ++i)
706  y_ptr[m][i] = y2_ptr[m][i];
707  }
708 
709 #ifdef IFPACK_FLOPCOUNTERS
710  ApplyInverseFlops_ += NumVectors * (8 * NumGlobalRows_ + 4 * NumGlobalNonzeros_);
711 #endif
712  return(0);
713 } //ApplyInverseGS_CrsMatrix()
714 
715 //
716 //==============================================================================
717 // ApplyInverseGS_FastCrsMatrix() requires Epetra_CrsMatrix + OptimizeStorage()
718 
719 int Ifpack_PointRelaxation::
720 ApplyInverseGS_FastCrsMatrix(const Epetra_CrsMatrix* A, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
721 {
722  int* IndexOffset;
723  int* Indices;
724  double* Values;
725  IFPACK_CHK_ERR(A->ExtractCrsDataPointers(IndexOffset, Indices, Values));
726 
727  int NumVectors = X.NumVectors();
728 
729  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
730  if (IsParallel_) {
731  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
732  }
733  else
734  Y2 = Teuchos::rcp( &Y, false );
735 
736  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
737  X.ExtractView(&x_ptr);
738  Y.ExtractView(&y_ptr);
739  Y2->ExtractView(&y2_ptr);
740  Diagonal_->ExtractView(&d_ptr);
741 
742  for (int iter = 0 ; iter < NumSweeps_ ; ++iter) {
743 
744  // only one data exchange per sweep
745  if (IsParallel_)
746  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
747 
748  if(!DoBackwardGS_){
749  /* Forward Mode */
750  for (int i = 0 ; i < NumMyRows_ ; ++i) {
751 
752  int col;
753  double diag = d_ptr[i];
754 
755  for (int m = 0 ; m < NumVectors ; ++m) {
756 
757  double dtemp = 0.0;
758 
759  for (int k = IndexOffset[i] ; k < IndexOffset[i + 1] ; ++k) {
760 
761  col = Indices[k];
762  dtemp += Values[k] * y2_ptr[m][col];
763  }
764 
765  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
766  }
767  }
768  }
769  else {
770  /* Backward Mode */
771  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
772 
773  int col;
774  double diag = d_ptr[i];
775 
776  for (int m = 0 ; m < NumVectors ; ++m) {
777 
778  double dtemp = 0.0;
779  for (int k = IndexOffset[i] ; k < IndexOffset[i + 1] ; ++k) {
780 
781  col = Indices[k];
782  dtemp += Values[k] * y2_ptr[m][col];
783  }
784 
785  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
786 
787  }
788  }
789  }
790 
791 
792  if (IsParallel_)
793  for (int m = 0 ; m < NumVectors ; ++m)
794  for (int i = 0 ; i < NumMyRows_ ; ++i)
795  y_ptr[m][i] = y2_ptr[m][i];
796  }
797 
798 #ifdef IFPACK_FLOPCOUNTERS
799  ApplyInverseFlops_ += NumVectors * (8 * NumGlobalRows_ + 4 * NumGlobalNonzeros_);
800 #endif
801  return(0);
802 } //ApplyInverseGS_FastCrsMatrix()
803 
804 //==============================================================================
805 int Ifpack_PointRelaxation::
806 ApplyInverseSGS(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
807 {
808  const Epetra_CrsMatrix* CrsMatrix = dynamic_cast<const Epetra_CrsMatrix*>(&*Matrix_);
809  // try to pick the best option; performances may be improved
810  // if several sweeps are used.
811  if (CrsMatrix != 0)
812  {
813  if (CrsMatrix->StorageOptimized())
814  return(ApplyInverseSGS_FastCrsMatrix(CrsMatrix, X, Y));
815  else
816  return(ApplyInverseSGS_CrsMatrix(CrsMatrix, X, Y));
817  }
818  else
819  return(ApplyInverseSGS_RowMatrix(X, Y));
820 }
821 
822 //==============================================================================
823 int Ifpack_PointRelaxation::
824 ApplyInverseSGS_RowMatrix(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
825 {
826  int NumVectors = X.NumVectors();
827  int Length = Matrix().MaxNumEntries();
828  vector<int> Indices(Length);
829  vector<double> Values(Length);
830 
831  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
832  if (IsParallel_) {
833  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
834  }
835  else
836  Y2 = Teuchos::rcp( &Y, false );
837 
838  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
839  X.ExtractView(&x_ptr);
840  Y.ExtractView(&y_ptr);
841  Y2->ExtractView(&y2_ptr);
842  Diagonal_->ExtractView(&d_ptr);
843 
844  for (int iter = 0 ; iter < NumSweeps_ ; ++iter) {
845 
846  // only one data exchange per sweep
847  if (IsParallel_)
848  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
849 
850  for (int i = 0 ; i < NumMyRows_ ; ++i) {
851 
852  int NumEntries;
853  int col;
854  double diag = d_ptr[i];
855 
856  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
857  &Values[0], &Indices[0]));
858 
859  for (int m = 0 ; m < NumVectors ; ++m) {
860 
861  double dtemp = 0.0;
862 
863  for (int k = 0 ; k < NumEntries ; ++k) {
864 
865  col = Indices[k];
866  dtemp += Values[k] * y2_ptr[m][col];
867  }
868 
869  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
870  }
871  }
872 
873  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
874 
875  int NumEntries;
876  int col;
877  double diag = d_ptr[i];
878 
879  IFPACK_CHK_ERR(Matrix_->ExtractMyRowCopy(i, Length,NumEntries,
880  &Values[0], &Indices[0]));
881 
882  for (int m = 0 ; m < NumVectors ; ++m) {
883 
884  double dtemp = 0.0;
885  for (int k = 0 ; k < NumEntries ; ++k) {
886 
887  col = Indices[k];
888  dtemp += Values[k] * y2_ptr[m][col];
889  }
890 
891  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
892 
893  }
894  }
895 
896  if (IsParallel_)
897  for (int m = 0 ; m < NumVectors ; ++m)
898  for (int i = 0 ; i < NumMyRows_ ; ++i)
899  y_ptr[m][i] = y2_ptr[m][i];
900  }
901 
902 #ifdef IFPACK_FLOPCOUNTERS
903  ApplyInverseFlops_ += NumVectors * (8 * NumGlobalRows_ + 4 * NumGlobalNonzeros_);
904 #endif
905  return(0);
906 }
907 
908 //==============================================================================
909 int Ifpack_PointRelaxation::
910 ApplyInverseSGS_CrsMatrix(const Epetra_CrsMatrix* A, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
911 {
912  int NumVectors = X.NumVectors();
913 
914  int* Indices;
915  double* Values;
916 
917  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
918  if (IsParallel_) {
919  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
920  }
921  else
922  Y2 = Teuchos::rcp( &Y, false );
923 
924  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
925  X.ExtractView(&x_ptr);
926  Y.ExtractView(&y_ptr);
927  Y2->ExtractView(&y2_ptr);
928  Diagonal_->ExtractView(&d_ptr);
929 
930  for (int iter = 0 ; iter < NumSweeps_ ; ++iter) {
931 
932  // only one data exchange per sweep
933  if (IsParallel_)
934  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
935 
936  for (int i = 0 ; i < NumMyRows_ ; ++i) {
937 
938  int NumEntries;
939  int col;
940  double diag = d_ptr[i];
941 
942  IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
943 
944  for (int m = 0 ; m < NumVectors ; ++m) {
945 
946  double dtemp = 0.0;
947 
948  for (int k = 0; k < NumEntries; ++k) {
949 
950  col = Indices[k];
951  dtemp += Values[k] * y2_ptr[m][col];
952  }
953 
954  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
955  }
956  }
957 
958  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
959 
960  int NumEntries;
961  int col;
962  double diag = d_ptr[i];
963 
964  IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
965 
966  for (int m = 0 ; m < NumVectors ; ++m) {
967 
968  double dtemp = 0.0;
969  for (int k = 0; k < NumEntries; ++k) {
970 
971  col = Indices[k];
972  dtemp += Values[k] * y2_ptr[m][col];
973  }
974 
975  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
976 
977  }
978  }
979 
980  if (IsParallel_)
981  for (int m = 0 ; m < NumVectors ; ++m)
982  for (int i = 0 ; i < NumMyRows_ ; ++i)
983  y_ptr[m][i] = y2_ptr[m][i];
984  }
985 
986 #ifdef IFPACK_FLOPCOUNTERS
987  ApplyInverseFlops_ += NumVectors * (8 * NumGlobalRows_ + 4 * NumGlobalNonzeros_);
988 #endif
989  return(0);
990 }
991 
992 //==============================================================================
993 // this requires Epetra_CrsMatrix + OptimizeStorage()
994 int Ifpack_PointRelaxation::
995 ApplyInverseSGS_FastCrsMatrix(const Epetra_CrsMatrix* A, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
996 {
997  int* IndexOffset;
998  int* Indices;
999  double* Values;
1000  IFPACK_CHK_ERR(A->ExtractCrsDataPointers(IndexOffset, Indices, Values));
1001 
1002  int NumVectors = X.NumVectors();
1003 
1004  Teuchos::RefCountPtr< Epetra_MultiVector > Y2;
1005  if (IsParallel_) {
1006  Y2 = Teuchos::rcp( new Epetra_MultiVector(Importer_->TargetMap(), NumVectors) );
1007  }
1008  else
1009  Y2 = Teuchos::rcp( &Y, false );
1010 
1011  double** y_ptr, ** y2_ptr, ** x_ptr, *d_ptr;
1012  X.ExtractView(&x_ptr);
1013  Y.ExtractView(&y_ptr);
1014  Y2->ExtractView(&y2_ptr);
1015  Diagonal_->ExtractView(&d_ptr);
1016 
1017  for (int iter = 0 ; iter < NumSweeps_ ; ++iter) {
1018 
1019  // only one data exchange per sweep
1020  if (IsParallel_)
1021  IFPACK_CHK_ERR(Y2->Import(Y,*Importer_,Insert));
1022 
1023  for (int i = 0 ; i < NumMyRows_ ; ++i) {
1024 
1025  int col;
1026  double diag = d_ptr[i];
1027 
1028  // no need to extract row i
1029  //IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
1030 
1031  for (int m = 0 ; m < NumVectors ; ++m) {
1032 
1033  double dtemp = 0.0;
1034 
1035  for (int k = IndexOffset[i] ; k < IndexOffset[i + 1] ; ++k) {
1036 
1037  col = Indices[k];
1038  dtemp += Values[k] * y2_ptr[m][col];
1039  }
1040 
1041  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
1042  }
1043  }
1044 
1045  for (int i = NumMyRows_ - 1 ; i > -1 ; --i) {
1046 
1047  int col;
1048  double diag = d_ptr[i];
1049 
1050  // no need to extract row i
1051  //IFPACK_CHK_ERR(A->ExtractMyRowView(i, NumEntries, Values, Indices));
1052 
1053  for (int m = 0 ; m < NumVectors ; ++m) {
1054 
1055  double dtemp = 0.0;
1056  for (int k = IndexOffset[i] ; k < IndexOffset[i + 1] ; ++k) {
1057 
1058  col = Indices[k];
1059  dtemp += Values[k] * y2_ptr[m][col];
1060  }
1061 
1062  y2_ptr[m][i] += DampingFactor_ * (x_ptr[m][i] - dtemp) * diag;
1063 
1064  }
1065  }
1066 
1067  if (IsParallel_)
1068  for (int m = 0 ; m < NumVectors ; ++m)
1069  for (int i = 0 ; i < NumMyRows_ ; ++i)
1070  y_ptr[m][i] = y2_ptr[m][i];
1071  }
1072 
1073 #ifdef IFPACK_FLOPCOUNTERS
1074  ApplyInverseFlops_ += NumVectors * (8 * NumGlobalRows_ + 4 * NumGlobalNonzeros_);
1075 #endif
1076  return(0);
1077 }
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the matrix to an Epetra_MultiVector.
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
string Ifpack_toString(const int &x)
Convertes an integer to string.
Ifpack_PointRelaxation(const Epetra_RowMatrix *Matrix)
Ifpack_PointRelaxation constructor with given Epetra_RowMatrix.
virtual double Condest() const
Returns the condition number estimate, or -1.0 if not computed.
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.
virtual int Compute()
Computes the preconditioners.
virtual int Initialize()
Computes all it is necessary to initialize the preconditioner.
virtual const Epetra_RowMatrix & Matrix() const
Returns a pointer to the matrix to be preconditioned.
virtual bool IsComputed() const
Returns true if the preconditioner has been successfully computed.
virtual ostream & Print(ostream &os) const
Prints object to an output stream.
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.