Debian Codegraph @

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