Debian Codegraph @

packages/belos/tpetra/test/RCG/test_rcg_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 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosTpetraAdapter.hpp"
19 #include "BelosRCGSolMgr.hpp"
20
21 #include <Teuchos_CommandLineProcessor.hpp>
22 #include <Teuchos_ParameterList.hpp>
23 #include <Teuchos_GlobalMPISession.hpp>
24 #include <Tpetra_Core.hpp>
25 #include <Tpetra_CrsMatrix.hpp>
26
27 // I/O for Harwell-Boeing files
28 #define HIDE_TPETRA_INOUT_IMPLEMENTATIONS
29 #include <Tpetra_MatrixIO.hpp>
30
31 using namespace Teuchos;
32 using Tpetra::Operator;
33 using Tpetra::CrsMatrix;
34 using Tpetra::MultiVector;
35 using std::endl;
36 using std::cout;
37 using std::vector;
38 using Teuchos::tuple;
39
40 int main(int argc, char *argv[]) {
41
42 typedef Tpetra::MultiVector<>::scalar_type ST;
43 typedef ScalarTraits<ST> SCT;
44 typedef SCT::magnitudeType MT;
45 typedef Tpetra::Operator<ST> OP;
46 typedef Tpetra::MultiVector<ST> MV;
47 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
48 typedef Belos::MultiVecTraits<ST,MV> MVT;
49
50 GlobalMPISession mpisess(&argc,&argv,&cout);
51
52 const ST one = SCT::one();
53
54 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
55 int MyPID = rank(*comm);
56
57 //
58 // Get test parameters from command-line processor
59 //
60 bool verbose = false, proc_verbose = false, debug = false;
61 int frequency = -1; // how often residuals are printed by solver
62 std::string filename("bcsstk14.hb");
63 int numBlocks = 30; // maximum number of blocks the solver can use for the Krylov subspace
64 int recycleBlocks = 3; // maximum number of blocks the solver can use for the recycle space
65 int numrhs = 1; // number of right-hand sides to solve for
66 int maxiters = -1; // maximum number of iterations allowed per linear system
67 MT tol = 1.0e-5; // relative residual tolerance
68
69 CommandLineProcessor cmdp(false,true);
70 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
71 cmdp.setOption("debug","nodebug",&debug,"Run debugging checks.");
72 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
73 cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver.");
74 cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space).");
75 cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space.");
76 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
77 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
78 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
79 if (cmdp.parse(argc,argv) != 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
89 proc_verbose = ( verbose && (MyPID==0) );
90
91 if (proc_verbose) {
92 std::cout << Belos::Belos_Version() << std::endl << std::endl;
93 }
94
95 RCP<CrsMatrix<ST> > A;
96 Tpetra::Utils::readHBMatrix(filename,comm,A);
97 RCP<const Tpetra::Map<> > map = A->getDomainMap();
98
99 // Create initial vectors
100 RCP<MV> B, X;
101 X = rcp( new MV(map,numrhs) );
102 MVT::MvRandom( *X );
103 B = rcp( new MV(map,numrhs) );
104 OPT::Apply( *A, *X, *B );
105 MVT::MvInit( *X, 0.0 );
106
107 //
108 // ********Other information used by block solver***********
109 // *****************(can be user specified)******************
110 //
111 int numIters1;
112 const int NumGlobalElements = B->getGlobalLength();
113 if (maxiters == -1)
114 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
115 //
116 ParameterList belosList;
117 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
118 belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space
119 belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space
120 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
121 if (verbose) {
122 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
123 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
124 if (frequency > 0)
125 belosList.set( "Output Frequency", frequency );
126 }
127 else
128 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
129 //
130 // Construct an unpreconditioned linear problem instance.
131 //
132 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
133 bool set = problem.setProblem();
134 if (set == false) {
135 if (proc_verbose)
136 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
137 return -1;
138 }
139 //
140 // *******************************************************************
141 // *********************Start the RCG iteration***********************
142 // *******************************************************************
143 //
144 Belos::RCGSolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(belosList) );
145
146 //
147 // **********Print out information about problem*******************
148 //
149 if (proc_verbose) {
150 std::cout << std::endl << std::endl;
151 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
152 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
153 std::cout << "Max number of RCG iterations: " << maxiters << std::endl;
154 std::cout << "Relative residual tolerance: " << tol << std::endl;
155 std::cout << std::endl;
156 }
157 //
158 // Perform solve
159 //
160 Belos::ReturnType ret = solver.solve();
161 numIters1=solver.getNumIters();
162 //
163 // Compute actual residuals.
164 //
165 bool badRes = false;
166 std::vector<MT> actual_resids( numrhs );
167 std::vector<MT> rhs_norm( numrhs );
168 MV resid(map, numrhs);
169 OPT::Apply( *A, *X, resid );
170 MVT::MvAddMv( -one, resid, one, *B, resid );
171 MVT::MvNorm( resid, actual_resids );
172 MVT::MvNorm( *B, rhs_norm );
173 if (proc_verbose) {
174 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
175 }
176 for ( int i=0; i<numrhs; i++) {
177 MT actRes = actual_resids[i]/rhs_norm[i];
178 if (proc_verbose) {
179 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
180 }
181 if (actRes > tol) badRes = true;
182 }
183
184 if (proc_verbose) { std::cout << "Solve took " << numIters1 << " iterations." << std::endl; }
185
186 if ( ret!=Belos::Converged || badRes ) {
187 if (proc_verbose) {
188 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
189 }
190 return -1;
191 }
192 //
193 // Default return value
194 //
195 if (proc_verbose) {
196 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
197 }
198 return 0;
199 //
200 } // end test_rcg_hb.cpp