Debian Codegraph @

packages/belos/tpetra/test/BlockCG/test_pseudo_stochastic_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 "BelosPseudoBlockStochasticCGSolMgr.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_StandardCatchMacros.hpp>
30
31 template <typename ScalarType>
32 int run (int argc, char *argv[])
33 {
34 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
35 using OP = typename Tpetra::Operator<ST>;
36 using MV = typename Tpetra::MultiVector<ST>;
37
38 using STS = typename Teuchos::ScalarTraits<ST>;
39 using MT = typename STS::magnitudeType;
40
41 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
42 using MVT = typename Belos::MultiVecTraits<ST,MV>;
43
44 using Teuchos::Comm;
45 using Teuchos::ParameterList;
46 using Teuchos::RCP;
47 using Teuchos::rcp;
48 using Teuchos::rcpFromRef;
49
50 Tpetra::ScopeGuard tpetraScope (&argc, &argv);
51
52 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
53 const int MyPID = comm->getRank();
54
55 bool success = false;
56 bool verbose = false;
57
58 try
59 {
60 // Get test parameters from command-line processor
61 bool proc_verbose = false;
62 bool debug = false;
63 int frequency = -1;
64 int numrhs = 1;
65 int blocksize = 1;
66 int maxiters = -1;
67 std::string filename ("bcsstk14.hb");
68 MT tol = 1.0e-5;
69
70 Teuchos::CommandLineProcessor cmdp (false, true);
71 cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and "
72 "results.");
73 cmdp.setOption ("debug", "nodebug", &debug, "Run debugging checks.");
74 cmdp.setOption ("frequency", &frequency, "Solver's frequency for printing "
75 "residuals (#iters). -1 means \"never\"; 1 means \"every "
76 "iteration.\"");
77 cmdp.setOption ("tol", &tol, "Relative residual tolerance used by solver.");
78 cmdp.setOption ("filename", &filename, "Filename for Harwell-Boeing test "
79 "matrix.");
80 cmdp.setOption ("num-rhs", &numrhs, "Number of right-hand sides for which "
81 "to solve.");
82 cmdp.setOption ("max-iters", &maxiters, "Maximum number of iterations per "
83 "linear system (-1 := adapted to problem/block size).");
84 cmdp.setOption ("block-size", &blocksize, "Block size to be used by the "
85 "solver.");
86 if (cmdp.parse (argc, argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
87 return -1;
88 }
89 if (debug) {
90 verbose = true;
91 }
92 if (!verbose) {
93 frequency = -1; // reset frequency if test is not verbose
94 }
95
96 proc_verbose = verbose && MyPID == 0;
97 if (proc_verbose) {
98 std::cout << Belos::Belos_Version () << std::endl << std::endl;
99 }
100
101 // Get the data from the HB file and build the Map,Matrix
102 RCP<Tpetra::CrsMatrix<ST> > A;
103 Tpetra::Utils::readHBMatrix (filename, comm, A);
104 RCP<const Tpetra::Map<> > map = A->getDomainMap ();
105
106 // Create initial vectors
107 RCP<MV> B, X;
108 X = rcp (new MV (map, numrhs));
109 MVT::MvRandom (*X);
110 B = rcp (new MV (map, numrhs));
111 OPT::Apply (*A, *X, *B);
112 MVT::MvInit (*X, STS::zero ());
113
114 // Other information used by block solver (can be user specified)
115 const int NumGlobalElements = B->getGlobalLength();
116 if (maxiters == -1) {
117 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
118 }
119
120 ParameterList belosList;
121 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
122 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
123 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
124 int verbLevel = Belos::Errors + Belos::Warnings;
125 if (debug) {
126 verbLevel += Belos::Debug;
127 }
128 if (verbose) {
129 verbLevel += Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails;
130 }
131 belosList.set( "Verbosity", verbLevel );
132 if (verbose) {
133 if (frequency > 0) {
134 belosList.set( "Output Frequency", frequency );
135 }
136 }
137
138 // Construct an unpreconditioned linear problem instance.
139 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
140 bool set = problem.setProblem ();
141 if (! set) {
142 if (proc_verbose) {
143 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
144 }
145 return EXIT_FAILURE;
146 }
147
148 // Start the block CG iteration
149 using solver_type = typename Belos::PseudoBlockStochasticCGSolMgr<ST,MV,OP>;
150 solver_type solver (rcpFromRef (problem), rcpFromRef (belosList));
151
152 // Print out information about problem
153 if (proc_verbose) {
154 std::cout << std::endl << std::endl;
155 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
156 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
157 std::cout << "Block size used by solver: " << blocksize << std::endl;
158 std::cout << "Max number of CG iterations: " << maxiters << std::endl;
159 std::cout << "Relative residual tolerance: " << tol << std::endl;
160 std::cout << std::endl;
161 }
162
163 // Perform solve
164 Belos::ReturnType ret = solver.solve();
165
166 // Compute actual residuals.
167 bool badRes = false;
168 std::vector<MT> actual_resids (numrhs);
169 std::vector<MT> rhs_norm (numrhs);
170 MV resid (map, numrhs);
171 OPT::Apply (*A, *X, resid);
172 MVT::MvAddMv (-STS::one (), resid, STS::one (), *B, resid);
173 MVT::MvNorm (resid, actual_resids);
174 MVT::MvNorm (*B, rhs_norm);
175 if (proc_verbose) {
176 std::cout << "---------- Actual Residuals (normalized) ----------" << std::endl << std::endl;
177 }
178 for (int i=0; i<numrhs; i++) {
179 MT actRes = actual_resids[i]/rhs_norm[i];
180 if (proc_verbose) {
181 std::cout << "Problem " << i << " : \t" << actRes << std::endl;
182 }
183 if (actRes > tol) badRes = true;
184 }
185
186 success = ret == Belos::Converged && ! badRes;
187 if (success) {
188 if (proc_verbose) {
189 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
190 }
191 } else {
192 if (proc_verbose) {
193 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
194 }
195 }
196 }
197 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
198
199 return success ? EXIT_SUCCESS : EXIT_FAILURE;
200 }
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
209