Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_bl_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"
25 
26 #ifdef HAVE_MPI
27 #include <mpi.h>
28 #endif
29 
30 // I/O for Harwell-Boeing files
31 #ifdef HAVE_BELOS_TRIUTILS
32 #include "Trilinos_Util_iohb.h"
33 #endif
34 
35 #include "MyMultiVec.hpp"
36 #include "MyBetterOperator.hpp"
37 #include "MyOperator.hpp"
38 
39 using namespace Teuchos;
40 
41 int main(int argc, char *argv[]) {
42  //
43  typedef std::complex<double> ST;
44  typedef ScalarTraits<ST> SCT;
45  typedef SCT::magnitudeType MT;
46  typedef Belos::MultiVec<ST> MV;
47  typedef Belos::Operator<ST> OP;
48  typedef Belos::MultiVecTraits<ST,MV> MVT;
50  ST one = SCT::one();
51  ST zero = SCT::zero();
52 
53  Teuchos::GlobalMPISession session(&argc, &argv, NULL);
54  int MyPID = session.getRank();
55  //
56  using Teuchos::RCP;
57  using Teuchos::rcp;
58 
59  bool success = false;
60  bool verbose = false;
61  bool debug = false;
62  try {
63  int info = 0;
64  bool norm_failure = false;
65  bool proc_verbose = false;
66  bool pseudo = false; // use pseudo block GMRES to solve this linear system.
67  int frequency = -1; // how often residuals are printed by solver
68  int blocksize = 1;
69  int numrhs = 1;
70  int maxrestarts = 15;
71  int length = 50;
72  std::string filename("mhd1280b.cua");
73  std::string ortho("ICGS");
74  MT tol = 1.0e-5; // relative residual tolerance
75 
76  CommandLineProcessor cmdp(false,true);
77  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
78  cmdp.setOption("debug","no-debug",&debug,"Print debug messages.");
79  cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block GMRES to solve the linear systems.");
80  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
81  cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
82  cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
83  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
84  cmdp.setOption("num-restarts",&maxrestarts,"Maximum number of restarts allowed for the GMRES solver.");
85  cmdp.setOption("blocksize",&blocksize,"Block size used by GMRES.");
86  cmdp.setOption("subspace-length",&length,"Maximum dimension of block-subspace used by GMRES solver.");
87  cmdp.setOption("ortho-type",&ortho,"Orthogonalization type, either DGKS, ICGS or IMGS");
88  if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
89  return EXIT_FAILURE;
90  }
91 
92  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
93  if (proc_verbose) {
94  std::cout << Belos::Belos_Version() << std::endl << std::endl;
95  }
96  if (!verbose)
97  frequency = -1; // reset frequency if test is not verbose
98 
99 #ifndef HAVE_BELOS_TRIUTILS
100  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
101  if (MyPID==0) {
102  std::cout << "End Result: TEST FAILED" << std::endl;
103  }
104  return EXIT_FAILURE;
105 #endif
106 
107  // Get the data from the HB file
108  int dim,dim2,nnz;
109  MT *dvals;
110  int *colptr,*rowind;
111  ST *cvals;
112  nnz = -1;
113  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
114  &colptr,&rowind,&dvals);
115  if (info == 0 || nnz < 0) {
116  if (MyPID==0) {
117  std::cout << "Error reading '" << filename << "'" << std::endl;
118  std::cout << "End Result: TEST FAILED" << std::endl;
119  }
120  return EXIT_FAILURE;
121  }
122  // Convert interleaved doubles to std::complex values
123  cvals = new ST[nnz];
124  for (int ii=0; ii<nnz; ii++) {
125  cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
126  }
127  // Build the problem matrix
129  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
130  //
131  // ********Other information used by block solver***********
132  // *****************(can be user specified)******************
133  //
134  int maxits = dim/blocksize; // maximum number of iterations to run
135  //
136  ParameterList belosList;
137  belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization
138  belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
139  belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
140  belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
141  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
142  belosList.set( "Orthogonalization", ortho ); // Orthogonalization type
143  if (verbose) {
144  int verbosity = Belos::Errors + Belos::Warnings +
146  if (debug)
147  verbosity += Belos::OrthoDetails + Belos::Debug;
148  belosList.set( "Verbosity", verbosity );
149  if (frequency > 0)
150  belosList.set( "Output Frequency", frequency );
151  }
152  else
153  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
154  //
155  // Construct the right-hand side and solution multivectors.
156  // NOTE: The right-hand side will be constructed such that the solution is
157  // a vectors of one.
158  //
159  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
160  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
161  MVT::MvRandom( *soln );
162  OPT::Apply( *A, *soln, *rhs );
163  MVT::MvInit( *soln, zero );
164  //
165  // Construct an unpreconditioned linear problem instance.
166  //
168  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
169  bool set = problem->setProblem();
170  if (set == false) {
171  if (proc_verbose)
172  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
173  return EXIT_FAILURE;
174  }
175 
176  // Use a debugging status test to save absolute residual history.
177  // Debugging status tests are peer to the native status tests that are called whenever convergence is checked.
179 
180  //
181  // *******************************************************************
182  // *************Start the block Gmres iteration***********************
183  // *******************************************************************
184  //
186  if (pseudo)
187  solver = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>( problem, Teuchos::rcp(&belosList,false) ) );
188  else
189  solver = Teuchos::rcp( new Belos::BlockGmresSolMgr<ST,MV,OP>( problem, Teuchos::rcp(&belosList,false) ) );
190 
191  solver->setDebugStatusTest( Teuchos::rcp(&debugTest, false) );
192 
193  //
194  // **********Print out information about problem*******************
195  //
196  if (proc_verbose) {
197  std::cout << std::endl << std::endl;
198  std::cout << "Dimension of matrix: " << dim << std::endl;
199  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
200  std::cout << "Block size used by solver: " << blocksize << std::endl;
201  std::cout << "Max number of Gmres iterations: " << maxits << std::endl;
202  std::cout << "Relative residual tolerance: " << tol << std::endl;
203  std::cout << std::endl;
204  }
205  //
206  // Perform solve
207  //
208  Belos::ReturnType ret = solver->solve();
209  //
210  // Compute actual residuals.
211  //
212  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
213  OPT::Apply( *A, *soln, *temp );
214  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
215  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
216  MVT::MvNorm( *temp, norm_num );
217  MVT::MvNorm( *rhs, norm_denom );
218  for (int i=0; i<numrhs; ++i) {
219  if (proc_verbose)
220  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
221  if ( norm_num[i] / norm_denom[i] > tol ) {
222  norm_failure = true;
223  }
224  }
225 
226  // Print absolute residual norm logging.
227  const std::vector<MT> residualLog = debugTest.getLogResNorm();
228  if (numrhs==1 && proc_verbose && residualLog.size())
229  {
230  std::cout << "Absolute residual 2-norm [ " << residualLog.size() << " ] : ";
231  for (unsigned int i=0; i<residualLog.size(); i++)
232  std::cout << residualLog[i] << " ";
233  std::cout << std::endl;
234  std::cout << "Final abs 2-norm / rhs 2-norm : " << residualLog[residualLog.size()-1] / norm_denom[0] << std::endl;
235  }
236 
237  // Clean up.
238  delete [] dvals;
239  delete [] colptr;
240  delete [] rowind;
241  delete [] cvals;
242 
243  success = ret==Belos::Converged && !norm_failure;
244  if (success) {
245  if (proc_verbose)
246  std::cout << "End Result: TEST PASSED" << std::endl;
247  } else {
248  if (proc_verbose)
249  std::cout << "End Result: TEST FAILED" << std::endl;
250  }
251  }
252  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
253 
254  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
255 } // 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)
Interface to Block GMRES and Flexible GMRES.
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)
The Belos::BlockGmresSolMgr provides a solver manager for the BlockGmres linear solver.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
#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
Interface to standard and &quot;pseudoblock&quot; GMRES.
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.
The Belos::PseudoBlockGmresSolMgr provides a solver manager for the BlockGmres linear solver...
Belos header file which uses auto-configuration information to include necessary C++ headers...
Simple example of a user&#39;s defined Belos::Operator class.