Debian Codegraph @

packages/belos/tpetra/example/BlockGmres/PseudoBlockGmresTpetraExFile.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/BlockGmres/BlockGmresEpetraExFile.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
19 // Tpetra
20 #include <Tpetra_Map.hpp>
21 #include <Tpetra_Core.hpp>
22 #include <Tpetra_Vector.hpp>
23 #include <Tpetra_CrsMatrix.hpp>
24
25 // Teuchos
26 #include <Teuchos_RCP.hpp>
27 #include <Teuchos_Comm.hpp>
28 #include <Teuchos_CommHelpers.hpp>
29 #include <Teuchos_DefaultComm.hpp>
30 #include "Teuchos_ParameterList.hpp"
31 #include "Teuchos_StandardCatchMacros.hpp"
32 #include "Teuchos_CommandLineProcessor.hpp"
33
34 // Belos
35 #include "BelosConfigDefs.hpp"
36 #include "BelosPseudoBlockGmresSolMgr.hpp"
37 #include "BelosLinearProblem.hpp"
38 #include "BelosTpetraTestFramework.hpp"
39
40
41 template <typename ScalarType>
42 int run(int argc, char *argv[]) {
43
44 using ST = typename Tpetra::Vector<ScalarType>::scalar_type;
45 using LO = typename Tpetra::Vector<>::local_ordinal_type;
46 using GO = typename Tpetra::Vector<>::global_ordinal_type;
47 using NT = typename Tpetra::Vector<>::node_type;
48
49 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
50 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
51 using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType;
52
53 using tmap_t = Tpetra::Map<LO,GO,NT>;
54 using tvector_t = Tpetra::Vector<ST,LO,GO,NT>;
55 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
56
57 using MVT = typename Belos::MultiVecTraits<ST,MV>;
58 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
59
60 using Teuchos::RCP;
61 using Teuchos::rcp;
62 using Teuchos::ParameterList;
63
64 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout);
65 const auto comm = Tpetra::getDefaultComm();
66 const int myPID = comm->getRank();
67
68 bool verbose = false;
69 bool success = true;
70
71 try {
72 bool procVerbose = false;
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 maxSubspace = 50; // maximum number of blocks the solver can use for the subspace
78 int maxRestarts = 15; // number of restarts allowed
79 std::string filename("orsirr1.hb");
80 MT tol = 1.0e-5; // relative residual tolerance
81
82 Teuchos::CommandLineProcessor cmdp(false,true);
83 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
84 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
85 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
86 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
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-subspace",&maxSubspace,"Maximum number of blocks the solver can use for the subspace.");
91 cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver.");
92 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
93 return -1;
94 }
95 if (!verbose)
96 frequency = -1; // reset frequency if test is not verbose
97
98 procVerbose = ( verbose && (myPID==0) );
99
100 if (procVerbose) {
101 std::cout << Belos::Belos_Version() << std::endl << std::endl;
102 }
103
104 // Get the problem
105 Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm );
106 RCP<tcrsmatrix_t> A = reader.readFromFile( filename );
107 RCP<const tmap_t> Map = A->getDomainMap();
108
109 // Create initial vectors
110 RCP<MV> B, X;
111 X = rcp( new MV(Map,numrhs) );
112 MVT::MvRandom( *X );
113 B = rcp( new MV(Map,numrhs) );
114 OPT::Apply( *A, *X, *B );
115 MVT::MvInit( *X, 0.0 );
116
117 // ********Other information used by block solver***********
118 // *****************(can be user specified)******************
119
120 const int numGlobalElements = B->getGlobalLength();
121 if (maxIters == -1)
122 maxIters = numGlobalElements - 1; // maximum number of iterations to run
123
124 ParameterList belosList;
125 belosList.set( "Num Blocks", maxSubspace); // Maximum number of blocks in Krylov factorization
126 belosList.set( "Block Size", blockSize ); // BlockSize to be used by iterative solver
127 belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed
128 belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed
129 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
130 if (verbose) {
131 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
132 Belos::TimingDetails + Belos::StatusTestDetails );
133 if (frequency > 0)
134 belosList.set( "Output Frequency", frequency );
135 }
136 else
137 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
138
139 // Construct an unpreconditioned linear problem instance.
140 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
141 bool set = problem.setProblem();
142 if (set == false) {
143 if (procVerbose)
144 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
145 return -1;
146 }
147
148 // *******************************************************************
149 // *************Start the block Gmres iteration*************************
150 // *******************************************************************
151
152 // Create an iterative solver manager.
153 RCP< Belos::SolverManager<ST,MV,OP> > newSolver
154 = rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
155
156 // **********Print out information about problem*******************
157
158 if (procVerbose) {
159 std::cout << std::endl << std::endl;
160 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
161 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
162 std::cout << "Block size used by solver: " << blockSize << std::endl;
163 std::cout << "Max number of restarts allowed: " << maxRestarts << std::endl;
164 std::cout << "Max number of Gmres iterations per restart cycle: " << maxIters << std::endl;
165 std::cout << "Relative residual tolerance: " << tol << std::endl;
166 std::cout << std::endl;
167 }
168
169 // Perform solve
170 Belos::ReturnType ret = newSolver->solve();
171
172 // Compute actual residuals.
173 bool badRes = false;
174 std::vector<ST> actualResids( numrhs );
175 std::vector<ST> rhsNorm( numrhs );
176 MV resid(Map, numrhs);
177 OPT::Apply( *A, *X, resid );
178 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
179 MVT::MvNorm( resid, actualResids );
180 MVT::MvNorm( *B, rhsNorm );
181 if (procVerbose) {
182 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
183 for ( int i=0; i<numrhs; i++) {
184 ST actRes = actualResids[i]/rhsNorm[i];
185 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
186 if (actRes > tol) badRes = true;
187 }
188 }
189
190 if (ret!=Belos::Converged || badRes) {
191 success = false;
192 if (procVerbose)
193 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
194 } else {
195 success = true;
196 if (procVerbose)
197 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
198 }
199 }
200 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
201
202 return success ? EXIT_SUCCESS : EXIT_FAILURE;
203 }
204
205 int main(int argc, char *argv[]) {
206 // run with different ST
207 return run<double>(argc,argv);
208 // run<float>(argc,argv); // FAILS
209 }