Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_hybrid_gmres_complex_hb.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 // This driver reads a problem from a Harwell-Boeing (HB) file.
11 // The right-hand-side from the HB file is used instead of random vectors.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosGmresPolySolMgr.hpp"
24 
25 #ifdef HAVE_MPI
26 #include <mpi.h>
27 #endif
28 
29 // I/O for Harwell-Boeing files
30 #ifdef HAVE_BELOS_TRIUTILS
31 #include "Trilinos_Util_iohb.h"
32 #endif
33 
34 #include "MyMultiVec.hpp"
35 #include "MyBetterOperator.hpp"
36 #include "MyOperator.hpp"
37 
38 using namespace Teuchos;
39 
40 int main(int argc, char *argv[]) {
41  //
42  typedef std::complex<double> ST;
43  typedef ScalarTraits<ST> SCT;
44  typedef SCT::magnitudeType MT;
45  typedef Belos::MultiVec<ST> MV;
46  typedef Belos::Operator<ST> OP;
47  typedef Belos::MultiVecTraits<ST,MV> MVT;
49  ST one = SCT::one();
50  ST zero = SCT::zero();
51 
52  Teuchos::GlobalMPISession session(&argc, &argv, NULL);
53  int MyPID = session.getRank();
54  //
55  using Teuchos::RCP;
56  using Teuchos::rcp;
57 
58  bool success = false;
59  bool verbose = false;
60  try {
61  int info = 0;
62  bool norm_failure = false;
63  bool proc_verbose = false;
64  bool userandomrhs = true;
65  int frequency = -1; // frequency of status test output.
66  int blocksize = 1; // blocksize
67  int numrhs = 1; // number of right-hand sides to solve for
68  int maxiters = -1; // maximum number of iterations allowed per linear system
69  int maxdegree = 25; // maximum degree of polynomial
70  int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
71  int maxrestarts = 15; // number of restarts allowed
72  std::string outersolver("Block Gmres");
73  std::string filename("mhd1280b.cua");
74  std::string polyType("Arnoldi");
75  MT tol = 1.0e-5; // relative residual tolerance
76  MT polytol = tol/10; // relative residual tolerance for polynomial construction
77 
78  Teuchos::CommandLineProcessor cmdp(false,true);
79  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
80  cmdp.setOption("use-random-rhs","use-rhs",&userandomrhs,"Use linear system RHS or random RHS to generate polynomial.");
81  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
82  cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
83  cmdp.setOption("outersolver",&outersolver,"Name of outer solver to be used with GMRES poly");
84  cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
85  cmdp.setOption("poly-tol",&polytol,"Relative residual tolerance used to construct the GMRES polynomial.");
86  cmdp.setOption("poly-type",&polyType,"Polynomial type (Roots, Arnoldi, or Gmres).");
87  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
88  cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
89  cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
90  cmdp.setOption("max-degree",&maxdegree,"Maximum degree of the GMRES polynomial.");
91  cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
92  cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
94  return EXIT_FAILURE;
95  }
96 
97  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
98  if (proc_verbose) {
99  std::cout << Belos::Belos_Version() << std::endl << std::endl;
100  }
101  if (!verbose)
102  frequency = -1; // reset frequency if test is not verbose
103 
104 #ifndef HAVE_BELOS_TRIUTILS
105  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
106  if (MyPID==0) {
107  std::cout << "End Result: TEST FAILED" << std::endl;
108  }
109  return EXIT_FAILURE;
110 #endif
111 
112  // Get the data from the HB file
113  int dim,dim2,nnz;
114  MT *dvals;
115  int *colptr,*rowind;
116  ST *cvals;
117  nnz = -1;
118  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
119  &colptr,&rowind,&dvals);
120  if (info == 0 || nnz < 0) {
121  if (MyPID==0) {
122  std::cout << "Error reading '" << filename << "'" << std::endl;
123  std::cout << "End Result: TEST FAILED" << std::endl;
124  }
125  return EXIT_FAILURE;
126  }
127  // Convert interleaved doubles to std::complex values
128  cvals = new ST[nnz];
129  for (int ii=0; ii<nnz; ii++) {
130  cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
131  }
132  // Build the problem matrix
134  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
135  //
136  // Construct the right-hand side and solution multivectors.
137  // NOTE: The right-hand side will be constructed such that the solution is
138  // a vectors of one.
139  //
140  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
141  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
142  MVT::MvRandom( *soln );
143  OPT::Apply( *A, *soln, *rhs );
144  MVT::MvInit( *soln, zero );
145  //
146  // Construct an unpreconditioned linear problem instance.
147  //
149  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
150  problem->setInitResVec( rhs );
151  bool set = problem->setProblem();
152  if (set == false) {
153  if (proc_verbose)
154  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
155  return EXIT_FAILURE;
156  }
157  //
158  // ********Other information used by block solver***********
159  // *****************(can be user specified)******************
160  //
161  if (maxiters == -1)
162  maxiters = dim/blocksize - 1; // maximum number of iterations to run
163 
164  ParameterList belosList;
165  belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
166  belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
167  belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
168  belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
169  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
170  int verbosity = Belos::Errors + Belos::Warnings;
171  if (verbose) {
173  if (frequency > 0)
174  belosList.set( "Output Frequency", frequency );
175  }
176  belosList.set( "Verbosity", verbosity );
177 
178  ParameterList polyList;
179  polyList.set( "Maximum Degree", maxdegree ); // Maximum degree of the GMRES polynomial
180  polyList.set( "Polynomial Tolerance", polytol ); // Polynomial convergence tolerance requested
181  polyList.set( "Polynomial Type", polyType ); // Type of polynomial to construct
182  polyList.set( "Verbosity", verbosity ); // Verbosity for polynomial construction
183  polyList.set( "Random RHS", userandomrhs ); // Use RHS from linear system or random vector
184  if ( outersolver != "" ) {
185  polyList.set( "Outer Solver", outersolver );
186  polyList.set( "Outer Solver Params", belosList );
187  }
188 
189  // Use a debugging status test to save absolute residual history.
190  // Debugging status tests are peer to the native status tests that are called whenever convergence is checked.
192 
193  //
194  // *******************************************************************
195  // *************Start the block Gmres iteration***********************
196  // *******************************************************************
197  //
198  RCP< Belos::SolverManager<ST,MV,OP> > solver = rcp( new Belos::GmresPolySolMgr<ST,MV,OP>( problem, rcp(&polyList,false) ) );
199 
200  // The debug status test does not work for the GmresPolySolMgr right now.
201  // solver->setDebugStatusTest( Teuchos::rcp(&debugTest, false) );
202 
203  //
204  // **********Print out information about problem*******************
205  //
206  if (proc_verbose) {
207  std::cout << std::endl << std::endl;
208  std::cout << "Dimension of matrix: " << dim << std::endl;
209  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
210  std::cout << "Block size used by solver: " << blocksize << std::endl;
211  std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
212  std::cout << "Relative residual tolerance: " << tol << std::endl;
213  std::cout << std::endl;
214  }
215  //
216  // Perform solve
217  //
218  Belos::ReturnType ret = solver->solve();
219  //
220  std::cout << "Belos::GmresPolySolMgr returned achievedTol: " << solver->achievedTol() << std::endl << std::endl;
221  //
222  // Compute actual residuals.
223  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
224  OPT::Apply( *A, *soln, *temp );
225  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
226  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
227  MVT::MvNorm( *temp, norm_num );
228  MVT::MvNorm( *rhs, norm_denom );
229  for (int i=0; i<numrhs; ++i) {
230  if (proc_verbose)
231  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
232  if ( norm_num[i] / norm_denom[i] > tol ) {
233  norm_failure = true;
234  }
235  }
236 
237  // Print absolute residual norm logging.
238  const std::vector<MT> residualLog = debugTest.getLogResNorm();
239  if (numrhs==1 && proc_verbose && residualLog.size())
240  {
241  std::cout << "Absolute residual 2-norm [ " << residualLog.size() << " ] : ";
242  for (unsigned int i=0; i<residualLog.size(); i++)
243  std::cout << residualLog[i] << " ";
244  std::cout << std::endl;
245  std::cout << "Final abs 2-norm / rhs 2-norm : " << residualLog[residualLog.size()-1] / norm_denom[0] << std::endl;
246  }
247 
248  // Clean up.
249  delete [] dvals;
250  delete [] colptr;
251  delete [] rowind;
252  delete [] cvals;
253 
254  success = ret==Belos::Converged && !norm_failure;
255  if (success) {
256  if (proc_verbose)
257  std::cout << "End Result: TEST PASSED" << std::endl;
258  } else {
259  if (proc_verbose)
260  std::cout << "End Result: TEST FAILED" << std::endl;
261  }
262  }
263  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
264 
265  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
266 } // end test_bl_gmres_complex_hb.cpp
std::string Belos_Version()
int main(int argc, char *argv[])
const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > & getLogResNorm() const
Returns the log of the absolute residual norm from the iteration.
A Belos::StatusTest debugging class for storing the absolute residual norms generated during a solve...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Belos::StatusTest debugging class for storing the absolute residual norms generated during a solve...
Traits class which defines basic operations on multivectors.
Simple example of a user&#39;s defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:33
std::string filename
Alternative run-time polymorphic interface for operators.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Declaration and definition of Belos::GmresPolySolMgr (hybrid block GMRES linear solver).
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
A linear system to solve, and its associated information.
const double tol
Class which describes the linear problem to be solved by the iterative solver.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
The GMRES polynomial can be created in conjunction with any standard preconditioner.
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:123
Interface for multivectors used by Belos&#39; linear solvers.
Class which defines basic traits for the operator type.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Simple example of a user&#39;s defined Belos::Operator class.