Debian Codegraph @

packages/belos/tpetra/test/BlockGmres/test_hybrid_gmres_hb.cpp

raw
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 tests variations of Hybrid GMRES using the Belos GMRES
11 // Polynomial solver manager. One can specify an 'outer solver'
12 // to which the GMRES Polynomial is applied as a preconditioner.
13 // If the 'outer solver' string is empty, the polynomial will be applied
14 // alone.
15 //
16 // Alternately, one can apply the GMRES polynomial without the 'outer
17 // solver' functionality by using the BelosTpetraOperator interface.
18 // See the FGMRES test driver for an example of its use.
19 //
20 // This driver reads a problem from a Harwell-Boeing (HB) file.
21 // The right-hand-side corresponds to a randomly generated solution.
22 // The initial guesses are all set to zero.
23 //
24 //
25 #include "BelosConfigDefs.hpp"
26 #include "BelosLinearProblem.hpp"
27 #include "BelosTpetraAdapter.hpp"
28 #include "BelosGmresPolySolMgr.hpp"
29
30 // I/O for Harwell-Boeing files
31 #define HIDE_TPETRA_INOUT_IMPLEMENTATIONS
32 #include <Tpetra_MatrixIO.hpp>
33
34 #include <Teuchos_CommandLineProcessor.hpp>
35 #include <Teuchos_ParameterList.hpp>
36 #include <Teuchos_StandardCatchMacros.hpp>
37 #include <Tpetra_Core.hpp>
38 #include <Tpetra_CrsMatrix.hpp>
39
40 using namespace Teuchos;
41 using Tpetra::Operator;
42 using Tpetra::CrsMatrix;
43 using Tpetra::MultiVector;
44 using std::endl;
45 using std::cout;
46 using std::vector;
47 using Teuchos::tuple;
48
49 int main(int argc, char *argv[]) {
50 typedef Tpetra::MultiVector<>::scalar_type ST;
51 typedef ScalarTraits<ST> SCT;
52 typedef SCT::magnitudeType MT;
53 typedef Tpetra::Operator<ST> OP;
54 typedef Tpetra::MultiVector<ST> MV;
55 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
56 typedef Belos::MultiVecTraits<ST,MV> MVT;
57
58 Tpetra::ScopeGuard tpetraScope(&argc,&argv);
59
60 bool success = false;
61 bool verbose = false;
62 try {
63 const ST one = SCT::one();
64
65 int MyPID = 0;
66
67 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
68 //
69 // Get test parameters from command-line processor
70 //
71 bool proc_verbose = false;
72 bool userandomrhs = false; // use linear problem RHS or random RHS to generate poly
73 int frequency = -1; // frequency of status test output.
74 int blocksize = 1; // blocksize
75 int numrhs = 1; // number of right-hand sides to solve for
76 int maxiters = -1; // maximum number of iterations allowed per linear system
77 int maxdegree = 25; // maximum degree of polynomial
78 int maxsubspace = 50; // maximum number of blocks the solver can use for the subspace
79 int maxrestarts = 15; // number of restarts allowed
80 std::string outersolver("Block Gmres"); // name of outer solver
81 std::string polytype("Roots"); // polynomial configuration.
82 std::string filename("bcsstk14.hb"); // name of matrix file
83 MT tol = 1.0e-5; // relative residual tolerance
84 MT polytol = tol/10; // relative residual tolerance for polynomial construction
85
86 Teuchos::CommandLineProcessor cmdp(false,true);
87 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
88 cmdp.setOption("use-random-rhs","use-rhs",&userandomrhs,"Use linear system RHS or random RHS to generate polynomial.");
89 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
90 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
91 cmdp.setOption("outersolver",&outersolver,"Name of outer solver to be used with GMRES poly");
92 cmdp.setOption("poly-type",&polytype,"Name of the polynomial to be generated. Arnoldi, Gmres, or Roots.");
93 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
94 cmdp.setOption("poly-tol",&polytol,"Relative residual tolerance used to construct the GMRES polynomial.");
95 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
96 cmdp.setOption("block-size",&blocksize,"Block size used by GMRES.");
97 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
98 cmdp.setOption("max-degree",&maxdegree,"Maximum degree of the GMRES polynomial.");
99 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of blocks the solver can use for the subspace.");
100 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
101 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
102 return EXIT_FAILURE;
103 }
104 if (!verbose) {
105 frequency = -1; // reset frequency if test is not verbose
106 }
107
108 MyPID = rank(*comm);
109 proc_verbose = ( verbose && (MyPID==0) );
110
111 if (proc_verbose) {
112 std::cout << Belos::Belos_Version() << std::endl << std::endl;
113 }
114
115 //
116 // Get the data from the HB file and build the Map,Matrix
117 //
118 RCP<CrsMatrix<ST> > A;
119 Tpetra::Utils::readHBMatrix(filename,comm,A);
120 RCP<const Tpetra::Map<> > map = A->getDomainMap();
121
122 // Create initial vectors
123 RCP<MV> B, X;
124 X = rcp( new MV(map,numrhs) );
125 MVT::MvRandom( *X );
126 B = rcp( new MV(map,numrhs) );
127 OPT::Apply( *A, *X, *B );
128 MVT::MvInit( *X, 0.0 );
129
130 //
131 // ********Other information used by block solver***********
132 // *****************(can be user specified)******************
133 //
134 const int NumGlobalElements = B->getGlobalLength();
135 if (maxiters == -1) {
136 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
137 }
138 //
139 //
140 //Parameter list used by the outer solver:
141 ParameterList belosList;
142 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
143 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
144 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
145 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
146 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
147 int verbosity = Belos::Errors + Belos::Warnings;
148 if (verbose) {
149 verbosity += Belos::FinalSummary + Belos::TimingDetails + Belos::StatusTestDetails;
150 if (frequency > 0)
151 belosList.set( "Output Frequency", frequency );
152 }
153 belosList.set( "Verbosity", verbosity );
154
155 // Parameter list used by the GMRES Polynomial (inner) solver
156 ParameterList polyList;
157 polyList.set( "Polynomial Type", polytype ); // Type of polynomial to be generated
158 polyList.set( "Maximum Degree", maxdegree ); // Maximum degree of the GMRES polynomial
159 polyList.set( "Polynomial Tolerance", polytol ); // Polynomial convergence tolerance requested
160 polyList.set( "Verbosity", verbosity ); // Verbosity for polynomial construction
161 polyList.set( "Random RHS", userandomrhs ); // Use RHS from linear system or random vector
162 // to generate the polynomial.
163
164 //
165 // Pass the outer solver name and parameters to the polynomial solver manager.
166 //
167 if ( outersolver != "" ) {
168 polyList.set( "Outer Solver", outersolver );
169 polyList.set( "Outer Solver Params", belosList );
170 }
171
172 //
173 // Construct an unpreconditioned linear problem instance.
174 //
175 // The polynomial preconditioning will be handled by the solver manager.
176 // One could pass another preconditioner such as ILU to the linear problem
177 // to be used in combination with the polynomial preconditioner.
178 //
179 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
180 problem.setInitResVec( B );
181 bool set = problem.setProblem();
182 if (set == false) {
183 if (proc_verbose)
184 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
185 return EXIT_FAILURE;
186 }
187 //
188 // *******************************************************************
189 // *************Start the Hybrid Gmres iteration***********************
190 // ********* Using the GMRES Poly Solver Manager *********************
191 // *******************************************************************
192 //
193 Belos::GmresPolySolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(polyList) );
194
195 //
196 // **********Print out information about problem*******************
197 //
198 if (proc_verbose) {
199 std::cout << std::endl << std::endl;
200 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
201 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
202 std::cout << "Block size used by solver: " << blocksize << std::endl;
203 std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
204 std::cout << "Relative residual tolerance: " << tol << std::endl;
205 std::cout << std::endl;
206 }
207
208 //
209 // Perform solve
210 //
211 Belos::ReturnType ret = solver.solve();
212
213 //
214 // Compute actual residuals.
215 //
216 bool badRes = false;
217 std::vector<MT> actual_resids( numrhs );
218 std::vector<MT> rhs_norm( numrhs );
219 MV resid(map, numrhs);
220 OPT::Apply( *A, *X, resid );
221 MVT::MvAddMv( -one, resid, one, *B, resid );
222 MVT::MvNorm( resid, actual_resids );
223 MVT::MvNorm( *B, rhs_norm );
224 if (proc_verbose) {
225 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
226 }
227 for ( int i=0; i<numrhs; i++) {
228 MT actRes = actual_resids[i]/rhs_norm[i];
229 if (proc_verbose) {
230 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
231 }
232 if (actRes > tol) badRes = true;
233 }
234
235 success = (ret==Belos::Converged && !badRes);
236
237 if (success) {
238 if (proc_verbose)
239 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
240 } else {
241 if (proc_verbose)
242 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
243 }
244 }
245 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
246
247 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
248 } // end test_hybrid_gmres_hb.cpp