Debian Codegraph @

packages/belos/tpetra/example/BlockGmres/BlockGmresTpetraGaleriEx.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 // Tpetra
11 #include <Tpetra_Core.hpp>
12 #include <Tpetra_CrsMatrix.hpp>
13 #include <Tpetra_MultiVector.hpp>
14
15 // Galeri
16 #include <Galeri_XpetraMaps.hpp>
17 #include <Galeri_XpetraMatrixTypes.hpp>
18 #include <Galeri_XpetraProblemFactory.hpp>
19
20 // Teuchos
21 #include <Teuchos_RCP.hpp>
22 #include <Teuchos_Comm.hpp>
23 #include <Teuchos_CommHelpers.hpp>
24 #include <Teuchos_DefaultComm.hpp>
25 #include "Teuchos_ParameterList.hpp"
26 #include <Teuchos_TypeNameTraits.hpp>
27 #include <Teuchos_oblackholestream.hpp>
28 #include "Teuchos_StandardCatchMacros.hpp"
29 #include "Teuchos_CommandLineProcessor.hpp"
30
31 // Belos
32 #include "BelosTpetraAdapter.hpp"
33 #include "BelosBlockGmresSolMgr.hpp"
34 #include "BelosPseudoBlockGmresSolMgr.hpp"
35
36 // ****************************************************************************
37 // BEGIN RUN ROUTINE
38 // ****************************************************************************
39
40 template <typename ScalarType>
41 int run(int argc, char *argv[]) {
42
43 // Belos solvers have the following template parameters:
44 //
45 // - Scalar: The type of dot product results.
46 // - MV: The type of (multi)vectors.
47 // - OP: The type of operators (functions from multivector to
48 // multivector). A matrix (like Tpetra::CrsMatrix) is an example
49 // of an operator; an Ifpack2 preconditioner is another example.
50 //
51 // Here, ST is set by the main function, MV is Tpetra::MultiVector, and OP is
52 // Tpetra::Operator.
53 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
54 using LO = typename Tpetra::MultiVector<>::local_ordinal_type;
55 using GO = typename Tpetra::MultiVector<>::global_ordinal_type;
56 using NT = typename Tpetra::MultiVector<>::node_type;
57
58 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
59 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
60 using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType;
61
62 using tmap_t = Tpetra::Map<LO,GO,NT>;
63 using tvector_t = Tpetra::Vector<ST,LO,GO,NT>;
64 using trowmatrix_t = Tpetra::RowMatrix<ST,LO,GO,NT>;
65 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
66
67 using MVT = typename Belos::MultiVecTraits<ST,MV>;
68 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
69
70 using Teuchos::RCP;
71 using Teuchos::rcp;
72 using Teuchos::ParameterList;
73
74 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout);
75 const auto comm = Tpetra::getDefaultComm();
76 const int myPID = comm->getRank();
77
78 bool verbose = false;
79 bool success = true;
80
81 try {
82 bool procVerbose = false;
83 bool debug = false;
84 bool pseudo = false; // use pseudo-block or block gmres
85 int frequency = -1; // frequency of status test output
86 int blockSize = 1; // blockSize
87 int numrhs = 1; // number of right-hand sides to solve for
88 int maxIters = -1; // maximum number of iterations allowed per linear system
89 int maxSubspace = 50; // maximum number of blocks the solver can use for the subspace
90 int maxRestarts = 15; // number of restarts allowed
91 int nx = 10; // number of discretization points in each direction
92 MT tol = 1.0e-5; // relative residual tolerance
93 std::string ortho = "DGKS";// orthogonalization method
94
95 Teuchos::CommandLineProcessor cmdp(false,true);
96 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
97 cmdp.setOption("debug","nondebug",&debug,"Print debugging information from solver.");
98 cmdp.setOption("pseudo","block",&pseudo,"Use pseudo-block or block GMRES solver.");
99 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
100 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
101 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
102 cmdp.setOption("block-size",&blockSize,"Block size used by GMRES.");
103 cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
104 cmdp.setOption("max-subspace",&maxSubspace,"Maximum number of blocks the solver can use for the subspace.");
105 cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver.");
106 cmdp.setOption("nx",&nx,"Number of discretization points in each direction of 3D Laplacian.");
107 cmdp.setOption("ortho",&ortho,"Orthogonalization being used by GMRES solver.");
108 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
109 return -1;
110 }
111 if (!verbose)
112 frequency = -1; // reset frequency if test is not verbose
113
114 procVerbose = ( verbose && (myPID==0) ); // Only print on the zero processor
115
116 if (procVerbose) {
117 std::cout << Belos::Belos_Version() << std::endl << std::endl;
118 }
119
120 // Set up the test problem.
121 //
122 // We use Trilinos' Galeri package to construct a test problem.
123 // Here, we use a discretization of the 2-D Laplacian operator.
124 // The global mesh size is nx * nx.
125
126 Teuchos::ParameterList GaleriList;
127 GaleriList.set ("n", nx * nx * nx);
128 GaleriList.set ("nx", nx);
129 GaleriList.set ("ny", nx);
130 GaleriList.set ("nz", nx);
131
132 auto Map = RCP{Galeri::Xpetra::CreateMap<ST,GO,tmap_t>("Cartesian3D", comm, GaleriList)};
133 auto GaleriProblem = Galeri::Xpetra::BuildProblem<ST,LO,GO,tmap_t,tcrsmatrix_t,MV>("Laplace3D", Map, GaleriList);
134
135 // Create matrix from problem
136 auto A = GaleriProblem->BuildMatrix();
137
138 // Create RHS using random solution vector
139 RCP<MV> B = rcp (new MV (Map, numrhs));
140 RCP<MV> X = rcp (new MV (Map, numrhs));
141 RCP<MV> Xexact = rcp (new MV (Map, numrhs));
142 MVT::MvRandom(*Xexact);
143
144 OPT::Apply(*A, *Xexact, *B );
145
146 // ********Other information used by block solver***********
147 // *****************(can be user specified)******************
148
149 const int numGlobalElements = B->getGlobalLength();
150 if (maxIters == -1)
151 maxIters = numGlobalElements/blockSize - 1; // maximum number of iterations to run
152
153 ParameterList belosList;
154 belosList.set( "Num Blocks", maxSubspace); // Maximum number of blocks in Krylov factorization
155 belosList.set( "Block Size", blockSize ); // BlockSize to be used by iterative solver
156 belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed
157 belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed
158 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
159 belosList.set( "Orthogonalization", ortho ); // Orthogonalization used by iterative solver
160 int verbosity = Belos::Errors + Belos::Warnings;
161 if (verbose) {
162 verbosity += Belos::TimingDetails + Belos::StatusTestDetails;
163 if (frequency > 0)
164 belosList.set( "Output Frequency", frequency );
165 }
166 if (debug) {
167 verbosity += Belos::Debug;
168 }
169 belosList.set( "Verbosity", verbosity );
170
171 // Construct an unpreconditioned linear problem instance.
172 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
173 bool set = problem.setProblem();
174 if (set == false) {
175 if (procVerbose)
176 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
177 return -1;
178 }
179
180 // *******************************************************************
181 // *************Start the block Gmres iteration*************************
182 // *******************************************************************
183
184 // Create an iterative solver manager.
185 RCP< Belos::SolverManager<ST,MV,OP> > newSolver;
186 if (pseudo && (blockSize == 1))
187 newSolver = rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
188 else
189 newSolver = rcp( new Belos::BlockGmresSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)));
190
191 // **********Print out information about problem*******************
192
193 if (procVerbose) {
194 std::cout << std::endl << std::endl;
195 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
196 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
197 std::cout << "Block size used by solver: " << blockSize << std::endl;
198 std::cout << "Max number of restarts allowed: " << maxRestarts << std::endl;
199 std::cout << "Max number of Gmres iterations per linear system: " << maxIters << std::endl;
200 std::cout << "Relative residual tolerance: " << tol << std::endl;
201 std::cout << std::endl;
202 }
203
204 // Perform solve
205 Belos::ReturnType ret = newSolver->solve();
206
207 // Get the number of iterations for this solve.
208 int numIters = newSolver->getNumIters();
209 if (procVerbose)
210 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
211
212 // Compute actual residuals.
213 bool badRes = false;
214 std::vector<ST> actualResids( numrhs );
215 std::vector<ST> rhsNorm( numrhs );
216 MV resid(Map, numrhs);
217 OPT::Apply( *A, *X, resid );
218 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
219 MVT::MvNorm( resid, actualResids );
220 MVT::MvNorm( *B, rhsNorm );
221 if (procVerbose) {
222 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
223 for ( int i=0; i<numrhs; i++) {
224 ST actRes = actualResids[i]/rhsNorm[i];
225 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
226 if (actRes > tol) badRes = true;
227 }
228 }
229
230 if (ret!=Belos::Converged || badRes) {
231 success = false;
232 if (procVerbose)
233 std::cout << "End Result: TEST FAILED" << std::endl;
234 } else {
235 if (procVerbose)
236 std::cout << "End Result: TEST PASSED" << std::endl;
237 }
238 }
239 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
240
241 return success ? EXIT_SUCCESS : EXIT_FAILURE;
242 }
243
244 int main(int argc, char *argv[]) {
245 // run with different ST
246 return run<double>(argc,argv);
247 // run<float>(argc,argv); // FAILS
248 }