Debian Codegraph @

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