Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_bl_cg_real_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 "BelosBlockCGSolMgr.hpp"
23 
24 #ifdef HAVE_MPI
25 #include <mpi.h>
26 #endif
27 
28 // I/O for Harwell-Boeing files
29 #ifdef HAVE_BELOS_TRIUTILS
30 #include "Trilinos_Util_iohb.h"
31 #endif
32 
33 #include "MyMultiVec.hpp"
34 #include "MyBetterOperator.hpp"
35 #include "MyOperator.hpp"
36 
37 using namespace Teuchos;
38 
39 int main(int argc, char *argv[]) {
40  //
41  typedef double ST;
42 
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  //
54  using Teuchos::RCP;
55  using Teuchos::rcp;
56 
57  bool success = false;
58  bool verbose = false;
59  try {
60  int info = 0;
61  int MyPID = 0;
62  bool pseudo = false; // use pseudo block CG to solve this linear system.
63  bool norm_failure = false;
64  bool proc_verbose = false;
65  bool use_single_red = false;
66  bool combineConvInner = false;
67  int frequency = -1; // how often residuals are printed by solver
68  int blocksize = 1;
69  int numrhs = 1;
70  std::string filename("A.hb");
71  MT tol = 1.0e-5; // relative residual tolerance
72 
73  CommandLineProcessor cmdp(false,true);
74  cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
75  cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block CG to solve the linear systems.");
76  cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
77  cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
78  cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
79  cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
80  cmdp.setOption("blocksize",&blocksize,"Block size used by CG .");
81  cmdp.setOption("use-single-red","use-standard-red",&use_single_red,"Use single-reduction variant of CG iteration.");
82  cmdp.setOption("combine-conv-inner","separate-conv-inner",&combineConvInner,"Combine convergence detection and inner products of single-reduce CG iteration.");
83  if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
84  return -1;
85  }
86 
87  proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
88  if (proc_verbose) {
89  std::cout << Belos::Belos_Version() << std::endl << std::endl;
90  }
91  if (!verbose)
92  frequency = -1; // reset frequency if test is not verbose
93 
94 
95 #ifndef HAVE_BELOS_TRIUTILS
96  std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
97  if (MyPID==0) {
98  std::cout << "End Result: TEST FAILED" << std::endl;
99  }
100  return -1;
101 #endif
102 
103  // Get the data from the HB file
104  int dim,dim2,nnz;
105  int *colptr,*rowind;
106  ST *cvals;
107  nnz = -1;
108  info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
109  &colptr,&rowind,&cvals);
110  if (info == 0 || nnz < 0) {
111  if (MyPID==0) {
112  std::cout << "Error reading '" << filename << "'" << std::endl;
113  std::cout << "End Result: TEST FAILED" << std::endl;
114  }
115  return -1;
116  }
117  // Build the problem matrix
119  = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
120  // for (int j=0; j<nnz; j++)
121  // std::cout << cvals[j] << std::endl;
122  // A->Print(std::cout);
123  //
124  // ********Other information used by block solver***********
125  // *****************(can be user specified)******************
126  //
127  int maxits = dim/blocksize; // maximum number of iterations to run
128  //
129  ParameterList belosList;
130  belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
131  belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
132  belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
133  if (blocksize == 1) {
134  if (use_single_red)
135  belosList.set( "Use Single Reduction", use_single_red ); // Use single reduction CG iteration
136  if (combineConvInner)
137  belosList.set( "Fold Convergence Detection Into Allreduce", combineConvInner );
138  }
139  if (verbose) {
140  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
142  if (frequency > 0)
143  belosList.set( "Output Frequency", frequency );
144  }
145  else
146  belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
147  //
148  // Construct the right-hand side and solution multivectors.
149  // NOTE: The right-hand side will be constructed such that the solution is
150  // a vectors of one.
151  //
152  RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
153  RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
154  MVT::MvRandom( *soln );
155  OPT::Apply( *A, *soln, *rhs );
156  MVT::MvInit( *soln, zero );
157  //
158  // Construct an unpreconditioned linear problem instance.
159  //
161  rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
162  bool set = problem->setProblem();
163  if (set == false) {
164  if (proc_verbose)
165  std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
166  return -1;
167  }
168 
169  //
170  // *******************************************************************
171  // *************Start the block CG iteration***********************
172  // *******************************************************************
173  //
175  if (pseudo)
176  solver = Teuchos::rcp( new Belos::PseudoBlockCGSolMgr<ST,MV,OP>( problem, Teuchos::rcp(&belosList,false) ) );
177  else
178  solver = Teuchos::rcp( new Belos::BlockCGSolMgr<ST,MV,OP>( problem, Teuchos::rcp(&belosList,false) ) );
179 
180  //
181  // **********Print out information about problem*******************
182  //
183  if (proc_verbose) {
184  std::cout << std::endl << std::endl;
185  std::cout << "Dimension of matrix: " << dim << std::endl;
186  std::cout << "Number of right-hand sides: " << numrhs << std::endl;
187  std::cout << "Block size used by solver: " << blocksize << std::endl;
188  std::cout << "Max number of CG iterations: " << maxits << std::endl;
189  std::cout << "Relative residual tolerance: " << tol << std::endl;
190  std::cout << std::endl;
191  }
192  //
193  // Perform solve
194  //
195  Belos::ReturnType ret = solver->solve();
196  //
197  // Compute actual residuals.
198  //
199  RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
200  OPT::Apply( *A, *soln, *temp );
201  MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
202  std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
203  MVT::MvNorm( *temp, norm_num );
204  MVT::MvNorm( *rhs, norm_denom );
205  for (int i=0; i<numrhs; ++i) {
206  if (proc_verbose)
207  std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
208  if ( norm_num[i] / norm_denom[i] > tol ) {
209  norm_failure = true;
210  }
211  }
212 
213  // Test achievedTol output
214  MT ach_tol = solver->achievedTol();
215  if (proc_verbose)
216  std::cout << "Achieved tol : "<<ach_tol<<std::endl;
217 
218  // Clean up.
219  delete [] colptr;
220  delete [] rowind;
221  delete [] cvals;
222 
223  success = ret==Belos::Converged && !norm_failure;
224 
225  if (success) {
226  if (proc_verbose)
227  std::cout << "End Result: TEST PASSED" << std::endl;
228  } else {
229  if (proc_verbose)
230  std::cout << "End Result: TEST FAILED" << std::endl;
231  }
232  }
233  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
234 
235  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
236 } // end test_bl_cg_complex_hb.cpp
std::string Belos_Version()
int main(int argc, char *argv[])
The Belos::PseudoBlockCGSolMgr provides a solver manager for the BlockCG linear solver.
The Belos::BlockCGSolMgr provides a powerful and fully-featured solver manager over the CG and BlockC...
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Traits class which defines basic operations on multivectors.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
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)
#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
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:123
The Belos::BlockCGSolMgr provides a solver manager for the BlockCG linear solver. ...
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.