Debian Codegraph @

packages/belos/tpetra/example/GCRODR/GCRODRTpetraExFile.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 // Update of belos/epetra/example/GCRODR/GCRODREpetraExFile.cpp
11 //
12 // This driver reads a problem from a Harwell-Boeing (HB) file.
13 // The right-hand-side corresponds to a randomly generated solution.
14 // The initial guesses are all set to zero.
15 //
16 // NOTE: No preconditioner is used in this example.
17
18 // Tpetra
19 #include <Tpetra_Map.hpp>
20 #include <Tpetra_Core.hpp>
21 #include <Tpetra_Vector.hpp>
22 #include <Tpetra_CrsMatrix.hpp>
23
24 // Teuchos
25 #include <Teuchos_RCP.hpp>
26 #include <Teuchos_Comm.hpp>
27 #include <Teuchos_CommHelpers.hpp>
28 #include <Teuchos_DefaultComm.hpp>
29 #include "Teuchos_ParameterList.hpp"
30 #include "Teuchos_StandardCatchMacros.hpp"
31 #include "Teuchos_CommandLineProcessor.hpp"
32
33 // Belos
34 #include "BelosConfigDefs.hpp"
35 #include "BelosGCRODRSolMgr.hpp"
36 #include "BelosLinearProblem.hpp"
37 #include "BelosTpetraTestFramework.hpp"
38
39
40 template <typename ScalarType>
41 int run(int argc, char *argv[]) {
42
43 using ST = typename Tpetra::Vector<ScalarType>::scalar_type;
44 using LO = typename Tpetra::Vector<>::local_ordinal_type;
45 using GO = typename Tpetra::Vector<>::global_ordinal_type;
46 using NT = typename Tpetra::Vector<>::node_type;
47
48 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
49 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
50 using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType;
51
52 using tmap_t = Tpetra::Map<LO,GO,NT>;
53 using tvector_t = Tpetra::Vector<ST,LO,GO,NT>;
54 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
55
56 using MVT = typename Belos::MultiVecTraits<ST,MV>;
57 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
58
59 using Teuchos::RCP;
60 using Teuchos::rcp;
61 using Teuchos::ParameterList;
62
63 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout);
64 const auto Comm = Tpetra::getDefaultComm();
65 const int MyPID = Comm->getRank();
66
67 bool verbose = false;
68 bool success = true;
69
70 try {
71 bool proc_verbose = false;
72 bool debug = false;
73 int frequency = -1; // frequency of status test output.
74 int numrhs = 1; // number of right-hand sides to solve for
75 int maxiters = -1; // maximum number of iterations allowed per linear system
76 int maxsubspace = 250; // maximum number of blocks the solver can use for the subspace
77 int recycle = 50; // maximum number of blocks the solver can use for the subspace
78 int maxrestarts = 15; // number of restarts allowed
79 std::string filename("sherman5.hb");
80 std::string ortho("IMGS");
81 MT tol = 1.0e-8; // relative residual tolerance
82
83 Teuchos::CommandLineProcessor cmdp(false,true);
84 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
85 cmdp.setOption("debug","nodebug",&debug,"Print debugging information from the solver.");
86 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
87 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
88 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
89 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
90 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
91 cmdp.setOption("max-subspace",&maxsubspace,"Maximum number of vectors in search space (including recycle space).");
92 cmdp.setOption("recycle",&recycle,"Number of vectors in recycle space.");
93 cmdp.setOption("max-cycles",&maxrestarts,"Maximum number of cycles allowed for GCRO-DR solver.");
94 cmdp.setOption("ortho-type",&ortho,"Orthogonalization type. Must be one of DGKS, ICGS, IMGS.");
95 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
96 return -1;
97 }
98 if (!verbose)
99 frequency = -1; // reset frequency if test is not verbose
100
101 proc_verbose = ( verbose && (MyPID==0) );
102
103 if (proc_verbose) {
104 std::cout << Belos::Belos_Version() << std::endl << std::endl;
105 }
106
107 // Get the problem
108 Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( Comm );
109 RCP<tcrsmatrix_t> A = reader.readFromFile( filename );
110 RCP<const tmap_t> Map = A->getDomainMap();
111
112 // Create initial vectors
113 RCP<MV> B, X;
114 X = rcp( new MV(Map,numrhs) );
115 MVT::MvRandom( *X );
116 B = rcp( new MV(Map,numrhs) );
117 OPT::Apply( *A, *X, *B );
118 MVT::MvInit( *X, 0.0 );
119
120 // ********Other information used by block solver***********
121 // *****************(can be user specified)******************
122
123 const int NumGlobalElements = B->getGlobalLength();
124 if (maxiters == -1)
125 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
126
127 ParameterList belosList;
128 belosList.set( "Num Blocks", maxsubspace); // Maximum number of blocks in Krylov factorization
129 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
130 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
131 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
132 belosList.set( "Num Recycled Blocks", recycle ); // Number of vectors in recycle space
133 belosList.set( "Orthogonalization", ortho ); // Orthogonalization type
134
135 int verbosity = Belos::Errors + Belos::Warnings;
136 if (verbose) {
137 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
138 if (frequency > 0)
139 belosList.set( "Output Frequency", frequency );
140 }
141 if (debug) {
142 verbosity += Belos::Debug;
143 }
144 belosList.set( "Verbosity", verbosity );
145
146 // Construct an unpreconditioned linear problem instance.
147 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
148 bool set = problem.setProblem();
149 if (set == false) {
150 if (proc_verbose)
151 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
152 return -1;
153 }
154
155 // *******************************************************************
156 // *************Start the GCRO-DR iteration*************************
157 // *******************************************************************
158
159 // Create an iterative solver manager.
160 RCP< Belos::SolverManager<ST,MV,OP> > newSolver
161 = rcp( new Belos::GCRODRSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
162
163 // **********Print out information about problem*******************
164
165 if (proc_verbose) {
166 std::cout << std::endl << std::endl;
167 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
168 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
169 std::cout << "Max number of restarts allowed: " << maxrestarts << std::endl;
170 std::cout << "Max number of iterations per restart cycle: " << maxiters << std::endl;
171 std::cout << "Relative residual tolerance: " << tol << std::endl;
172 std::cout << std::endl;
173 }
174
175 // Perform solve
176 Belos::ReturnType ret = newSolver->solve();
177
178 // Get the number of iterations for this solve.
179 int numIters = newSolver->getNumIters();
180 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
181
182 // Compute actual residuals.
183 bool badRes = false;
184 std::vector<ST> actual_resids( numrhs );
185 std::vector<ST> rhs_norm( numrhs );
186 MV resid(Map, numrhs);
187 OPT::Apply( *A, *X, resid );
188 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
189 MVT::MvNorm( resid, actual_resids );
190 MVT::MvNorm( *B, rhs_norm );
191 if (proc_verbose) {
192 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
193 for ( int i=0; i<numrhs; i++) {
194 ST actRes = actual_resids[i]/rhs_norm[i];
195 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
196 if (actRes > tol) badRes = true;
197 }
198 }
199
200 if (ret!=Belos::Converged || badRes) {
201 success = false;
202 if (proc_verbose)
203 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
204 } else {
205 success = true;
206 if (proc_verbose)
207 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
208 }
209 }
210 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
211
212 return success ? EXIT_SUCCESS : EXIT_FAILURE;
213 }
214
215 int main(int argc, char *argv[]) {
216 // run with different ST
217 return run<double>(argc,argv);
218 // run<float>(argc,argv); // FAILS
219 }