LifeV
SolverAmesos.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Solver Amesos
30 
31  @author Gilles Fourestey <gilles.fourestey@epfl.ch>
32  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
33  @maintainer Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
34 
35  @date 29-08-2004
36  */
37 
38 #include <lifev/core/algorithm/SolverAmesos.hpp>
39 #include <lifev/core/util/LifeDebug.hpp>
40 #include <lifev/core/util/LifeChrono.hpp>
41 #include <lifev/core/filter/GetPot.hpp>
42 
43 namespace LifeV
44 {
45 
46 // ===================================================
47 // Constructors & Destructor
48 // ===================================================
49 SolverAmesos::SolverAmesos ( const commPtr_Type& comm ) :
50  M_matrix (),
51  M_problem (),
52  M_solver (),
54  M_displayer ( comm )
55 {
56 }
57 
58 // ===================================================
59 // Methods
60 // ===================================================
61 Real
62 SolverAmesos::computeResidual ( const vector_type& solution, const vector_type& rhs )
63 {
64  vector_type Ax (solution.map() );
65  vector_type res (rhs);
66 
67  res.epetraVector().Update (1, Ax.epetraVector(), -1);
68 
69  Real residual;
70 
71  res.norm2 (&residual);
72 
73  return residual;
74 }
75 
76 Int
78  vector_type& solution,
79  const matrix_ptrtype& /*basePrecMatrix*/ )
80 {
81  bool verbose = M_trilinosParameterList.get ( "Verbose", true );
82  if ( verbose )
83  {
84  M_displayer.leaderPrint ( "SLV- Amesos solving system ... " );
85  }
86 
87  LifeChrono chrono;
88  chrono.start();
89 
90  M_problem.SetLHS ( &solution.epetraVector() );
91  M_problem.SetRHS ( &rhsFull.epetraVector() );
92 
93  AMESOS_CHK_ERR ( M_solver->Solve() );
94 
95  chrono.stop();
96 
97  if ( verbose )
98  {
99  M_displayer.leaderPrintMax ( "done in " , chrono.diff() );
100  }
101 
102  return 0;
103 }
104 
105 void
107 {
108  /*
109  // 1) The symbolic factorization
110  // (parameter doesn't always exist)
111  std::cout << " Amesos: Total symbolic factorization time " << M_sfact_time << std::endl;
112 
113  // 2) The numeric factorization
114  // (always exists if NumericFactorization() is called)
115  std::cout << " Amesos: Total numeric factorization time " << M_nfact_time << std::endl;
116  // 3) Solving the linear system
117  // (always exists if Solve() is called)
118  std::cout << " Amesos: Total solve time " << M_solve_time << std::endl;
119 
120  // 4) Converting the matrix to the accepted format for the solver
121  // (always exists if SymbolicFactorization() is called)
122  std::cout << " Amesos: matrix convertion time " << M_mtx_conv_time << std::endl;
123 
124  // 5) Redistributing the matrix for each solve to the accepted format for the solver
125  std::cout << " Amesos: Total matrix redistribution time " << M_mtx_redist_time << std::endl;
126 
127  // 6) Redistributing the vector for each solve to the accepted format for the solver
128  std::cout << " Amesos: Total vector redistribution time " << M_vec_redist_time << std::endl;
129  */
130 
131  if ( M_trilinosParameterList.get ( "PrintTiming", false ) )
132  {
133  M_solver->PrintTiming();
134  }
135 
136  if ( M_trilinosParameterList.get ( "PrintStatus", false ) )
137  {
138  M_solver->PrintStatus();
139  }
140 }
141 
143 {
144  return true;
145 }
146 
148 {
149 
150 }
151 
152 void SolverAmesos::setupPreconditioner ( const GetPot& /*dataFile*/, const std::string& /*section*/ )
153 {
154 
155 }
156 
157 void SolverAmesos::setReusePreconditioner ( const bool& /*reusePreconditioner*/ )
158 {
159 
160 }
161 
162 void SolverAmesos::showMe ( std::ostream& output ) const
163 {
164  M_trilinosParameterList.print ( output );
165 }
166 
167 // ===================================================
168 // Set Methods
169 // ===================================================
171 {
172  M_matrix = matrix.matrixPtr();
173  M_problem.SetOperator ( M_matrix.get() );
174 
175  // After setting the matrix we can perform symbolic & numeric factorization
176  AMESOS_CHK_ERR ( M_solver->SymbolicFactorization() );
177  AMESOS_CHK_ERR ( M_solver->NumericFactorization() );
178 
179  return 0;
180 }
181 
182 void SolverAmesos::setOperator ( const Epetra_Operator& /*oper*/ )
183 {
184  ASSERT ( false, "SolverAmesos::setOperator: not coded" );
185 }
186 
187 void SolverAmesos::setDataFromGetPot ( const GetPot& dataFile, const std::string& section )
188 {
189  // Status parameters
190  M_trilinosParameterList.set ( "OutputLevel", dataFile ( ( section + "/amesos/outputlevel").data(), 0 ) );
191  M_trilinosParameterList.set ( "PrintStatus", dataFile ( ( section + "/amesos/print_status").data(), false ) );
192  M_trilinosParameterList.set ( "PrintTiming", dataFile ( ( section + "/amesos/print_timing").data(), false ) );
193  M_trilinosParameterList.set ( "ComputeVectorNorms", dataFile ( ( section + "/amesos/computevectornorms").data(), false ) );
194  M_trilinosParameterList.set ( "ComputeTrueResidual", dataFile ( ( section + "/amesos/computeresidual").data(), false ) );
195 
196  // Control parameters
197  M_trilinosParameterList.set ( "AddZeroToDiag", dataFile ( ( section + "/amesos/addzerotodiag").data(), false ) );
198  M_trilinosParameterList.set ( "Refactorize", dataFile ( ( section + "/amesos/refactorize").data(), false ) );
199  M_trilinosParameterList.set ( "RcondThreshold", dataFile ( ( section + "/amesos/rcondthreshold").data(), 1.e-2) );
200  M_trilinosParameterList.set ( "Redistribute", dataFile ( ( section + "/amesos/redistribute").data(), true ) ); // SuperLU
201  M_trilinosParameterList.set ( "MaxProcs", dataFile ( ( section + "/amesos/maxprocs").data(), -1) ); // ScalaPack
202  M_trilinosParameterList.set ( "ScaleMethod", dataFile ( ( section + "/amesos/scalemethod").data(), 1) );
203 
204  // Type of the matrix: symmetric, SDP, general
205  M_trilinosParameterList.set ( "MatrixProperty", dataFile ( ( section + "/amesos/matrixproperty").data(), "general" ) );
206 
207  // Type of the solver
208  M_trilinosParameterList.set ( "SolverType", dataFile ( ( section + "/amesos/solvertype" ).data(), "Klu" ) );
209 }
210 
212 {
213  // Create the solver
214  if ( M_solver == NULL )
215  {
216  createSolver ( M_trilinosParameterList.get ( "SolverType", "Klu" ) );
217  }
218 
219  // Set the parameters
220  M_solver->SetParameters ( M_trilinosParameterList );
221 }
222 
223 // ===================================================
224 // Get Methods
225 // ===================================================
226 Int
228 {
229  return 1;
230 }
231 
232 Real
234 {
235  return 0.;
236 }
237 
238 // ===================================================
239 // Private Methods
240 // ===================================================
241 void SolverAmesos::createSolver ( const std::string& solverType )
242 {
243  Amesos factory;
244  M_solver = factory.Create ( solverType, M_problem );
245 
246  if ( M_solver == 0 )
247  {
249  {
250  std::cerr << std::endl << std::endl;
251  std::cerr << "SolverAmesos: Selected solver << " << solverType << " is not available. Bailing out." << std::endl;
252  }
253 
254  // return ok not to break the test harness
255 #ifdef HAVE_MPI
256  MPI_Finalize();
257 #endif
258  exit ( EXIT_SUCCESS );
259  }
260 }
261 
262 } // namespace LifeV
void start()
Start the timer.
Definition: LifeChrono.hpp:93
VectorEpetra vector_type
void printStatus()
Display status of the solver.
Displayer(const commPtr_Type &comm)
Definition: Displayer.cpp:56
std::shared_ptr< matrix_type > matrix_ptrtype
MatrixEpetra< Real > matrix_type
int32_type Int
Generic integer data.
Definition: LifeV.hpp:188
Real trueResidual()
Return the true residual.
VectorEpetra(const MapEpetra &map, const MapEpetraType &mapType=Unique, const combineMode_Type combineMode=Add)
Constructor - Using Maps.
void updateInverseJacobian(const UInt &iQuadPt)
Real computeResidual(const vector_type &solution, const vector_type &rhs)
Compute the residual.
void setOperator(const Epetra_Operator &oper)
Method to set a general linear operator (of class derived from Epetra_Operator) defining the linear s...
const bool & isLeader() const
Determine if it is the leader.
Definition: Displayer.cpp:76
Int setMatrix(const matrix_type &matrix)
Set matrix from MatrixEpetra.
void norm2(Real *result) const
Compute and store the norm 2 in the given pointed variable.
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
Int solveSystem(vector_type &rhsFull, vector_type &solution, const matrix_ptrtype &)
Solves the system and returns the number of iterations.
void setReusePreconditioner(const bool &)
Specify if the preconditioner should be reuse or not.
const MapEpetra & map() const
Return the MapEpetra of the vector.
void setDataFromGetPot(const GetPot &dataFile, const std::string &section)
Method to setup the solver using GetPot.
SolverAmesos - Class to wrap linear solver.
void showMe(std::ostream &output=std::cout) const
Print informations about the solver.
double Real
Generic real data.
Definition: LifeV.hpp:175
SolverAmesos(const commPtr_Type &comm)
Default constructor.
Int numIterations()
Return the total number of iterations.
void resetPreconditioner()
Delete the stored preconditioner.
void stop()
Stop the timer.
Definition: LifeChrono.hpp:100
bool isPreconditionerSet() const
Return true if the preconditioner is set.
VectorEpetra(const VectorEpetra &vector)
Copy constructor.
void createSolver(const std::string &solverType)
Create a solver using a factory.
void setParameters()
Set the current parameters with the internal parameters list.
void setupPreconditioner(const GetPot &dataFile, const std::string &section)
Setup the preconditioner.