Debian Codegraph @

packages/belos/tpetra/example/RCG/RCGTpetraExFile.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 from the HB file is used instead of random vectors.
12 // The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16
17 // Tpetra
18 #include <Tpetra_Core.hpp>
19 #include <Tpetra_CrsMatrix.hpp>
20 #include <Tpetra_Map.hpp>
21 #include <Tpetra_MatrixIO.hpp>
22
23 // Belos
24 #include "BelosConfigDefs.hpp"
25 #include "BelosLinearProblem.hpp"
26 #include "BelosTpetraAdapter.hpp"
27 #include "BelosRCGSolMgr.hpp"
28
29 // Teuchos
30 #include <Teuchos_CommandLineProcessor.hpp>
31 #include <Teuchos_ParameterList.hpp>
32 #include <Teuchos_StandardCatchMacros.hpp>
33
34 template<typename ScalarType>
35 int run(int argc, char *argv[])
36 {
37 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
38 using LO = typename Tpetra::MultiVector<>::local_ordinal_type;
39 using GO = typename Tpetra::MultiVector<>::global_ordinal_type;
40 using NT = typename Tpetra::MultiVector<>::node_type;
41
42 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
43 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
44
45 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
46 using tmap_t = Tpetra::Map<LO,GO,NT>;
47 using tmultivector_t = Tpetra::MultiVector<ST,LO,GO,NT>;
48
49 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
50 using MVT = typename Belos::MultiVecTraits<ST,MV>;
51
52 using Teuchos::ParameterList;
53 using Teuchos::RCP;
54 using Teuchos::rcp;
55
56 // This calls MPI_Init and MPI_Finalize as necessary.
57 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
58 RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm();
59
60 bool verbose = true;
61 bool success = true;
62
63 try
64 {
65 int MyPID = rank(*comm);
66
67 // Get test parameters from command-line processor
68 bool proc_verbose = false;
69 int frequency = -1; // frequency of status test output.
70 std::string filename("bcsstk14.hb"); // default input filename
71 double tol = 1.0e-6; // relative residual tolerance
72 int numBlocks = 100; // maximum number of blocks the solver can use for the Krylov subspace
73 int recycleBlocks = 10; // maximum number of blocks the solver can use for the recycle space
74 int numrhs = 2; // number of right-hand sides to solve for
75 int maxiters = 4000; // maximum number of iterations allowed per linear system
76
77 Teuchos::CommandLineProcessor cmdp(false,true);
78 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
79 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
80 cmdp.setOption("filename",&filename,"Filename for test matrix.");
81 cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver.");
82 cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space).");
83 cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space.");
84 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
85 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
86 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
87 return -1;
88 }
89 if (!verbose)
90 frequency = -1; // reset frequency if test is not verbose
91
92 // Get the problem
93 RCP<tcrsmatrix_t> A;
94 Tpetra::Utils::readHBMatrix(filename, comm, A);
95 RCP<const tmap_t> rowMap = A->getDomainMap();
96
97 proc_verbose = ( verbose && (MyPID==0) );
98
99 // Construct initial guess and right-hand sides
100 RCP<tmultivector_t> B, X;
101 X = rcp(new MV(rowMap, numrhs));
102 MVT::MvRandom(*X);
103 B = rcp(new MV( rowMap, numrhs));
104 OPT::Apply( *A, *X, *B );
105 MVT::MvInit( *X, 0.0 );
106
107 // Other information used by block solver (can be user specified)
108 const int NumGlobalElements = B->getGlobalLength();
109 if (maxiters == -1)
110 maxiters = NumGlobalElements - 1; // maximum number of iterations to run
111
112 //
113 ParameterList belosList;
114 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
115 belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space
116 belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space
117 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
118 if (verbose) {
119 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
120 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
121 if (frequency > 0)
122 belosList.set( "Output Frequency", frequency );
123 }
124 else
125 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
126
127 // Construct an unpreconditioned linear problem instance.
128 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
129 bool set = problem.setProblem();
130 if (set == false) {
131 if (proc_verbose)
132 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
133 return -1;
134 }
135
136 // Create an iterative solver manager.
137 RCP< Belos::SolverManager<ST,MV,OP> > newSolver
138 = rcp( new Belos::RCGSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
139
140 // Print out information about problem
141 if (proc_verbose) {
142 std::cout << std::endl << std::endl;
143 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
144 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
145 std::cout << "Max number of RCG iterations: " << maxiters << std::endl;
146 std::cout << "Max number of vectors in Krylov space: " << numBlocks << std::endl;
147 std::cout << "Number of vectors in recycle space: " << recycleBlocks << 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 = newSolver->solve();
154
155 // Compute actual residuals
156 bool badRes = false;
157 std::vector<ST> actual_resids( numrhs );
158 std::vector<ST> rhs_norm( numrhs );
159 MV resid(rowMap, numrhs);
160 OPT::Apply( *A, *X, resid );
161 MVT::MvAddMv( -1.0, resid, 1.0, *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 for ( int i=0; i<numrhs; i++) {
167 double actRes = actual_resids[i]/rhs_norm[i];
168 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
169 if (actRes > tol) badRes = true;
170 }
171 }
172
173 if (ret!=Belos::Converged || badRes) {
174 success = false;
175 if (proc_verbose)
176 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
177 }
178 else {
179 success = true;
180 if (proc_verbose)
181 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
182 }
183 }
184 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
185
186 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
187 }
188
189 int main(int argc, char *argv[]) {
190 return run<double>(argc,argv);
191
192 // wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON
193 // return run<float>(argc,argv);
194 }