Debian Codegraph @

packages/belos/tpetra/test/BlockCG/test_bl_cg_complex_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 "BelosBlockCGSolMgr.hpp"
20 #include "BelosTpetraTestFramework.hpp"
21
22 #include <Teuchos_CommandLineProcessor.hpp>
23 #include <Teuchos_ParameterList.hpp>
24 #include <Teuchos_GlobalMPISession.hpp>
25
26 #include <Tpetra_Core.hpp>
27 #include <Tpetra_CrsMatrix.hpp>
28
29 // I/O for Harwell-Boeing files
30 #include <Trilinos_Util_iohb.h>
31
32 template <typename ScalarType>
33 int run(int argc, char *argv[])
34 {
35 using BSC = typename Tpetra::MultiVector<ScalarType>::scalar_type;
36 using ST = typename std::complex<BSC>;
37
38 using OP = typename Tpetra::Operator<ST>;
39 using MV = typename Tpetra::MultiVector<ST>;
40 using tcrsmatrix_t = Tpetra::CrsMatrix<ST>;
41
42 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
43 using MVT = typename Belos::MultiVecTraits<ST,MV>;
44
45 using SCT = typename Teuchos::ScalarTraits<ST>;
46 using MT = typename SCT::magnitudeType;
47
48 using Tpetra::Operator;
49 using Tpetra::CrsMatrix;
50 using Tpetra::MultiVector;
51
52 using Teuchos::GlobalMPISession;
53 using Teuchos::Comm;
54 using Teuchos::RCP;
55 using Teuchos::CommandLineProcessor;
56 using Teuchos::ParameterList;
57
58 const ST one = SCT::one();
59
60 GlobalMPISession mpisess(&argc,&argv,&std::cout);
61 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
62 int MyPID = rank(*comm);
63
64 // Get test parameters from command-line processor
65 bool verbose = false, proc_verbose = false, debug = false;
66 int frequency = -1; // how often residuals are printed by solver
67 int numrhs = 1; // total number of right-hand sides to solve for
68 int blocksize = 1; // blocksize used by solver
69 int maxiters = -1; // maximum number of iterations for solver to use
70 std::string filename("mhd1280b.cua");
71 MT tol = 1.0e-5; // relative residual tolerance
72
73 CommandLineProcessor cmdp(false,true);
74 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
75 cmdp.setOption("debug","nodebug",&debug,"Run debugging checks.");
76 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
77 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
78 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
79 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
80 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
81 cmdp.setOption("block-size",&blocksize,"Block size to be used by the CG solver.");
82 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
83 return -1;
84 }
85 if (debug) {
86 verbose = true;
87 }
88 if (!verbose) {
89 frequency = -1; // reset frequency if test is not verbose
90 }
91
92 proc_verbose = ( verbose && (MyPID==0) );
93 if (proc_verbose) {
94 std::cout << Belos::Belos_Version() << std::endl << std::endl;
95 }
96
97 Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm );
98 RCP<tcrsmatrix_t> A = reader.readFromFile( filename );
99 RCP<const Tpetra::Map<> > map = A->getMap();
100
101 // Create initial vectors
102 RCP<MV> B, X;
103 X = rcp( new MV(map,numrhs) );
104 MVT::MvRandom( *X );
105 B = rcp( new MV(map,numrhs) );
106 OPT::Apply( *A, *X, *B );
107 MVT::MvInit( *X, 0.0 );
108
109 // Other information used by block solver (can be user specified)
110 const int NumGlobalElements = B->getGlobalLength();
111 if (maxiters == -1) {
112 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
113 }
114
115 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 if (ret!=Belos::Converged || badRes) {
180 if (proc_verbose) {
181 std::cout << "\nEnd Result: TEST FAILED" << std::endl;
182 }
183 return -1;
184 }
185
186 // Default return value
187 if (proc_verbose) {
188 std::cout << "\nEnd Result: TEST PASSED" << std::endl;
189 }
190
191 return 0;
192 } // end test_bl_cg_complex_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