IFPACK  Development
Ifpack_Amesos.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 "Ifpack_Preconditioner.h"
45 #include "Ifpack_Amesos.h"
46 #include "Ifpack_Condest.h"
47 #include "Epetra_MultiVector.h"
48 #include "Epetra_Map.h"
49 #include "Epetra_Comm.h"
50 #include "Amesos.h"
51 #include "Epetra_LinearProblem.h"
52 #include "Epetra_RowMatrix.h"
53 #include "Epetra_Time.h"
54 #include "Teuchos_ParameterList.hpp"
55 
56 static bool FirstTime = true;
57 
58 //==============================================================================
59 Ifpack_Amesos::Ifpack_Amesos(Epetra_RowMatrix* Matrix_in) :
60  Matrix_(Teuchos::rcp( Matrix_in, false )),
61  Label_("Amesos_Klu"),
62  IsEmpty_(false),
63  IsInitialized_(false),
64  IsComputed_(false),
65  UseTranspose_(false),
66  NumInitialize_(0),
67  NumCompute_(0),
68  NumApplyInverse_(0),
69  InitializeTime_(0.0),
70  ComputeTime_(0.0),
71  ApplyInverseTime_(0.0),
72  ComputeFlops_(0),
73  ApplyInverseFlops_(0),
74  Condest_(-1.0)
75 {
76  Problem_ = Teuchos::rcp( new Epetra_LinearProblem );
77 }
78 
79 //==============================================================================
81  Matrix_(Teuchos::rcp( &rhs.Matrix(), false )),
82  Label_(rhs.Label()),
83  IsEmpty_(false),
84  IsInitialized_(false),
85  IsComputed_(false),
86  NumInitialize_(rhs.NumInitialize()),
87  NumCompute_(rhs.NumCompute()),
88  NumApplyInverse_(rhs.NumApplyInverse()),
89  InitializeTime_(rhs.InitializeTime()),
90  ComputeTime_(rhs.ComputeTime()),
91  ApplyInverseTime_(rhs.ApplyInverseTime()),
92  ComputeFlops_(rhs.ComputeFlops()),
93  ApplyInverseFlops_(rhs.ApplyInverseFlops()),
94  Condest_(rhs.Condest())
95 {
96 
97  Problem_ = Teuchos::rcp( new Epetra_LinearProblem );
98 
99  // copy the RHS list in *this.List
100  Teuchos::ParameterList RHSList(rhs.List());
101  List_ = RHSList;
102 
103  // I do not have a copy constructor for Amesos,
104  // so Initialize() and Compute() of this object
105  // are called if the rhs did so
106  if (rhs.IsInitialized()) {
107  IsInitialized_ = true;
108  Initialize();
109  }
110  if (rhs.IsComputed()) {
111  IsComputed_ = true;
112  Compute();
113  }
114 
115 }
116 //==============================================================================
117 int Ifpack_Amesos::SetParameters(Teuchos::ParameterList& List_in)
118 {
119 
120  List_ = List_in;
121  Label_ = List_in.get("amesos: solver type", Label_);
122  return(0);
123 }
124 
125 //==============================================================================
127 {
128 
129  IsEmpty_ = false;
130  IsInitialized_ = false;
131  IsComputed_ = false;
132 
133  if (Matrix_ == Teuchos::null)
134  IFPACK_CHK_ERR(-1);
135 
136 #if 0
137  // better to avoid strange games with maps, this class should be
138  // used for Ifpack_LocalFilter'd matrices only
139  if (Comm().NumProc() != 1) {
140  cout << "Class Ifpack_Amesos must be used for serial runs;" << endl;
141  cout << "for parallel runs you should declare objects as:" << endl;
142  cout << "Ifpack_AdditiveSchwarz<Ifpack_Amesos> APrec(Matrix)" << endl;
143  exit(EXIT_FAILURE);
144  }
145 #endif
146 
147  // only square matrices
148  if (Matrix_->NumGlobalRows() != Matrix_->NumGlobalCols())
149  IFPACK_CHK_ERR(-1);
150 
151  // if the matrix has a dimension of 0, this is an empty preconditioning object.
152  if (Matrix_->NumGlobalRows() == 0) {
153  IsEmpty_ = true;
154  IsInitialized_ = true;
155  ++NumInitialize_;
156  return(0);
157  }
158 
159  Problem_->SetOperator(const_cast<Epetra_RowMatrix*>(Matrix_.get()));
160 
161  // create timer, which also starts it.
162  if (Time_ == Teuchos::null)
163  Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
164 
165  Amesos Factory;
166  Solver_ = Teuchos::rcp( Factory.Create((char*)Label_.c_str(),*Problem_) );
167 
168  if (Solver_ == Teuchos::null)
169  {
170  // try to create KLU, it is generally enabled
171  Solver_ = Teuchos::rcp( Factory.Create("Amesos_Klu",*Problem_) );
172  }
173  if (Solver_ == Teuchos::null)
174  {
175  // finally try to create LAPACK, it is generally enabled
176  // NOTE: the matrix is dense, so this should only be for
177  // small problems...
178  if (FirstTime)
179  {
180  cerr << "IFPACK WARNING: In class Ifpack_Amesos:" << endl;
181  cerr << "IFPACK WARNING: Using LAPACK because other Amesos" << endl;
182  cerr << "IFPACK WARNING: solvers are not available. LAPACK" << endl;
183  cerr << "IFPACK WARNING: allocates memory to store the matrix as" << endl;
184  cerr << "IFPACK WARNING: dense, I hope you have enough memory..." << endl;
185  cerr << "IFPACK WARNING: (file " << __FILE__ << ", line " << __LINE__
186  << ")" << endl;
187  FirstTime = false;
188  }
189  Solver_ = Teuchos::rcp( Factory.Create("Amesos_Lapack",*Problem_) );
190  }
191  // if empty, give up.
192  if (Solver_ == Teuchos::null)
193  IFPACK_CHK_ERR(-1);
194 
195  IFPACK_CHK_ERR(Solver_->SetUseTranspose(UseTranspose_));
196  Solver_->SetParameters(List_);
197  IFPACK_CHK_ERR(Solver_->SymbolicFactorization());
198 
199  IsInitialized_ = true;
200  ++NumInitialize_;
201  InitializeTime_ += Time_->ElapsedTime();
202  return(0);
203 }
204 
205 //==============================================================================
207 {
208 
209  if (!IsInitialized())
210  IFPACK_CHK_ERR(Initialize());
211 
212  if (IsEmpty_) {
213  IsComputed_ = true;
214  ++NumCompute_;
215  return(0);
216  }
217 
218  IsComputed_ = false;
219  Time_->ResetStartTime();
220 
221  if (Matrix_ == Teuchos::null)
222  IFPACK_CHK_ERR(-1);
223 
224  IFPACK_CHK_ERR(Solver_->NumericFactorization());
225 
226  IsComputed_ = true;
227  ++NumCompute_;
228  ComputeTime_ += Time_->ElapsedTime();
229  return(0);
230 }
231 
232 //==============================================================================
233 int Ifpack_Amesos::SetUseTranspose(bool UseTranspose_in)
234 {
235  // store the value in UseTranspose_. If we have the solver, we pass to it
236  // right away, otherwise we wait till when it is created.
237  UseTranspose_ = UseTranspose_in;
238  if (Solver_ != Teuchos::null)
239  IFPACK_CHK_ERR(Solver_->SetUseTranspose(UseTranspose_in));
240 
241  return(0);
242 }
243 
244 //==============================================================================
245 int Ifpack_Amesos::
246 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
247 {
248  // check for maps ???
249  IFPACK_CHK_ERR(Matrix_->Apply(X,Y));
250  return(0);
251 }
252 
253 //==============================================================================
254 int Ifpack_Amesos::
255 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
256 {
257  if (IsEmpty_) {
258  ++NumApplyInverse_;
259  return(0);
260  }
261 
262  if (IsComputed() == false)
263  IFPACK_CHK_ERR(-1);
264 
265  if (X.NumVectors() != Y.NumVectors())
266  IFPACK_CHK_ERR(-1); // wrong input
267 
268  Time_->ResetStartTime();
269 
270  // AztecOO gives X and Y pointing to the same memory location,
271  // need to create an auxiliary vector, Xcopy
272  Teuchos::RefCountPtr<const Epetra_MultiVector> Xcopy;
273  if (X.Pointers()[0] == Y.Pointers()[0])
274  Xcopy = Teuchos::rcp( new Epetra_MultiVector(X) );
275  else
276  Xcopy = Teuchos::rcp( &X, false );
277 
278  Problem_->SetLHS(&Y);
279  Problem_->SetRHS((Epetra_MultiVector*)Xcopy.get());
280  IFPACK_CHK_ERR(Solver_->Solve());
281 
282  ++NumApplyInverse_;
283  ApplyInverseTime_ += Time_->ElapsedTime();
284 
285  return(0);
286 }
287 
288 //==============================================================================
290 {
291  return(-1.0);
292 }
293 
294 //==============================================================================
295 const char* Ifpack_Amesos::Label() const
296 {
297  return((char*)Label_.c_str());
298 }
299 
300 //==============================================================================
302 {
303  return(UseTranspose_);
304 }
305 
306 //==============================================================================
308 {
309  return(false);
310 }
311 
312 //==============================================================================
313 const Epetra_Comm & Ifpack_Amesos::Comm() const
314 {
315  return(Matrix_->Comm());
316 }
317 
318 //==============================================================================
319 const Epetra_Map & Ifpack_Amesos::OperatorDomainMap() const
320 {
321  return(Matrix_->OperatorDomainMap());
322 }
323 
324 //==============================================================================
325 const Epetra_Map & Ifpack_Amesos::OperatorRangeMap() const
326 {
327  return(Matrix_->OperatorRangeMap());
328 }
329 
330 //==============================================================================
331 double Ifpack_Amesos::Condest(const Ifpack_CondestType CT,
332  const int MaxIters, const double Tol,
333  Epetra_RowMatrix* Matrix_in)
334 {
335 
336  if (!IsComputed()) // cannot compute right now
337  return(-1.0);
338 
339  if (Condest_ == -1.0)
340  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
341 
342  return(Condest_);
343 }
344 
345 //==============================================================================
346 std::ostream& Ifpack_Amesos::Print(std::ostream& os) const
347 {
348  if (!Comm().MyPID()) {
349  os << endl;
350  os << "================================================================================" << endl;
351  os << "Ifpack_Amesos: " << Label () << endl << endl;
352  os << "Condition number estimate = " << Condest() << endl;
353  os << "Global number of rows = " << Matrix_->NumGlobalRows() << endl;
354  os << endl;
355  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
356  os << "----- ------- -------------- ------------ --------" << endl;
357  os << "Initialize() " << std::setw(5) << NumInitialize_
358  << " " << std::setw(15) << InitializeTime_
359  << " 0.0 0.0" << endl;
360  os << "Compute() " << std::setw(5) << NumCompute_
361  << " " << std::setw(15) << ComputeTime_
362  << " " << std::setw(15) << 1.0e-6 * ComputeFlops_;
363  if (ComputeTime_ != 0.0)
364  os << " " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
365  else
366  os << " " << std::setw(15) << 0.0 << endl;
367  os << "ApplyInverse() " << std::setw(5) << NumApplyInverse_
368  << " " << std::setw(15) << ApplyInverseTime_
369  << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
370  if (ApplyInverseTime_ != 0.0)
371  os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
372  else
373  os << " " << std::setw(15) << 0.0 << endl;
374  os << "================================================================================" << endl;
375  os << endl;
376  }
377 
378  return(os);
379 }
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
virtual double ComputeTime() const
Returns the total time spent in Compute().
virtual int NumInitialize() const
Returns the number of calls to Initialize().
virtual double ApplyInverseTime() const
Returns the total time spent in ApplyInverse().
virtual int SetParameters(Teuchos::ParameterList &List)
Sets all the parameters for the preconditioner.
virtual double ComputeFlops() const
Returns the total number of flops to computate the preconditioner.
virtual int NumCompute() const
Returns the number of calls to Compute().
Ifpack_Amesos(Epetra_RowMatrix *Matrix)
Constructor.
virtual double ApplyInverseFlops() const
Returns the total number of flops to apply the preconditioner.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the matrix to an Epetra_MultiVector.
virtual const Epetra_RowMatrix & Matrix() const
Returns a const reference to the internally stored matrix.
virtual bool IsComputed() const
Returns true if the preconditioner has been successfully computed.
Ifpack_Amesos: a class to use Amesos&#39; factorizations as preconditioners.
Definition: Ifpack_Amesos.h:81
virtual int Compute()
Computes the preconditioners.
virtual int Initialize()
Initializes the preconditioners.
virtual int SetUseTranspose(bool UseTranspose_in)
If set true, transpose of this operator will be applied (not implemented).
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
virtual const char * Label() const
Returns a character string describing the operator.
virtual double Condest() const
Returns the estimated condition number, never computes it.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the preconditioner to X, returns the result in Y.
virtual double InitializeTime() const
Returns the total time spent in Initialize().
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
virtual bool HasNormInf() const
Returns true if the this object can provide an approximate Inf-norm, false otherwise.
virtual bool IsInitialized() const
Returns true is the preconditioner has been successfully initialized.
virtual double NormInf() const
Returns the infinity norm of the global matrix (not implemented)
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
virtual std::ostream & Print(std::ostream &os) const
Prints on ostream basic information about this object.