Debian Codegraph @

packages/belos/tpetra/example/BlockCG/BlockCGTpetraExFile.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 // Update of belos/epetra/example/BlockCG/BlockCGEpetraExFile.cpp
11 //
12 // This driver reads a problem from a Harwell-Boeing (HB) file.
13 // The right-hand-side corresponds to a randomly generated solution.
14 // The initial guesses are all set to zero.
15 //
16 // NOTE: No preconditioner is used in this example.
17 //
18
19 // Tpetra
20 #include <Tpetra_Map.hpp>
21 #include <Tpetra_Core.hpp>
22 #include <Tpetra_Vector.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 // Teuchos
29 #include <Teuchos_RCP.hpp>
30 #include <Teuchos_Comm.hpp>
31 #include <Teuchos_CommHelpers.hpp>
32 #include <Teuchos_DefaultComm.hpp>
33 #include "Teuchos_ParameterList.hpp"
34 #include "Teuchos_StandardCatchMacros.hpp"
35 #include "Teuchos_CommandLineProcessor.hpp"
36
37 // Belos
38 #include "BelosConfigDefs.hpp"
39 #include "BelosBlockCGSolMgr.hpp"
40 #include "BelosLinearProblem.hpp"
41 #include "BelosTpetraAdapter.hpp"
42
43
44 template <typename ScalarType>
45 int run(int argc, char *argv[]) {
46
47 using ST = typename Tpetra::Vector<ScalarType>::scalar_type;
48 using LO = typename Tpetra::Vector<>::local_ordinal_type;
49 using GO = typename Tpetra::Vector<>::global_ordinal_type;
50 using NT = typename Tpetra::Vector<>::node_type;
51
52 using SCT = typename Teuchos::ScalarTraits<ST>;
53 using MT = typename SCT::magnitudeType;
54 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
55 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
56
57 using tmap_t = Tpetra::Map<LO,GO,NT>;
58 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
59
60 using MVT = typename Belos::MultiVecTraits<ST,MV>;
61 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
62
63 using Teuchos::RCP;
64 using Teuchos::rcp;
65 using Teuchos::ParameterList;
66
67 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout);
68 const auto Comm = Tpetra::getDefaultComm();
69 const int MyPID = Comm->getRank();
70
71 bool verbose = false;
72 bool success = true;
73 try {
74 bool procVerbose = false;
75 int frequency = -1; // frequency of status test output.
76 int blockSize = 1; // blockSize
77 int numrhs = 1; // number of right-hand sides to solve for
78 int maxIters = -1; // maximum number of iterations allowed per linear system
79 std::string filename("bcsstk14.hb");
80 MT tol = 1.0e-5; // relative residual tolerance
81
82 Teuchos::CommandLineProcessor cmdp(false,true);
83 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
84 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
85 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
86 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
87 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
88 cmdp.setOption("block-size",&blockSize,"Block size used by CG.");
89 cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
90 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
91 return -1;
92 }
93 if (!verbose)
94 frequency = -1; // reset frequency if test is not verbose
95
96 procVerbose = verbose && (MyPID==0); /* Only print on the zero processor */
97
98 // Get the problem
99 RCP<tcrsmatrix_t> A;
100 Tpetra::Utils::readHBMatrix(filename,Comm,A);
101 RCP<const tmap_t> Map = A->getDomainMap();
102
103 // Create initial vectors
104 RCP<MV> B, X;
105 X = rcp( new MV(Map,numrhs) );
106 MVT::MvRandom( *X );
107 B = rcp( new MV(Map,numrhs) );
108 OPT::Apply( *A, *X, *B );
109 MVT::MvInit( *X, 0.0 );
110
111 // ********Other information used by block solver************
112 // *****************(can be user specified)******************
113
114 const int numGlobalElements = B->getGlobalLength();
115 if (maxIters == -1)
116 maxIters = numGlobalElements/blockSize - 1; // maximum number of iterations to run
117 //
118 ParameterList belosList;
119 belosList.set( "Block Size", blockSize ); // BlockSize to be used by iterative solver
120 belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed
121 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
122 if (verbose) {
123 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
124 Belos::TimingDetails + Belos::StatusTestDetails );
125 if (frequency > 0)
126 belosList.set( "Output Frequency", frequency );
127 }
128 else
129 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
130
131 // Construct an unpreconditioned linear problem instance.
132 Belos::LinearProblem<ST,MV,OP> problem( A, X, B );
133 bool set = problem.setProblem();
134 if (set == false) {
135 if (procVerbose)
136 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
137 return -1;
138 }
139
140 // Create an iterative solver manager.
141 RCP< Belos::BlockCGSolMgr<ST,MV,OP> > newSolver
142 = rcp( new Belos::BlockCGSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
143
144 // **********Print out information about problem*******************
145
146 if (procVerbose) {
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 << "Max number of iterations allowed: " << maxIters << std::endl;
151 std::cout << "Block size used by solver: " << blockSize << 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 = newSolver->solve();
158
159 // Get the number of iterations for this solve.
160 int numIters = newSolver->getNumIters();
161 if (procVerbose)
162 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
163
164 // Compute actual residuals.
165 bool badRes = false;
166 std::vector<ST> actualResids( numrhs );
167 std::vector<ST> rhsNorm( numrhs );
168 MV resid(Map, numrhs);
169 OPT::Apply( *A, *X, resid );
170 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
171 MVT::MvNorm( resid, actualResids );
172 MVT::MvNorm( *B, rhsNorm );
173 if (procVerbose) {
174 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
175 for ( int i=0; i<numrhs; i++) {
176 ST actRes = actualResids[i]/rhsNorm[i];
177 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
178 if (actRes > tol) badRes = true;
179 }
180 }
181
182 if (ret!=Belos::Converged || badRes) {
183 success = false;
184 if (procVerbose)
185 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
186 } else {
187 success = true;
188 if (procVerbose)
189 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
190 }
191 }
192 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
193
194 return success ? EXIT_SUCCESS : EXIT_FAILURE;
195 }
196
197 int main(int argc, char *argv[]) {
198 // run with different ST
199 return run<double>(argc,argv);
200 // run<float>(argc,argv); // FAILS
201 }