Debian Codegraph @

packages/belos/tpetra/test/BlockGmres/test_bl_gmres_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 "BelosBlockGmresSolMgr.hpp"
20
21 // I/O for Harwell-Boeing files
22 #include <Tpetra_MatrixIO.hpp>
23
24 #include <Teuchos_CommandLineProcessor.hpp>
25 #include <Teuchos_ParameterList.hpp>
26 #include <Teuchos_StandardCatchMacros.hpp>
27 #include <Tpetra_Core.hpp>
28 #include <Tpetra_CrsMatrix.hpp>
29
30 using namespace Teuchos;
31 using Tpetra::Operator;
32 using Tpetra::CrsMatrix;
33 using Tpetra::MultiVector;
34 using std::endl;
35 using std::cout;
36 using std::vector;
37 using Teuchos::tuple;
38
39 int main(int argc, char *argv[]) {
40 typedef Tpetra::MultiVector<>::scalar_type ST;
41 typedef ScalarTraits<ST> SCT;
42 typedef SCT::magnitudeType MT;
43 typedef Tpetra::Operator<ST> OP;
44 typedef Tpetra::MultiVector<ST> MV;
45 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
46 typedef Belos::MultiVecTraits<ST,MV> MVT;
47
48 Tpetra::ScopeGuard tpetraScope(&argc,&argv);
49
50 bool success = false;
51 bool verbose = false;
52 try {
53 const ST one = SCT::one();
54
55 int MyPID = 0;
56
57 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
58 //
59 // Get test parameters from command-line processor
60 //
61 bool proc_verbose = false;
62 bool debug = false;
63 int frequency = -1; // how often residuals are printed by solver
64 int numrhs = 1; // total number of right-hand sides to solve for
65 int blocksize = 1; // blocksize used by solver
66 int maxiters = -1; // maximum number of iterations for solver to use
67 std::string ortho("DGKS"); // orthogonalization type
68 std::string filename("bcsstk14.hb");
69 MT tol = 1.0e-5; // relative residual tolerance
70
71 CommandLineProcessor cmdp(false,true);
72 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
73 cmdp.setOption("debug","nodebug",&debug,"Run debugging checks.");
74 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
75 cmdp.setOption("tol",&tol,"Relative residual tolerance used by Gmres solver.");
76 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
77 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
78 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
79 cmdp.setOption("block-size",&blocksize,"Block size to be used by the Gmres solver.");
80 cmdp.setOption("ortho-type",&ortho,"Orthogonalization type, either DGKS, ICGS or IMGS (or TSQR if enabled)");
81 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
82 return -1;
83 }
84 if (debug) {
85 verbose = true;
86 }
87 if (!verbose) {
88 frequency = -1; // reset frequency if test is not verbose
89 }
90
91 MyPID = rank(*comm);
92 proc_verbose = ( verbose && (MyPID==0) );
93
94 if (proc_verbose) {
95 std::cout << Belos::Belos_Version() << std::endl << std::endl;
96 }
97
98 //
99 // Get the data from the HB file and build the Map,Matrix
100 //
101 RCP<CrsMatrix<ST> > A;
102 Tpetra::Utils::readHBMatrix(filename,comm,A);
103 RCP<const Tpetra::Map<> > map = A->getDomainMap();
104
105 // Create initial vectors
106 RCP<MV> B, X;
107 X = rcp( new MV(map,numrhs) );
108 MVT::MvRandom( *X );
109 B = rcp( new MV(map,numrhs) );
110 OPT::Apply( *A, *X, *B );
111 MVT::MvInit( *X, 0.0 );
112
113 //
114 // ********Other information used by block solver***********
115 // *****************(can be user specified)******************
116 //
117 const int NumGlobalElements = B->getGlobalLength();
118 if (maxiters == -1) {
119 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
120 }
121 //
122 ParameterList belosList;
123 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
124 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
125 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
126 belosList.set( "Orthogonalization", ortho ); // Orthogonalization type
127 if ( ortho == "DGKS" )
128 belosList.set( "Orthogonalization Constant", 1.41421356);
129
130 int verbLevel = Belos::Errors + Belos::Warnings;
131 if (debug) {
132 verbLevel += Belos::Debug;
133 }
134 if (verbose) {
135 verbLevel += Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails;
136 }
137 belosList.set( "Verbosity", verbLevel );
138 if (verbose) {
139 if (frequency > 0) {
140 belosList.set( "Output Frequency", frequency );
141 }
142 }
143 //
144 // Construct an unpreconditioned linear problem instance.
145 //
146 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
147 problem.setInitResVec( 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 block Gmres iteration***********************
157 // *******************************************************************
158 //
159 Belos::BlockGmresSolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(belosList) );
160
161 //
162 // **********Print out information about problem*******************
163 //
164 if (proc_verbose) {
165 std::cout << std::endl << std::endl;
166 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
167 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
168 std::cout << "Block size used by solver: " << blocksize << std::endl;
169 std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
170 std::cout << "Relative residual tolerance: " << tol << std::endl;
171 std::cout << std::endl;
172 }
173 //
174 // Perform solve
175 //
176 Belos::ReturnType ret = solver.solve();
177 //
178 // Compute actual residuals.
179 //
180 bool badRes = false;
181 std::vector<MT> actual_resids( numrhs );
182 std::vector<MT> rhs_norm( numrhs );
183 MV resid(map, numrhs);
184 OPT::Apply( *A, *X, resid );
185 MVT::MvAddMv( -one, resid, one, *B, resid );
186 MVT::MvNorm( resid, actual_resids );
187 MVT::MvNorm( *B, rhs_norm );
188 if (proc_verbose) {
189 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
190 }
191 for ( int i=0; i<numrhs; i++) {
192 MT actRes = actual_resids[i]/rhs_norm[i];
193 if (proc_verbose) {
194 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
195 }
196 if (actRes > tol) badRes = true;
197 }
198
199 success = (ret==Belos::Converged && !badRes);
200
201 if (success) {
202 if (proc_verbose)
203 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
204 } else {
205 if (proc_verbose)
206 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
207 }
208 }
209 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
210
211 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
212 } // end test_bl_cg_hb.cpp