52 #include "Teuchos_CommandLineProcessor.hpp" 53 #include "Teuchos_ParameterList.hpp" 54 #include "Teuchos_StandardCatchMacros.hpp" 61 #ifdef HAVE_BELOS_TRIUTILS 62 #include "Trilinos_Util_iohb.h" 71 int main(
int argc,
char *argv[]) {
75 typedef ScalarTraits<ST> SCT;
76 typedef SCT::magnitudeType MT;
82 ST zero = SCT::zero();
84 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
95 bool norm_failure =
false;
96 bool proc_verbose =
false;
97 bool use_single_red =
false;
98 bool combineConvInner =
false;
102 std::string filename(
"A.hb");
105 CommandLineProcessor cmdp(
false,
true);
106 cmdp.setOption(
"verbose",
"quiet",&verbose,
"Print messages and results.");
107 cmdp.setOption(
"pseudo",
"regular",&pseudo,
"Use pseudo-block CG to solve the linear systems.");
108 cmdp.setOption(
"frequency",&frequency,
"Solvers frequency for printing residuals (#iters).");
109 cmdp.setOption(
"filename",&filename,
"Filename for Harwell-Boeing test matrix.");
110 cmdp.setOption(
"tol",&tol,
"Relative residual tolerance used by CG solver.");
111 cmdp.setOption(
"num-rhs",&numrhs,
"Number of right-hand sides to be solved for.");
112 cmdp.setOption(
"blocksize",&blocksize,
"Block size used by CG .");
113 cmdp.setOption(
"use-single-red",
"use-standard-red",&use_single_red,
"Use single-reduction variant of CG iteration.");
114 cmdp.setOption(
"combine-conv-inner",
"separate-conv-inner",&combineConvInner,
"Combine convergence detection and inner products of single-reduce CG iteration.");
115 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
119 proc_verbose = verbose && (MyPID==0);
127 #ifndef HAVE_BELOS_TRIUTILS 128 std::cout <<
"This test requires Triutils. Please configure with --enable-triutils." << std::endl;
130 std::cout <<
"End Result: TEST FAILED" << std::endl;
140 info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
141 &colptr,&rowind,&cvals);
142 if (info == 0 || nnz < 0) {
144 std::cout <<
"Error reading '" << filename <<
"'" << std::endl;
145 std::cout <<
"End Result: TEST FAILED" << std::endl;
150 RCP< MyBetterOperator<ST> > A
159 int maxits = dim/blocksize;
161 ParameterList belosList;
162 belosList.set(
"Block Size", blocksize );
163 belosList.set(
"Maximum Iterations", maxits );
164 belosList.set(
"Convergence Tolerance", tol );
165 if ((blocksize == 1) && use_single_red) {
166 belosList.set(
"Use Single Reduction", use_single_red );
167 if (combineConvInner)
168 belosList.set(
"Fold Convergence Detection Into Allreduce", combineConvInner );
174 belosList.set(
"Output Frequency", frequency );
183 RCP<MyMultiVec<ST> > soln = rcp(
new MyMultiVec<ST>(dim,numrhs) );
185 MVT::MvRandom( *soln );
186 OPT::Apply( *A, *soln, *rhs );
187 MVT::MvInit( *soln, zero );
191 RCP<Belos::LinearProblem<ST,MV,OP> > problem =
193 bool set = problem->setProblem();
196 std::cout << std::endl <<
"ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
205 Teuchos::RCP< Belos::SolverManager<ST,MV,OP> > solver;
215 std::cout << std::endl << std::endl;
216 std::cout <<
"Dimension of matrix: " << dim << std::endl;
217 std::cout <<
"Number of right-hand sides: " << numrhs << std::endl;
218 std::cout <<
"Block size used by solver: " << blocksize << std::endl;
219 std::cout <<
"Max number of CG iterations: " << maxits << std::endl;
220 std::cout <<
"Relative residual tolerance: " << tol << std::endl;
221 std::cout << std::endl;
230 RCP<MyMultiVec<ST> > temp = rcp(
new MyMultiVec<ST>(dim,numrhs) );
231 OPT::Apply( *A, *soln, *temp );
232 MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
233 std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
234 MVT::MvNorm( *temp, norm_num );
235 MVT::MvNorm( *rhs, norm_denom );
236 for (
int i=0; i<numrhs; ++i) {
238 std::cout <<
"Relative residual "<<i<<
" : " << norm_num[i] / norm_denom[i] << std::endl;
239 if ( norm_num[i] / norm_denom[i] > tol ) {
245 MT ach_tol = solver->achievedTol();
247 std::cout <<
"Achieved tol : "<<ach_tol<<std::endl;
258 std::cout <<
"End Result: TEST PASSED" << std::endl;
261 std::cout <<
"End Result: TEST FAILED" << std::endl;
264 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
266 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
int main(int argc, char *argv[])
std::string Belos_Version()
The Belos::PseudoBlockCGSolMgr provides a solver manager for the BlockCG linear solver.
The Belos::BlockCGSolMgr provides a powerful and fully-featured solver manager over the CG and BlockC...
Traits class which defines basic operations on multivectors.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
Simple example of a user's defined Belos::MultiVec class.
Alternative run-time polymorphic interface for operators.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
ReturnType
Whether the Belos solve converged for all linear systems.
The Belos::BlockCGSolMgr provides a solver manager for the BlockCG linear solver. ...
Interface for multivectors used by Belos' linear solvers.
Class which defines basic traits for the operator type.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Simple example of a user's defined Belos::Operator class.