Debian Codegraph @

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