Debian Codegraph @

packages/belos/epetra/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 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 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosBlockCGSolMgr.hpp"
20 #include "BelosEpetraUtils.h"
21 #include "Trilinos_Util.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Epetra_Map.h"
24 #include "Teuchos_CommandLineProcessor.hpp"
25 #include "Teuchos_ParameterList.hpp"
26 #include "Teuchos_StandardCatchMacros.hpp"
27
28 int main(int argc, char *argv[]) {
29 //
30 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
31 //
32 using Teuchos::ParameterList;
33 using Teuchos::RCP;
34 using Teuchos::rcp;
35
36 bool success = false;
37 bool verbose = false;
38 try {
39 //
40 // Get test parameters from command-line processor
41 //
42 bool proc_verbose = false;
43 int frequency = -1; // how often residuals are printed by solver
44 int numrhs = 1; // total number of right-hand sides to solve for
45 int blocksize = 1; // blocksize used by solver
46 int maxiters = -1; // maximum number of iterations for solver to use
47 std::string filename("bcsstk14.hb");
48 double tol = 1.0e-5; // relative residual tolerance
49 bool use_single_red = false; // use single-reduction iteration.
50
51 Teuchos::CommandLineProcessor cmdp(false,true);
52 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
53 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
54 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
55 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
56 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
57 cmdp.setOption("block-size",&blocksize,"Block size to be used by CG solver.");
58 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
59 cmdp.setOption("use-single-red","use-standard-red",&use_single_red,"Use single-reduction CG iteration.");
60 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
61 return -1;
62 }
63 if (!verbose)
64 frequency = -1; // reset frequency if test is not verbose
65 //
66 // Get the problem
67 //
68 int MyPID;
69 RCP<Epetra_CrsMatrix> A;
70 RCP<Epetra_MultiVector> B, X;
71 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
72 if(return_val != 0) return return_val;
73 proc_verbose = ( verbose && (MyPID==0) );
74 //
75 // Solve using Belos
76 //
77 typedef double ST;
78 typedef Epetra_Operator OP;
79 typedef Epetra_MultiVector MV;
80 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
81 typedef Belos::MultiVecTraits<ST,MV> MVT;
82 //
83 // *****Construct initial guess and random right-hand-sides *****
84 //
85 if (numrhs != 1) {
86 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
87 MVT::MvRandom( *X );
88 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
89 OPT::Apply( *A, *X, *B );
90 MVT::MvInit( *X, 0.0 );
91 }
92 //
93 // ********Other information used by block solver***********
94 // *****************(can be user specified)******************
95 //
96 const int NumGlobalElements = B->GlobalLength();
97 if (maxiters == -1)
98 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
99 //
100 ParameterList belosList;
101 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
102 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
103 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
104 if ((blocksize==1) && use_single_red)
105 belosList.set( "Use Single Reduction", use_single_red ); // Use single reduction CG iteration
106 if (verbose) {
107 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
108 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
109 if (frequency > 0)
110 belosList.set( "Output Frequency", frequency );
111 }
112 else
113 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
114 //
115 // Construct an unpreconditioned linear problem instance.
116 //
117 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
118 bool set = problem.setProblem();
119 if (set == false) {
120 if (proc_verbose)
121 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
122 return -1;
123 }
124 //
125 // Create an iterative solver manager.
126 //
127 RCP< Belos::SolverManager<double,MV,OP> > newSolver
128 = rcp( new Belos::BlockCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) );
129 //
130 // **********Print out information about problem*******************
131 //
132 if (proc_verbose) {
133 std::cout << std::endl << std::endl;
134 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
135 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
136 std::cout << "Block size used by solver: " << blocksize << std::endl;
137 std::cout << "Max number of CG iterations: " << maxiters << std::endl;
138 std::cout << "Relative residual tolerance: " << tol << std::endl;
139 std::cout << std::endl;
140 }
141 //
142 // Perform solve
143 //
144 Belos::ReturnType ret = newSolver->solve();
145 //
146 // Compute actual residuals.
147 //
148 bool badRes = false;
149 std::vector<double> actual_resids( numrhs );
150 std::vector<double> rhs_norm( numrhs );
151 Epetra_MultiVector resid(A->Map(), numrhs);
152 OPT::Apply( *A, *X, resid );
153 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
154 MVT::MvNorm( resid, actual_resids );
155 MVT::MvNorm( *B, rhs_norm );
156 if (proc_verbose) {
157 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
158 for ( int i=0; i<numrhs; i++) {
159 double actRes = actual_resids[i]/rhs_norm[i];
160 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
161 if (actRes > tol) badRes = true;
162 }
163 }
164
165 success = ret==Belos::Converged && !badRes;
166
167 if (success) {
168 if (proc_verbose)
169 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
170 } else {
171 if (proc_verbose)
172 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
173 }
174 }
175 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
176
177 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
178 }